From 3abe2993612496bc475c332a2d7a79fbcf607034 Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 30 Aug 2017 19:15:04 -0700 Subject: [PATCH] More work on new trait structure --- src/language.rs | 16 ++++++------- src/maaru_lang/mod.rs | 56 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/src/language.rs b/src/language.rs index c277a84..c76062a 100644 --- a/src/language.rs +++ b/src/language.rs @@ -97,22 +97,20 @@ impl LanguageInterface for (PL, PL::Evaluator) where PL: Programmin /* below here is new versions of everything */ -#[derive(Debug, PartialEq)] -pub struct EvalStage { - index: u8, - name: String, -} +#[derive(Debug)] pub struct EvalOptions { - debug_stages: Vec, + pub debug_tokens: bool, + pub debug_parse: bool, + pub debug_type: bool, + pub debug_eval: bool, } pub trait ProgrammingLanguageInterface { - fn evaluate_in_repl(&mut self, input: &str, eval_options: EvalOptions); + fn evaluate_in_repl(&mut self, input: &str, eval_options: EvalOptions) -> Vec; fn get_language_name(&self) -> String; - fn get_stages(&self) -> Vec; } pub trait CompileableLanguage : ProgrammingLanguageInterface { - fn compile(&mut self); + fn compile(&mut self) -> LLVMCodeString; } diff --git a/src/maaru_lang/mod.rs b/src/maaru_lang/mod.rs index 3666d93..e29beaa 100644 --- a/src/maaru_lang/mod.rs +++ b/src/maaru_lang/mod.rs @@ -4,10 +4,64 @@ pub mod parser; pub mod eval; pub mod compilation; -use language::{ProgrammingLanguage, EvaluationMachine, ParseError, TokenError, LLVMCodeString}; +use language::{ProgrammingLanguageInterface, EvalOptions, ProgrammingLanguage, EvaluationMachine, ParseError, TokenError, LLVMCodeString}; pub use self::eval::Evaluator as MaaruEvaluator; +pub struct NewMaaru<'a> { + evaluator: MaaruEvaluator<'a> +} + +impl<'a> NewMaaru<'a> { + fn new() -> NewMaaru<'a> { + NewMaaru { + evaluator: MaaruEvaluator::new(None), + } + } +} + +impl<'a> ProgrammingLanguageInterface for NewMaaru<'a> { + fn get_language_name(&self) -> String { + "Maaru".to_string() + } + + fn evaluate_in_repl(&mut self, input: &str, options: EvalOptions) -> Vec { + let mut output = vec![]; + let tokens = match tokenizer::tokenize(input) { + Ok(tokens) => { + if options.debug_tokens { + output.push(format!("{:?}", tokens)); + } + tokens + }, + Err(err) => { + let msg = format!("Tokenization error: {:?}\n", err.msg); + output.push(msg); + return output; + } + }; + + let ast = match parser::parse(&tokens, &[]) { + Ok(ast) => { + if options.debug_parse { + output.push(format!("{:?}", ast)); + } + ast + }, + Err(err) => { + let msg = format!("Parse error: {:?}\n", err.msg); + output.push(msg); + return output; + } + }; + + let evaluation_output = self.evaluator.run(ast); + output.extend(evaluation_output); + + return output; + } +} + pub struct Maaru<'a> { marker: PhantomData<&'a ()> } impl<'a> Maaru<'a> { pub fn new() -> Maaru <'a> {