From f6536e7ebd253eecd3ef9814c425029464339259 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 1 Oct 2017 19:09:55 -0700 Subject: [PATCH] Evaluation stuff --- src/schala_lang/eval.rs | 67 +++++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/src/schala_lang/eval.rs b/src/schala_lang/eval.rs index 3e9ee8a..8b597e3 100644 --- a/src/schala_lang/eval.rs +++ b/src/schala_lang/eval.rs @@ -3,6 +3,17 @@ use schala_lang::parsing::{AST, Statement, Declaration, Expression, ExpressionTy pub struct ReplState { } +type EvalResult = Result; + +enum FullyEvaluatedExpr { + UnsignedInt(u64), + SignedInt(i64), + Float(f64), + Str(String), + Bool(bool), + Other +} + impl ReplState { pub fn new() -> ReplState { ReplState { } @@ -11,9 +22,17 @@ impl ReplState { pub fn evaluate(&mut self, ast: AST) -> String { let mut acc = String::new(); for statement in ast.0 { - if let Some(output) = self.eval_statement(statement) { - acc.push_str(&output); - acc.push_str("\n"); + match self.eval_statement(statement) { + Ok(output) => { + if let Some(s) = output { + acc.push_str(&s); + acc.push_str("\n"); + } + }, + Err(error) => { + acc.push_str(&format!("Error: {}", error)); + return acc; + }, } } format!("{}", acc) @@ -21,28 +40,44 @@ impl ReplState { } impl ReplState { - fn eval_statement(&mut self, statement: Statement) -> Option { + fn eval_statement(&mut self, statement: Statement) -> EvalResult> { + use self::FullyEvaluatedExpr::*; match statement { - Statement::ExpressionStatement(expr) => self.eval_expr(expr), - Statement::Declaration(decl) => self.eval_decl(decl), + Statement::ExpressionStatement(expr) => { + self.eval_expr(expr).map( |eval| { + match eval { + UnsignedInt(n) => Some(format!("{}", n)), + SignedInt(n) => Some(format!("{}", n)), + Float(f) => Some(format!("{}", f)), + Str(s) => Some(format!("\"{}\"", s)), + Bool(b) => Some(format!("{}", b)), + _ => None, + } + }) + }, + Statement::Declaration(decl) => { + self.eval_decl(decl); + Ok(None) + } } } - fn eval_decl(&mut self, decl: Declaration) -> Option { - Some("UNIMPLEMENTED".to_string()) + fn eval_decl(&mut self, decl: Declaration) -> EvalResult<()> { + Err("Not implmemented".to_string()) } - fn eval_expr(&mut self, expr: Expression) -> Option { + fn eval_expr(&mut self, expr: Expression) -> EvalResult { use self::ExpressionType::*; + use self::FullyEvaluatedExpr::*; let expr_type = expr.0; - Some(match expr_type { - IntLiteral(n) => format!("{}", n), - FloatLiteral(f) => format!("{}", f), - StringLiteral(s) => format!("{}", s), - BoolLiteral(b) => format!("{}", b), - _ => format!("UNIMPLEMENTED"), - }) + match expr_type { + IntLiteral(n) => Ok(UnsignedInt(n)), + FloatLiteral(f) => Ok(Float(f)), + StringLiteral(s) => Ok(Str(s.to_string())), + BoolLiteral(b) => Ok(Bool(b)), + _ => Err(format!("Unimplemented")), + } } }