Compare commits

...

3 Commits

Author SHA1 Message Date
Greg Shuflin
ec6f4b510e Use resolver 2 2021-10-07 01:33:37 -07:00
Greg Shuflin
c92e88900c Update main crate to 2018 edition 2021-10-07 01:30:30 -07:00
Greg Shuflin
c9a4c83fce Run cargo fmt on schala-repl code 2021-10-07 01:19:35 -07:00
14 changed files with 804 additions and 661 deletions

View File

@ -2,6 +2,8 @@
name = "schala" name = "schala"
version = "0.1.0" version = "0.1.0"
authors = ["greg <greg.shuflin@protonmail.com>"] authors = ["greg <greg.shuflin@protonmail.com>"]
edition = "2018"
resolver = "2"
[dependencies] [dependencies]

View File

@ -3,6 +3,7 @@ name = "schala-lang-codegen"
version = "0.1.0" version = "0.1.0"
authors = ["greg <greg.shuflin@protonmail.com>"] authors = ["greg <greg.shuflin@protonmail.com>"]
edition = "2018" edition = "2018"
resolver = "2"
[lib] [lib]
proc-macro = true proc-macro = true

View File

@ -3,6 +3,7 @@ name = "schala-lang"
version = "0.1.0" version = "0.1.0"
authors = ["greg <greg.shuflin@protonmail.com>"] authors = ["greg <greg.shuflin@protonmail.com>"]
edition = "2018" edition = "2018"
resolver = "2"
[dependencies] [dependencies]
itertools = "0.10" itertools = "0.10"

View File

@ -3,6 +3,7 @@ name = "schala-repl"
version = "0.1.0" version = "0.1.0"
authors = ["greg <greg.shuflin@protonmail.com>"] authors = ["greg <greg.shuflin@protonmail.com>"]
edition = "2018" edition = "2018"
resolver = "2"
[dependencies] [dependencies]
llvm-sys = "70.0.2" llvm-sys = "70.0.2"

View File

