From 29d4cb53a4bcf76e1158bf89799a6ce5fb0a421c Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 23 Jan 2016 01:32:06 -0800 Subject: [PATCH] Add infrastructure to do function evaluation Right now you can successfully evaluate a function definition (returning Null), but cannot call a function --- src/eval.rs | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/eval.rs b/src/eval.rs index 3af8852..49e7387 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; -use parser::{AST, ASTNode, Expression}; +use parser::{AST, ASTNode, Expression, Function}; struct Varmap { map: HashMap @@ -20,14 +20,34 @@ impl Varmap { } } +struct Funcmap { + map: HashMap, +} + +impl Funcmap { + fn new() -> Funcmap { + let map = HashMap::new(); + Funcmap { map: map } + } + + fn add_function(&mut self, name: String, function: Function) { + self.map.insert(name, function); + } + + fn lookup_function(&mut self, name: String) -> Option<&Function> { + self.map.get(&name) + } +} + pub struct Evaluator { - varmap: Varmap + varmap: Varmap, + funcmap: Funcmap, } impl Evaluator { pub fn new() -> Evaluator { - Evaluator { varmap: Varmap::new() } + Evaluator { varmap: Varmap::new(), funcmap: Funcmap::new() } } pub fn run(&mut self, ast: AST) -> Vec { @@ -46,7 +66,7 @@ impl Evaluable for ASTNode { use parser::ASTNode::*; match self { &ExprNode(ref expr) => expr.is_reducible(), - _ => unimplemented!(), + &FuncNode(ref function) => true, } } } @@ -72,7 +92,7 @@ impl Evaluator { } } - format!("{}", node) //TODO make better + format!("{}", node) } fn step(&mut self, node: ASTNode) -> ASTNode { @@ -89,7 +109,11 @@ impl Evaluator { ExprNode(expr) } }, - _ => unimplemented!(), + FuncNode(func) => { + let fn_name = func.prototype.name.clone(); + self.funcmap.add_function(fn_name, func); + ExprNode(Expression::Null) + }, } }