Add infrastructure to do function evaluation

Right now you can successfully evaluate a function definition (returning
Null), but cannot call a function
This commit is contained in:
greg 2016-01-23 01:32:06 -08:00
parent 3915c1f035
commit 29d4cb53a4

View File

@ -1,5 +1,5 @@
use std::collections::HashMap; use std::collections::HashMap;
use parser::{AST, ASTNode, Expression}; use parser::{AST, ASTNode, Expression, Function};
struct Varmap { struct Varmap {
map: HashMap<String, Expression> map: HashMap<String, Expression>
@ -20,14 +20,34 @@ impl Varmap {
} }
} }
struct Funcmap {
map: HashMap<String, Function>,
}
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 { pub struct Evaluator {
varmap: Varmap varmap: Varmap,
funcmap: Funcmap,
} }
impl Evaluator { impl Evaluator {
pub fn new() -> Evaluator { pub fn new() -> Evaluator {
Evaluator { varmap: Varmap::new() } Evaluator { varmap: Varmap::new(), funcmap: Funcmap::new() }
} }
pub fn run(&mut self, ast: AST) -> Vec<String> { pub fn run(&mut self, ast: AST) -> Vec<String> {
@ -46,7 +66,7 @@ impl Evaluable for ASTNode {
use parser::ASTNode::*; use parser::ASTNode::*;
match self { match self {
&ExprNode(ref expr) => expr.is_reducible(), &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 { fn step(&mut self, node: ASTNode) -> ASTNode {
@ -89,7 +109,11 @@ impl Evaluator {
ExprNode(expr) ExprNode(expr)
} }
}, },
_ => unimplemented!(), FuncNode(func) => {
let fn_name = func.prototype.name.clone();
self.funcmap.add_function(fn_name, func);
ExprNode(Expression::Null)
},
} }
} }