Compare commits

..

No commits in common. "ec6f4b510e562fcf1419bdf392111322d2d633dc" and "77bf42be6cf1329ee3089bc1af2bad6f8cda671f" have entirely different histories.

14 changed files with 655 additions and 798 deletions

View File

@ -2,8 +2,6 @@
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,7 +3,6 @@ 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,7 +3,6 @@ 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,7 +3,6 @@ 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,76 +1,80 @@
use std::collections::HashSet;
use std::time; use std::time;
use std::collections::HashSet;
pub trait ProgrammingLanguageInterface { pub trait ProgrammingLanguageInterface {
fn get_language_name(&self) -> String; fn get_language_name(&self) -> String;
fn get_source_file_suffix(&self) -> String; fn get_source_file_suffix(&self) -> String;
fn run_computation(&mut self, _request: ComputationRequest) -> ComputationResponse { fn run_computation(&mut self, _request: ComputationRequest) -> ComputationResponse {
ComputationResponse { ComputationResponse {
main_output: Err(format!("Computation pipeline not implemented")), main_output: Err(format!("Computation pipeline not implemented")),
global_output_stats: GlobalOutputStats::default(), global_output_stats: GlobalOutputStats::default(),
debug_responses: vec![], debug_responses: vec![],
}
} }
}
fn request_meta(&mut self, _request: LangMetaRequest) -> LangMetaResponse { fn request_meta(&mut self, _request: LangMetaRequest) -> LangMetaResponse {
LangMetaResponse::Custom { LangMetaResponse::Custom { kind: format!("not-implemented"), value: format!("") }
kind: format!("not-implemented"), }
value: format!(""),
}
}
} }
pub struct ComputationRequest<'a> { pub struct ComputationRequest<'a> {
pub source: &'a str, pub source: &'a str,
pub debug_requests: HashSet<DebugAsk>, pub debug_requests: HashSet<DebugAsk>,
} }
pub struct ComputationResponse { pub struct ComputationResponse {
pub main_output: Result<String, String>, pub main_output: Result<String, String>,
pub global_output_stats: GlobalOutputStats, pub global_output_stats: GlobalOutputStats,
pub debug_responses: Vec<DebugResponse>, pub debug_responses: Vec<DebugResponse>,
} }
#[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 { ByStage { stage_name: String, token: Option<String> },
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 { source: String }, Docs {
Custom { kind: String, value: String }, source: String,
ImmediateDebug(DebugAsk), },
Custom {
kind: String,
value: String
},
ImmediateDebug(DebugAsk),
} }
pub enum LangMetaResponse { pub enum LangMetaResponse {
StageNames(Vec<String>), StageNames(Vec<String>),
Docs { doc_string: String }, Docs {
Custom { kind: String, value: String }, doc_string: String,
ImmediateDebug(DebugResponse), },
Custom {
kind: String,
value: String
},
ImmediateDebug(DebugResponse),
} }

View File

@ -1,95 +1,91 @@
#![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 colored;
extern crate getopts; extern crate getopts;
extern crate itertools;
extern crate linefeed; extern crate linefeed;
extern crate itertools;
extern crate colored;
#[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 language;
mod repl; mod repl;
mod language;
pub use language::{ pub use language::{ProgrammingLanguageInterface,
ComputationRequest, ComputationResponse, DebugAsk, DebugResponse, GlobalOutputStats, ComputationRequest, ComputationResponse,
LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface, LangMetaRequest, LangMetaResponse,
}; 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() let options = command_line_options().parse(std::env::args()).unwrap_or_else(|e| {
.parse(std::env::args()) println!("{:?}", e);
.unwrap_or_else(|e| { exit(1);
println!("{:?}", e); });
exit(1);
});
if options.opt_present("help") { if options.opt_present("help") {
println!("{}", command_line_options().usage("Schala metainterpreter")); println!("{}", command_line_options().usage("Schala metainterpreter"));
exit(0); exit(0);
}
match options.free[..] {
[] | [_] => {
let mut repl = repl::Repl::new(langs);
repl.run_repl();
} }
[_, ref filename, ..] => {
match options.free[..] { run_noninteractive(filename, langs);
[] | [_] => { }
let mut repl = repl::Repl::new(langs); };
repl.run_repl();
}
[_, ref filename, ..] => {
run_noninteractive(filename, langs);
}
};
} }
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 let ext = path.extension().and_then(|e| e.to_str()).unwrap_or_else(|| {
.extension() println!("Source file lacks extension");
.and_then(|e| e.to_str()) exit(1);
.unwrap_or_else(|| { });
println!("Source file lacks extension"); let mut language = Box::new(languages.into_iter().find(|lang| lang.get_source_file_suffix() == ext)
exit(1); .unwrap_or_else(|| {
}); println!("Extension .{} not recognized", ext);
let mut language = Box::new( exit(1);
languages }));
.into_iter()
.find(|lang| lang.get_source_file_suffix() == ext)
.unwrap_or_else(|| {
println!("Extension .{} not recognized", ext);
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();
source_file.read_to_string(&mut buffer).unwrap(); source_file.read_to_string(&mut buffer).unwrap();
let request = ComputationRequest { let request = ComputationRequest {
source: &buffer, source: &buffer,
debug_requests: HashSet::new(), debug_requests: HashSet::new(),
}; };
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", "help", "Show help text"); options.optflag("h",
options.optflag("w", "webapp", "Start up web interpreter"); "help",
options "Show help text");
options.optflag("w",
"webapp",
"Start up web interpreter");
options
} }

