2016-12-23 17:15:23 -08:00
|
|
|
extern crate take_mut;
|
|
|
|
|
2016-01-20 03:52:11 -08:00
|
|
|
use std::collections::HashMap;
|
2017-01-05 04:00:29 -08:00
|
|
|
use std::collections::VecDeque;
|
2017-01-03 02:45:36 -08:00
|
|
|
use parser::{AST, Statement, Expression, Function};
|
2017-01-04 04:18:55 -08:00
|
|
|
use std::rc::Rc;
|
2017-01-05 04:39:31 -08:00
|
|
|
use std::io::{Write, Stdout, BufWriter};
|
2016-01-18 02:24:14 -08:00
|
|
|
|
2017-01-05 04:12:05 -08:00
|
|
|
use parser::Expression::*;
|
|
|
|
use parser::Statement::*;
|
|
|
|
|
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),
|
2017-01-04 16:56:17 -08:00
|
|
|
AddBinding(Rc<String>, Expression),
|
2017-01-03 00:26:48 -08:00
|
|
|
AddFunctionBinding(Function),
|
2016-12-21 19:22:08 -08:00
|
|
|
}
|
|
|
|
|
2017-01-03 01:53:44 -08:00
|
|
|
pub struct Evaluator<'a> {
|
|
|
|
parent: Option<&'a Evaluator<'a>>,
|
2016-12-29 12:21:14 -08:00
|
|
|
functions: HashMap<String, Function>,
|
|
|
|
variables: HashMap<String, Expression>,
|
2017-01-05 04:39:31 -08:00
|
|
|
stdout: BufWriter<Stdout>,
|
2016-01-20 03:52:11 -08:00
|
|
|
}
|
|
|
|
|
2017-01-03 01:53:44 -08:00
|
|
|
impl<'a> Evaluator<'a> {
|
|
|
|
pub fn new(parent: Option<&'a Evaluator>) -> Evaluator<'a> {
|
|
|
|
Evaluator {
|
2016-12-29 12:21:14 -08:00
|
|
|
functions: HashMap::new(),
|
|
|
|
variables: HashMap::new(),
|
2017-01-03 01:53:44 -08:00
|
|
|
parent: parent,
|
2017-01-05 04:39:31 -08:00
|
|
|
stdout: BufWriter::new(::std::io::stdout()),
|
2016-12-29 12:21:14 -08:00
|
|
|
}
|
2016-01-23 01:32:06 -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()
|
2017-01-03 02:11:59 -08:00
|
|
|
.map(|astnode| format!("{}", self.reduction_loop(astnode)))
|
2016-12-29 02:01:48 -08:00
|
|
|
.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) {
|
2017-01-03 01:53:44 -08:00
|
|
|
self.variables.insert(var, value);
|
2016-01-23 19:19:38 -08:00
|
|
|
}
|
|
|
|
|
2017-01-03 02:41:22 -08:00
|
|
|
fn lookup_binding(&self, var: &str) -> Option<Expression> {
|
|
|
|
match self.variables.get(var) {
|
2017-01-03 01:53:44 -08:00
|
|
|
Some(expr) => Some(expr.clone()),
|
|
|
|
None => match self.parent {
|
|
|
|
Some(env) => env.lookup_binding(var),
|
|
|
|
None => None
|
2016-01-24 15:52:18 -08:00
|
|
|
}
|
|
|
|
}
|
2016-01-23 19:19:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_function(&mut self, name: String, function: Function) {
|
2017-01-03 01:53:44 -08:00
|
|
|
self.functions.insert(name, function);
|
2016-01-23 19:19:38 -08:00
|
|
|
}
|
|
|
|
|
2017-01-04 16:56:17 -08:00
|
|
|
fn lookup_function(&self, name: &str) -> Option<Function> {
|
|
|
|
match self.functions.get(name) {
|
2017-01-03 01:53:44 -08:00
|
|
|
Some(func) => Some(func.clone()),
|
|
|
|
None => match self.parent {
|
|
|
|
Some(env) => env.lookup_function(name),
|
|
|
|
None => None
|
2016-12-29 12:21:14 -08:00
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-03 02:45:36 -08:00
|
|
|
impl Evaluable for Statement {
|
2016-01-20 02:26:46 -08:00
|
|
|
fn is_reducible(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
&ExprNode(ref expr) => expr.is_reducible(),
|
2017-01-02 18:41:46 -08:00
|
|
|
&FuncDefNode(_) => true,
|
2016-01-20 02:26:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Evaluable for Expression {
|
|
|
|
fn is_reducible(&self) -> bool {
|
|
|
|
match *self {
|
2016-01-21 19:12:07 -08:00
|
|
|
Null => false,
|
2016-01-20 02:26:46 -08:00
|
|
|
StringLiteral(_) => false,
|
2017-01-02 18:41:46 -08:00
|
|
|
Lambda(_) => false,
|
2016-01-20 02:26:46 -08:00
|
|
|
Number(_) => false,
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 03:35:46 -08:00
|
|
|
impl Expression {
|
|
|
|
fn is_truthy(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
Null => false,
|
2017-01-04 04:18:55 -08:00
|
|
|
StringLiteral(ref s) if **s == "" => false,
|
2016-12-31 03:35:46 -08:00
|
|
|
Number(0.0) => false,
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-06 00:58:47 -08:00
|
|
|
fn is_assignment(op: &str) -> bool {
|
|
|
|
match op {
|
|
|
|
"=" | "+=" | "-=" |
|
|
|
|
"*=" | "/=" => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 01:53:44 -08:00
|
|
|
impl<'a> Evaluator<'a> {
|
2017-01-03 02:45:36 -08:00
|
|
|
fn reduction_loop(&mut self, mut node: Statement) -> Statement {
|
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
|
|
|
}
|
|
|
|
}
|
2017-01-03 01:53:44 -08:00
|
|
|
node
|
2016-01-18 02:24:14 -08:00
|
|
|
}
|
|
|
|
|
2017-01-03 02:45:36 -08:00
|
|
|
fn step(&mut self, node: Statement) -> Statement {
|
2017-01-06 00:58:47 -08:00
|
|
|
println!("Step: {:?}", node);
|
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 {
|
2017-01-05 04:39:31 -08:00
|
|
|
Print(s) => {
|
|
|
|
write!(self.stdout, "{}\n", s).unwrap();
|
|
|
|
}
|
2016-12-29 12:21:14 -08:00
|
|
|
AddBinding(var, value) => {
|
2017-01-04 16:56:17 -08:00
|
|
|
self.add_binding((*var).clone(), value);
|
2017-01-03 00:26:48 -08:00
|
|
|
},
|
|
|
|
AddFunctionBinding(function) => {
|
2017-01-04 04:18:55 -08:00
|
|
|
self.add_function((*function.prototype.name).clone(), function);
|
2016-12-29 12:21:14 -08:00
|
|
|
}
|
2016-12-22 02:05:19 -08:00
|
|
|
}
|
2016-12-21 19:22:08 -08:00
|
|
|
}
|
|
|
|
|
2017-01-03 02:45:36 -08:00
|
|
|
fn reduce_astnode(&mut self, node: Statement) -> Reduction<Statement> {
|
2016-01-21 00:54:54 -08:00
|
|
|
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
|
|
|
}
|
2017-01-02 18:41:46 -08:00
|
|
|
FuncDefNode(func) => {
|
2017-01-03 00:26:48 -08:00
|
|
|
let binding = Some(SideEffect::AddFunctionBinding(func.clone()));
|
|
|
|
(ExprNode(Expression::Lambda(func)), binding)
|
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
|
|
|
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),
|
2017-01-02 18:41:46 -08:00
|
|
|
e @ Lambda(_) => (e, None),
|
2017-01-03 02:41:22 -08:00
|
|
|
Variable(ref 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
|
|
|
}
|
2017-01-05 03:47:52 -08:00
|
|
|
BinExp(op, mut left, mut right) => {
|
2016-01-22 02:15:02 -08:00
|
|
|
if right.is_reducible() {
|
2017-01-05 03:47:52 -08:00
|
|
|
let mut side_effect = None;
|
|
|
|
take_mut::take(right.as_mut(), |expr| { let (a, b) = self.reduce_expr(expr); side_effect = b; a});
|
|
|
|
return (BinExp(op, left, right), side_effect);
|
2016-01-22 02:15:02 -08:00
|
|
|
}
|
|
|
|
|
2017-01-04 04:18:55 -08:00
|
|
|
if *op == "=" {
|
2017-01-06 00:58:47 -08:00
|
|
|
return match *left {
|
2016-01-22 02:15:02 -08:00
|
|
|
Variable(var) => {
|
2017-01-05 03:47:52 -08:00
|
|
|
let binding = SideEffect::AddBinding(var, *right);
|
2017-01-06 00:58:47 -08:00
|
|
|
(Null, Some(binding))
|
|
|
|
},
|
|
|
|
_ => (Null, None)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_assignment(&*op) {
|
|
|
|
let new_op = Rc::new(String::from(match &op[..] {
|
|
|
|
"+=" => "+",
|
|
|
|
"-=" => "-",
|
|
|
|
"*=" => "*",
|
|
|
|
"/=" => "/",
|
|
|
|
_ => unreachable!(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
let reduction =
|
|
|
|
BinExp(Rc::new(String::from("=")),
|
|
|
|
Box::new(*left.clone()),
|
|
|
|
Box::new(BinExp(new_op, left, right))
|
|
|
|
);
|
|
|
|
|
|
|
|
return (reduction, None);
|
2016-01-22 02:15:02 -08:00
|
|
|
}
|
|
|
|
|
2016-01-21 17:42:45 -08:00
|
|
|
if left.is_reducible() {
|
2017-01-05 03:47:52 -08:00
|
|
|
let mut side_effect = None;
|
|
|
|
take_mut::take(left.as_mut(), |expr| { let (a, b) = self.reduce_expr(expr); side_effect = b; a});
|
|
|
|
(BinExp(op, left, right), side_effect)
|
2016-01-21 17:42:45 -08:00
|
|
|
} else {
|
2017-01-05 03:47:52 -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
|
|
|
}
|
2017-01-05 04:00:29 -08:00
|
|
|
While(test, body) => {
|
|
|
|
let mut block = VecDeque::from(body.clone());
|
|
|
|
block.push_back(While(test.clone(), body.clone()));
|
|
|
|
let reduction = Conditional(test, Box::new(Block(block)), None);
|
|
|
|
(reduction, None)
|
2017-01-03 03:19:52 -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),
|
2017-01-02 22:15:14 -08:00
|
|
|
None => (Null, None),
|
2016-12-31 03:35:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2017-01-04 04:18:55 -08:00
|
|
|
fn reduce_binop(&mut self, op: Rc<String>, left: Expression, right: Expression) -> Expression {
|
2017-01-06 00:58:47 -08:00
|
|
|
println!("Got op {:?}, l: {:?}, r: {:?}", op, left, right);
|
2017-01-02 23:34:59 -08:00
|
|
|
let truthy = Number(1.0);
|
|
|
|
let falsy = Null;
|
2017-01-02 22:14:01 -08:00
|
|
|
match (&op[..], left, right) {
|
|
|
|
("+", Number(l), Number(r)) => Number(l + r),
|
2017-01-04 04:18:55 -08:00
|
|
|
("+", StringLiteral(s1), StringLiteral(s2)) => StringLiteral(Rc::new(format!("{}{}", *s1, *s2))),
|
2017-01-05 18:10:57 -08:00
|
|
|
("+", StringLiteral(s1), Number(r)) => StringLiteral(Rc::new(format!("{}{}", *s1, r))),
|
|
|
|
("+", Number(l), StringLiteral(s1)) => StringLiteral(Rc::new(format!("{}{}", l, *s1))),
|
2017-01-02 22:14:01 -08:00
|
|
|
("-", Number(l), Number(r)) => Number(l - r),
|
|
|
|
("*", Number(l), Number(r)) => Number(l * r),
|
|
|
|
("/", Number(l), Number(r)) if r != 0.0 => Number(l / r),
|
|
|
|
("%", Number(l), Number(r)) => Number(l % r),
|
2017-01-02 23:34:59 -08:00
|
|
|
("<", Number(l), Number(r)) => if l < r { truthy } else { falsy },
|
|
|
|
("<=", Number(l), Number(r)) => if l <= r { truthy } else { falsy },
|
|
|
|
(">", Number(l), Number(r)) => if l > r { truthy } else { falsy },
|
|
|
|
(">=", Number(l), Number(r)) => if l >= r { truthy } else { falsy },
|
|
|
|
("==", Number(l), Number(r)) => if l == r { truthy } else { falsy },
|
|
|
|
("==", Null, Null) => truthy,
|
|
|
|
("==", StringLiteral(s1), StringLiteral(s2)) => if s1 == s2 { truthy } else { falsy },
|
|
|
|
("==", _, _) => falsy,
|
|
|
|
_ => falsy,
|
2016-01-21 17:42:45 -08:00
|
|
|
}
|
2016-01-18 02:24:14 -08:00
|
|
|
}
|
2016-01-23 19:19:38 -08:00
|
|
|
|
2017-01-04 04:18:55 -08:00
|
|
|
fn reduce_call(&mut self, name: Rc<String>, arguments: Vec<Expression>) -> Reduction<Expression> {
|
2017-01-05 04:12:05 -08:00
|
|
|
if let Some(res) = handle_builtin(&*name, &arguments) {
|
|
|
|
return res;
|
2016-12-22 02:05:19 -08:00
|
|
|
}
|
|
|
|
|
2017-01-04 16:56:17 -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
|
|
|
}
|
|
|
|
|
2017-01-03 01:53:44 -08:00
|
|
|
let mut evaluator = Evaluator::new(Some(self));
|
2016-01-24 01:03:57 -08:00
|
|
|
for (binding, expr) in function.prototype.parameters.iter().zip(arguments.iter()) {
|
2017-01-04 04:18:55 -08:00
|
|
|
evaluator.add_binding((**binding).clone(), expr.clone());
|
2016-01-24 01:03:57 -08:00
|
|
|
}
|
|
|
|
|
2017-01-03 02:11:59 -08:00
|
|
|
let nodes = function.body.iter().map(|node| node.clone());
|
|
|
|
let mut retval = ExprNode(Null);
|
2017-01-03 01:53:44 -08:00
|
|
|
for n in nodes {
|
2017-01-03 02:11:59 -08:00
|
|
|
retval = evaluator.reduction_loop(n);
|
2016-01-24 01:03:57 -08:00
|
|
|
}
|
|
|
|
|
2017-01-03 02:11:59 -08:00
|
|
|
match retval {
|
|
|
|
ExprNode(expr) => (expr, None),
|
|
|
|
FuncDefNode(_) => panic!("This should never happen! A maximally-reduced node\
|
|
|
|
should never be a function definition!")
|
|
|
|
}
|
2016-01-23 19:19:38 -08:00
|
|
|
}
|
2016-01-18 02:24:14 -08:00
|
|
|
}
|
2017-01-05 04:12:05 -08:00
|
|
|
|
|
|
|
fn handle_builtin(name: &str, arguments: &Vec<Expression>) -> Option<Reduction<Expression>> {
|
|
|
|
match name {
|
|
|
|
"print" => {
|
|
|
|
let mut s = String::new();
|
|
|
|
for arg in arguments {
|
|
|
|
s.push_str(&format!("{} ", arg));
|
|
|
|
}
|
|
|
|
return Some((Null, Some(SideEffect::Print(s))));
|
|
|
|
},
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|