@ -1,5 +1,5 @@
use std::time;
use std::collections::HashSet; use std::collections::HashSet;
use std::time;
pub trait ProgrammingLanguageInterface { pub trait ProgrammingLanguageInterface {
fn get_language_name(&self) -> String; fn get_language_name(&self) -> String;
@ -14,7 +14,10 @@ pub trait ProgrammingLanguageInterface {
} }
fn request_meta(&mut self, _request: LangMetaRequest) -> LangMetaResponse { fn request_meta(&mut self, _request: LangMetaRequest) -> LangMetaResponse {
LangMetaResponse::Custom { kind: format!("not-implemented"), value: format!("") } LangMetaResponse::Custom {
kind: format!("not-implemented"),
value: format!(""),
}
} }
} }
@ -32,49 +35,42 @@ pub struct ComputationResponse {
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct GlobalOutputStats { pub struct GlobalOutputStats {
pub total_duration: time::Duration, pub total_duration: time::Duration,
pub stage_durations: Vec<(String, time::Duration)> pub stage_durations: Vec<(String, time::Duration)>,
} }
#[derive(Debug, Clone, Hash, Eq, PartialEq, Deserialize, Serialize)] #[derive(Debug, Clone, Hash, Eq, PartialEq, Deserialize, Serialize)]
pub enum DebugAsk { pub enum DebugAsk {
Timing, Timing,
ByStage { stage_name: String, token: Option<String> }, ByStage {
stage_name: String,
token: Option<String>,
},
} }
impl DebugAsk { impl DebugAsk {
pub fn is_for_stage(&self, name: &str) -> bool { pub fn is_for_stage(&self, name: &str) -> bool {
match self { match self {
DebugAsk::ByStage { stage_name, .. } if stage_name == name => true, DebugAsk::ByStage { stage_name, .. } if stage_name == name => true,
_ => false _ => false,
} }
} }
} }
pub struct DebugResponse { pub struct DebugResponse {
pub ask: DebugAsk, pub ask: DebugAsk,
pub value: String pub value: String,
} }
pub enum LangMetaRequest { pub enum LangMetaRequest {
StageNames, StageNames,
Docs { Docs { source: String },
source: String, Custom { kind: String, value: String },
},
Custom {
kind: String,
value: String
},
ImmediateDebug(DebugAsk), ImmediateDebug(DebugAsk),
} }
pub enum LangMetaResponse { pub enum LangMetaResponse {
StageNames(Vec<String>), StageNames(Vec<String>),
Docs { Docs { doc_string: String },
doc_string: String, Custom { kind: String, value: String },
},
Custom {
kind: String,
value: String
},
ImmediateDebug(DebugResponse), ImmediateDebug(DebugResponse),
} }

View File

@ -1,35 +1,37 @@
#![feature(box_patterns, box_syntax, proc_macro_hygiene, decl_macro)] #![feature(box_patterns, box_syntax, proc_macro_hygiene, decl_macro)]
#![feature(plugin)] #![feature(plugin)]
extern crate getopts;
extern crate linefeed;
extern crate itertools;
extern crate colored; extern crate colored;
extern crate getopts;
extern crate itertools;
extern crate linefeed;
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
extern crate serde_json;
extern crate includedir; extern crate includedir;
extern crate phf; extern crate phf;
extern crate serde_json;
use std::collections::HashSet; use std::collections::HashSet;
use std::path::Path;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::path::Path;
use std::process::exit; use std::process::exit;
mod repl;
mod language; mod language;
mod repl;
pub use language::{ProgrammingLanguageInterface, pub use language::{
ComputationRequest, ComputationResponse, ComputationRequest, ComputationResponse, DebugAsk, DebugResponse, GlobalOutputStats,
LangMetaRequest, LangMetaResponse, LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface,
DebugResponse, DebugAsk, GlobalOutputStats}; };
include!(concat!(env!("OUT_DIR"), "/static.rs")); include!(concat!(env!("OUT_DIR"), "/static.rs"));
const VERSION_STRING: &'static str = "0.1.0"; const VERSION_STRING: &'static str = "0.1.0";
pub fn start_repl(langs: Vec<Box<dyn ProgrammingLanguageInterface>>) { pub fn start_repl(langs: Vec<Box<dyn ProgrammingLanguageInterface>>) {
let options = command_line_options().parse(std::env::args()).unwrap_or_else(|e| { let options = command_line_options()
.parse(std::env::args())
.unwrap_or_else(|e| {
println!("{:?}", e); println!("{:?}", e);
exit(1); exit(1);
}); });
@ -52,15 +54,22 @@ pub fn start_repl(langs: Vec<Box<dyn ProgrammingLanguageInterface>>) {
fn run_noninteractive(filename: &str, languages: Vec<Box<dyn ProgrammingLanguageInterface>>) { fn run_noninteractive(filename: &str, languages: Vec<Box<dyn ProgrammingLanguageInterface>>) {
let path = Path::new(filename); let path = Path::new(filename);
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or_else(|| { let ext = path
.extension()
.and_then(|e| e.to_str())
.unwrap_or_else(|| {
println!("Source file lacks extension"); println!("Source file lacks extension");
exit(1); exit(1);
}); });
let mut language = Box::new(languages.into_iter().find(|lang| lang.get_source_file_suffix() == ext) let mut language = Box::new(
languages
.into_iter()
.find(|lang| lang.get_source_file_suffix() == ext)
.unwrap_or_else(|| { .unwrap_or_else(|| {
println!("Extension .{} not recognized", ext); println!("Extension .{} not recognized", ext);
exit(1); exit(1);
})); }),
);
let mut source_file = File::open(path).unwrap(); let mut source_file = File::open(path).unwrap();
let mut buffer = String::new(); let mut buffer = String::new();
@ -74,18 +83,13 @@ fn run_noninteractive(filename: &str, languages: Vec<Box<dyn ProgrammingLanguage
let response = language.run_computation(request); let response = language.run_computation(request);
match response.main_output { match response.main_output {
Ok(s) => println!("{}", s), Ok(s) => println!("{}", s),
Err(s) => println!("{}", s) Err(s) => println!("{}", s),
}; };
} }
fn command_line_options() -> getopts::Options { fn command_line_options() -> getopts::Options {
let mut options = getopts::Options::new(); let mut options = getopts::Options::new();
options.optflag("h", options.optflag("h", "help", "Show help text");
"help", options.optflag("w", "webapp", "Start up web interpreter");
"Show help text");
options.optflag("w",
"webapp",
"Start up web interpreter");
options options
} }

View File

@ -1,4 +1,4 @@
use super::{Repl, InterpreterDirectiveOutput}; use super::{InterpreterDirectiveOutput, Repl};
use crate::repl::directive_actions::DirectiveAction; use crate::repl::directive_actions::DirectiveAction;
use colored::*; use colored::*;
@ -24,11 +24,26 @@ pub enum CommandTree {
impl CommandTree { impl CommandTree {
pub fn nonterm_no_further_tab_completions(s: &str, help: Option<&str>) -> CommandTree { pub fn nonterm_no_further_tab_completions(s: &str, help: Option<&str>) -> CommandTree {
CommandTree::NonTerminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), children: vec![], action: DirectiveAction::Null } CommandTree::NonTerminal {
name: s.to_string(),
help_msg: help.map(|x| x.to_string()),
children: vec![],
action: DirectiveAction::Null,
}
} }
pub fn terminal(s: &str, help: Option<&str>, children: Vec<CommandTree>, action: DirectiveAction) -> CommandTree { pub fn terminal(
CommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), children, action} s: &str,
help: Option<&str>,
children: Vec<CommandTree>,
action: DirectiveAction,
) -> CommandTree {
CommandTree::Terminal {
name: s.to_string(),
help_msg: help.map(|x| x.to_string()),
children,
action,
}
} }
pub fn nonterm(s: &str, help: Option<&str>, children: Vec<CommandTree>) -> CommandTree { pub fn nonterm(s: &str, help: Option<&str>, children: Vec<CommandTree>) -> CommandTree {
@ -36,30 +51,34 @@ impl CommandTree {
name: s.to_string(), name: s.to_string(),
help_msg: help.map(|x| x.to_string()), help_msg: help.map(|x| x.to_string()),
children, children,
action: DirectiveAction::Null action: DirectiveAction::Null,
} }
} }
pub fn get_cmd(&self) -> &str { pub fn get_cmd(&self) -> &str {
match self { match self {
CommandTree::Terminal { name, .. } => name.as_str(), CommandTree::Terminal { name, .. } => name.as_str(),
CommandTree::NonTerminal {name, ..} => name.as_str(), CommandTree::NonTerminal { name, .. } => name.as_str(),
CommandTree::Top(_) => "", CommandTree::Top(_) => "",
} }
} }
pub fn get_help(&self) -> &str { pub fn get_help(&self) -> &str {
match self { match self {
CommandTree::Terminal { help_msg, ..} => help_msg.as_ref().map(|s| s.as_str()).unwrap_or("<no help text provided>"), CommandTree::Terminal { help_msg, .. } => help_msg
CommandTree::NonTerminal { help_msg, .. } => help_msg.as_ref().map(|s| s.as_str()).unwrap_or("<no help text provided>"), .as_ref()
CommandTree::Top(_) => "" .map(|s| s.as_str())
.unwrap_or("<no help text provided>"),
CommandTree::NonTerminal { help_msg, .. } => help_msg
.as_ref()
.map(|s| s.as_str())
.unwrap_or("<no help text provided>"),
CommandTree::Top(_) => "",
} }
} }
pub fn get_children(&self) -> &Vec<CommandTree> { pub fn get_children(&self) -> &Vec<CommandTree> {
use CommandTree::*; use CommandTree::*;
match self { match self {
Terminal { children, .. } | Terminal { children, .. } | NonTerminal { children, .. } | Top(children) => children,
NonTerminal { children, .. } |
Top(children) => children
} }
} }
pub fn get_subcommands(&self) -> Vec<&str> { pub fn get_subcommands(&self) -> Vec<&str> {
@ -72,28 +91,32 @@ impl CommandTree {
let res: Result<(DirectiveAction, usize), String> = loop { let res: Result<(DirectiveAction, usize), String> = loop {
match dir_pointer { match dir_pointer {
CommandTree::Top(subcommands) | CommandTree::NonTerminal { children: subcommands, .. } => { CommandTree::Top(subcommands)
| CommandTree::NonTerminal {
children: subcommands,
..
} => {
let next_command = match arguments.get(idx) { let next_command = match arguments.get(idx) {
Some(cmd) => cmd, Some(cmd) => cmd,
None => break Err(format!("Command requires arguments")) None => break Err(format!("Command requires arguments")),
}; };
idx += 1; idx += 1;
match subcommands.iter().find(|sc| sc.get_cmd() == *next_command) { match subcommands.iter().find(|sc| sc.get_cmd() == *next_command) {
Some(command_tree) => { Some(command_tree) => {
dir_pointer = command_tree; dir_pointer = command_tree;
}, }
None => break Err(format!("Command {} not found", next_command)) None => break Err(format!("Command {} not found", next_command)),
}; };
}, }
CommandTree::Terminal { action, .. } => { CommandTree::Terminal { action, .. } => {
break Ok((action.clone(), idx)); break Ok((action.clone(), idx));
}, }
} }
}; };
match res { match res {
Ok((action, idx)) => action.perform(repl, &arguments[idx..]), Ok((action, idx)) => action.perform(repl, &arguments[idx..]),
Err(err) => Some(err.red().to_string()) Err(err) => Some(err.red().to_string()),
} }
} }
} }

View File

@ -1,6 +1,6 @@
use super::{Repl, InterpreterDirectiveOutput}; use super::{InterpreterDirectiveOutput, Repl};
use crate::language::{DebugAsk, DebugResponse, LangMetaRequest, LangMetaResponse};
use crate::repl::help::help; use crate::repl::help::help;
use crate::language::{LangMetaRequest, LangMetaResponse, DebugAsk, DebugResponse};
use std::fmt::Write as FmtWrite; use std::fmt::Write as FmtWrite;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -28,7 +28,7 @@ impl DirectiveAction {
QuitProgram => { QuitProgram => {
repl.save_before_exit(); repl.save_before_exit();
::std::process::exit(0) ::std::process::exit(0)
}, }
ListPasses => { ListPasses => {
let language_state = repl.get_cur_language_state(); let language_state = repl.get_cur_language_state();
let pass_names = match language_state.request_meta(LangMetaRequest::StageNames) { let pass_names = match language_state.request_meta(LangMetaRequest::StageNames) {
@ -44,25 +44,31 @@ impl DirectiveAction {
} }
} }
Some(buf) Some(buf)
}, }
ShowImmediate => { ShowImmediate => {
let cur_state = repl.get_cur_language_state(); let cur_state = repl.get_cur_language_state();
let stage_name = match arguments.get(0) { let stage_name = match arguments.get(0) {
Some(s) => s.to_string(), Some(s) => s.to_string(),
None => return Some(format!("Must specify a thing to debug")), None => return Some(format!("Must specify a thing to debug")),
}; };
let meta = LangMetaRequest::ImmediateDebug(DebugAsk::ByStage { stage_name: stage_name.clone(), token: None }); let meta = LangMetaRequest::ImmediateDebug(DebugAsk::ByStage {
stage_name: stage_name.clone(),
token: None,
});
let meta_response = cur_state.request_meta(meta); let meta_response = cur_state.request_meta(meta);
let response = match meta_response { let response = match meta_response {
LangMetaResponse::ImmediateDebug(DebugResponse { ask, value }) => match ask { LangMetaResponse::ImmediateDebug(DebugResponse { ask, value }) => match ask {
DebugAsk::ByStage { stage_name: ref this_stage_name, ..} if *this_stage_name == stage_name => value, DebugAsk::ByStage {
_ => return Some(format!("Wrong debug stage")) stage_name: ref this_stage_name,
..
} if *this_stage_name == stage_name => value,
_ => return Some(format!("Wrong debug stage")),
}, },
_ => return Some(format!("Invalid language meta response")), _ => return Some(format!("Invalid language meta response")),
}; };
Some(response) Some(response)
}, }
Show => { Show => {
let this_stage_name = match arguments.get(0) { let this_stage_name = match arguments.get(0) {
Some(s) => s.to_string(), Some(s) => s.to_string(),
@ -71,24 +77,29 @@ impl DirectiveAction {
let token = arguments.get(1).map(|s| s.to_string()); let token = arguments.get(1).map(|s| s.to_string());
repl.options.debug_asks.retain(|ask| match ask { repl.options.debug_asks.retain(|ask| match ask {
DebugAsk::ByStage { stage_name, .. } if *stage_name == this_stage_name => false, DebugAsk::ByStage { stage_name, .. } if *stage_name == this_stage_name => false,
_ => true _ => true,
}); });
let ask = DebugAsk::ByStage { stage_name: this_stage_name, token }; let ask = DebugAsk::ByStage {
stage_name: this_stage_name,
token,
};
repl.options.debug_asks.insert(ask); repl.options.debug_asks.insert(ask);
None None
}, }
Hide => { Hide => {
let stage_name_to_remove = match arguments.get(0) { let stage_name_to_remove = match arguments.get(0) {
Some(s) => s.to_string(), Some(s) => s.to_string(),
None => return Some(format!("Must specify a stage to hide")), None => return Some(format!("Must specify a stage to hide")),
}; };
repl.options.debug_asks.retain(|ask| match ask { repl.options.debug_asks.retain(|ask| match ask {
DebugAsk::ByStage { stage_name, .. } if *stage_name == stage_name_to_remove => false, DebugAsk::ByStage { stage_name, .. } if *stage_name == stage_name_to_remove => {
_ => true false
}
_ => true,
}); });
None None
}, }
TotalTimeOff => total_time_off(repl, arguments), TotalTimeOff => total_time_off(repl, arguments),
TotalTimeOn => total_time_on(repl, arguments), TotalTimeOn => total_time_on(repl, arguments),
StageTimeOff => stage_time_off(repl, arguments), StageTimeOff => stage_time_off(repl, arguments),
@ -119,14 +130,16 @@ fn stage_time_off(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput {
} }
fn doc(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput { fn doc(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
arguments.get(0).map(|cmd| { arguments
.get(0)
.map(|cmd| {
let source = cmd.to_string(); let source = cmd.to_string();
let meta = LangMetaRequest::Docs { source }; let meta = LangMetaRequest::Docs { source };
let cur_state = repl.get_cur_language_state(); let cur_state = repl.get_cur_language_state();
match cur_state.request_meta(meta) { match cur_state.request_meta(meta) {
LangMetaResponse::Docs { doc_string } => Some(doc_string), LangMetaResponse::Docs { doc_string } => Some(doc_string),
_ => Some(format!("Invalid doc response")) _ => Some(format!("Invalid doc response")),
} }
}).unwrap_or(Some(format!(":docs needs an argument"))) })
.unwrap_or(Some(format!(":docs needs an argument")))
} }

View File

@ -2,14 +2,19 @@ use crate::repl::command_tree::CommandTree;
use crate::repl::directive_actions::DirectiveAction; use crate::repl::directive_actions::DirectiveAction;
pub fn directives_from_pass_names(pass_names: &Vec<String>) -> CommandTree { pub fn directives_from_pass_names(pass_names: &Vec<String>) -> CommandTree {
let passes_directives: Vec<CommandTree> = pass_names.iter() let passes_directives: Vec<CommandTree> = pass_names
.iter()
.map(|pass_name| { .map(|pass_name| {
if pass_name == "parsing" { if pass_name == "parsing" {
CommandTree::nonterm(pass_name, None, vec![ CommandTree::nonterm(
pass_name,
None,
vec![
CommandTree::nonterm_no_further_tab_completions("compact", None), CommandTree::nonterm_no_further_tab_completions("compact", None),
CommandTree::nonterm_no_further_tab_completions("expanded", None), CommandTree::nonterm_no_further_tab_completions("expanded", None),
CommandTree::nonterm_no_further_tab_completions("trace", None), CommandTree::nonterm_no_further_tab_completions("trace", None),
]) ],
)
} else { } else {
CommandTree::nonterm_no_further_tab_completions(pass_name, None) CommandTree::nonterm_no_further_tab_completions(pass_name, None)
} }
@ -24,32 +29,76 @@ fn get_list(passes_directives: &Vec<CommandTree>, include_help: bool) -> Vec<Com
vec![ vec![
CommandTree::terminal("exit", Some("exit the REPL"), vec![], QuitProgram), CommandTree::terminal("exit", Some("exit the REPL"), vec![], QuitProgram),
CommandTree::terminal("quit", Some("exit the REPL"), vec![], QuitProgram), CommandTree::terminal("quit", Some("exit the REPL"), vec![], QuitProgram),
CommandTree::terminal("help", Some("Print this help message"), if include_help { get_list(passes_directives, false) } else { vec![] }, Help), CommandTree::terminal(
CommandTree::nonterm("debug", "help",
Some("Print this help message"),
if include_help {
get_list(passes_directives, false)
} else {
vec![]
},
Help,
),
CommandTree::nonterm(
"debug",
Some("Configure debug information"), Some("Configure debug information"),
vec![ vec![
CommandTree::terminal("list-passes", Some("List all registered compiler passes"), vec![], ListPasses), CommandTree::terminal(
CommandTree::terminal("show-immediate", None, passes_directives.clone(), ShowImmediate), "list-passes",
CommandTree::terminal("show", Some("Show debug output for a specific pass"), passes_directives.clone(), Show), Some("List all registered compiler passes"),
CommandTree::terminal("hide", Some("Hide debug output for a specific pass"), passes_directives.clone(), Hide), vec![],
CommandTree::nonterm("total-time", None, vec![ ListPasses,
),
CommandTree::terminal(
"show-immediate",
None,
passes_directives.clone(),
ShowImmediate,
),
CommandTree::terminal(
"show",
Some("Show debug output for a specific pass"),
passes_directives.clone(),
Show,
),
CommandTree::terminal(
"hide",
Some("Hide debug output for a specific pass"),
passes_directives.clone(),
Hide,
),
CommandTree::nonterm(
"total-time",
None,
vec![
CommandTree::terminal("on", None, vec![], TotalTimeOn), CommandTree::terminal("on", None, vec![], TotalTimeOn),
CommandTree::terminal("off", None, vec![], TotalTimeOff), CommandTree::terminal("off", None, vec![], TotalTimeOff),
]), ],
CommandTree::nonterm("stage-times", Some("Computation time per-stage"), vec![ ),
CommandTree::nonterm(
"stage-times",
Some("Computation time per-stage"),
vec![
CommandTree::terminal("on", None, vec![], StageTimeOn), CommandTree::terminal("on", None, vec![], StageTimeOn),
CommandTree::terminal("off", None, vec![], StageTimeOff), CommandTree::terminal("off", None, vec![], StageTimeOff),
]) ],
]
), ),
CommandTree::nonterm("lang", ],
),
CommandTree::nonterm(
"lang",
Some("switch between languages, or go directly to a langauge by name"), Some("switch between languages, or go directly to a langauge by name"),
vec![ vec![
CommandTree::nonterm_no_further_tab_completions("next", None), CommandTree::nonterm_no_further_tab_completions("next", None),
CommandTree::nonterm_no_further_tab_completions("prev", None), CommandTree::nonterm_no_further_tab_completions("prev", None),
CommandTree::nonterm("go", None, vec![]), CommandTree::nonterm("go", None, vec![]),
] ],
),
CommandTree::terminal(
"doc",
Some("Get language-specific help for an item"),
vec![],
Doc,
), ),
CommandTree::terminal("doc", Some("Get language-specific help for an item"), vec![], Doc),
] ]
} }

View File

@ -1,8 +1,8 @@
use std::fmt::Write as FmtWrite; use std::fmt::Write as FmtWrite;
use colored::*;
use super::command_tree::CommandTree; use super::command_tree::CommandTree;
use super::{Repl, InterpreterDirectiveOutput}; use super::{InterpreterDirectiveOutput, Repl};
use colored::*;
pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput { pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
match arguments { match arguments {
@ -17,7 +17,8 @@ pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
let children = dir.get_children(); let children = dir.get_children();
writeln!(buf, "`{}` - {}", cmd, dir.get_help()).unwrap(); writeln!(buf, "`{}` - {}", cmd, dir.get_help()).unwrap();
for sub in children.iter() { for sub in children.iter() {
writeln!(buf, "\t`{} {}` - {}", cmd, sub.get_cmd(), sub.get_help()).unwrap(); writeln!(buf, "\t`{} {}` - {}", cmd, sub.get_cmd(), sub.get_help())
.unwrap();
} }
buf buf
} }
@ -26,11 +27,16 @@ pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
} }
} }
fn get_directive_from_commands<'a>(commands: &[&str], dirs: &'a CommandTree) -> Option<&'a CommandTree> { fn get_directive_from_commands<'a>(
commands: &[&str],
dirs: &'a CommandTree,
) -> Option<&'a CommandTree> {
let mut directive_list = dirs.get_children(); let mut directive_list = dirs.get_children();
let mut matched_directive = None; let mut matched_directive = None;
for cmd in commands { for cmd in commands {
let found = directive_list.iter().find(|directive| directive.get_cmd() == *cmd); let found = directive_list
.iter()
.find(|directive| directive.get_cmd() == *cmd);
if let Some(dir) = found { if let Some(dir) = found {
directive_list = dir.get_children(); directive_list = dir.get_children();
} }
@ -44,16 +50,34 @@ fn global_help(repl: &mut Repl) -> InterpreterDirectiveOutput {
let mut buf = String::new(); let mut buf = String::new();
let sigil = repl.interpreter_directive_sigil; let sigil = repl.interpreter_directive_sigil;
writeln!(buf, "{} version {}", "Schala REPL".bright_red().bold(), crate::VERSION_STRING).unwrap(); writeln!(
buf,
"{} version {}",
"Schala REPL".bright_red().bold(),
crate::VERSION_STRING
)
.unwrap();
writeln!(buf, "-----------------------").unwrap(); writeln!(buf, "-----------------------").unwrap();
for directive in repl.get_directives().get_children() { for directive in repl.get_directives().get_children() {
writeln!(buf, "{}{} - {}", sigil, directive.get_cmd(), directive.get_help()).unwrap(); writeln!(
buf,
"{}{} - {}",
sigil,
directive.get_cmd(),
directive.get_help()
)
.unwrap();
} }
let ref lang = repl.get_cur_language_state(); let ref lang = repl.get_cur_language_state();
writeln!(buf, "").unwrap(); writeln!(buf, "").unwrap();
writeln!(buf, "Language-specific help for {}", lang.get_language_name()).unwrap(); writeln!(
buf,
"Language-specific help for {}",
lang.get_language_name()
)
.unwrap();
writeln!(buf, "-----------------------").unwrap(); writeln!(buf, "-----------------------").unwrap();
Some(buf) Some(buf)
} }

View File

@ -1,8 +1,9 @@
use std::sync::Arc;
use std::collections::HashSet; use std::collections::HashSet;
use std::sync::Arc;
use crate::language::{ProgrammingLanguageInterface, use crate::language::{
ComputationRequest, LangMetaResponse, LangMetaRequest}; ComputationRequest, LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface,
};
mod command_tree; mod command_tree;
use self::command_tree::CommandTree; use self::command_tree::CommandTree;
@ -30,7 +31,7 @@ pub struct Repl {
#[derive(Clone)] #[derive(Clone)]
enum PromptStyle { enum PromptStyle {
Normal, Normal,
Multiline Multiline,
} }
impl Repl { impl Repl {
@ -49,7 +50,10 @@ impl Repl {
pub fn run_repl(&mut self) { pub fn run_repl(&mut self) {
println!("Schala MetaInterpreter version {}", crate::VERSION_STRING); println!("Schala MetaInterpreter version {}", crate::VERSION_STRING);
println!("Type {}help for help with the REPL", self.interpreter_directive_sigil); println!(
"Type {}help for help with the REPL",
self.interpreter_directive_sigil
);
self.load_options(); self.load_options();
self.handle_repl_loop(); self.handle_repl_loop();
self.save_before_exit(); self.save_before_exit();
@ -57,12 +61,14 @@ impl Repl {
} }
fn load_options(&mut self) { fn load_options(&mut self) {
self.line_reader.load_history(HISTORY_SAVE_FILE).unwrap_or(()); self.line_reader
.load_history(HISTORY_SAVE_FILE)
.unwrap_or(());
match ReplOptions::load_from_file(OPTIONS_SAVE_FILE) { match ReplOptions::load_from_file(OPTIONS_SAVE_FILE) {
Ok(options) => { Ok(options) => {
self.options = options; self.options = options;
}, }
Err(()) => () Err(()) => (),
}; };
} }
@ -77,11 +83,11 @@ impl Repl {
Err(e) => { Err(e) => {
println!("readline IO Error: {}", e); println!("readline IO Error: {}", e);
break 'main; break 'main;
}, }
Ok(Eof) | Ok(Signal(_)) => break 'main, Ok(Eof) | Ok(Signal(_)) => break 'main,
Ok(Input(ref input)) => input, Ok(Input(ref input)) => input,
} }
} };
} }
self.update_line_reader(); self.update_line_reader();
let line = self.line_reader.read_line(); let line = self.line_reader.read_line();
@ -111,10 +117,10 @@ impl Repl {
Some(directive_output) => println!("<> {}", directive_output), Some(directive_output) => println!("<> {}", directive_output),
None => (), None => (),
} }
continue continue;
} }
}, }
_ => self.handle_input(input) _ => self.handle_input(input),
}; };
for repl_response in repl_responses.iter() { for repl_response in repl_responses.iter() {
@ -124,8 +130,10 @@ impl Repl {
} }
fn update_line_reader(&mut self) { fn update_line_reader(&mut self) {
let tab_complete_handler = TabCompleteHandler::new(self.interpreter_directive_sigil, self.get_directives()); let tab_complete_handler =
self.line_reader.set_completer(Arc::new(tab_complete_handler)); //TODO fix this here TabCompleteHandler::new(self.interpreter_directive_sigil, self.get_directives());
self.line_reader
.set_completer(Arc::new(tab_complete_handler)); //TODO fix this here
self.set_prompt(PromptStyle::Normal); self.set_prompt(PromptStyle::Normal);
} }
@ -139,17 +147,16 @@ impl Repl {
} }
fn save_before_exit(&self) { fn save_before_exit(&self) {
self.line_reader.save_history(HISTORY_SAVE_FILE).unwrap_or(()); self.line_reader
.save_history(HISTORY_SAVE_FILE)
.unwrap_or(());
self.options.save_to_file(OPTIONS_SAVE_FILE); self.options.save_to_file(OPTIONS_SAVE_FILE);
} }
fn handle_interpreter_directive(&mut self, input: &str) -> InterpreterDirectiveOutput { fn handle_interpreter_directive(&mut self, input: &str) -> InterpreterDirectiveOutput {
let mut iter = input.chars(); let mut iter = input.chars();
iter.next(); iter.next();
let arguments: Vec<&str> = iter let arguments: Vec<&str> = iter.as_str().split_whitespace().collect();
.as_str()
.split_whitespace()
.collect();
if arguments.len() < 1 { if arguments.len() < 1 {
return None; return None;
@ -170,7 +177,10 @@ impl Repl {
debug_requests.insert(ask.clone()); debug_requests.insert(ask.clone());
} }
let request = ComputationRequest { source: input, debug_requests }; let request = ComputationRequest {
source: input,
debug_requests,
};
let ref mut language_state = self.get_cur_language_state(); let ref mut language_state = self.get_cur_language_state();
let response = language_state.run_computation(request); let response = language_state.run_computation(request);
response::handle_computation_response(response, &self.options) response::handle_computation_response(response, &self.options)
@ -187,13 +197,12 @@ impl Repl {
} }
} }
struct TabCompleteHandler { struct TabCompleteHandler {
sigil: char, sigil: char,
top_level_commands: CommandTree, top_level_commands: CommandTree,
} }
use linefeed::complete::{Completion, Completer}; use linefeed::complete::{Completer, Completion};
use linefeed::terminal::Terminal; use linefeed::terminal::Terminal;
impl TabCompleteHandler { impl TabCompleteHandler {
@ -206,7 +215,13 @@ impl TabCompleteHandler {
} }
impl<T: Terminal> Completer<T> for TabCompleteHandler { impl<T: Terminal> Completer<T> for TabCompleteHandler {
fn complete(&self, word: &str, prompter: &::linefeed::prompter::Prompter<T>, start: usize, _end: usize) -> Option<Vec<Completion>> { fn complete(
&self,
word: &str,
prompter: &::linefeed::prompter::Prompter<T>,
start: usize,
_end: usize,
) -> Option<Vec<Completion>> {
let line = prompter.buffer(); let line = prompter.buffer();
if !line.starts_with(self.sigil) { if !line.starts_with(self.sigil) {
@ -222,25 +237,33 @@ impl<T: Terminal> Completer<T> for TabCompleteHandler {
None => { None => {
let top = match command_tree { let top = match command_tree {
Some(CommandTree::Top(_)) => true, Some(CommandTree::Top(_)) => true,
_ => false _ => false,
}; };
let word = if top { word.get(1..).unwrap() } else { word }; let word = if top { word.get(1..).unwrap() } else { word };
for cmd in command_tree.map(|x| x.get_subcommands()).unwrap_or(vec![]).into_iter() { for cmd in command_tree
.map(|x| x.get_subcommands())
.unwrap_or(vec![])
.into_iter()
{
if cmd.starts_with(word) { if cmd.starts_with(word) {
completions.push(Completion { completions.push(Completion {
completion: format!("{}{}", if top { ":" } else { "" }, cmd), completion: format!("{}{}", if top { ":" } else { "" }, cmd),
display: Some(cmd.to_string()), display: Some(cmd.to_string()),
suffix: ::linefeed::complete::Suffix::Some(' ') suffix: ::linefeed::complete::Suffix::Some(' '),
}) })
} }
} }
break; break;
}, }
Some(s) => { Some(s) => {
let new_ptr: Option<&CommandTree> = command_tree.and_then(|cm| match cm { let new_ptr: Option<&CommandTree> = command_tree.and_then(|cm| match cm {
CommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s), CommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s),
CommandTree::NonTerminal { children, .. } => children.iter().find(|c| c.get_cmd() == s), CommandTree::NonTerminal { children, .. } => {
CommandTree::Terminal { children, .. } => children.iter().find(|c| c.get_cmd() == s), children.iter().find(|c| c.get_cmd() == s)
}
CommandTree::Terminal { children, .. } => {
children.iter().find(|c| c.get_cmd() == s)
}
}); });
command_tree = new_ptr; command_tree = new_ptr;
} }

View File

@ -1,8 +1,8 @@
use crate::language::DebugAsk; use crate::language::DebugAsk;
use std::io::{Read, Write};
use std::collections::HashSet; use std::collections::HashSet;
use std::fs::File; use std::fs::File;
use std::io::{Read, Write};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct ReplOptions { pub struct ReplOptions {
@ -21,8 +21,7 @@ impl ReplOptions {
} }
pub fn save_to_file(&self, filename: &str) { pub fn save_to_file(&self, filename: &str) {
let res = File::create(filename) let res = File::create(filename).and_then(|mut file| {
.and_then(|mut file| {
let buf = crate::serde_json::to_string(self).unwrap(); let buf = crate::serde_json::to_string(self).unwrap();
file.write_all(buf.as_bytes()) file.write_all(buf.as_bytes())
}); });

View File

@ -3,12 +3,12 @@ use std::fmt;
use std::fmt::Write; use std::fmt::Write;
use super::ReplOptions; use super::ReplOptions;
use crate::language::{ DebugAsk, ComputationResponse}; use crate::language::{ComputationResponse, DebugAsk};
pub struct ReplResponse { pub struct ReplResponse {
label: Option<String>, label: Option<String>,
text: String, text: String,
color: Option<Color> color: Option<Color>,
} }
impl fmt::Display for ReplResponse { impl fmt::Display for ReplResponse {
@ -18,15 +18,21 @@ impl fmt::Display for ReplResponse {
write!(buf, "({})", label).unwrap(); write!(buf, "({})", label).unwrap();
} }
write!(buf, "=> {}", self.text).unwrap(); write!(buf, "=> {}", self.text).unwrap();
write!(f, "{}", match self.color { write!(
f,
"{}",
match self.color {
Some(c) => buf.color(c), Some(c) => buf.color(c),
None => buf.normal() None => buf.normal(),
}) }
)
} }
} }
pub fn handle_computation_response(
pub fn handle_computation_response(response: ComputationResponse, options: &ReplOptions) -> Vec<ReplResponse> { response: ComputationResponse,
options: &ReplOptions,
) -> Vec<ReplResponse> {
let mut responses = vec![]; let mut responses = vec![];
if options.show_total_time { if options.show_total_time {
@ -58,10 +64,17 @@ pub fn handle_computation_response(response: ComputationResponse, options: &Repl
} }
responses.push(match response.main_output { responses.push(match response.main_output {
Ok(s) => ReplResponse { label: None, text: s, color: None }, Ok(s) => ReplResponse {
Err(e) => ReplResponse { label: Some("Error".to_string()), text: e, color: Some(Color::Red) }, label: None,
text: s,
color: None,
},
Err(e) => ReplResponse {
label: Some("Error".to_string()),
text: e,
color: Some(Color::Red),
},
}); });
responses responses
} }

View File

@ -1,9 +1,3 @@
extern crate schala_repl;
//extern crate maaru_lang;
//extern crate rukka_lang;
//extern crate robo_lang;
extern crate schala_lang;
use schala_repl::{ProgrammingLanguageInterface, start_repl}; use schala_repl::{ProgrammingLanguageInterface, start_repl};
extern { } extern { }