2016-12-23 17:15:23 -08:00
|
|
|
extern crate take_mut;
|
|
|
|
|
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-12-29 04:31:25 -08:00
|
|
|
type Reduction<T> = (T, Option<SideEffect>);
|
|
|
|
|
2016-12-21 19:22:08 -08:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum SideEffect {
|
|
|
|
Print(String),
|
2016-12-29 12:21:14 -08:00
|
|
|
AddBinding(String, Expression),
|
2016-12-21 19:22:08 -08:00
|
|
|
}
|
|
|
|
|
2016-12-29 12:21:14 -08:00
|
|
|
struct EnvFrame {
|
|
|
|
functions: HashMap<String, Function>,
|
|
|
|
variables: HashMap<String, Expression>,
|
2016-01-20 03:52:11 -08:00
|
|
|
}
|
|
|
|
|
2016-12-29 12:21:14 -08:00
|
|
|
impl EnvFrame {
|
|
|
|
fn new() -> EnvFrame {
|
|
|
|
EnvFrame {
|
|
|
|
functions: HashMap::new(),
|
|
|
|
variables: HashMap::new(),
|
|
|
|
}
|
2016-01-23 01:32:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 02:24:14 -08:00
|
|
|
pub struct Evaluator {
|
2016-12-29 12:21:14 -08:00
|
|
|
frames: Vec<EnvFrame>,
|
2016-01-18 02:24:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Evaluator {
|
|
|
|
pub fn new() -> Evaluator {
|
2016-12-29 02:01:48 -08:00
|
|
|
Evaluator {
|
2016-12-29 12:21:14 -08:00
|
|
|
frames: vec!(EnvFrame::new()),
|
2016-01-23 20:49:16 -08:00
|
|
|
}
|
2016-01-18 02:24:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(&mut self, ast: AST) -> Vec<String> {
|
2016-12-29 02:01:48 -08:00
|
|
|
ast.into_iter()
|
|
|
|
.map(|astnode| self.reduce(astnode))
|
|
|
|
.collect()
|
2016-01-18 02:24:14 -08:00
|
|
|
}
|
2016-01-23 19:19:38 -08:00
|
|
|
|
|
|
|
fn add_binding(&mut self, var: String, value: Expression) {
|
2016-01-24 15:48:56 -08:00
|
|
|
match self.frames.last_mut() {
|
2016-12-29 12:21:14 -08:00
|
|
|
Some(frame) => frame.variables.insert(var, value),
|
|
|
|
None => panic!("Evaluator should ensure that frames always has at least one element"),
|
2016-01-24 15:48:56 -08:00
|
|
|
};
|
2016-01-23 19:19:38 -08:00
|
|
|
}
|
|
|
|
|
2016-12-29 12:21:14 -08:00
|
|
|
fn lookup_binding(&self, var: String) -> Option<Expression> {
|
2016-01-25 01:36:59 -08:00
|
|
|
for frame in self.frames.iter().rev() {
|
2016-12-29 12:21:14 -08:00
|
|
|
match frame.variables.get(&var) {
|
2016-01-24 15:52:18 -08:00
|
|
|
None => (),
|
|
|
|
Some(expr) => return Some(expr.clone()),
|
|
|
|
}
|
|
|
|
}
|
2016-12-29 12:21:14 -08:00
|
|
|
None
|
2016-01-23 19:19:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_function(&mut self, name: String, function: Function) {
|
2016-12-29 12:21:14 -08:00
|
|
|
match self.frames.last_mut() {
|
|
|
|
Some(frame) => frame.functions.insert(name, function),
|
|
|
|
None => panic!("Evaluator should ensure that frames always has at least one element"),
|
|
|
|
};
|
2016-01-23 19:19:38 -08:00
|
|
|
}
|
|
|
|
|
2016-01-24 12:14:03 -08:00
|
|
|
fn lookup_function(&self, name: String) -> Option<Function> {
|
2016-12-29 12:21:14 -08:00
|
|
|
for frame in self.frames.iter().rev() {
|
|
|
|
match frame.functions.get(&name) {
|
|
|
|
None => (),
|
|
|
|
Some(function) => return Some(function.clone()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
2016-01-24 12:14:03 -08:00
|
|
|
}
|
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-12-29 02:01:48 -08:00
|
|
|
&FuncNode(_) => 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-12-31 03:35:46 -08:00
|
|
|
impl Expression {
|
|
|
|
fn is_truthy(&self) -> bool {
|
|
|
|
use parser::Expression::*;
|
|
|
|
match *self {
|
|
|
|
Null => false,
|
|
|
|
StringLiteral(ref s) if s == "" => false,
|
|
|
|
Number(0.0) => 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-12-29 02:01:48 -08:00
|
|
|
break;
|
2016-01-18 02:24:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12-21 19:22:08 -08:00
|
|
|
let (new_node, side_effect) = self.reduce_astnode(node);
|
|
|
|
if let Some(s) = side_effect {
|
|
|
|
self.perform_side_effect(s);
|
|
|
|
}
|
|
|
|
new_node
|
2016-01-21 00:54:54 -08:00
|
|
|
}
|
|
|
|
|
2016-12-21 19:22:08 -08:00
|
|
|
fn perform_side_effect(&mut self, side_effect: SideEffect) {
|
2016-12-22 02:05:19 -08:00
|
|
|
use self::SideEffect::*;
|
|
|
|
match side_effect {
|
|
|
|
Print(s) => println!("{}", s),
|
2016-12-29 12:21:14 -08:00
|
|
|
AddBinding(var, value) => {
|
|
|
|
self.add_binding(var, value);
|
|
|
|
}
|
2016-12-22 02:05:19 -08:00
|
|
|
}
|
2016-12-21 19:22:08 -08:00
|
|
|
}
|
|
|
|
|
2016-12-29 04:31:25 -08:00
|
|
|
fn reduce_astnode(&mut self, node: ASTNode) -> Reduction<ASTNode> {
|
2016-01-21 00:54:54 -08:00
|
|
|
use parser::ASTNode::*;
|
|
|
|
match node {
|
|
|
|
ExprNode(expr) => {
|
|
|
|
if expr.is_reducible() {
|
2016-12-21 19:22:08 -08:00
|
|
|
let (new_expr, side_effect) = self.reduce_expr(expr);
|
|
|
|
(ExprNode(new_expr), side_effect)
|
2016-01-21 00:54:54 -08:00
|
|
|
} else {
|
2016-12-21 19:22:08 -08:00
|
|
|
(ExprNode(expr), None)
|
2016-01-21 00:54:54 -08:00
|
|
|
}
|
2016-12-29 02:01:48 -08:00
|
|
|
}
|
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-12-21 19:22:08 -08:00
|
|
|
(ExprNode(Expression::Null), None)
|
2016-12-29 02:01:48 -08:00
|
|
|
}
|
2016-01-21 00:54:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-29 04:31:25 -08:00
|
|
|
fn reduce_expr(&mut self, expression: Expression) -> Reduction<Expression> {
|
2016-01-21 00:54:54 -08:00
|
|
|
use parser::Expression::*;
|
|
|
|
match expression {
|
2016-12-21 19:22:08 -08:00
|
|
|
Null => (Null, None),
|
2016-12-29 02:01:48 -08:00
|
|
|
e @ StringLiteral(_) => (e, None),
|
|
|
|
e @ Number(_) => (e, None),
|
2016-01-21 01:16:01 -08:00
|
|
|
Variable(var) => {
|
2016-01-23 19:19:38 -08:00
|
|
|
match self.lookup_binding(var) {
|
2016-12-21 19:22:08 -08:00
|
|
|
None => (Null, None),
|
|
|
|
Some(expr) => (expr, None),
|
2016-01-22 02:59:33 -08:00
|
|
|
}
|
2016-12-29 02:01:48 -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);
|
2016-12-21 19:22:08 -08:00
|
|
|
return (BinExp(op, Box::new(left), Box::new(new.0)), new.1);
|
2016-01-22 02:15:02 -08:00
|
|
|
}
|
|
|
|
|
2016-12-29 02:01:48 -08:00
|
|
|
// special case for variable assignment
|
2016-01-22 02:15:02 -08:00
|
|
|
if op == "=" {
|
|
|
|
match left {
|
|
|
|
Variable(var) => {
|
2016-12-29 12:21:14 -08:00
|
|
|
let binding = SideEffect::AddBinding(var, right);
|
|
|
|
return (Null, Some(binding));
|
2016-12-29 02:01:48 -08:00
|
|
|
}
|
2016-12-29 12:21:14 -08:00
|
|
|
_ => return (Null, None)
|
2016-01-22 02:15:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 17:42:45 -08:00
|
|
|
if left.is_reducible() {
|
|
|
|
let new = self.reduce_expr(left);
|
2016-12-21 19:22:08 -08:00
|
|
|
(BinExp(op, Box::new(new.0), Box::new(right)), new.1)
|
2016-01-21 17:42:45 -08:00
|
|
|
} else {
|
2016-12-21 19:22:08 -08:00
|
|
|
(self.reduce_binop(op, left, right), None) //can assume both arguments are maximally reduced
|
2016-01-21 17:42:45 -08:00
|
|
|
}
|
2016-12-29 02:01:48 -08:00
|
|
|
}
|
2016-12-23 17:15:23 -08:00
|
|
|
Call(name, mut args) => {
|
|
|
|
let mut f = true;
|
|
|
|
for arg in args.iter_mut() {
|
|
|
|
if arg.is_reducible() {
|
2016-12-29 02:01:48 -08:00
|
|
|
take_mut::take(arg, |arg| self.reduce_expr(arg).0);
|
2016-12-23 17:15:23 -08:00
|
|
|
f = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if f {
|
|
|
|
self.reduce_call(name, args)
|
|
|
|
} else {
|
|
|
|
(Call(name, args), None)
|
|
|
|
}
|
2016-12-29 02:01:48 -08:00
|
|
|
}
|
2016-12-31 00:57:00 -08:00
|
|
|
Conditional(box test, then_block, else_block) => {
|
|
|
|
if test.is_reducible() {
|
|
|
|
let (new_test, new_effect) = self.reduce_expr(test);
|
|
|
|
(Conditional(Box::new(new_test), then_block, else_block), new_effect)
|
|
|
|
} else {
|
2016-12-31 03:35:46 -08:00
|
|
|
if test.is_truthy() {
|
|
|
|
(*then_block, None)
|
2016-12-31 00:57:00 -08:00
|
|
|
} else {
|
2016-12-31 03:35:46 -08:00
|
|
|
match else_block {
|
|
|
|
Some(box expr) => (expr, None),
|
|
|
|
None => (Null, None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Block(mut exprs) => {
|
|
|
|
let first = exprs.pop_front();
|
|
|
|
match first {
|
|
|
|
None => (Null, None),
|
|
|
|
Some(expr) => {
|
|
|
|
if exprs.len() == 0 {
|
|
|
|
(expr, None)
|
|
|
|
} else {
|
|
|
|
if expr.is_reducible() {
|
|
|
|
let (new, side_effect) = self.reduce_expr(expr);
|
|
|
|
exprs.push_front(new);
|
|
|
|
(Block(exprs), side_effect)
|
|
|
|
} else {
|
|
|
|
(Block(exprs), None)
|
|
|
|
}
|
|
|
|
}
|
2016-12-31 00:57:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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[..] {
|
2016-12-29 02:01:48 -08:00
|
|
|
"+" => {
|
|
|
|
match (left, right) {
|
|
|
|
(Number(l), Number(r)) => Number(l + r),
|
|
|
|
(StringLiteral(s1), StringLiteral(s2)) => {
|
|
|
|
StringLiteral(format!("{}{}", s1, s2))
|
|
|
|
}
|
|
|
|
_ => Null,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"-" => {
|
|
|
|
match (left, right) {
|
|
|
|
(Number(l), Number(r)) => Number(l - r),
|
|
|
|
_ => Null,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"*" => {
|
|
|
|
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-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
|
|
|
|
2016-12-29 02:01:48 -08:00
|
|
|
fn reduce_call(&mut self,
|
|
|
|
name: String,
|
|
|
|
arguments: Vec<Expression>)
|
2016-12-29 04:31:25 -08:00
|
|
|
-> Reduction<Expression> {
|
2016-01-23 19:19:38 -08:00
|
|
|
use parser::Expression::*;
|
2016-12-22 02:05:19 -08:00
|
|
|
|
2016-12-29 02:01:48 -08:00
|
|
|
// ugly hack for now
|
2016-12-22 02:05:19 -08:00
|
|
|
if name == "print" {
|
|
|
|
let mut s = String::new();
|
|
|
|
for arg in arguments {
|
|
|
|
s.push_str(&format!("{}\n", arg));
|
|
|
|
}
|
|
|
|
return (Null, Some(SideEffect::Print(s)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-24 12:14:03 -08:00
|
|
|
let function = match self.lookup_function(name) {
|
2016-01-24 01:03:57 -08:00
|
|
|
Some(func) => func,
|
2016-12-29 02:01:48 -08:00
|
|
|
None => return (Null, None),
|
2016-01-23 19:19:38 -08:00
|
|
|
};
|
|
|
|
|
2016-01-24 01:03:57 -08:00
|
|
|
if function.prototype.parameters.len() != arguments.len() {
|
2016-12-29 02:01:48 -08:00
|
|
|
return (Null, None);
|
2016-01-24 01:03:57 -08:00
|
|
|
}
|
|
|
|
|
2016-12-29 12:21:14 -08:00
|
|
|
let mut frame = EnvFrame::new();
|
2016-01-24 01:03:57 -08:00
|
|
|
for (binding, expr) in function.prototype.parameters.iter().zip(arguments.iter()) {
|
2016-12-29 12:21:14 -08:00
|
|
|
frame.variables.insert(binding.clone(), expr.clone());
|
2016-01-24 01:03:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 15:48:56 -08:00
|
|
|
retval = expr.clone();
|
|
|
|
while retval.is_reducible() {
|
2016-12-21 19:22:08 -08:00
|
|
|
let r = self.reduce_expr(retval);
|
|
|
|
retval = r.0;
|
|
|
|
if let Some(s) = r.1 {
|
2016-12-29 15:53:48 -08:00
|
|
|
self.perform_side_effect(s);
|
2016-12-21 19:22:08 -08:00
|
|
|
}
|
2016-01-24 15:48:56 -08:00
|
|
|
}
|
2016-01-24 01:03:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
self.frames.pop();
|
2016-12-29 15:53:48 -08:00
|
|
|
(retval, None)
|
2016-01-23 19:19:38 -08:00
|
|
|
}
|
2016-01-18 02:24:14 -08:00
|
|
|
}
|