Cleaned up Repl struct

This commit is contained in:
greg 2017-09-01 02:08:26 -07:00
parent b5a6c5903e
commit 8dc8d15437
5 changed files with 222 additions and 237 deletions

View File

@ -21,6 +21,7 @@ pub struct EvalOptions {
pub debug_tokens: bool, pub debug_tokens: bool,
pub debug_parse: bool, pub debug_parse: bool,
pub debug_type: bool, pub debug_type: bool,
pub show_llvm_ir: bool,
pub trace_evaluation: bool, pub trace_evaluation: bool,
} }
@ -77,7 +78,7 @@ impl TraceArtifact {
} }
pub trait ProgrammingLanguageInterface { pub trait ProgrammingLanguageInterface {
fn evaluate_in_repl(&mut self, input: &str, eval_options: EvalOptions) -> ReplOutput; fn evaluate_in_repl(&mut self, input: &str, eval_options: &EvalOptions) -> ReplOutput;
fn get_language_name(&self) -> String; fn get_language_name(&self) -> String;
fn compile(&mut self, _input: &str) -> LLVMCodeString { fn compile(&mut self, _input: &str) -> LLVMCodeString {
LLVMCodeString("".to_string()) LLVMCodeString("".to_string())

View File

@ -24,7 +24,7 @@ impl<'a> ProgrammingLanguageInterface for Maaru<'a> {
"Maaru".to_string() "Maaru".to_string()
} }
fn evaluate_in_repl(&mut self, input: &str, options: EvalOptions) -> ReplOutput { fn evaluate_in_repl(&mut self, input: &str, options: &EvalOptions) -> ReplOutput {
let mut output = ReplOutput::default(); let mut output = ReplOutput::default();
let tokens = match tokenizer::tokenize(input) { let tokens = match tokenizer::tokenize(input) {

View File

@ -68,7 +68,6 @@ fn main() {
.unwrap_or(0); .unwrap_or(0);
let mut options = EvalOptions::default(); let mut options = EvalOptions::default();
let show_llvm_ir = option_matches.opt_present("v");
options.trace_evaluation = option_matches.opt_present("t"); options.trace_evaluation = option_matches.opt_present("t");
let compile = !option_matches.opt_present("i"); let compile = !option_matches.opt_present("i");
@ -76,7 +75,7 @@ fn main() {
match option_matches.free[..] { match option_matches.free[..] {
[] | [_] => { [] | [_] => {
let mut repl = Repl::new(languages, initial_index); let mut repl = Repl::new(languages, initial_index);
repl.show_llvm_ir = show_llvm_ir; repl.options.show_llvm_ir = option_matches.opt_present("v");
repl.run(); repl.run();
} }
[_, ref filename, _..] => { [_, ref filename, _..] => {
@ -86,43 +85,11 @@ fn main() {
}; };
} }
fn program_options() -> getopts::Options { fn run_noninteractive<T: ProgrammingLanguageInterface>(filename: &str, language: &mut T, options: EvalOptions, compile: bool) {
let mut options = getopts::Options::new();
options.optflag("i",
"interpret",
"Interpret source file instead of compiling");
options.optflag("t",
"trace-evaluation",
"Print out trace of evaluation");
options.optflag("v",
"llvm-in-repl",
"Show LLVM IR in REPL");
options.optflag("",
"list-languages",
"Show a list of all supported languages");
options.optopt("l",
"lang",
"Start up REPL in a language",
"LANGUAGE");
options.optflag("h",
"help",
"Show help text");
options.optflag("m",
"virtual-machine",
"Start up a virtual machine instead of an interpreter");
options.optflag("a",
"assembler",
"Assemble file into bytecode");
options
}
fn run_noninteractive<T: ProgrammingLanguageInterface>(filename: &str, language: &mut T, _options: EvalOptions, compile: bool) {
let mut source_file = File::open(&Path::new(filename)).unwrap(); let mut source_file = File::open(&Path::new(filename)).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 options = EvalOptions::default();
if compile { if compile {
if !language.can_compile() { if !language.can_compile() {
panic!("Trying to compile a non-compileable language"); panic!("Trying to compile a non-compileable language");
@ -131,16 +98,14 @@ fn run_noninteractive<T: ProgrammingLanguageInterface>(filename: &str, language:
compilation_sequence(llvm_bytecode, filename); compilation_sequence(llvm_bytecode, filename);
} }
} else { } else {
let interpretor_output = language.evaluate_in_repl(&buffer, options); let interpretor_output = language.evaluate_in_repl(&buffer, &options);
interpretor_output.print_to_screen(); interpretor_output.print_to_screen();
} }
} }
type LineReader = linefeed::Reader<linefeed::terminal::DefaultTerminal>; type LineReader = linefeed::Reader<linefeed::terminal::DefaultTerminal>;
struct Repl { struct Repl {
pub show_tokens: bool, options: EvalOptions,
pub show_parse: bool,
pub show_llvm_ir: bool,
languages: Vec<Box<ProgrammingLanguageInterface>>, languages: Vec<Box<ProgrammingLanguageInterface>>,
current_language_index: usize, current_language_index: usize,
interpreter_directive_sigil: char, interpreter_directive_sigil: char,
@ -153,9 +118,7 @@ impl Repl {
reader.set_prompt(">> "); reader.set_prompt(">> ");
let i = if initial_index < languages.len() { initial_index } else { 0 }; let i = if initial_index < languages.len() { initial_index } else { 0 };
Repl { Repl {
show_tokens: false, options: EvalOptions::default(),
show_parse: false,
show_llvm_ir: false,
languages: languages, languages: languages,
current_language_index: i, current_language_index: i,
interpreter_directive_sigil: '.', interpreter_directive_sigil: '.',
@ -190,16 +153,7 @@ impl Repl {
fn input_handler(&mut self, input: &str) -> String { fn input_handler(&mut self, input: &str) -> String {
let ref mut language = self.languages[self.current_language_index]; let ref mut language = self.languages[self.current_language_index];
let interpretor_output = language.evaluate_in_repl(input, &self.options);
let options = language::EvalOptions::default();
/*
options.show_tokens = self.show_tokens;
options.show_parse = self.show_parse;
options.show_llvm_ir = self.show_llvm_ir;
*/
let interpretor_output = language.evaluate_in_repl(input, options);
interpretor_output.to_string() interpretor_output.to_string()
} }
@ -266,13 +220,13 @@ impl Repl {
} }
}; };
match commands.get(2) { match commands.get(2) {
Some(&"tokens") => self.show_tokens = show, Some(&"tokens") => self.options.debug_tokens = show,
Some(&"parse") => self.show_parse = show, Some(&"parse") => self.options.debug_parse = show,
Some(&"eval") => { Some(&"eval") => {
//let ref mut language = self.languages[self.current_language_index]; //let ref mut language = self.languages[self.current_language_index];
//language.set_option("trace_evaluation", show); //language.set_option("trace_evaluation", show);
}, },
Some(&"llvm") => self.show_llvm_ir = show, Some(&"llvm") => self.options.show_llvm_ir = show,
Some(e) => { Some(e) => {
println!("Bad `show`/`hide` argument: {}", e); println!("Bad `show`/`hide` argument: {}", e);
return true; return true;
@ -335,3 +289,33 @@ pub fn compilation_sequence(llvm_code: LLVMCodeString, sourcefile: &str) {
} }
} }
fn program_options() -> getopts::Options {
let mut options = getopts::Options::new();
options.optflag("i",
"interpret",
"Interpret source file instead of compiling");
options.optflag("t",
"trace-evaluation",
"Print out trace of evaluation");
options.optflag("v",
"llvm-in-repl",
"Show LLVM IR in REPL");
options.optflag("",
"list-languages",
"Show a list of all supported languages");
options.optopt("l",
"lang",
"Start up REPL in a language",
"LANGUAGE");
options.optflag("h",
"help",
"Show help text");
options.optflag("m",
"virtual-machine",
"Start up a virtual machine instead of an interpreter");
options.optflag("a",
"assembler",
"Assemble file into bytecode");
options
}

View File

@ -137,7 +137,7 @@ impl ProgrammingLanguageInterface for Robo {
"Robo".to_string() "Robo".to_string()
} }
fn evaluate_in_repl(&mut self, input: &str, _eval_options: EvalOptions) -> ReplOutput { fn evaluate_in_repl(&mut self, input: &str, _eval_options: &EvalOptions) -> ReplOutput {
let mut output = ReplOutput::default(); let mut output = ReplOutput::default();
let tokens = match tokenize(input) { let tokens = match tokenize(input) {
Ok(tokens) => tokens, Ok(tokens) => tokens,

View File

@ -16,7 +16,7 @@ impl ProgrammingLanguageInterface for Schala {
"Schala".to_string() "Schala".to_string()
} }
fn evaluate_in_repl(&mut self, input: &str, options: EvalOptions) -> ReplOutput { fn evaluate_in_repl(&mut self, input: &str, options: &EvalOptions) -> ReplOutput {
let mut output = ReplOutput::default(); let mut output = ReplOutput::default();
let tokens = match parsing::tokenize(input) { let tokens = match parsing::tokenize(input) {
Ok(tokens) => { Ok(tokens) => {