From abbbb34901a20035af07c28576fc3e8fbf6dff26 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 2 Oct 2017 20:11:27 -0700 Subject: [PATCH] Some very basic evaluation stuff --- src/schala_lang/eval.rs | 33 ++++++++++++++++++++++++++++++++- src/schala_lang/parsing.rs | 2 +- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/schala_lang/eval.rs b/src/schala_lang/eval.rs index bb13fd0..f9fc283 100644 --- a/src/schala_lang/eval.rs +++ b/src/schala_lang/eval.rs @@ -1,4 +1,4 @@ -use schala_lang::parsing::{AST, Statement, Declaration, Expression, ExpressionType, TypeAnno}; +use schala_lang::parsing::{AST, Statement, Declaration, Expression, ExpressionType, Operation, TypeAnno}; pub struct ReplState { } @@ -72,9 +72,40 @@ impl ReplState { FloatLiteral(f) => Ok(Float(f)), StringLiteral(s) => Ok(Str(s.to_string())), BoolLiteral(b) => Ok(Bool(b)), + PrefixExp(op, expr) => self.eval_prefix_exp(op, expr), + BinExp(op, lhs, rhs) => self.eval_binexp(op, lhs, rhs), _ => Err(format!("Unimplemented")), } } + + fn eval_binexp(&mut self, op: Operation, lhs: Box, rhs: Box) -> EvalResult { + use self::FullyEvaluatedExpr::*; + let evaled_lhs = self.eval_expr(*lhs)?; + let evaled_rhs = self.eval_expr(*rhs)?; + let opstr: &str = &op.0; + Ok(match (opstr, evaled_lhs, evaled_rhs) { + ("+", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l + r), + ("-", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l - r), + ("*", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l * r), + ("/", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l / r), + ("%", UnsignedInt(l), UnsignedInt(r)) => UnsignedInt(l % r), + _ => return Err(format!("Runtime error: not yet implemented")), + }) + } + + fn eval_prefix_exp(&mut self, op: Operation, expr: Box) -> EvalResult { + use self::FullyEvaluatedExpr::*; + let evaled_expr = self.eval_expr(*expr)?; + let opstr: &str = &op.0; + + Ok(match (opstr, evaled_expr) { + ("!", Bool(true)) => Bool(false), + ("!", Bool(false)) => Bool(true), + ("-", UnsignedInt(n)) => SignedInt(-1*(n as i64)), + ("-", SignedInt(n)) => SignedInt(-1*(n as i64)), + _ => return Err(format!("Runtime error: not yet implemented")), + }) + } } pub enum TypeCheck { diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index 092d6b2..3197786 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -463,7 +463,7 @@ pub struct MatchArm { pub struct Pattern(Rc); #[derive(Debug, PartialEq)] -pub struct Operation(Rc); +pub struct Operation(pub Rc); impl Operation { fn min_precedence() -> i32 {