From 8ef5a28aff26c8c6108c7a52d56c48117863adc1 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 3 Mar 2018 00:25:08 -0800 Subject: [PATCH] Evaluator now only prints when a builtin print is called --- schala-lib/src/lib.rs | 3 +-- source_files/first.schala | 2 +- src/schala_lang/eval.rs | 26 +++++++++++++++++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/schala-lib/src/lib.rs b/schala-lib/src/lib.rs index e95a391..5b55adf 100644 --- a/schala-lib/src/lib.rs +++ b/schala-lib/src/lib.rs @@ -110,8 +110,7 @@ fn run_noninteractive(filename: &str, languages: Vec State<'a> { fn eval_application(&mut self, f: Expression, arguments: Vec) -> EvalResult { use self::ExpressionType::*; match f { + Expression(Value(ref identifier), _) if self.is_builtin(identifier) => self.eval_builtin(identifier, arguments), Expression(Value(identifier), _) => { match self.lookup(&identifier) { Some(&ValueEntry::Function { ref body, ref param_names }) => { @@ -190,7 +191,30 @@ impl<'a> State<'a> { x => Err(format!("Trying to apply {:?} which is not a function", x)), } } - + fn is_builtin(&self, name: &Rc) -> bool { + match &name.as_ref()[..] { + "print" | "println" => true, + _ => false + } + } + fn eval_builtin(&mut self, name: &Rc, args: Vec) -> EvalResult { + use self::FullyEvaluatedExpr::*; + match &name.as_ref()[..] { + "print" => { + for arg in args { + print!("{}", arg.to_string()); + } + Ok(Tuple(vec![])) + }, + "println" => { + for arg in args { + println!("{}", arg.to_string()); + } + Ok(Tuple(vec![])) + }, + _ => unreachable!() + } + } fn eval_value(&mut self, name: Rc) -> EvalResult { use self::ValueEntry::*; match self.lookup(&name) {