2018-05-13 18:02:54 -07:00
|
|
|
use std::cell::RefCell;
|
2017-10-13 03:05:18 -07:00
|
|
|
use std::rc::Rc;
|
2018-03-03 13:26:22 -08:00
|
|
|
use std::fmt::Write;
|
2018-05-12 02:27:54 -07:00
|
|
|
use std::io;
|
2018-03-03 13:26:22 -08:00
|
|
|
|
|
|
|
use itertools::Itertools;
|
|
|
|
|
2018-05-11 01:56:12 -07:00
|
|
|
use util::StateStack;
|
2018-05-11 01:30:02 -07:00
|
|
|
use ast_reducing::{ReducedAST, Stmt, Expr, Lit, Func};
|
2018-05-15 23:48:36 -07:00
|
|
|
use typechecking::{TypeContext, SymbolSpec, Symbol};
|
2017-09-30 23:30:02 -07:00
|
|
|
|
2018-02-24 13:56:04 -08:00
|
|
|
pub struct State<'a> {
|
2018-05-13 18:02:54 -07:00
|
|
|
values: StateStack<'a, Rc<String>, ValueEntry>,
|
2018-05-14 09:46:25 -07:00
|
|
|
type_context_handle: Rc<RefCell<TypeContext>>,
|
2017-10-13 03:05:18 -07:00
|
|
|
}
|
2018-05-11 01:56:12 -07:00
|
|
|
|
2018-05-12 01:18:44 -07:00
|
|
|
macro_rules! builtin_binding {
|
|
|
|
($name:expr, $values:expr) => {
|
|
|
|
$values.insert(Rc::new(format!($name)), ValueEntry::Binding { constant: true, val: Expr::Func(Func::BuiltIn(Rc::new(format!($name)))) });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 01:56:12 -07:00
|
|
|
impl<'a> State<'a> {
|
2018-05-14 09:46:25 -07:00
|
|
|
pub fn new(type_context_handle: Rc<RefCell<TypeContext>>) -> State<'a> {
|
2018-05-12 01:18:44 -07:00
|
|
|
let mut values = StateStack::new(Some(format!("global")));
|
|
|
|
builtin_binding!("print", values);
|
|
|
|
builtin_binding!("println", values);
|
2018-05-12 02:27:54 -07:00
|
|
|
builtin_binding!("getline", values);
|
2018-05-13 18:02:54 -07:00
|
|
|
State { values, type_context_handle }
|
2018-05-11 01:56:12 -07:00
|
|
|
}
|
2018-05-11 02:33:19 -07:00
|
|
|
|
|
|
|
pub fn debug_print(&self) -> String {
|
|
|
|
format!("Values: {:?}", self.values)
|
|
|
|
}
|
2018-05-11 01:56:12 -07:00
|
|
|
}
|
|
|
|
|
2018-05-11 01:57:29 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum ValueEntry {
|
|
|
|
Binding {
|
2018-05-11 02:24:38 -07:00
|
|
|
constant: bool,
|
2018-05-11 01:57:29 -07:00
|
|
|
val: /*FullyEvaluatedExpr*/ Expr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type EvalResult<T> = Result<T, String>;
|
|
|
|
|
2018-05-11 00:25:43 -07:00
|
|
|
impl Expr {
|
|
|
|
fn to_repl(&self) -> String {
|
2018-05-11 01:07:18 -07:00
|
|
|
use self::Lit::*;
|
2018-05-11 23:23:54 -07:00
|
|
|
use self::Func::*;
|
2018-05-11 01:07:18 -07:00
|
|
|
match self {
|
|
|
|
Expr::Lit(ref l) => match l {
|
|
|
|
Nat(n) => format!("{}", n),
|
|
|
|
Int(i) => format!("{}", i),
|
|
|
|
Float(f) => format!("{}", f),
|
|
|
|
Bool(b) => format!("{}", b),
|
2018-05-12 12:58:57 -07:00
|
|
|
StringLit(s) => format!("\"{}\"", s),
|
2018-05-14 22:55:53 -07:00
|
|
|
Custom(s) => format!("{}", s),
|
2018-05-11 01:07:18 -07:00
|
|
|
},
|
2018-05-11 23:23:54 -07:00
|
|
|
Expr::Func(f) => match f {
|
|
|
|
BuiltIn(name) => format!("<built-in function {}>", name),
|
|
|
|
UserDefined { name: None, .. } => format!("<function>"),
|
|
|
|
UserDefined { name: Some(name), .. } => format!("<function {}>", name),
|
|
|
|
},
|
2018-05-12 12:56:39 -07:00
|
|
|
Expr::Tuple(exprs) => {
|
|
|
|
let mut buf = String::new();
|
|
|
|
write!(buf, "(").unwrap();
|
|
|
|
for term in exprs.iter().map(|e| Some(e)).intersperse(None) {
|
|
|
|
match term {
|
|
|
|
Some(e) => write!(buf, "{}", e.to_repl()).unwrap(),
|
|
|
|
None => write!(buf, ", ").unwrap(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
write!(buf, ")").unwrap();
|
|
|
|
buf
|
|
|
|
},
|
2018-05-11 01:07:18 -07:00
|
|
|
_ => format!("{:?}", self),
|
|
|
|
}
|
2018-05-11 00:25:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 02:27:57 -07:00
|
|
|
impl<'a> State<'a> {
|
2018-05-12 02:20:50 -07:00
|
|
|
pub fn evaluate(&mut self, ast: ReducedAST, repl: bool) -> Vec<Result<String, String>> {
|
2018-05-09 03:38:02 -07:00
|
|
|
let mut acc = vec![];
|
2018-05-13 17:24:21 -07:00
|
|
|
|
|
|
|
// handle prebindings
|
|
|
|
for statement in ast.0.iter() {
|
|
|
|
self.prebinding(statement);
|
|
|
|
}
|
|
|
|
|
2018-05-09 03:38:02 -07:00
|
|
|
for statement in ast.0 {
|
2018-05-11 00:25:43 -07:00
|
|
|
match self.statement(statement) {
|
|
|
|
Ok(Some(ref output)) if repl => acc.push(Ok(output.to_repl())),
|
|
|
|
Ok(_) => (),
|
2018-05-09 03:38:02 -07:00
|
|
|
Err(error) => {
|
2018-05-12 12:37:05 -07:00
|
|
|
acc.push(Err(format!("Runtime error: {}", error)));
|
2018-05-09 03:38:02 -07:00
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
acc
|
|
|
|
}
|
|
|
|
|
2018-05-13 17:24:21 -07:00
|
|
|
fn prebinding(&mut self, stmt: &Stmt) {
|
|
|
|
match stmt {
|
|
|
|
Stmt::PreBinding { name, func } => {
|
|
|
|
let v_entry = ValueEntry::Binding { constant: true, val: Expr::Func(func.clone()) };
|
|
|
|
self.values.insert(name.clone(), v_entry);
|
|
|
|
},
|
|
|
|
Stmt::Expr(_expr) => {
|
|
|
|
//TODO have this support things like nested function defs
|
|
|
|
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 01:57:29 -07:00
|
|
|
fn statement(&mut self, stmt: Stmt) -> EvalResult<Option<Expr>> {
|
2018-05-11 01:07:18 -07:00
|
|
|
match stmt {
|
2018-05-11 02:24:38 -07:00
|
|
|
Stmt::Binding { name, constant, expr } => {
|
|
|
|
let val = self.expression(expr)?;
|
|
|
|
self.values.insert(name.clone(), ValueEntry::Binding { constant, val });
|
2018-05-11 01:07:18 -07:00
|
|
|
Ok(None)
|
|
|
|
},
|
2018-05-11 01:30:02 -07:00
|
|
|
Stmt::Expr(expr) => Ok(Some(self.expression(expr)?)),
|
2018-05-13 17:24:21 -07:00
|
|
|
Stmt::PreBinding {..} | Stmt::Noop => Ok(None),
|
2018-05-11 01:07:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-12 13:51:12 -07:00
|
|
|
fn block(&mut self, stmts: Vec<Stmt>) -> EvalResult<Expr> {
|
|
|
|
let mut ret = None;
|
|
|
|
for stmt in stmts {
|
|
|
|
ret = self.statement(stmt)?;
|
|
|
|
}
|
|
|
|
Ok(ret.unwrap_or(Expr::Unit))
|
|
|
|
}
|
|
|
|
|
2018-05-11 01:57:29 -07:00
|
|
|
fn expression(&mut self, expr: Expr) -> EvalResult<Expr> {
|
2018-05-11 01:07:18 -07:00
|
|
|
use self::Expr::*;
|
|
|
|
match expr {
|
2018-05-11 01:30:02 -07:00
|
|
|
literal @ Lit(_) => Ok(literal),
|
2018-05-11 23:34:26 -07:00
|
|
|
Call { box f, args } => {
|
|
|
|
let f = match self.expression(f)? {
|
|
|
|
Func(f) => f,
|
|
|
|
other => return Err(format!("Tried to call {:?} which is not a function", other)),
|
|
|
|
};
|
|
|
|
self.apply_function(f, args)
|
|
|
|
},
|
2018-05-11 17:24:11 -07:00
|
|
|
Val(v) => self.value(v),
|
2018-05-11 23:23:54 -07:00
|
|
|
func @ Func(_) => Ok(func),
|
2018-05-12 12:56:39 -07:00
|
|
|
Tuple(exprs) => Ok(Tuple(exprs.into_iter().map(|expr| self.expression(expr)).collect::<Result<Vec<Expr>,_>>()?)),
|
2018-05-12 13:51:12 -07:00
|
|
|
Conditional { box cond, then_clause, else_clause } => self.conditional(cond, then_clause, else_clause),
|
2018-05-12 03:51:42 -07:00
|
|
|
Assign { box val, box expr } => {
|
|
|
|
let name = match val {
|
|
|
|
Expr::Val(name) => name,
|
|
|
|
_ => return Err(format!("Trying to assign to a non-value")),
|
|
|
|
};
|
|
|
|
|
|
|
|
let constant = match self.values.lookup(&name) {
|
2018-05-12 12:37:05 -07:00
|
|
|
None => return Err(format!("{} is undefined", name)),
|
2018-05-12 03:51:42 -07:00
|
|
|
Some(ValueEntry::Binding { constant, .. }) => constant.clone(),
|
|
|
|
};
|
|
|
|
if constant {
|
2018-05-12 12:37:05 -07:00
|
|
|
return Err(format!("trying to update {}, a non-mutable binding", name));
|
2018-05-12 03:51:42 -07:00
|
|
|
}
|
|
|
|
let val = self.expression(expr)?;
|
|
|
|
self.values.insert(name.clone(), ValueEntry::Binding { constant: false, val });
|
|
|
|
Ok(Expr::Unit)
|
|
|
|
},
|
2018-05-11 23:23:54 -07:00
|
|
|
e => Err(format!("Expr {:?} eval not implemented", e))
|
2018-05-11 01:07:18 -07:00
|
|
|
}
|
2018-05-09 02:27:57 -07:00
|
|
|
}
|
2018-05-11 01:30:02 -07:00
|
|
|
|
2018-05-11 01:57:29 -07:00
|
|
|
fn apply_function(&mut self, f: Func, args: Vec<Expr>) -> EvalResult<Expr> {
|
2018-05-11 01:30:02 -07:00
|
|
|
match f {
|
|
|
|
Func::BuiltIn(sigil) => self.apply_builtin(sigil, args),
|
2018-05-12 00:59:50 -07:00
|
|
|
Func::UserDefined { params, body, name } => {
|
|
|
|
|
|
|
|
if params.len() != args.len() {
|
2018-05-12 12:37:05 -07:00
|
|
|
return Err(format!("calling a {}-argument function with {} args", params.len(), args.len()))
|
2018-05-12 00:59:50 -07:00
|
|
|
}
|
2018-05-13 18:02:54 -07:00
|
|
|
let mut func_state = State {
|
|
|
|
values: self.values.new_frame(name.map(|n| format!("{}", n))),
|
|
|
|
type_context_handle: self.type_context_handle.clone(),
|
|
|
|
};
|
2018-05-12 00:59:50 -07:00
|
|
|
for (param, val) in params.into_iter().zip(args.into_iter()) {
|
2018-05-13 17:32:41 -07:00
|
|
|
let val = func_state.expression(val)?;
|
2018-05-12 00:59:50 -07:00
|
|
|
func_state.values.insert(param, ValueEntry::Binding { constant: true, val });
|
|
|
|
}
|
|
|
|
// TODO figure out function return semantics
|
2018-05-12 13:51:12 -07:00
|
|
|
func_state.block(body)
|
2018-05-11 01:30:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 01:57:29 -07:00
|
|
|
fn apply_builtin(&mut self, name: Rc<String>, args: Vec<Expr>) -> EvalResult<Expr> {
|
2018-05-11 01:30:02 -07:00
|
|
|
use self::Expr::*;
|
|
|
|
use self::Lit::*;
|
|
|
|
let evaled_args: Result<Vec<Expr>, String> = args.into_iter().map(|arg| self.expression(arg)).collect();
|
|
|
|
let evaled_args = evaled_args?;
|
|
|
|
|
|
|
|
Ok(match (name.as_str(), evaled_args.as_slice()) {
|
2018-05-11 01:46:42 -07:00
|
|
|
/* binops */
|
2018-05-11 01:30:02 -07:00
|
|
|
("+", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Nat(l + r)),
|
2018-05-11 01:35:39 -07:00
|
|
|
("++", &[Lit(StringLit(ref s1)), Lit(StringLit(ref s2))]) => Lit(StringLit(Rc::new(format!("{}{}", s1, s2)))),
|
|
|
|
("-", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Nat(l - r)),
|
|
|
|
("*", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Nat(l * r)),
|
|
|
|
("/", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Float((l as f64)/ (r as f64))),
|
|
|
|
("//", &[Lit(Nat(l)), Lit(Nat(r))]) => if r == 0 {
|
2018-05-12 12:37:05 -07:00
|
|
|
return Err(format!("divide by zero"));
|
2018-05-11 01:30:02 -07:00
|
|
|
} else {
|
2018-05-11 01:35:39 -07:00
|
|
|
Lit(Nat(l / r))
|
2018-05-11 01:30:02 -07:00
|
|
|
},
|
2018-05-11 01:35:39 -07:00
|
|
|
("%", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Nat(l % r)),
|
|
|
|
("^", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Nat(l ^ r)),
|
|
|
|
("&", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Nat(l & r)),
|
|
|
|
("|", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Nat(l | r)),
|
2018-05-11 01:46:42 -07:00
|
|
|
|
2018-05-12 13:58:21 -07:00
|
|
|
("==", &[Lit(Nat(l)), Lit(Nat(r))]) => Lit(Bool(l == r)),
|
|
|
|
("==", &[Lit(Int(l)), Lit(Int(r))]) => Lit(Bool(l == r)),
|
|
|
|
("==", &[Lit(Float(l)), Lit(Float(r))]) => Lit(Bool(l == r)),
|
|
|
|
("==", &[Lit(Bool(l)), Lit(Bool(r))]) => Lit(Bool(l == r)),
|
|
|
|
("==", &[Lit(StringLit(ref l)), Lit(StringLit(ref r))]) => Lit(Bool(l == r)),
|
|
|
|
|
2018-05-11 01:46:42 -07:00
|
|
|
/* prefix ops */
|
|
|
|
("!", &[Lit(Bool(true))]) => Lit(Bool(false)),
|
|
|
|
("!", &[Lit(Bool(false))]) => Lit(Bool(true)),
|
|
|
|
("-", &[Lit(Nat(n))]) => Lit(Int(-1*(n as i64))),
|
|
|
|
("-", &[Lit(Int(n))]) => Lit(Int(-1*(n as i64))),
|
|
|
|
("+", &[Lit(Int(n))]) => Lit(Int(n)),
|
|
|
|
("+", &[Lit(Nat(n))]) => Lit(Nat(n)),
|
|
|
|
|
2018-05-12 01:18:44 -07:00
|
|
|
|
2018-05-12 03:51:42 -07:00
|
|
|
/* builtin functions */
|
2018-05-12 01:18:44 -07:00
|
|
|
("print", &[ref anything]) => {
|
|
|
|
print!("{}", anything.to_repl());
|
|
|
|
Expr::Unit
|
|
|
|
},
|
|
|
|
("println", &[ref anything]) => {
|
|
|
|
println!("{}", anything.to_repl());
|
|
|
|
Expr::Unit
|
|
|
|
},
|
2018-05-12 02:27:54 -07:00
|
|
|
("getline", &[]) => {
|
|
|
|
let mut buf = String::new();
|
|
|
|
io::stdin().read_line(&mut buf).expect("Error readling line in 'getline'");
|
2018-05-12 14:00:48 -07:00
|
|
|
Lit(StringLit(Rc::new(buf.trim().to_string())))
|
2018-05-12 02:27:54 -07:00
|
|
|
},
|
2018-05-12 12:37:05 -07:00
|
|
|
(x, args) => return Err(format!("bad or unimplemented builtin {:?} | {:?}", x, args)),
|
2018-05-11 01:30:02 -07:00
|
|
|
})
|
|
|
|
}
|
2018-05-11 17:24:11 -07:00
|
|
|
|
2018-05-12 13:51:12 -07:00
|
|
|
fn conditional(&mut self, cond: Expr, then_clause: Vec<Stmt>, else_clause: Vec<Stmt>) -> EvalResult<Expr> {
|
|
|
|
let cond = self.expression(cond)?;
|
|
|
|
Ok(match cond {
|
|
|
|
Expr::Lit(Lit::Bool(true)) => self.block(then_clause)?,
|
|
|
|
Expr::Lit(Lit::Bool(false)) => self.block(else_clause)?,
|
|
|
|
_ => return Err(format!("Conditional with non-boolean condition"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-11 17:24:11 -07:00
|
|
|
fn value(&mut self, name: Rc<String>) -> EvalResult<Expr> {
|
|
|
|
use self::ValueEntry::*;
|
2018-05-14 22:55:53 -07:00
|
|
|
use self::Func::*;
|
2018-05-13 17:24:21 -07:00
|
|
|
//TODO add a layer of indirection here to talk to the symbol table first, and only then look up
|
|
|
|
//in the values table
|
2018-05-14 09:46:25 -07:00
|
|
|
|
|
|
|
let type_context = self.type_context_handle.borrow();
|
2018-05-14 22:55:53 -07:00
|
|
|
Ok(match type_context.symbol_table.values.get(&name) {
|
2018-05-15 23:48:36 -07:00
|
|
|
Some(Symbol { name, spec }) => match spec {
|
|
|
|
SymbolSpec::Custom(_typename) => {
|
2018-05-14 22:55:53 -07:00
|
|
|
Expr::Lit(Lit::Custom(name.clone()))
|
|
|
|
},
|
2018-05-15 23:48:36 -07:00
|
|
|
SymbolSpec::Func => match self.values.lookup(&name) {
|
2018-05-14 22:55:53 -07:00
|
|
|
Some(Binding { val: Expr::Func(UserDefined { name, params, body }), .. }) => {
|
|
|
|
Expr::Func(UserDefined { name: name.clone(), params: params.clone(), body: body.clone() })
|
|
|
|
},
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
/* see if it's an ordinary variable TODO make variables go in symbol table */
|
|
|
|
None => match self.values.lookup(&name) {
|
|
|
|
Some(Binding { val, .. }) => val.clone(),
|
|
|
|
None => return Err(format!("Couldn't find value {}", name)),
|
2018-05-11 17:24:11 -07:00
|
|
|
}
|
2018-05-14 22:55:53 -07:00
|
|
|
})
|
2018-05-11 23:23:54 -07:00
|
|
|
}
|
2018-05-09 02:27:57 -07:00
|
|
|
}
|
2018-05-12 02:18:34 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod eval_tests {
|
2018-05-14 23:25:28 -07:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use typechecking::TypeContext;
|
2018-05-12 02:18:34 -07:00
|
|
|
use tokenizing::tokenize;
|
|
|
|
use parsing::parse;
|
|
|
|
use eval::State;
|
|
|
|
|
|
|
|
macro_rules! fresh_env {
|
|
|
|
($string:expr, $correct:expr) => {
|
2018-05-14 23:25:28 -07:00
|
|
|
let type_context = Rc::new(RefCell::new(TypeContext::new()));
|
|
|
|
let mut state = State::new(type_context);
|
2018-05-12 02:20:50 -07:00
|
|
|
let all_output = state.evaluate(parse(tokenize($string)).0.unwrap().reduce(), true);
|
2018-05-12 03:51:42 -07:00
|
|
|
let ref output = all_output.last().unwrap();
|
|
|
|
assert_eq!(**output, Ok($correct.to_string()));
|
2018-05-12 02:18:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_basic_eval() {
|
|
|
|
fresh_env!("1 + 2", "3");
|
2018-05-12 03:51:42 -07:00
|
|
|
fresh_env!("var a = 1; a = 2", "Unit");
|
|
|
|
fresh_env!("var a = 1; a = 2; a", "2");
|
2018-05-12 12:58:57 -07:00
|
|
|
fresh_env!(r#"("a", 1 + 2)"#, r#"("a", 3)"#);
|
2018-05-12 02:18:34 -07:00
|
|
|
}
|
2018-05-13 17:32:41 -07:00
|
|
|
|
2018-05-14 23:33:11 -07:00
|
|
|
#[test]
|
2018-05-13 17:32:41 -07:00
|
|
|
fn function_eval() {
|
|
|
|
fresh_env!("fn oi(x) { x + 1 }; oi(4)", "5");
|
|
|
|
fresh_env!("fn oi(x) { x + 1 }; oi(1+2)", "4");
|
|
|
|
}
|
2018-05-14 23:33:11 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn scopes() {
|
|
|
|
let scope_ok = r#"
|
|
|
|
const a = 20
|
|
|
|
fn haha() {
|
|
|
|
const a = 10
|
|
|
|
a
|
|
|
|
}
|
|
|
|
haha()
|
|
|
|
"#;
|
|
|
|
fresh_env!(scope_ok, "10");
|
|
|
|
let scope_ok = r#"
|
|
|
|
const a = 20
|
|
|
|
fn haha() {
|
|
|
|
const a = 10
|
|
|
|
a
|
|
|
|
}
|
|
|
|
a
|
|
|
|
"#;
|
|
|
|
fresh_env!(scope_ok, "20");
|
|
|
|
}
|
2018-05-12 02:18:34 -07:00
|
|
|
}
|