Start function call reduction
Move the varmap and funcmap functions to the Evaluator, to mkae it easier to facilitate separate frames
This commit is contained in:
parent
6bb227d052
commit
dcde5d6018
55
src/eval.rs
55
src/eval.rs
@ -10,14 +10,6 @@ impl Varmap {
|
|||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
Varmap { map: map }
|
Varmap { map: map }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_binding(&mut self, var: String, value: Expression) {
|
|
||||||
self.map.insert(var, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lookup_binding(&mut self, var: String) -> Option<&Expression> {
|
|
||||||
self.map.get(&var)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Funcmap {
|
struct Funcmap {
|
||||||
@ -29,14 +21,6 @@ impl Funcmap {
|
|||||||
let map = HashMap::new();
|
let map = HashMap::new();
|
||||||
Funcmap { map: map }
|
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 {
|
||||||
@ -55,6 +39,22 @@ impl Evaluator {
|
|||||||
self.reduce(astnode)
|
self.reduce(astnode)
|
||||||
}).collect()
|
}).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn add_binding(&mut self, var: String, value: Expression) {
|
||||||
|
self.varmap.map.insert(var, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lookup_binding(&mut self, var: String) -> Option<&Expression> {
|
||||||
|
self.varmap.map.get(&var)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_function(&mut self, name: String, function: Function) {
|
||||||
|
self.funcmap.map.insert(name, function);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lookup_function(&mut self, name: String) -> Option<&Function> {
|
||||||
|
self.funcmap.map.get(&name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trait Evaluable {
|
trait Evaluable {
|
||||||
@ -111,7 +111,7 @@ impl Evaluator {
|
|||||||
},
|
},
|
||||||
FuncNode(func) => {
|
FuncNode(func) => {
|
||||||
let fn_name = func.prototype.name.clone();
|
let fn_name = func.prototype.name.clone();
|
||||||
self.funcmap.add_function(fn_name, func);
|
self.add_function(fn_name, func);
|
||||||
ExprNode(Expression::Null)
|
ExprNode(Expression::Null)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -120,10 +120,11 @@ impl Evaluator {
|
|||||||
fn reduce_expr(&mut self, expression: Expression) -> Expression {
|
fn reduce_expr(&mut self, expression: Expression) -> Expression {
|
||||||
use parser::Expression::*;
|
use parser::Expression::*;
|
||||||
match expression {
|
match expression {
|
||||||
|
Null => Null,
|
||||||
e@StringLiteral(_) => e,
|
e@StringLiteral(_) => e,
|
||||||
e@Number(_) => e,
|
e@Number(_) => e,
|
||||||
Variable(var) => {
|
Variable(var) => {
|
||||||
match self.varmap.lookup_binding(var) {
|
match self.lookup_binding(var) {
|
||||||
None => Null,
|
None => Null,
|
||||||
Some(expr) => expr.clone()
|
Some(expr) => expr.clone()
|
||||||
}
|
}
|
||||||
@ -138,7 +139,7 @@ impl Evaluator {
|
|||||||
if op == "=" {
|
if op == "=" {
|
||||||
match left {
|
match left {
|
||||||
Variable(var) => {
|
Variable(var) => {
|
||||||
self.varmap.add_binding(var, right);
|
self.add_binding(var, right);
|
||||||
return Null;
|
return Null;
|
||||||
},
|
},
|
||||||
_ => ()
|
_ => ()
|
||||||
@ -152,7 +153,7 @@ impl Evaluator {
|
|||||||
self.reduce_binop(op, left, right) //can assume both arguments are maximally reduced
|
self.reduce_binop(op, left, right) //can assume both arguments are maximally reduced
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => unimplemented!(),
|
Call(name, args) => self.reduce_call(name, args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,12 +183,22 @@ impl Evaluator {
|
|||||||
},
|
},
|
||||||
"=" => match (left, right) {
|
"=" => match (left, right) {
|
||||||
(Variable(var), right) => {
|
(Variable(var), right) => {
|
||||||
self.varmap.add_binding(var, right);
|
self.add_binding(var, right);
|
||||||
Null
|
Null
|
||||||
},
|
},
|
||||||
_ => Null,
|
_ => Null,
|
||||||
},
|
},
|
||||||
_ => unimplemented!(),
|
_ => Null,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn reduce_call(&mut self, name: String, arguments: Vec<Expression>) -> Expression {
|
||||||
|
use parser::Expression::*;
|
||||||
|
let function = match self.lookup_function(name) {
|
||||||
|
Some(ref func) => func.clone(),
|
||||||
|
None => return Null
|
||||||
|
};
|
||||||
|
|
||||||
|
Null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user