From 78ba4e1ed362541239d08883db602f83c5a56e28 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 11 May 2018 17:24:11 -0700 Subject: [PATCH] Variable lookup --- schala-lang/src/ast_reducing.rs | 10 ++++++---- schala-lang/src/eval.rs | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/schala-lang/src/ast_reducing.rs b/schala-lang/src/ast_reducing.rs index 3555907..c63f4e2 100644 --- a/schala-lang/src/ast_reducing.rs +++ b/schala-lang/src/ast_reducing.rs @@ -6,7 +6,7 @@ use builtin::{BinOp, PrefixOp}; #[derive(Debug)] pub struct ReducedAST(pub Vec); -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum Stmt { Binding { name: Rc, @@ -16,10 +16,11 @@ pub enum Stmt { Expr(Expr), } -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum Expr { Lit(Lit), Func(Func), + Val(Rc), Call { f: Func, args: Vec, @@ -27,7 +28,7 @@ pub enum Expr { UnimplementedSigilValue } -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum Lit { Nat(u64), Int(i64), @@ -36,7 +37,7 @@ pub enum Lit { StringLit(Rc), } -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum Func { BuiltIn(Rc), UserDefined { @@ -70,6 +71,7 @@ impl Expression { &BoolLiteral(ref b) => Expr::Lit(Lit::Bool(*b)), &BinExp(ref binop, ref lhs, ref rhs) => binop.reduce(lhs, rhs), &PrefixExp(ref op, ref arg) => op.reduce(arg), + &Value(ref name) => Expr::Val(name.clone()), e => Expr::UnimplementedSigilValue, } } diff --git a/schala-lang/src/eval.rs b/schala-lang/src/eval.rs index d3d0e87..eceaed8 100644 --- a/schala-lang/src/eval.rs +++ b/schala-lang/src/eval.rs @@ -383,6 +383,7 @@ impl<'a> State<'a> { match expr { literal @ Lit(_) => Ok(literal), Call { f, args } => self.apply_function(f, args), + Val(v) => self.value(v), _ => Err(format!("NOT IMPLEMENTED YET")) } } @@ -430,4 +431,27 @@ impl<'a> State<'a> { _ => return Err(format!("Runtime error: not yet implemented")), }) } + + fn value(&mut self, name: Rc) -> EvalResult { + use self::ValueEntry::*; + match self.values.lookup(&name) { + None => return Err(format!("Value {} not found", *name)), + Some(lookup) => match lookup { + &Binding { ref val, .. } => Ok(val.clone()), + _ => Err(format!("Functions not done")), + } + } + /* + fn eval_value(&mut self, name: Rc) -> EvalResult { + use self::ValueEntry::*; + match self.lookup(&name) { + None => return Err(format!("Value {} not found", *name)), + Some(lookup) => match lookup { + &Binding { ref val } => Ok(val.clone()), + &Function { .. } => Ok(FullyEvaluatedExpr::FuncLit(name.clone())) + } + } + } + */ + } }