From 9d6bdf22da3ea47b099319bb1d96c8c2a35ea2ac Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 23 Jan 2017 01:32:57 -0800 Subject: [PATCH] More conversions to trait version --- src/compilation.rs | 3 +-- src/language.rs | 4 ++-- src/main.rs | 38 +++++++++++++------------------------- 3 files changed, 16 insertions(+), 29 deletions(-) diff --git a/src/compilation.rs b/src/compilation.rs index 1b66870..65dc903 100644 --- a/src/compilation.rs +++ b/src/compilation.rs @@ -11,7 +11,7 @@ use parser::{AST, Statement, Function, Prototype, Expression, BinOp}; use llvm_wrap as LLVMWrap; -pub fn compilation_sequence(ast: AST, sourcefile: &str) { +pub fn compilation_sequence(llvm_code: String, sourcefile: &str) { use std::process::Command; let ll_filename = "out.ll"; @@ -22,7 +22,6 @@ pub fn compilation_sequence(ast: AST, sourcefile: &str) { _ => panic!("Bad filename {}", sourcefile), }; - let llvm_code = compile_ast(ast); println!("Compilation process finished for {}", ll_filename); File::create(ll_filename) .and_then(|mut f| f.write_all(llvm_code.as_bytes())) diff --git a/src/language.rs b/src/language.rs index ee93deb..a114450 100644 --- a/src/language.rs +++ b/src/language.rs @@ -14,6 +14,6 @@ pub trait ProgrammingLanguage { fn tokenize(input: &str) -> Result, TokenError>; fn parse(input: Vec) -> Result; - fn evaluate(input: Self::AST, evaluator: &mut Evaluator) -> Vec; - fn compile(input: &Self::AST); + fn evaluate(ast: Self::AST, evaluator: &mut Evaluator) -> Vec; + fn compile(ast: Self::AST) -> String; } diff --git a/src/main.rs b/src/main.rs index e929723..7ba7fab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,10 +7,7 @@ use std::fs::File; use std::io::Read; use std::process; -use tokenizer::tokenize; mod tokenizer; - -use parser::parse; mod parser; use eval::Evaluator; @@ -40,7 +37,8 @@ fn main() { repl.run(); } [_, ref filename, _..] => { - run_noninteractive(filename, !option_matches.opt_present("i"), trace); + let language = Schala { }; + run_noninteractive(filename, !option_matches.opt_present("i"), trace, &language); } }; } @@ -59,12 +57,12 @@ fn program_options() -> getopts::Options { options } -fn run_noninteractive(filename: &str, compile: bool, trace_evaluation: bool) { +fn run_noninteractive<'a, T: ProgrammingLanguage>>(filename: &str, compile: bool, trace_evaluation: bool, language: &T) { let mut source_file = File::open(&Path::new(filename)).unwrap(); let mut buffer = String::new(); source_file.read_to_string(&mut buffer).unwrap(); - let tokens = match tokenize(&buffer) { + let tokens = match T::tokenize(&buffer) { Ok(t) => t, Err(e) => { println!("Tokenization error: {}", e.msg); @@ -72,20 +70,20 @@ fn run_noninteractive(filename: &str, compile: bool, trace_evaluation: bool) { } }; - let ast = match parse(&tokens, &[]) { + let ast = match T::parse(tokens) { Ok(ast) => ast, Err(err) => { println!("Parse error: {:?}", err.msg); - println!("Remaining tokens: {:?}", err.remaining_tokens); + /*println!("Remaining tokens: {:?}", err.remaining_tokens);*/ std::process::exit(1) } }; if compile { - compilation_sequence(ast, filename); + compilation_sequence(T::compile(ast), filename); } else { let mut evaluator = Evaluator::new_with_opts(None, trace_evaluation); - let results = evaluator.run(ast); + let results = T::evaluate(ast, &mut evaluator); for result in results.iter() { println!("{}", result); } @@ -140,13 +138,6 @@ impl<'a> Repl<'a> { println!("Exiting..."); } - fn new_input_handler(input: &str) -> String { - - let language = Schala {}; - - unimplemented!() - } - fn input_handler(&mut self, input: &str) -> String { let schala = Schala { }; self.input_handler_new(input, schala) @@ -179,12 +170,9 @@ impl<'a> Repl<'a> { output.push_str(&format!("AST: {:?}\n", ast)); } - /* if self.show_llvm_ir { - let s = compile_ast(ast); + let s = T::compile(ast); output.push_str(&s); - */ - if false { } else { // for now only handle last output let mut full_output: Vec = T::evaluate(ast, &mut self.evaluator); @@ -258,11 +246,11 @@ impl<'a> ProgrammingLanguage> for Schala { fn parse(input: Vec) -> Result { parser::parse(&input, &[]).map_err(|x| ParseError { msg: x.msg }) } - fn evaluate(input: Self::AST, evaluator: &mut Evaluator) -> Vec { - evaluator.run(input) + fn evaluate(ast: Self::AST, evaluator: &mut Evaluator) -> Vec { + evaluator.run(ast) } - fn compile(input: &Self::AST) { - unimplemented!() + fn compile(ast: Self::AST) -> String { + compile_ast(ast) } }