2016-01-20 03:52:11 -08:00
|
|
|
use std::collections::HashMap;
|
2016-01-23 01:32:06 -08:00
|
|
|
use parser::{AST, ASTNode, Expression, Function};
|
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();
|
|
|
|
Varmap { map: map }
|
2016-01-20 03:52:11 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 01:32:06 -08:00
|
|
|
struct Funcmap {
|
|
|
|
map: HashMap<String, Function>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Funcmap {
|
|
|
|
fn new() -> Funcmap {
|
|
|
|
let map = HashMap::new();
|
|
|
|
Funcmap { map: map }
|
|
|
|
}
|
2016-01-24 01:03:57 -08:00
|
|
|
|
|
|
|
fn lookup_function(&self, name: String) -> Option<Function> {
|
|
|
|
self.map.get(&name).map(|x| x.clone())
|
|
|
|
}
|
2016-01-23 01:32:06 -08:00
|
|
|
}
|
|
|
|
|
2016-01-18 02:24:14 -08:00
|
|
|
pub struct Evaluator {
|
2016-01-23 01:32:06 -08:00
|
|
|
varmap: Varmap,
|
|
|
|
funcmap: Funcmap,
|
2016-01-23 20:49:16 -08:00
|
|
|
frames: Vec<Varmap>,
|
2016-01-18 02:24:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Evaluator {
|
|
|
|
|
|
|
|
pub fn new() -> Evaluator {
|
2016-01-23 20:49:16 -08:00
|
|
|
Evaluator { varmap: Varmap::new(),
|
|
|
|
funcmap: Funcmap::new(),
|
|
|
|
frames: Vec::new(),
|
|
|
|
}
|
2016-01-18 02:24:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(&mut self, ast: AST) -> Vec<String> {
|
|
|
|
ast.into_iter().map(|astnode| {
|
2016-01-23 01:49:53 -08:00
|
|
|
self.reduce(astnode)
|
2016-01-18 02:24:14 -08:00
|
|
|
}).collect()
|
|
|
|
}
|
2016-01-23 19:19:38 -08:00
|
|
|
|
|
|
|
fn add_binding(&mut self, var: String, value: Expression) {
|
|
|
|
self.varmap.map.insert(var, value);
|
|
|
|
}
|
|
|
|
|
2016-01-23 20:49:16 -08:00
|
|
|
fn lookup_binding(&mut self, var: String) -> Option<Expression> {
|
|
|
|
self.varmap.map.get(&var).map(|expr| expr.clone())
|
|
|
|
.or_else(|| {
|
|
|
|
for frame in self.frames.iter() {
|
|
|
|
match frame.map.get(&var) {
|
|
|
|
None => (),
|
|
|
|
Some(expr) => return Some(expr.clone()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
})
|
2016-01-23 19:19:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_function(&mut self, name: String, function: Function) {
|
|
|
|
self.funcmap.map.insert(name, function);
|
|
|
|
}
|
|
|
|
|
2016-01-18 02:24:14 -08:00
|
|
|
}
|
|
|
|
|
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(),
|
2016-01-23 01:32:06 -08:00
|
|
|
&FuncNode(ref function) => true,
|
2016-01-20 02:26:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2016-01-23 01:49:53 -08:00
|
|
|
fn reduce(&mut self, mut node: ASTNode) -> String {
|
2016-01-18 02:24:14 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 01:32:06 -08:00
|
|
|
format!("{}", node)
|
2016-01-18 02:24:14 -08:00
|
|
|
}
|
|
|
|
|
2016-01-20 02:26:46 -08:00
|
|
|
fn step(&mut self, node: ASTNode) -> ASTNode {
|
2016-01-23 01:49:53 -08:00
|
|
|
self.reduce_astnode(node)
|
2016-01-21 00:54:54 -08:00
|
|
|
}
|
|
|
|
|
2016-01-23 01:49:53 -08:00
|
|
|
fn reduce_astnode(&mut self, node: ASTNode) -> ASTNode {
|
2016-01-21 00:54:54 -08:00
|
|
|
use parser::ASTNode::*;
|
|
|
|
match node {
|
|
|
|
ExprNode(expr) => {
|
|
|
|
if expr.is_reducible() {
|
|
|
|
ExprNode(self.reduce_expr(expr))
|
|
|
|
} else {
|
|
|
|
ExprNode(expr)
|
|
|
|
}
|
|
|
|
},
|
2016-01-23 01:32:06 -08:00
|
|
|
FuncNode(func) => {
|
|
|
|
let fn_name = func.prototype.name.clone();
|
2016-01-23 19:19:38 -08:00
|
|
|
self.add_function(fn_name, func);
|
2016-01-23 01:32:06 -08:00
|
|
|
ExprNode(Expression::Null)
|
|
|
|
},
|
2016-01-21 00:54:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reduce_expr(&mut self, expression: Expression) -> Expression {
|
|
|
|
use parser::Expression::*;
|
|
|
|
match expression {
|
2016-01-23 19:19:38 -08:00
|
|
|
Null => Null,
|
2016-01-21 00:54:54 -08:00
|
|
|
e@StringLiteral(_) => e,
|
|
|
|
e@Number(_) => e,
|
2016-01-21 01:16:01 -08:00
|
|
|
Variable(var) => {
|
2016-01-23 19:19:38 -08:00
|
|
|
match self.lookup_binding(var) {
|
2016-01-22 02:59:33 -08:00
|
|
|
None => Null,
|
2016-01-23 20:49:16 -08:00
|
|
|
Some(expr) => expr,
|
2016-01-22 02:59:33 -08:00
|
|
|
}
|
2016-01-21 01:16:01 -08:00
|
|
|
},
|
2016-01-21 17:42:45 -08:00
|
|
|
BinExp(op, box left, box right) => {
|
2016-01-22 02:15:02 -08:00
|
|
|
if right.is_reducible() {
|
|
|
|
let new = self.reduce_expr(right);
|
|
|
|
return BinExp(op, Box::new(left), Box::new(new));
|
|
|
|
}
|
|
|
|
|
|
|
|
//special case for variable assignment
|
|
|
|
if op == "=" {
|
|
|
|
match left {
|
|
|
|
Variable(var) => {
|
2016-01-23 19:19:38 -08:00
|
|
|
self.add_binding(var, right);
|
2016-01-22 02:15:02 -08:00
|
|
|
return Null;
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 17:42:45 -08:00
|
|
|
if left.is_reducible() {
|
|
|
|
let new = self.reduce_expr(left);
|
|
|
|
BinExp(op, Box::new(new), Box::new(right))
|
|
|
|
} else {
|
2016-01-22 02:15:02 -08:00
|
|
|
self.reduce_binop(op, left, right) //can assume both arguments are maximally reduced
|
2016-01-21 17:42:45 -08:00
|
|
|
}
|
|
|
|
},
|
2016-01-23 19:19:38 -08:00
|
|
|
Call(name, args) => self.reduce_call(name, args)
|
2016-01-21 00:54:54 -08:00
|
|
|
}
|
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-22 04:55:01 -08:00
|
|
|
(StringLiteral(s1), StringLiteral(s2)) => StringLiteral(format!("{}{}", s1, s2)),
|
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
|
|
|
},
|
2016-01-22 02:38:06 -08:00
|
|
|
"*" => match (left, right) {
|
|
|
|
(Number(l), Number(r)) => Number(l * r),
|
|
|
|
_ => Null,
|
|
|
|
},
|
|
|
|
"/" => match (left, right) {
|
|
|
|
(Number(l), Number(r)) if r != 0.0 => Number(l / r),
|
|
|
|
_ => Null,
|
|
|
|
},
|
|
|
|
"%" => match (left, right) {
|
|
|
|
(Number(l), Number(r)) => Number(l % r),
|
|
|
|
_ => Null,
|
|
|
|
},
|
2016-01-21 17:42:45 -08:00
|
|
|
"=" => match (left, right) {
|
2016-01-22 02:15:02 -08:00
|
|
|
(Variable(var), right) => {
|
2016-01-23 19:19:38 -08:00
|
|
|
self.add_binding(var, right);
|
2016-01-22 02:15:02 -08:00
|
|
|
Null
|
|
|
|
},
|
|
|
|
_ => Null,
|
2016-01-21 17:42:45 -08:00
|
|
|
},
|
2016-01-23 19:19:38 -08:00
|
|
|
_ => Null,
|
2016-01-21 17:42:45 -08:00
|
|
|
}
|
2016-01-18 02:24:14 -08:00
|
|
|
}
|
2016-01-23 19:19:38 -08:00
|
|
|
|
|
|
|
fn reduce_call(&mut self, name: String, arguments: Vec<Expression>) -> Expression {
|
|
|
|
use parser::Expression::*;
|
2016-01-24 01:03:57 -08:00
|
|
|
let x = self.funcmap.lookup_function(name);
|
|
|
|
let function = match x {
|
|
|
|
Some(func) => func,
|
2016-01-23 19:19:38 -08:00
|
|
|
None => return Null
|
|
|
|
};
|
|
|
|
|
2016-01-24 01:03:57 -08:00
|
|
|
if function.prototype.parameters.len() != arguments.len() {
|
|
|
|
return Null
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut frame: Varmap = Varmap::new();
|
|
|
|
for (binding, expr) in function.prototype.parameters.iter().zip(arguments.iter()) {
|
|
|
|
frame.map.insert(binding.clone(), expr.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
self.frames.push(frame);
|
2016-01-24 12:10:34 -08:00
|
|
|
let mut retval = Null;
|
2016-01-24 01:03:57 -08:00
|
|
|
for expr in function.body.iter() {
|
2016-01-24 12:10:34 -08:00
|
|
|
retval = self.reduce_expr(expr.clone());
|
2016-01-24 01:03:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
self.frames.pop();
|
2016-01-24 12:10:34 -08:00
|
|
|
retval
|
2016-01-23 19:19:38 -08:00
|
|
|
}
|
2016-01-18 02:24:14 -08:00
|
|
|
}
|