Make function binding a SideEffect

This commit is contained in:
greg 2017-01-03 00:26:48 -08:00
parent 2722533efd
commit 872e9ce7ee

View File

@ -9,6 +9,7 @@ type Reduction<T> = (T, Option<SideEffect>);
enum SideEffect { enum SideEffect {
Print(String), Print(String),
AddBinding(String, Expression), AddBinding(String, Expression),
AddFunctionBinding(Function),
} }
struct EnvFrame { struct EnvFrame {
@ -140,6 +141,9 @@ impl Evaluator {
Print(s) => println!("{}", s), Print(s) => println!("{}", s),
AddBinding(var, value) => { AddBinding(var, value) => {
self.add_binding(var, value); self.add_binding(var, value);
},
AddFunctionBinding(function) => {
self.add_function(function.prototype.name.clone(), function);
} }
} }
} }
@ -156,10 +160,8 @@ impl Evaluator {
} }
} }
FuncDefNode(func) => { FuncDefNode(func) => {
let fn_name = func.prototype.name.clone(); let binding = Some(SideEffect::AddFunctionBinding(func.clone()));
// TODO get rid of this clone (ExprNode(Expression::Lambda(func)), binding)
self.add_function(fn_name, func.clone());
(ExprNode(Expression::Lambda(func)), None)
} }
} }
} }