View File

@ -1,4 +1,4 @@
use super::{InterpreterDirectiveOutput, Repl}; use super::{Repl, InterpreterDirectiveOutput};
use crate::repl::directive_actions::DirectiveAction; use crate::repl::directive_actions::DirectiveAction;
use colored::*; use colored::*;
@ -7,116 +7,93 @@ use colored::*;
/// and then execute it with any remaining arguments /// and then execute it with any remaining arguments
#[derive(Clone)] #[derive(Clone)]
pub enum CommandTree { pub enum CommandTree {
Terminal { Terminal {
name: String, name: String,
children: Vec<CommandTree>, children: Vec<CommandTree>,
help_msg: Option<String>, help_msg: Option<String>,
action: DirectiveAction, action: DirectiveAction,
}, },
NonTerminal { NonTerminal {
name: String, name: String,
children: Vec<CommandTree>, children: Vec<CommandTree>,
help_msg: Option<String>, help_msg: Option<String>,
action: DirectiveAction, action: DirectiveAction,
}, },
Top(Vec<CommandTree>), Top(Vec<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 { CommandTree::NonTerminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), children: vec![], action: DirectiveAction::Null }
name: s.to_string(), }
help_msg: help.map(|x| x.to_string()),
children: vec![],
action: DirectiveAction::Null,
}
}
pub fn terminal( pub fn terminal(s: &str, help: Option<&str>, children: Vec<CommandTree>, action: DirectiveAction) -> CommandTree {
s: &str, CommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), children, action}
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 {
CommandTree::NonTerminal { CommandTree::NonTerminal {
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 { }
match self { pub fn get_help(&self) -> &str {
CommandTree::Terminal { help_msg, .. } => help_msg match self {
.as_ref() CommandTree::Terminal { help_msg, ..} => help_msg.as_ref().map(|s| s.as_str()).unwrap_or("<no help text provided>"),
.map(|s| s.as_str()) CommandTree::NonTerminal { help_msg, .. } => help_msg.as_ref().map(|s| s.as_str()).unwrap_or("<no help text provided>"),
.unwrap_or("<no help text provided>"), CommandTree::Top(_) => ""
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> { }
use CommandTree::*; pub fn get_children(&self) -> &Vec<CommandTree> {
match self { use CommandTree::*;
Terminal { children, .. } | NonTerminal { children, .. } | Top(children) => children, match self {
} Terminal { children, .. } |
} NonTerminal { children, .. } |
pub fn get_subcommands(&self) -> Vec<&str> { Top(children) => children
self.get_children().iter().map(|x| x.get_cmd()).collect()
} }
}
pub fn get_subcommands(&self) -> Vec<&str> {
self.get_children().iter().map(|x| x.get_cmd()).collect()
}
pub fn perform(&self, repl: &mut Repl, arguments: &Vec<&str>) -> InterpreterDirectiveOutput { pub fn perform(&self, repl: &mut Repl, arguments: &Vec<&str>) -> InterpreterDirectiveOutput {
let mut dir_pointer: &CommandTree = self; let mut dir_pointer: &CommandTree = self;
let mut idx = 0; let mut idx = 0;
let res: Result<(DirectiveAction, usize), String> = loop { let res: Result<(DirectiveAction, usize), String> = loop {
match dir_pointer { match dir_pointer {
CommandTree::Top(subcommands) CommandTree::Top(subcommands) | CommandTree::NonTerminal { children: subcommands, .. } => {
| CommandTree::NonTerminal { let next_command = match arguments.get(idx) {
children: subcommands, Some(cmd) => cmd,
.. None => break Err(format!("Command requires arguments"))
} => { };
let next_command = match arguments.get(idx) { idx += 1;
Some(cmd) => cmd, match subcommands.iter().find(|sc| sc.get_cmd() == *next_command) {
None => break Err(format!("Command requires arguments")), Some(command_tree) => {
}; dir_pointer = command_tree;
idx += 1; },
match subcommands.iter().find(|sc| sc.get_cmd() == *next_command) { None => break Err(format!("Command {} not found", next_command))
Some(command_tree) => { };
dir_pointer = command_tree; },
} CommandTree::Terminal { action, .. } => {
None => break Err(format!("Command {} not found", next_command)), break Ok((action.clone(), idx));
}; },
} }
CommandTree::Terminal { action, .. } => { };
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,145 +1,132 @@
use super::{InterpreterDirectiveOutput, Repl}; use super::{Repl, InterpreterDirectiveOutput};
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)]
pub enum DirectiveAction { pub enum DirectiveAction {
Null, Null,
Help, Help,
QuitProgram, QuitProgram,
ListPasses, ListPasses,
ShowImmediate, ShowImmediate,
Show, Show,
Hide, Hide,
TotalTimeOff, TotalTimeOff,
TotalTimeOn, TotalTimeOn,
StageTimeOff, StageTimeOff,
StageTimeOn, StageTimeOn,
Doc, Doc,
} }
impl DirectiveAction { impl DirectiveAction {
pub fn perform(&self, repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput { pub fn perform(&self, repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
use DirectiveAction::*; use DirectiveAction::*;
match self { match self {
Null => None, Null => None,
Help => help(repl, arguments), Help => help(repl, arguments),
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) {
LangMetaResponse::StageNames(names) => names, LangMetaResponse::StageNames(names) => names,
_ => vec![], _ => vec![],
}; };
let mut buf = String::new(); let mut buf = String::new();
for pass in pass_names.iter().map(|name| Some(name)).intersperse(None) { for pass in pass_names.iter().map(|name| Some(name)).intersperse(None) {
match pass { match pass {
Some(pass) => write!(buf, "{}", pass).unwrap(), Some(pass) => write!(buf, "{}", pass).unwrap(),
None => write!(buf, " -> ").unwrap(), None => write!(buf, " -> ").unwrap(),
} }
}
Some(buf)
}
ShowImmediate => {
let cur_state = repl.get_cur_language_state();
let stage_name = match arguments.get(0) {
Some(s) => s.to_string(),
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_response = cur_state.request_meta(meta);
let response = match meta_response {
LangMetaResponse::ImmediateDebug(DebugResponse { ask, value }) => match ask {
DebugAsk::ByStage {
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")),
};
Some(response)
}
Show => {
let this_stage_name = match arguments.get(0) {
Some(s) => s.to_string(),
None => return Some(format!("Must specify a stage to show")),
};
let token = arguments.get(1).map(|s| s.to_string());
repl.options.debug_asks.retain(|ask| match ask {
DebugAsk::ByStage { stage_name, .. } if *stage_name == this_stage_name => false,
_ => true,
});
let ask = DebugAsk::ByStage {
stage_name: this_stage_name,
token,
};
repl.options.debug_asks.insert(ask);
None
}
Hide => {
let stage_name_to_remove = match arguments.get(0) {
Some(s) => s.to_string(),
None => return Some(format!("Must specify a stage to hide")),
};
repl.options.debug_asks.retain(|ask| match ask {
DebugAsk::ByStage { stage_name, .. } if *stage_name == stage_name_to_remove => {
false
}
_ => true,
});
None
}
TotalTimeOff => total_time_off(repl, arguments),
TotalTimeOn => total_time_on(repl, arguments),
StageTimeOff => stage_time_off(repl, arguments),
StageTimeOn => stage_time_on(repl, arguments),
Doc => doc(repl, arguments),
} }
Some(buf)
},
ShowImmediate => {
let cur_state = repl.get_cur_language_state();
let stage_name = match arguments.get(0) {
Some(s) => s.to_string(),
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_response = cur_state.request_meta(meta);
let response = match meta_response {
LangMetaResponse::ImmediateDebug(DebugResponse { ask, value }) => match ask {
DebugAsk::ByStage { 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")),
};
Some(response)
},
Show => {
let this_stage_name = match arguments.get(0) {
Some(s) => s.to_string(),
None => return Some(format!("Must specify a stage to show")),
};
let token = arguments.get(1).map(|s| s.to_string());
repl.options.debug_asks.retain(|ask| match ask {
DebugAsk::ByStage { stage_name, .. } if *stage_name == this_stage_name => false,
_ => true
});
let ask = DebugAsk::ByStage { stage_name: this_stage_name, token };
repl.options.debug_asks.insert(ask);
None
},
Hide => {
let stage_name_to_remove = match arguments.get(0) {
Some(s) => s.to_string(),
None => return Some(format!("Must specify a stage to hide")),
};
repl.options.debug_asks.retain(|ask| match ask {
DebugAsk::ByStage { stage_name, .. } if *stage_name == stage_name_to_remove => false,
_ => true
});
None
},
TotalTimeOff => total_time_off(repl, arguments),
TotalTimeOn => total_time_on(repl, arguments),
StageTimeOff => stage_time_off(repl, arguments),
StageTimeOn => stage_time_on(repl, arguments),
Doc => doc(repl, arguments),
} }
}
} }
fn total_time_on(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput { fn total_time_on(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput {
repl.options.show_total_time = true; repl.options.show_total_time = true;
None None
} }
fn total_time_off(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput { fn total_time_off(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput {
repl.options.show_total_time = false; repl.options.show_total_time = false;
None None
} }
fn stage_time_on(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput { fn stage_time_on(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput {
repl.options.show_stage_times = true; repl.options.show_stage_times = true;
None None
} }
fn stage_time_off(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput { fn stage_time_off(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput {
repl.options.show_stage_times = false; repl.options.show_stage_times = false;
None None
} }
fn doc(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput { fn doc(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
arguments arguments.get(0).map(|cmd| {
.get(0) let source = cmd.to_string();
.map(|cmd| { let meta = LangMetaRequest::Docs { source };
let source = cmd.to_string(); let cur_state = repl.get_cur_language_state();
let meta = LangMetaRequest::Docs { source }; match cur_state.request_meta(meta) {
let cur_state = repl.get_cur_language_state(); LangMetaResponse::Docs { doc_string } => Some(doc_string),
match cur_state.request_meta(meta) { _ => Some(format!("Invalid doc response"))
LangMetaResponse::Docs { doc_string } => Some(doc_string), }
_ => Some(format!("Invalid doc response")), }).unwrap_or(Some(format!(":docs needs an argument")))
}
})
.unwrap_or(Some(format!(":docs needs an argument")))
} }

View File

@ -2,103 +2,54 @@ 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 let passes_directives: Vec<CommandTree> = pass_names.iter()
.iter() .map(|pass_name| {
.map(|pass_name| { if pass_name == "parsing" {
if pass_name == "parsing" { CommandTree::nonterm(pass_name, None, vec![
CommandTree::nonterm( CommandTree::nonterm_no_further_tab_completions("compact", None),
pass_name, CommandTree::nonterm_no_further_tab_completions("expanded", None),
None, CommandTree::nonterm_no_further_tab_completions("trace", None),
vec![ ])
CommandTree::nonterm_no_further_tab_completions("compact", None), } else {
CommandTree::nonterm_no_further_tab_completions("expanded", None), CommandTree::nonterm_no_further_tab_completions(pass_name, None)
CommandTree::nonterm_no_further_tab_completions("trace", None), }
], })
) .collect();
} else { CommandTree::Top(get_list(&passes_directives, true))
CommandTree::nonterm_no_further_tab_completions(pass_name, None)
}
})
.collect();
CommandTree::Top(get_list(&passes_directives, true))
} }
fn get_list(passes_directives: &Vec<CommandTree>, include_help: bool) -> Vec<CommandTree> { fn get_list(passes_directives: &Vec<CommandTree>, include_help: bool) -> Vec<CommandTree> {
use DirectiveAction::*; use DirectiveAction::*;
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( CommandTree::terminal("help", Some("Print this help message"), if include_help { get_list(passes_directives, false) } else { vec![] }, Help),
"help", CommandTree::nonterm("debug",
Some("Print this help message"), Some("Configure debug information"),
if include_help { vec![
get_list(passes_directives, false) CommandTree::terminal("list-passes", Some("List all registered compiler passes"), vec![], ListPasses),
} else { CommandTree::terminal("show-immediate", None, passes_directives.clone(), ShowImmediate),
vec![] 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),
Help, CommandTree::nonterm("total-time", None, vec![
), CommandTree::terminal("on", None, vec![], TotalTimeOn),
CommandTree::nonterm( CommandTree::terminal("off", None, vec![], TotalTimeOff),
"debug", ]),
Some("Configure debug information"), CommandTree::nonterm("stage-times", Some("Computation time per-stage"), vec![
vec![ CommandTree::terminal("on", None, vec![], StageTimeOn),
CommandTree::terminal( CommandTree::terminal("off", None, vec![], StageTimeOff),
"list-passes", ])
Some("List all registered compiler passes"), ]
vec![], ),
ListPasses, CommandTree::nonterm("lang",
), Some("switch between languages, or go directly to a langauge by name"),
CommandTree::terminal( vec![
"show-immediate", CommandTree::nonterm_no_further_tab_completions("next", None),
None, CommandTree::nonterm_no_further_tab_completions("prev", None),
passes_directives.clone(), CommandTree::nonterm("go", None, vec![]),
ShowImmediate, ]
), ),
CommandTree::terminal( CommandTree::terminal("doc", Some("Get language-specific help for an item"), vec![], Doc),
"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("off", None, vec![], TotalTimeOff),
],
),
CommandTree::nonterm(
"stage-times",
Some("Computation time per-stage"),
vec![
CommandTree::terminal("on", None, vec![], StageTimeOn),
CommandTree::terminal("off", None, vec![], StageTimeOff),
],
),
],
),
CommandTree::nonterm(
"lang",
Some("switch between languages, or go directly to a langauge by name"),
vec![
CommandTree::nonterm_no_further_tab_completions("next", None),
CommandTree::nonterm_no_further_tab_completions("prev", None),
CommandTree::nonterm("go", None, vec![]),
],
),
CommandTree::terminal(
"doc",
Some("Get language-specific help for an item"),
vec![],
Doc,
),
]
} }

View File

@ -1,83 +1,59 @@
use std::fmt::Write as FmtWrite; use std::fmt::Write as FmtWrite;
use super::command_tree::CommandTree;
use super::{InterpreterDirectiveOutput, Repl};
use colored::*; use colored::*;
use super::command_tree::CommandTree;
use super::{Repl, InterpreterDirectiveOutput};
pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput { pub fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
match arguments { match arguments {
[] => return global_help(repl), [] => return global_help(repl),
commands => { commands => {
let dirs = repl.get_directives(); let dirs = repl.get_directives();
Some(match get_directive_from_commands(commands, &dirs) { Some(match get_directive_from_commands(commands, &dirs) {
None => format!("Directive `{}` not found", commands.last().unwrap()), None => format!("Directive `{}` not found", commands.last().unwrap()),
Some(dir) => { Some(dir) => {
let mut buf = String::new(); let mut buf = String::new();
let cmd = dir.get_cmd(); let cmd = dir.get_cmd();
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()) writeln!(buf, "\t`{} {}` - {}", cmd, sub.get_cmd(), sub.get_help()).unwrap();
.unwrap(); }
} buf
buf
}
})
} }
})
} }
}
} }
fn get_directive_from_commands<'a>( fn get_directive_from_commands<'a>(commands: &[&str], dirs: &'a CommandTree) -> Option<&'a CommandTree> {
commands: &[&str], let mut directive_list = dirs.get_children();
dirs: &'a CommandTree, let mut matched_directive = None;
) -> Option<&'a CommandTree> { for cmd in commands {
let mut directive_list = dirs.get_children(); let found = directive_list.iter().find(|directive| directive.get_cmd() == *cmd);
let mut matched_directive = None; if let Some(dir) = found {
for cmd in commands { directive_list = dir.get_children();
let found = directive_list
.iter()
.find(|directive| directive.get_cmd() == *cmd);
if let Some(dir) = found {
directive_list = dir.get_children();
}
matched_directive = found;
} }
matched_directive
matched_directive = found;
}
matched_directive
} }
fn global_help(repl: &mut Repl) -> InterpreterDirectiveOutput { 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!( writeln!(buf, "{} version {}", "Schala REPL".bright_red().bold(), crate::VERSION_STRING).unwrap();
buf, writeln!(buf, "-----------------------").unwrap();
"{} version {}",
"Schala REPL".bright_red().bold(),
crate::VERSION_STRING
)
.unwrap();
writeln!(buf, "-----------------------").unwrap();
for directive in repl.get_directives().get_children() { for directive in repl.get_directives().get_children() {
writeln!( writeln!(buf, "{}{} - {}", sigil, directive.get_cmd(), directive.get_help()).unwrap();
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!( writeln!(buf, "Language-specific help for {}", lang.get_language_name()).unwrap();
buf, writeln!(buf, "-----------------------").unwrap();
"Language-specific help for {}", Some(buf)
lang.get_language_name()
)
.unwrap();
writeln!(buf, "-----------------------").unwrap();
Some(buf)
} }

View File

@ -1,9 +1,8 @@
use std::collections::HashSet;
use std::sync::Arc; use std::sync::Arc;
use std::collections::HashSet;
use crate::language::{ use crate::language::{ProgrammingLanguageInterface,
ComputationRequest, LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface, ComputationRequest, LangMetaResponse, LangMetaRequest};
};
mod command_tree; mod command_tree;
use self::command_tree::CommandTree; use self::command_tree::CommandTree;
@ -22,253 +21,231 @@ const OPTIONS_SAVE_FILE: &'static str = ".schala_repl";
type InterpreterDirectiveOutput = Option<String>; type InterpreterDirectiveOutput = Option<String>;
pub struct Repl { pub struct Repl {
pub interpreter_directive_sigil: char, pub interpreter_directive_sigil: char,
line_reader: ::linefeed::interface::Interface<::linefeed::terminal::DefaultTerminal>, line_reader: ::linefeed::interface::Interface<::linefeed::terminal::DefaultTerminal>,
language_states: Vec<Box<dyn ProgrammingLanguageInterface>>, language_states: Vec<Box<dyn ProgrammingLanguageInterface>>,
options: ReplOptions, options: ReplOptions,
} }
#[derive(Clone)] #[derive(Clone)]
enum PromptStyle { enum PromptStyle {
Normal, Normal,
Multiline, Multiline
} }
impl Repl { impl Repl {
pub fn new(initial_states: Vec<Box<dyn ProgrammingLanguageInterface>>) -> Repl { pub fn new(initial_states: Vec<Box<dyn ProgrammingLanguageInterface>>) -> Repl {
use linefeed::Interface; use linefeed::Interface;
let line_reader = Interface::new("schala-repl").unwrap(); let line_reader = Interface::new("schala-repl").unwrap();
let interpreter_directive_sigil = ':'; let interpreter_directive_sigil = ':';
Repl { Repl {
interpreter_directive_sigil, interpreter_directive_sigil,
line_reader, line_reader,
language_states: initial_states, language_states: initial_states,
options: ReplOptions::new(), options: ReplOptions::new(),
}
}
pub fn run_repl(&mut self) {
println!("Schala MetaInterpreter version {}", crate::VERSION_STRING);
println!("Type {}help for help with the REPL", self.interpreter_directive_sigil);
self.load_options();
self.handle_repl_loop();
self.save_before_exit();
println!("Exiting...");
}
fn load_options(&mut self) {
self.line_reader.load_history(HISTORY_SAVE_FILE).unwrap_or(());
match ReplOptions::load_from_file(OPTIONS_SAVE_FILE) {
Ok(options) => {
self.options = options;
},
Err(()) => ()
};
}
fn handle_repl_loop(&mut self) {
use linefeed::ReadResult::*;
let sigil = self.interpreter_directive_sigil;
'main: loop {
macro_rules! match_or_break {
($line:expr) => {
match $line {
Err(e) => {
println!("readline IO Error: {}", e);
break 'main;
},
Ok(Eof) | Ok(Signal(_)) => break 'main,
Ok(Input(ref input)) => input,
}
} }
} }
self.update_line_reader();
let line = self.line_reader.read_line();
let input: &str = match_or_break!(line);
pub fn run_repl(&mut self) { self.line_reader.add_history_unique(input.to_string());
println!("Schala MetaInterpreter version {}", crate::VERSION_STRING); let mut chars = input.chars().peekable();
println!( let repl_responses = match chars.nth(0) {
"Type {}help for help with the REPL", Some(ch) if ch == sigil => {
self.interpreter_directive_sigil if chars.peek() == Some(&'{') {
); let mut buf = String::new();
self.load_options(); buf.push_str(input.get(2..).unwrap());
self.handle_repl_loop(); 'multiline: loop {
self.save_before_exit(); self.set_prompt(PromptStyle::Multiline);
println!("Exiting..."); let new_line = self.line_reader.read_line();
} let new_input = match_or_break!(new_line);
if new_input.starts_with(":}") {
fn load_options(&mut self) { break 'multiline;
self.line_reader } else {
.load_history(HISTORY_SAVE_FILE) buf.push_str(new_input);
.unwrap_or(()); buf.push_str("\n");
match ReplOptions::load_from_file(OPTIONS_SAVE_FILE) { }
Ok(options) => {
self.options = options;
} }
Err(()) => (), self.handle_input(&buf)
}; } else {
} match self.handle_interpreter_directive(input) {
Some(directive_output) => println!("<> {}", directive_output),
fn handle_repl_loop(&mut self) { None => (),
use linefeed::ReadResult::*;
let sigil = self.interpreter_directive_sigil;
'main: loop {
macro_rules! match_or_break {
($line:expr) => {
match $line {
Err(e) => {
println!("readline IO Error: {}", e);
break 'main;
}
Ok(Eof) | Ok(Signal(_)) => break 'main,
Ok(Input(ref input)) => input,
}
};
} }
self.update_line_reader(); continue
let line = self.line_reader.read_line(); }
let input: &str = match_or_break!(line); },
_ => self.handle_input(input)
};
self.line_reader.add_history_unique(input.to_string()); for repl_response in repl_responses.iter() {
let mut chars = input.chars().peekable(); println!("{}", repl_response);
let repl_responses = match chars.nth(0) { }
Some(ch) if ch == sigil => { }
if chars.peek() == Some(&'{') { }
let mut buf = String::new();
buf.push_str(input.get(2..).unwrap());
'multiline: loop {
self.set_prompt(PromptStyle::Multiline);
let new_line = self.line_reader.read_line();
let new_input = match_or_break!(new_line);
if new_input.starts_with(":}") {
break 'multiline;
} else {
buf.push_str(new_input);
buf.push_str("\n");
}
}
self.handle_input(&buf)
} else {
match self.handle_interpreter_directive(input) {
Some(directive_output) => println!("<> {}", directive_output),
None => (),
}
continue;
}
}
_ => self.handle_input(input),
};
for repl_response in repl_responses.iter() { fn update_line_reader(&mut self) {
println!("{}", repl_response); let tab_complete_handler = 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);
}
fn set_prompt(&mut self, prompt_style: PromptStyle) {
let prompt_str = match prompt_style {
PromptStyle::Normal => ">> ".to_string(),
PromptStyle::Multiline => ">| ".to_string(),
};
self.line_reader.set_prompt(&prompt_str).unwrap();
}
fn save_before_exit(&self) {
self.line_reader.save_history(HISTORY_SAVE_FILE).unwrap_or(());
self.options.save_to_file(OPTIONS_SAVE_FILE);
}
fn handle_interpreter_directive(&mut self, input: &str) -> InterpreterDirectiveOutput {
let mut iter = input.chars();
iter.next();
let arguments: Vec<&str> = iter
.as_str()
.split_whitespace()
.collect();
if arguments.len() < 1 {
return None;
} }
fn update_line_reader(&mut self) { let directives = self.get_directives();
let tab_complete_handler = directives.perform(self, &arguments)
TabCompleteHandler::new(self.interpreter_directive_sigil, self.get_directives()); }
self.line_reader
.set_completer(Arc::new(tab_complete_handler)); //TODO fix this here fn get_cur_language_state(&mut self) -> &mut Box<dyn ProgrammingLanguageInterface> {
self.set_prompt(PromptStyle::Normal); //TODO this is obviously not complete
&mut self.language_states[0]
}
fn handle_input(&mut self, input: &str) -> Vec<ReplResponse> {
let mut debug_requests = HashSet::new();
for ask in self.options.debug_asks.iter() {
debug_requests.insert(ask.clone());
} }
fn set_prompt(&mut self, prompt_style: PromptStyle) { let request = ComputationRequest { source: input, debug_requests };
let prompt_str = match prompt_style { let ref mut language_state = self.get_cur_language_state();
PromptStyle::Normal => ">> ".to_string(), let response = language_state.run_computation(request);
PromptStyle::Multiline => ">| ".to_string(), response::handle_computation_response(response, &self.options)
}; }
self.line_reader.set_prompt(&prompt_str).unwrap(); fn get_directives(&mut self) -> CommandTree {
} let language_state = self.get_cur_language_state();
let pass_names = match language_state.request_meta(LangMetaRequest::StageNames) {
LangMetaResponse::StageNames(names) => names,
_ => vec![],
};
fn save_before_exit(&self) { directives_from_pass_names(&pass_names)
self.line_reader }
.save_history(HISTORY_SAVE_FILE)
.unwrap_or(());
self.options.save_to_file(OPTIONS_SAVE_FILE);
}
fn handle_interpreter_directive(&mut self, input: &str) -> InterpreterDirectiveOutput {
let mut iter = input.chars();
iter.next();
let arguments: Vec<&str> = iter.as_str().split_whitespace().collect();
if arguments.len() < 1 {
return None;
}
let directives = self.get_directives();
directives.perform(self, &arguments)
}
fn get_cur_language_state(&mut self) -> &mut Box<dyn ProgrammingLanguageInterface> {
//TODO this is obviously not complete
&mut self.language_states[0]
}
fn handle_input(&mut self, input: &str) -> Vec<ReplResponse> {
let mut debug_requests = HashSet::new();
for ask in self.options.debug_asks.iter() {
debug_requests.insert(ask.clone());
}
let request = ComputationRequest {
source: input,
debug_requests,
};
let ref mut language_state = self.get_cur_language_state();
let response = language_state.run_computation(request);
response::handle_computation_response(response, &self.options)
}
fn get_directives(&mut self) -> CommandTree {
let language_state = self.get_cur_language_state();
let pass_names = match language_state.request_meta(LangMetaRequest::StageNames) {
LangMetaResponse::StageNames(names) => names,
_ => vec![],
};
directives_from_pass_names(&pass_names)
}
} }
struct TabCompleteHandler { struct TabCompleteHandler {
sigil: char, sigil: char,
top_level_commands: CommandTree, top_level_commands: CommandTree,
} }
use linefeed::complete::{Completer, Completion}; use linefeed::complete::{Completion, Completer};
use linefeed::terminal::Terminal; use linefeed::terminal::Terminal;
impl TabCompleteHandler { impl TabCompleteHandler {
fn new(sigil: char, top_level_commands: CommandTree) -> TabCompleteHandler { fn new(sigil: char, top_level_commands: CommandTree) -> TabCompleteHandler {
TabCompleteHandler { TabCompleteHandler {
top_level_commands, top_level_commands,
sigil, sigil,
}
} }
}
} }
impl<T: Terminal> Completer<T> for TabCompleteHandler { impl<T: Terminal> Completer<T> for TabCompleteHandler {
fn complete( fn complete(&self, word: &str, prompter: &::linefeed::prompter::Prompter<T>, start: usize, _end: usize) -> Option<Vec<Completion>> {
&self, let line = prompter.buffer();
word: &str,
prompter: &::linefeed::prompter::Prompter<T>,
start: usize,
_end: usize,
) -> Option<Vec<Completion>> {
let line = prompter.buffer();
if !line.starts_with(self.sigil) { if !line.starts_with(self.sigil) {
return None; return None;
}
let mut words = line[1..(if start == 0 { 1 } else { start })].split_whitespace();
let mut completions = Vec::new();
let mut command_tree: Option<&CommandTree> = Some(&self.top_level_commands);
loop {
match words.next() {
None => {
let top = match command_tree {
Some(CommandTree::Top(_)) => true,
_ => false,
};
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()
{
if cmd.starts_with(word) {
completions.push(Completion {
completion: format!("{}{}", if top { ":" } else { "" }, cmd),
display: Some(cmd.to_string()),
suffix: ::linefeed::complete::Suffix::Some(' '),
})
}
}
break;
}
Some(s) => {
let new_ptr: Option<&CommandTree> = command_tree.and_then(|cm| match cm {
CommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s),
CommandTree::NonTerminal { children, .. } => {
children.iter().find(|c| c.get_cmd() == s)
}
CommandTree::Terminal { children, .. } => {
children.iter().find(|c| c.get_cmd() == s)
}
});
command_tree = new_ptr;
}
}
}
Some(completions)
} }
let mut words = line[1..(if start == 0 { 1 } else { start })].split_whitespace();
let mut completions = Vec::new();
let mut command_tree: Option<&CommandTree> = Some(&self.top_level_commands);
loop {
match words.next() {
None => {
let top = match command_tree {
Some(CommandTree::Top(_)) => true,
_ => false
};
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() {
if cmd.starts_with(word) {
completions.push(Completion {
completion: format!("{}{}", if top { ":" } else { "" }, cmd),
display: Some(cmd.to_string()),
suffix: ::linefeed::complete::Suffix::Some(' ')
})
}
}
break;
},
Some(s) => {
let new_ptr: Option<&CommandTree> = command_tree.and_then(|cm| match cm {
CommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s),
CommandTree::NonTerminal { children, .. } => children.iter().find(|c| c.get_cmd() == s),
CommandTree::Terminal { children, .. } => children.iter().find(|c| c.get_cmd() == s),
});
command_tree = new_ptr;
}
}
}
Some(completions)
}
} }

View File

@ -1,46 +1,47 @@
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 {
pub debug_asks: HashSet<DebugAsk>, pub debug_asks: HashSet<DebugAsk>,
pub show_total_time: bool, pub show_total_time: bool,
pub show_stage_times: bool, pub show_stage_times: bool,
} }
impl ReplOptions { impl ReplOptions {
pub fn new() -> ReplOptions { pub fn new() -> ReplOptions {
ReplOptions { ReplOptions {
debug_asks: HashSet::new(), debug_asks: HashSet::new(),
show_total_time: true, show_total_time: true,
show_stage_times: false, show_stage_times: false,
}
} }
}
pub fn save_to_file(&self, filename: &str) { pub fn save_to_file(&self, filename: &str) {
let res = File::create(filename).and_then(|mut file| { let res = File::create(filename)
let buf = crate::serde_json::to_string(self).unwrap(); .and_then(|mut file| {
file.write_all(buf.as_bytes()) let buf = crate::serde_json::to_string(self).unwrap();
}); file.write_all(buf.as_bytes())
if let Err(err) = res { });
println!("Error saving {} file {}", filename, err); if let Err(err) = res {
} println!("Error saving {} file {}", filename, err);
} }
}
pub fn load_from_file(filename: &str) -> Result<ReplOptions, ()> { pub fn load_from_file(filename: &str) -> Result<ReplOptions, ()> {
File::open(filename) File::open(filename)
.and_then(|mut file| { .and_then(|mut file| {
let mut contents = String::new(); let mut contents = String::new();
file.read_to_string(&mut contents)?; file.read_to_string(&mut contents)?;
Ok(contents) Ok(contents)
}) })
.and_then(|contents| { .and_then(|contents| {
let output: ReplOptions = crate::serde_json::from_str(&contents)?; let output: ReplOptions = crate::serde_json::from_str(&contents)?;
Ok(output) Ok(output)
}) })
.map_err(|_| ()) .map_err(|_| ())
} }
} }

View File

@ -3,78 +3,65 @@ use std::fmt;
use std::fmt::Write; use std::fmt::Write;
use super::ReplOptions; use super::ReplOptions;
use crate::language::{ComputationResponse, DebugAsk}; use crate::language::{ DebugAsk, ComputationResponse};
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 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut buf = String::new(); let mut buf = String::new();
if let Some(ref label) = self.label { if let Some(ref label) = self.label {
write!(buf, "({})", label).unwrap(); write!(buf, "({})", label).unwrap();
}
write!(buf, "=> {}", self.text).unwrap();
write!(
f,
"{}",
match self.color {
Some(c) => buf.color(c),
None => buf.normal(),
}
)
} }
write!(buf, "=> {}", self.text).unwrap();
write!(f, "{}", match self.color {
Some(c) => buf.color(c),
None => buf.normal()
})
}
} }
pub fn handle_computation_response(
response: ComputationResponse,
options: &ReplOptions,
) -> Vec<ReplResponse> {
let mut responses = vec![];
if options.show_total_time { pub fn handle_computation_response(response: ComputationResponse, options: &ReplOptions) -> Vec<ReplResponse> {
responses.push(ReplResponse { let mut responses = vec![];
label: Some("Total time".to_string()),
text: format!("{:?}", response.global_output_stats.total_duration),
color: None,
});
}
if options.show_stage_times { if options.show_total_time {
responses.push(ReplResponse { responses.push(ReplResponse {
label: Some("Stage times".to_string()), label: Some("Total time".to_string()),
text: format!("{:?}", response.global_output_stats.stage_durations), text: format!("{:?}", response.global_output_stats.total_duration),
color: None, color: None,
});
}
for debug_resp in response.debug_responses {
let stage_name = match debug_resp.ask {
DebugAsk::ByStage { stage_name, .. } => stage_name,
_ => continue,
};
responses.push(ReplResponse {
label: Some(stage_name.to_string()),
text: debug_resp.value,
color: Some(Color::Red),
});
}
responses.push(match response.main_output {
Ok(s) => ReplResponse {
label: None,
text: s,
color: None,
},
Err(e) => ReplResponse {
label: Some("Error".to_string()),
text: e,
color: Some(Color::Red),
},
}); });
}
responses if options.show_stage_times {
responses.push(ReplResponse {
label: Some("Stage times".to_string()),
text: format!("{:?}", response.global_output_stats.stage_durations),
color: None,
});
}
for debug_resp in response.debug_responses {
let stage_name = match debug_resp.ask {
DebugAsk::ByStage { stage_name, .. } => stage_name,
_ => continue,
};
responses.push(ReplResponse {
label: Some(stage_name.to_string()),
text: debug_resp.value,
color: Some(Color::Red),
});
}
responses.push(match response.main_output {
Ok(s) => ReplResponse { label: None, text: s, color: None },
Err(e) => ReplResponse { label: Some("Error".to_string()), text: e, color: Some(Color::Red) },
});
responses
} }

View File

@ -1,3 +1,9 @@
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 { }