Adding, subtracting works

This commit is contained in:
greg 2016-01-21 17:42:45 -08:00
parent 044f534ac5
commit 7114e446a4

View File

@ -103,9 +103,36 @@ impl Evaluator {
let expr = self.varmap.lookup_binding(var).unwrap();
expr.clone()
},
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)
}
},
_ => unimplemented!(),
}
}
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),
_ => unimplemented!(),
},
"-" => match (left, right) {
(Number(l), Number(r)) => Number(l - r),
_ => unimplemented!(),
},
"=" => match (left, right) {
_ => unimplemented!()
},
_ => unimplemented!(),
}
}
}