2016-01-20 03:52:11 -08:00
|
|
|
use std::collections::HashMap;
|
2016-01-20 02:26:46 -08:00
|
|
|
use parser::{AST, ASTNode, Expression};
|
2016-01-18 02:24:14 -08:00
|
|
|
|
2016-01-20 03:52:11 -08:00
|
|
|
struct Varmap {
|
|
|
|
map: HashMap<String, Expression>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Varmap {
|
|
|
|
fn new() -> Varmap {
|
2016-01-21 01:16:01 -08:00
|
|
|
let mut map = HashMap::new();
|
|
|
|
map.insert("a".to_string(), Expression::Number(10.0));
|
|
|
|
Varmap { map: map }
|
2016-01-20 03:52:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 02:24:14 -08:00
|
|
|
pub struct Evaluator {
|
2016-01-20 03:52:11 -08:00
|
|
|
varmap: Varmap
|
2016-01-18 02:24:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Evaluator {
|
|
|
|
|
|
|
|
pub fn new() -> Evaluator {
|
2016-01-20 03:52:11 -08:00
|
|
|
Evaluator { varmap: Varmap::new() }
|
2016-01-18 02:24:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(&mut self, ast: AST) -> Vec<String> {
|
|
|
|
ast.into_iter().map(|astnode| {
|
|
|
|
self.reduce_node(astnode)
|
|
|
|
}).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 01:30:48 -08:00
|
|
|
trait Evaluable {
|
|
|
|
fn is_reducible(&self) -> bool;
|
|
|
|
}
|
|
|
|
|
2016-01-20 02:26:46 -08:00
|
|
|
impl Evaluable for ASTNode {
|
|
|
|
fn is_reducible(&self) -> bool {
|
|
|
|
use parser::ASTNode::*;
|
|
|
|
match self {
|
|
|
|
&ExprNode(ref expr) => expr.is_reducible(),
|
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Evaluable for Expression {
|
|
|
|
fn is_reducible(&self) -> bool {
|
|
|
|
use parser::Expression::*;
|
|
|
|
match *self {
|
2016-01-21 19:12:07 -08:00
|
|
|
Null => false,
|
2016-01-20 02:26:46 -08:00
|
|
|
StringLiteral(_) => false,
|
|
|
|
Number(_) => false,
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 02:24:14 -08:00
|
|
|
impl Evaluator {
|
|
|
|
fn reduce_node(&mut self, mut node: ASTNode) -> String {
|
|
|
|
loop {
|
2016-01-20 02:26:46 -08:00
|
|
|
node = self.step(node);
|
|
|
|
if !node.is_reducible() {
|
2016-01-18 02:24:14 -08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
format!("{:?}", node) //TODO make better
|
|
|
|
}
|
|
|
|
|
2016-01-20 02:26:46 -08:00
|
|
|
fn step(&mut self, node: ASTNode) -> ASTNode {
|
|
|
|
println!("Doing one step, current node is {:?}", node);
|
2016-01-21 00:54:54 -08:00
|
|
|
self.reduce(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reduce(&mut self, node: ASTNode) -> ASTNode {
|
|
|
|
use parser::ASTNode::*;
|
|
|
|
match node {
|
|
|
|
ExprNode(expr) => {
|
|
|
|
if expr.is_reducible() {
|
|
|
|
ExprNode(self.reduce_expr(expr))
|
|
|
|
} else {
|
|
|
|
ExprNode(expr)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reduce_expr(&mut self, expression: Expression) -> Expression {
|
|
|
|
use parser::Expression::*;
|
|
|
|
match expression {
|
|
|
|
e@StringLiteral(_) => e,
|
|
|
|
e@Number(_) => e,
|
2016-01-21 01:16:01 -08:00
|
|
|
Variable(var) => {
|
|
|
|
let expr = self.varmap.lookup_binding(var).unwrap();
|
|
|
|
expr.clone()
|
|
|
|
},
|
2016-01-21 17:42:45 -08:00
|
|
|
BinExp(op, box left, box right) => {
|
|
|
|
if left.is_reducible() {
|
|
|
|
let new = self.reduce_expr(left);
|
|
|
|
BinExp(op, Box::new(new), Box::new(right))
|
|
|
|
} else if right.is_reducible() {
|
|
|
|
let new = self.reduce_expr(right);
|
|
|
|
BinExp(op, Box::new(left), Box::new(new))
|
|
|
|
} else {
|
|
|
|
self.reduce_binop(op, left, right)
|
|
|
|
}
|
|
|
|
},
|
2016-01-21 00:54:54 -08:00
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
2016-01-21 17:42:45 -08:00
|
|
|
}
|
2016-01-21 00:54:54 -08:00
|
|
|
|
2016-01-21 17:42:45 -08:00
|
|
|
fn reduce_binop(&mut self, op: String, left: Expression, right: Expression) -> Expression {
|
|
|
|
use parser::Expression::*;
|
|
|
|
match &op[..] {
|
|
|
|
"+" => match (left, right) {
|
|
|
|
(Number(l), Number(r)) => Number(l + r),
|
2016-01-21 19:12:07 -08:00
|
|
|
_ => Null,
|
2016-01-21 17:42:45 -08:00
|
|
|
},
|
|
|
|
"-" => match (left, right) {
|
|
|
|
(Number(l), Number(r)) => Number(l - r),
|
2016-01-21 19:12:07 -08:00
|
|
|
_ => Null,
|
2016-01-21 17:42:45 -08:00
|
|
|
},
|
|
|
|
"=" => match (left, right) {
|
|
|
|
_ => unimplemented!()
|
|
|
|
},
|
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
2016-01-18 02:24:14 -08:00
|
|
|
}
|
|
|
|
}
|