Cleaned up Repl struct
This commit is contained in:
parent
b5a6c5903e
commit
8dc8d15437
@ -21,6 +21,7 @@ pub struct EvalOptions {
|
||||
pub debug_tokens: bool,
|
||||
pub debug_parse: bool,
|
||||
pub debug_type: bool,
|
||||
pub show_llvm_ir: bool,
|
||||
pub trace_evaluation: bool,
|
||||
}
|
||||
|
||||
@ -77,7 +78,7 @@ impl TraceArtifact {
|
||||
}
|
||||
|
||||
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 compile(&mut self, _input: &str) -> LLVMCodeString {
|
||||
LLVMCodeString("".to_string())
|
||||
|
@ -24,7 +24,7 @@ impl<'a> ProgrammingLanguageInterface for Maaru<'a> {
|
||||
"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 tokens = match tokenizer::tokenize(input) {
|
||||
|
94
src/main.rs
94
src/main.rs
@ -68,7 +68,6 @@ fn main() {
|
||||
.unwrap_or(0);
|
||||
|
||||
let mut options = EvalOptions::default();
|
||||
let show_llvm_ir = option_matches.opt_present("v");
|
||||
options.trace_evaluation = option_matches.opt_present("t");
|
||||
|
||||
let compile = !option_matches.opt_present("i");
|
||||
@ -76,7 +75,7 @@ fn main() {
|
||||
match option_matches.free[..] {
|
||||
[] | [_] => {
|
||||
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();
|
||||
}
|
||||
[_, ref filename, _..] => {
|
||||
@ -86,43 +85,11 @@ fn main() {
|
||||
};
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
fn run_noninteractive<T: ProgrammingLanguageInterface>(filename: &str, language: &mut T, _options: EvalOptions, compile: bool) {
|
||||
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 buffer = String::new();
|
||||
source_file.read_to_string(&mut buffer).unwrap();
|
||||
|
||||
let options = EvalOptions::default();
|
||||
|
||||
if compile {
|
||||
if !language.can_compile() {
|
||||
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);
|
||||
}
|
||||
} else {
|
||||
let interpretor_output = language.evaluate_in_repl(&buffer, options);
|
||||
let interpretor_output = language.evaluate_in_repl(&buffer, &options);
|
||||
interpretor_output.print_to_screen();
|
||||
}
|
||||
}
|
||||
|
||||
type LineReader = linefeed::Reader<linefeed::terminal::DefaultTerminal>;
|
||||
struct Repl {
|
||||
pub show_tokens: bool,
|
||||
pub show_parse: bool,
|
||||
pub show_llvm_ir: bool,
|
||||
options: EvalOptions,
|
||||
languages: Vec<Box<ProgrammingLanguageInterface>>,
|
||||
current_language_index: usize,
|
||||
interpreter_directive_sigil: char,
|
||||
@ -153,9 +118,7 @@ impl Repl {
|
||||
reader.set_prompt(">> ");
|
||||
let i = if initial_index < languages.len() { initial_index } else { 0 };
|
||||
Repl {
|
||||
show_tokens: false,
|
||||
show_parse: false,
|
||||
show_llvm_ir: false,
|
||||
options: EvalOptions::default(),
|
||||
languages: languages,
|
||||
current_language_index: i,
|
||||
interpreter_directive_sigil: '.',
|
||||
@ -190,16 +153,7 @@ impl Repl {
|
||||
|
||||
fn input_handler(&mut self, input: &str) -> String {
|
||||
let ref mut language = self.languages[self.current_language_index];
|
||||
|
||||
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);
|
||||
let interpretor_output = language.evaluate_in_repl(input, &self.options);
|
||||
interpretor_output.to_string()
|
||||
}
|
||||
|
||||
@ -266,13 +220,13 @@ impl Repl {
|
||||
}
|
||||
};
|
||||
match commands.get(2) {
|
||||
Some(&"tokens") => self.show_tokens = show,
|
||||
Some(&"parse") => self.show_parse = show,
|
||||
Some(&"tokens") => self.options.debug_tokens = show,
|
||||
Some(&"parse") => self.options.debug_parse = show,
|
||||
Some(&"eval") => {
|
||||
//let ref mut language = self.languages[self.current_language_index];
|
||||
//language.set_option("trace_evaluation", show);
|
||||
},
|
||||
Some(&"llvm") => self.show_llvm_ir = show,
|
||||
Some(&"llvm") => self.options.show_llvm_ir = show,
|
||||
Some(e) => {
|
||||
println!("Bad `show`/`hide` argument: {}", e);
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -137,7 +137,7 @@ impl ProgrammingLanguageInterface for Robo {
|
||||
"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 tokens = match tokenize(input) {
|
||||
Ok(tokens) => tokens,
|
||||
|
@ -16,7 +16,7 @@ impl ProgrammingLanguageInterface for Schala {
|
||||
"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 tokens = match parsing::tokenize(input) {
|
||||
Ok(tokens) => {
|
||||
|
Loading…
Reference in New Issue
Block a user