From 77f72806bef37151e68e8e04ef40fa3826289d4c Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 23 Jan 2016 20:49:16 -0800 Subject: [PATCH] Add support for multiple frames --- src/eval.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/eval.rs b/src/eval.rs index 38dc8ca..8284258 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -26,12 +26,16 @@ impl Funcmap { pub struct Evaluator { varmap: Varmap, funcmap: Funcmap, + frames: Vec, } impl Evaluator { pub fn new() -> Evaluator { - Evaluator { varmap: Varmap::new(), funcmap: Funcmap::new() } + Evaluator { varmap: Varmap::new(), + funcmap: Funcmap::new(), + frames: Vec::new(), + } } pub fn run(&mut self, ast: AST) -> Vec { @@ -44,8 +48,18 @@ impl Evaluator { self.varmap.map.insert(var, value); } - fn lookup_binding(&mut self, var: String) -> Option<&Expression> { - self.varmap.map.get(&var) + fn lookup_binding(&mut self, var: String) -> Option { + self.varmap.map.get(&var).map(|expr| expr.clone()) + .or_else(|| { + for frame in self.frames.iter() { + match frame.map.get(&var) { + None => (), + Some(expr) => return Some(expr.clone()), + } + } + + None + }) } fn add_function(&mut self, name: String, function: Function) { @@ -126,7 +140,7 @@ impl Evaluator { Variable(var) => { match self.lookup_binding(var) { None => Null, - Some(expr) => expr.clone() + Some(expr) => expr, } }, BinExp(op, box left, box right) => {