schala/src/maaru_lang/eval.rs

482 lines
17 KiB
Rust
Raw Normal View History

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-08-29 00:28:19 -07:00
use maaru_lang::parser::{AST, Statement, Expression, Function, Callable, BinOp};
use std::rc::Rc;
use std::io::{Write, Stdout, BufWriter};
2017-01-12 01:33:34 -08:00
use std::convert::From;
2016-01-18 02:24:14 -08:00
2017-08-29 00:28:19 -07:00
use maaru_lang::parser::Expression::*;
use maaru_lang::parser::Statement::*;
2017-01-05 04:12:05 -08:00
type Reduction<T> = (T, Option<SideEffect>);
2017-01-12 01:33:34 -08:00
#[derive(Debug, Clone)]
enum ReducedValue {
StringLiteral(Rc<String>),
2017-02-19 12:58:18 -08:00
ListLiteral(VecDeque<Expression>),
StructLiteral(VecDeque<(Rc<String>, Expression)>),
2017-01-12 01:33:34 -08:00
Number(f64),
Lambda(Function),
}
impl From<ReducedValue> for Expression {
fn from(rv: ReducedValue) -> Expression {
match rv {
ReducedValue::Number(n) => Expression::Number(n),
ReducedValue::StringLiteral(n) => Expression::StringLiteral(n),
ReducedValue::Lambda(f) => Expression::Lambda(f),
2017-02-19 12:58:18 -08:00
ReducedValue::ListLiteral(items) => Expression::ListLiteral(items),
ReducedValue::StructLiteral(items) => Expression::StructLiteral(items),
2017-01-12 01:33:34 -08:00
}
}
}
impl From<Expression> for ReducedValue {
fn from(rv: Expression) -> ReducedValue {
match rv {
Expression::Number(n) => ReducedValue::Number(n),
Expression::StringLiteral(n) => ReducedValue::StringLiteral(n),
Expression::Lambda(f) => ReducedValue::Lambda(f),
2017-02-19 12:58:18 -08:00
Expression::ListLiteral(items) => ReducedValue::ListLiteral(items),
Expression::StructLiteral(items) => ReducedValue::StructLiteral(items),
2017-01-12 01:33:34 -08:00
_ => panic!("trying to store a non-fully-reduced variable"),
}
}
}
2017-02-19 15:17:01 -08:00
fn get_indexer(f: f64) -> Option<usize> {
if f.fract() == 0.0 {
if f.trunc() >= 0.0 {
return Some(f.trunc() as usize);
}
}
None
}
2016-12-21 19:22:08 -08:00
#[derive(Debug)]
enum SideEffect {
Print(String),
2017-01-12 01:33:34 -08:00
AddBinding(Rc<String>, ReducedValue),
2016-12-21 19:22:08 -08:00
}
pub struct Evaluator<'a> {
parent: Option<&'a Evaluator<'a>>,
2017-01-12 01:33:34 -08:00
variables: HashMap<String, ReducedValue>,
stdout: BufWriter<Stdout>,
2017-01-09 04:26:42 -08:00
pub trace_evaluation: bool,
2016-01-20 03:52:11 -08:00
}
impl<'a> Evaluator<'a> {
pub fn new(parent: Option<&'a Evaluator>) -> Evaluator<'a> {
Evaluator {
2016-12-29 12:21:14 -08:00
variables: HashMap::new(),
parent: parent,
stdout: BufWriter::new(::std::io::stdout()),
2017-01-09 04:26:42 -08:00
trace_evaluation: parent.map_or(false, |e| e.trace_evaluation),
2016-12-29 12:21:14 -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| format!("{}", self.reduction_loop(astnode)))
2016-12-29 02:01:48 -08:00
.collect()
2016-01-18 02:24:14 -08:00
}
2017-01-12 01:33:34 -08:00
fn add_binding(&mut self, var: String, value: ReducedValue) {
self.variables.insert(var, value);
}
2017-01-12 01:33:34 -08:00
fn lookup_binding(&self, var: &str) -> Option<ReducedValue> {
2017-01-03 02:41:22 -08:00
match self.variables.get(var) {
Some(expr) => Some(expr.clone()),
None => match self.parent {
Some(env) => env.lookup_binding(var),
None => None
}
}
}
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 {
fn is_reducible(&self) -> bool {
match self {
&ExprNode(ref expr) => expr.is_reducible(),
&FuncDefNode(_) => true,
}
}
}
impl Evaluable for Expression {
fn is_reducible(&self) -> bool {
match *self {
Null => false,
StringLiteral(_) => false,
Lambda(_) => false,
Number(_) => false,
2017-02-18 03:09:36 -08:00
ListLiteral(ref items) => {
items.iter().any(|x| x.is_reducible())
}
StructLiteral(ref items) => {
items.iter().any(|pair| pair.1.is_reducible())
}
_ => true,
}
}
}
2016-12-31 03:35:46 -08:00
impl Expression {
fn is_truthy(&self) -> bool {
match *self {
Null => false,
StringLiteral(ref s) if **s == "" => false,
Number(n) if n == 0.0 => false,
2016-12-31 03:35:46 -08:00
_ => true,
}
}
}
2017-01-13 20:05:17 -08:00
fn is_assignment(op: &BinOp) -> bool {
use self::BinOp::*;
match *op {
Assign | AddAssign | SubAssign |
MulAssign | DivAssign => true,
_ => false,
}
}
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 {
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
}
}
node
2016-01-18 02:24:14 -08:00
}
2017-01-03 02:45:36 -08:00
fn step(&mut self, node: Statement) -> Statement {
let mut trace = String::new();
2017-01-09 04:26:42 -08:00
if self.trace_evaluation {
trace.push_str(&format!("Step: {:?}", node));
2017-01-09 04:26:42 -08:00
}
2016-12-21 19:22:08 -08:00
let (new_node, side_effect) = self.reduce_astnode(node);
if self.trace_evaluation {
trace.push_str(&format!("{:?}", new_node));
}
2016-12-21 19:22:08 -08:00
if let Some(s) = side_effect {
if self.trace_evaluation {
trace.push_str(&format!(" | side-effect: {:?}", s));
}
2016-12-21 19:22:08 -08:00
self.perform_side_effect(s);
}
if self.trace_evaluation {
println!("{}", trace);
}
2016-12-21 19:22:08 -08:00
new_node
}
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) => {
write!(self.stdout, "{}\n", s).unwrap();
2017-01-11 22:32:12 -08:00
match self.stdout.flush() {
Ok(_) => (),
Err(_) => println!("Could not flush stdout"),
};
}
2016-12-29 12:21:14 -08:00
AddBinding(var, value) => {
self.add_binding((*var).clone(), value);
2017-01-03 00:26:48 -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> {
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)
} else {
2016-12-21 19:22:08 -08:00
(ExprNode(expr), None)
}
2016-12-29 02:01:48 -08:00
}
FuncDefNode(func) => {
2017-01-12 01:33:34 -08:00
let name = func.prototype.name.clone();
let reduced_value = ReducedValue::Lambda(func.clone());
let binding = Some(SideEffect::AddBinding(name, reduced_value));
2017-01-03 00:26:48 -08:00
(ExprNode(Expression::Lambda(func)), binding)
2016-12-29 02:01:48 -08:00
}
}
}
//TODO I probably want another Expression variant that holds a ReducedValue
fn reduce_expr(&mut self, expression: Expression) -> Reduction<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),
e @ Lambda(_) => (e, None),
2017-01-03 02:41:22 -08:00
Variable(ref var) => {
2017-01-12 01:33:34 -08:00
match self.lookup_binding(var).map(|x| x.into()) {
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-13 20:05:17 -08:00
if let BinOp::Assign = op {
return match *left {
2016-01-22 02:15:02 -08:00
Variable(var) => {
2017-01-12 01:33:34 -08:00
let reduced_value: ReducedValue = ReducedValue::from(*right);
let binding = SideEffect::AddBinding(var, reduced_value);
(Null, Some(binding))
},
_ => (Null, None)
};
}
2017-01-13 20:05:17 -08:00
if is_assignment(&op) {
use self::BinOp::*;
let new_op = match op {
AddAssign => Add,
SubAssign => Sub,
MulAssign => Mul,
DivAssign => Div,
_ => unreachable!(),
2017-01-13 20:05:17 -08:00
};
let reduction =
2017-01-13 20:05:17 -08:00
BinExp(BinOp::Assign,
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
}
Call(callable, mut args) => {
2016-12-23 17:15:23 -08:00
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(callable, args)
2016-12-23 17:15:23 -08:00
} else {
(Call(callable, args), None)
2016-12-23 17:15:23 -08:00
}
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)
}
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)
} 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)
}
}
}
}
}
Index(mut expr, mut index_expr) => {
if index_expr.is_reducible() {
let mut side_effect = None;
take_mut::take(index_expr.as_mut(), |expr| { let (a, b) = self.reduce_expr(expr); side_effect = b; a});
return (Index(expr, index_expr), side_effect)
}
if expr.is_reducible() {
let mut side_effect = None;
take_mut::take(expr.as_mut(), |expr| { let (a, b) = self.reduce_expr(expr); side_effect = b; a});
return (Index(expr, index_expr), side_effect);
}
2017-02-19 15:17:01 -08:00
match (*expr, *index_expr) {
(ListLiteral(list_items), Number(n)) => {
let indexed_expr = get_indexer(n).and_then(|i| list_items.get(i));
if let Some(e) = indexed_expr {
(e.clone(), None)
} else {
(Null, None)
}
}
(StructLiteral(items), StringLiteral(s)) => {
for item in items {
if s == item.0 {
return (item.1.clone(), None); //TODO this is hella inefficient
}
}
(Null, None)
},
_ => (Null, None)
}
2017-02-17 21:13:57 -08:00
}
2017-02-18 03:09:36 -08:00
ListLiteral(mut exprs) => {
let mut side_effect = None;
for expr in exprs.iter_mut() {
if expr.is_reducible() {
take_mut::take(expr, |expr| {
let (a, b) = self.reduce_expr(expr);
side_effect = b;
a
});
break;
}
}
(ListLiteral(exprs), side_effect)
},
StructLiteral(mut items) => {
let mut side_effect = None;
for pair in items.iter_mut() {
if pair.1.is_reducible() {
take_mut::take(pair, |pair| {
let (name, expr) = pair;
let (a, b) = self.reduce_expr(expr);
side_effect = b;
(name, a)
});
break;
}
}
(StructLiteral(items), side_effect)
}
}
2016-01-21 17:42:45 -08:00
}
2017-01-13 20:05:17 -08:00
fn reduce_binop(&mut self, op: BinOp, left: Expression, right: Expression) -> Expression {
use self::BinOp::*;
2017-01-02 23:34:59 -08:00
let truthy = Number(1.0);
let falsy = Null;
2017-01-13 20:05:17 -08:00
match (op, left, right) {
(Add, Number(l), Number(r)) => Number(l + r),
(Add, StringLiteral(s1), StringLiteral(s2)) => StringLiteral(Rc::new(format!("{}{}", *s1, *s2))),
(Add, StringLiteral(s1), Number(r)) => StringLiteral(Rc::new(format!("{}{}", *s1, r))),
(Add, Number(l), StringLiteral(s1)) => StringLiteral(Rc::new(format!("{}{}", l, *s1))),
(Sub, Number(l), Number(r)) => Number(l - r),
(Mul, Number(l), Number(r)) => Number(l * r),
(Div, Number(l), Number(r)) if r != 0.0 => Number(l / r),
(Mod, Number(l), Number(r)) => Number(l % r),
(Less, Number(l), Number(r)) => if l < r { truthy } else { falsy },
(LessEq, Number(l), Number(r)) => if l <= r { truthy } else { falsy },
(Greater, Number(l), Number(r)) => if l > r { truthy } else { falsy },
(GreaterEq, Number(l), Number(r)) => if l >= r { truthy } else { falsy },
(Equal, Number(l), Number(r)) => if l == r { truthy } else { falsy },
(Equal, Null, Null) => truthy,
(Equal, StringLiteral(s1), StringLiteral(s2)) => if s1 == s2 { truthy } else { falsy },
(Equal, _, _) => falsy,
2017-01-02 23:34:59 -08:00
_ => falsy,
2016-01-21 17:42:45 -08:00
}
2016-01-18 02:24:14 -08:00
}
fn reduce_call(&mut self, callable: Callable, arguments: Vec<Expression>) -> Reduction<Expression> {
if let Some(res) = handle_builtin(&callable, &arguments) {
2017-01-05 04:12:05 -08:00
return res;
2016-12-22 02:05:19 -08:00
}
let function = match callable {
Callable::Lambda(func) => func.clone(),
Callable::NamedFunction(name) => {
match self.lookup_binding(&*name) {
Some(ReducedValue::Lambda(func)) => func,
_ => return (Null, None),
}
}
};
if function.prototype.parameters.len() != arguments.len() {
2016-12-29 02:01:48 -08:00
return (Null, None);
}
let mut evaluator = Evaluator::new(Some(self));
for (binding, expr) in function.prototype.parameters.iter().zip(arguments.iter()) {
2017-01-12 01:33:34 -08:00
evaluator.add_binding((**binding).clone(), expr.clone().into());
}
let nodes = function.body.iter().map(|node| node.clone());
let mut retval = ExprNode(Null);
for n in nodes {
retval = evaluator.reduction_loop(n);
}
match retval {
ExprNode(expr) => (expr, None),
FuncDefNode(_) => panic!("This should never happen! A maximally-reduced node\
should never be a function definition!")
}
}
2016-01-18 02:24:14 -08:00
}
2017-01-05 04:12:05 -08:00
fn handle_builtin(callable: &Callable, arguments: &Vec<Expression>) -> Option<Reduction<Expression>> {
let name: &str = match *callable {
Callable::NamedFunction(ref name) => *&name,
_ => return None,
};
2017-01-05 04:12:05 -08:00
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
}
}