2018-08-14 21:17:43 -07:00
|
|
|
|
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;
|
|
|
|
|
2019-01-07 13:00:37 -08:00
|
|
|
use crate::util::ScopeStack;
|
|
|
|
use crate::reduced_ast::{BoundVars, ReducedAST, Stmt, Expr, Lit, Func, Alternative, Subpattern};
|
2019-10-18 18:24:57 -07:00
|
|
|
use crate::symbol_table::{SymbolSpec, Symbol, SymbolTable, ScopeSegment, FullyQualifiedSymbolName};
|
2019-08-12 14:10:07 -07:00
|
|
|
use crate::builtin::Builtin;
|
2017-09-30 23:30:02 -07:00
|
|
|
|
2019-08-16 10:39:21 -07:00
|
|
|
mod test;
|
|
|
|
|
2018-02-24 13:56:04 -08:00
|
|
|
pub struct State<'a> {
|
2018-08-19 21:31:28 -07:00
|
|
|
values: ScopeStack<'a, Rc<String>, ValueEntry>,
|
2018-05-20 20:36:57 -07:00
|
|
|
symbol_table_handle: Rc<RefCell<SymbolTable>>,
|
2017-10-13 03:05:18 -07:00
|
|
|
}
|
2018-05-11 01:56:12 -07:00
|
|
|
|
|
|
|
impl<'a> State<'a> {
|
2018-05-20 20:36:57 -07:00
|
|
|
pub fn new(symbol_table_handle: Rc<RefCell<SymbolTable>>) -> State<'a> {
|
2019-08-12 14:10:07 -07:00
|
|
|
let values = ScopeStack::new(Some(format!("global")));
|
2018-05-20 20:36:57 -07:00
|
|
|
State { values, symbol_table_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-11-05 03:41:03 -08:00
|
|
|
|
|
|
|
fn new_frame(&'a self, items: &'a Vec<Node>, bound_vars: &BoundVars) -> State<'a> {
|
|
|
|
let mut inner_state = State {
|
|
|
|
values: self.values.new_scope(None),
|
|
|
|
symbol_table_handle: self.symbol_table_handle.clone(),
|
|
|
|
};
|
|
|
|
for (bound_var, val) in bound_vars.iter().zip(items.iter()) {
|
|
|
|
if let Some(bv) = bound_var.as_ref() {
|
|
|
|
inner_state.values.insert(bv.clone(), ValueEntry::Binding { constant: true, val: val.clone() });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inner_state
|
|
|
|
}
|
2018-05-11 01:56:12 -07:00
|
|
|
}
|
|
|
|
|
2018-08-13 23:28:03 -07:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum Node {
|
|
|
|
Expr(Expr),
|
|
|
|
PrimObject {
|
|
|
|
name: Rc<String>,
|
|
|
|
tag: usize,
|
|
|
|
items: Vec<Node>,
|
2018-08-14 02:03:05 -07:00
|
|
|
},
|
|
|
|
PrimTuple {
|
|
|
|
items: Vec<Node>
|
2018-08-13 23:28:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn paren_wrapped_vec(terms: impl Iterator<Item=String>) -> String {
|
|
|
|
let mut buf = String::new();
|
|
|
|
write!(buf, "(").unwrap();
|
|
|
|
for term in terms.map(|e| Some(e)).intersperse(None) {
|
|
|
|
match term {
|
|
|
|
Some(e) => write!(buf, "{}", e).unwrap(),
|
|
|
|
None => write!(buf, ", ").unwrap(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
write!(buf, ")").unwrap();
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Node {
|
2019-08-19 19:38:24 -07:00
|
|
|
fn to_repl(&self, symbol_table: &SymbolTable) -> String {
|
2018-08-13 23:28:03 -07:00
|
|
|
match self {
|
2019-08-19 19:38:24 -07:00
|
|
|
Node::Expr(e) => e.to_repl(symbol_table),
|
2018-08-14 23:19:27 -07:00
|
|
|
Node::PrimObject { name, items, .. } if items.len() == 0 => format!("{}", name),
|
2019-08-19 19:38:24 -07:00
|
|
|
Node::PrimObject { name, items, .. } => format!("{}{}", name, paren_wrapped_vec(items.iter().map(|x| x.to_repl(symbol_table)))),
|
|
|
|
Node::PrimTuple { items } => format!("{}", paren_wrapped_vec(items.iter().map(|x| x.to_repl(symbol_table)))),
|
2018-08-13 23:28:03 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-05 03:17:03 -08:00
|
|
|
fn is_true(&self) -> bool {
|
|
|
|
match self {
|
2019-01-07 13:00:37 -08:00
|
|
|
Node::Expr(Expr::Lit(crate::reduced_ast::Lit::Bool(true))) => true,
|
2018-11-05 03:17:03 -08:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 23:28:03 -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-08-13 23:28:03 -07:00
|
|
|
val: /*FullyEvaluatedExpr*/ Node, //TODO make this use a subtype to represent fully evaluatedness
|
2018-05-11 01:57:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type EvalResult<T> = Result<T, String>;
|
|
|
|
|
2018-05-11 00:25:43 -07:00
|
|
|
impl Expr {
|
2018-08-13 23:28:03 -07:00
|
|
|
fn to_node(self) -> Node {
|
|
|
|
Node::Expr(self)
|
|
|
|
}
|
2019-08-19 19:38:24 -07:00
|
|
|
fn to_repl(&self, symbol_table: &SymbolTable) -> String {
|
2018-05-11 01:07:18 -07:00
|
|
|
use self::Lit::*;
|
2018-05-11 23:23:54 -07:00
|
|
|
use self::Func::*;
|
2018-06-04 18:17:03 -07:00
|
|
|
|
2019-08-19 19:38:24 -07:00
|
|
|
let _ = symbol_table;
|
|
|
|
|
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-11 01:07:18 -07:00
|
|
|
},
|
2018-05-11 23:23:54 -07:00
|
|
|
Expr::Func(f) => match f {
|
2019-08-12 14:10:07 -07:00
|
|
|
BuiltIn(builtin) => format!("<built-in function '{:?}'>", builtin),
|
2018-05-11 23:23:54 -07:00
|
|
|
UserDefined { name: None, .. } => format!("<function>"),
|
2018-08-05 17:15:58 -07:00
|
|
|
UserDefined { name: Some(name), .. } => format!("<function '{}'>", name),
|
2018-05-11 23:23:54 -07:00
|
|
|
},
|
2019-08-05 01:07:48 -07:00
|
|
|
Expr::Constructor { type_name, arity, .. } => {
|
|
|
|
format!("<constructor for `{}` arity {}>", type_name, arity)
|
2018-08-05 17:15:58 -07:00
|
|
|
},
|
2019-08-19 19:38:24 -07:00
|
|
|
Expr::Tuple(exprs) => paren_wrapped_vec(exprs.iter().map(|x| x.to_repl(symbol_table))),
|
2018-05-11 01:07:18 -07:00
|
|
|
_ => format!("{:?}", self),
|
|
|
|
}
|
2018-05-11 00:25:43 -07:00
|
|
|
}
|
2018-10-29 01:50:43 -07:00
|
|
|
|
|
|
|
fn replace_conditional_target_sigil(self, replacement: &Expr) -> Expr {
|
|
|
|
use self::Expr::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
ConditionalTargetSigilValue => replacement.clone(),
|
2019-08-30 19:10:16 -07:00
|
|
|
Unit | Lit(_) | Func(_) | Sym(_) | Constructor { .. } |
|
2019-08-12 11:40:16 -07:00
|
|
|
CaseMatch { .. } | UnimplementedSigilValue | ReductionError(_) => self,
|
2018-10-29 01:50:43 -07:00
|
|
|
Tuple(exprs) => Tuple(exprs.into_iter().map(|e| e.replace_conditional_target_sigil(replacement)).collect()),
|
|
|
|
Call { f, args } => {
|
|
|
|
let new_args = args.into_iter().map(|e| e.replace_conditional_target_sigil(replacement)).collect();
|
|
|
|
Call { f, args: new_args }
|
|
|
|
},
|
|
|
|
Conditional { .. } => panic!("Dunno if I need this, but if so implement"),
|
|
|
|
Assign { .. } => panic!("I'm pretty sure I don't need this"),
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2019-08-19 19:38:24 -07:00
|
|
|
Ok(Some(ref output)) if repl => {
|
|
|
|
let ref symbol_table = self.symbol_table_handle.borrow();
|
|
|
|
acc.push(Ok(output.to_repl(symbol_table)))
|
|
|
|
},
|
2018-05-11 00:25:43 -07:00
|
|
|
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 } => {
|
2018-08-13 23:28:03 -07:00
|
|
|
let v_entry = ValueEntry::Binding { constant: true, val: Node::Expr(Expr::Func(func.clone())) };
|
2018-05-13 17:24:21 -07:00
|
|
|
self.values.insert(name.clone(), v_entry);
|
|
|
|
},
|
|
|
|
Stmt::Expr(_expr) => {
|
|
|
|
//TODO have this support things like nested function defs
|
|
|
|
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:28:03 -07:00
|
|
|
fn statement(&mut self, stmt: Stmt) -> EvalResult<Option<Node>> {
|
2018-05-11 01:07:18 -07:00
|
|
|
match stmt {
|
2018-05-11 02:24:38 -07:00
|
|
|
Stmt::Binding { name, constant, expr } => {
|
2018-08-13 23:28:03 -07:00
|
|
|
let val = self.expression(Node::Expr(expr))?;
|
2018-05-11 02:24:38 -07:00
|
|
|
self.values.insert(name.clone(), ValueEntry::Binding { constant, val });
|
2018-05-11 01:07:18 -07:00
|
|
|
Ok(None)
|
|
|
|
},
|
2018-08-13 23:28:03 -07:00
|
|
|
Stmt::Expr(expr) => Ok(Some(self.expression(expr.to_node())?)),
|
2018-05-13 17:24:21 -07:00
|
|
|
Stmt::PreBinding {..} | Stmt::Noop => Ok(None),
|
2018-05-11 01:07:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:28:03 -07:00
|
|
|
fn block(&mut self, stmts: Vec<Stmt>) -> EvalResult<Node> {
|
2018-05-12 13:51:12 -07:00
|
|
|
let mut ret = None;
|
|
|
|
for stmt in stmts {
|
|
|
|
ret = self.statement(stmt)?;
|
|
|
|
}
|
2018-08-13 23:28:03 -07:00
|
|
|
Ok(ret.unwrap_or(Node::Expr(Expr::Unit)))
|
2018-05-12 13:51:12 -07:00
|
|
|
}
|
|
|
|
|
2018-08-13 23:28:03 -07:00
|
|
|
fn expression(&mut self, node: Node) -> EvalResult<Node> {
|
2018-05-11 01:07:18 -07:00
|
|
|
use self::Expr::*;
|
2018-08-13 23:28:03 -07:00
|
|
|
match node {
|
2018-08-14 02:03:05 -07:00
|
|
|
t @ Node::PrimTuple { .. } => Ok(t),
|
2018-08-13 23:28:03 -07:00
|
|
|
obj @ Node::PrimObject { .. } => Ok(obj),
|
|
|
|
Node::Expr(expr) => match expr {
|
|
|
|
literal @ Lit(_) => Ok(Node::Expr(literal)),
|
2018-10-17 19:39:48 -07:00
|
|
|
Call { box f, args } => self.call_expression(f, args),
|
2019-08-30 19:10:16 -07:00
|
|
|
Sym(v) => self.handle_sym(v),
|
2018-08-14 21:17:43 -07:00
|
|
|
Constructor { arity, ref name, tag, .. } if arity == 0 => Ok(Node::PrimObject { name: name.clone(), tag, items: vec![] }),
|
2018-08-13 23:28:03 -07:00
|
|
|
constructor @ Constructor { .. } => Ok(Node::Expr(constructor)),
|
|
|
|
func @ Func(_) => Ok(Node::Expr(func)),
|
|
|
|
Tuple(exprs) => {
|
2018-08-14 02:03:05 -07:00
|
|
|
let nodes = exprs.into_iter().map(|expr| self.expression(Node::Expr(expr))).collect::<Result<Vec<Node>,_>>()?;
|
|
|
|
Ok(Node::PrimTuple { items: nodes })
|
2018-08-13 23:28:03 -07:00
|
|
|
},
|
|
|
|
Conditional { box cond, then_clause, else_clause } => self.conditional(cond, then_clause, else_clause),
|
2018-10-18 01:46:30 -07:00
|
|
|
Assign { box val, box expr } => self.assign_expression(val, expr),
|
2018-08-14 12:43:06 -07:00
|
|
|
Unit => Ok(Node::Expr(Unit)),
|
2018-10-18 01:49:42 -07:00
|
|
|
CaseMatch { box cond, alternatives } => self.case_match_expression(cond, alternatives),
|
2018-10-29 01:50:43 -07:00
|
|
|
ConditionalTargetSigilValue => Ok(Node::Expr(ConditionalTargetSigilValue)),
|
2018-10-28 12:45:45 -07:00
|
|
|
UnimplementedSigilValue => Err(format!("Sigil value eval not implemented")),
|
2019-08-12 11:40:16 -07:00
|
|
|
ReductionError(err) => Err(format!("Reduction error: {}", err)),
|
2018-08-13 23:28:03 -07:00
|
|
|
}
|
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-10-17 19:39:48 -07:00
|
|
|
fn call_expression(&mut self, f: Expr, args: Vec<Expr>) -> EvalResult<Node> {
|
|
|
|
use self::Expr::*;
|
|
|
|
match self.expression(Node::Expr(f))? {
|
|
|
|
Node::Expr(Constructor { type_name, name, tag, arity }) => self.apply_data_constructor(type_name, name, tag, arity, args),
|
|
|
|
Node::Expr(Func(f)) => self.apply_function(f, args),
|
|
|
|
other => return Err(format!("Tried to call {:?} which is not a function or data constructor", other)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 00:55:37 -07:00
|
|
|
fn apply_data_constructor(&mut self, _type_name: Rc<String>, name: Rc<String>, tag: usize, arity: usize, args: Vec<Expr>) -> EvalResult<Node> {
|
2018-08-05 17:15:58 -07:00
|
|
|
if arity != args.len() {
|
2019-08-05 01:11:01 -07:00
|
|
|
return Err(format!("Data constructor {} requires {} arg(s)", name, arity));
|
2018-06-04 18:17:03 -07:00
|
|
|
}
|
2018-08-05 17:15:58 -07:00
|
|
|
|
2018-08-13 23:28:03 -07:00
|
|
|
let evaled_args = args.into_iter().map(|expr| self.expression(Node::Expr(expr))).collect::<Result<Vec<Node>,_>>()?;
|
2018-06-12 19:37:53 -07:00
|
|
|
//let evaled_args = vec![];
|
2018-08-13 23:28:03 -07:00
|
|
|
Ok(Node::PrimObject {
|
2018-08-05 17:15:58 -07:00
|
|
|
name: name.clone(),
|
|
|
|
items: evaled_args,
|
2018-08-14 02:03:05 -07:00
|
|
|
tag
|
2018-08-13 23:28:03 -07:00
|
|
|
})
|
2018-06-04 18:17:03 -07:00
|
|
|
}
|
|
|
|
|
2018-08-13 23:28:03 -07:00
|
|
|
fn apply_function(&mut self, f: Func, args: Vec<Expr>) -> EvalResult<Node> {
|
2018-05-11 01:30:02 -07:00
|
|
|
match f {
|
2019-08-19 21:49:46 -07:00
|
|
|
Func::BuiltIn(builtin) => Ok(self.apply_builtin(builtin, 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 {
|
2018-08-14 21:45:45 -07:00
|
|
|
values: self.values.new_scope(name.map(|n| format!("{}", n))),
|
2018-05-20 20:36:57 -07:00
|
|
|
symbol_table_handle: self.symbol_table_handle.clone(),
|
2018-05-13 18:02:54 -07:00
|
|
|
};
|
2018-05-12 00:59:50 -07:00
|
|
|
for (param, val) in params.into_iter().zip(args.into_iter()) {
|
2018-08-13 23:28:03 -07:00
|
|
|
let val = func_state.expression(Node::Expr(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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 21:49:46 -07:00
|
|
|
fn apply_builtin(&mut self, builtin: Builtin, args: Vec<Expr>) -> EvalResult<Node> {
|
2018-05-11 01:30:02 -07:00
|
|
|
use self::Expr::*;
|
|
|
|
use self::Lit::*;
|
2019-08-12 14:10:07 -07:00
|
|
|
use Builtin::*;
|
|
|
|
|
2019-08-19 21:49:46 -07:00
|
|
|
let evaled_args: Result<Vec<Node>, String> = args.into_iter().map(|arg| self.expression(arg.to_node()))
|
|
|
|
.collect();
|
2018-05-11 01:30:02 -07:00
|
|
|
let evaled_args = evaled_args?;
|
|
|
|
|
2019-08-12 14:10:07 -07:00
|
|
|
Ok(match (builtin, evaled_args.as_slice()) {
|
2019-09-10 03:40:41 -07:00
|
|
|
(FieldAccess, &[Node::PrimObject { .. }]) => {
|
|
|
|
//TODO implement field access
|
2019-08-20 00:20:07 -07:00
|
|
|
unimplemented!()
|
|
|
|
},
|
2019-08-19 21:49:46 -07:00
|
|
|
(binop, &[Node::Expr(ref lhs), Node::Expr(ref rhs)]) => match (binop, lhs, rhs) {
|
|
|
|
/* binops */
|
|
|
|
(Add, Lit(Nat(l)), Lit(Nat(r))) => Lit(Nat(l + r)),
|
|
|
|
(Concatenate, Lit(StringLit(ref s1)), Lit(StringLit(ref s2))) => Lit(StringLit(Rc::new(format!("{}{}", s1, s2)))),
|
|
|
|
(Subtract, Lit(Nat(l)), Lit(Nat(r))) => Lit(Nat(l - r)),
|
|
|
|
(Multiply, Lit(Nat(l)), Lit(Nat(r))) => Lit(Nat(l * r)),
|
|
|
|
(Divide, Lit(Nat(l)), Lit(Nat(r))) => Lit(Float((*l as f64)/ (*r as f64))),
|
|
|
|
(Quotient, Lit(Nat(l)), Lit(Nat(r))) => if *r == 0 {
|
|
|
|
return Err(format!("divide by zero"));
|
|
|
|
} else {
|
|
|
|
Lit(Nat(l / r))
|
|
|
|
},
|
|
|
|
(Modulo, Lit(Nat(l)), Lit(Nat(r))) => Lit(Nat(l % r)),
|
|
|
|
(Exponentiation, Lit(Nat(l)), Lit(Nat(r))) => Lit(Nat(l ^ r)),
|
|
|
|
(BitwiseAnd, Lit(Nat(l)), Lit(Nat(r))) => Lit(Nat(l & r)),
|
|
|
|
(BitwiseOr, Lit(Nat(l)), Lit(Nat(r))) => Lit(Nat(l | r)),
|
|
|
|
|
|
|
|
/* comparisons */
|
|
|
|
(Equality, Lit(Nat(l)), Lit(Nat(r))) => Lit(Bool(l == r)),
|
|
|
|
(Equality, Lit(Int(l)), Lit(Int(r))) => Lit(Bool(l == r)),
|
|
|
|
(Equality, Lit(Float(l)), Lit(Float(r))) => Lit(Bool(l == r)),
|
|
|
|
(Equality, Lit(Bool(l)), Lit(Bool(r))) => Lit(Bool(l == r)),
|
|
|
|
(Equality, Lit(StringLit(ref l)), Lit(StringLit(ref r))) => Lit(Bool(l == r)),
|
|
|
|
|
|
|
|
(LessThan, Lit(Nat(l)), Lit(Nat(r))) => Lit(Bool(l < r)),
|
|
|
|
(LessThan, Lit(Int(l)), Lit(Int(r))) => Lit(Bool(l < r)),
|
|
|
|
(LessThan, Lit(Float(l)), Lit(Float(r))) => Lit(Bool(l < r)),
|
|
|
|
|
|
|
|
(LessThanOrEqual, Lit(Nat(l)), Lit(Nat(r))) => Lit(Bool(l <= r)),
|
|
|
|
(LessThanOrEqual, Lit(Int(l)), Lit(Int(r))) => Lit(Bool(l <= r)),
|
|
|
|
(LessThanOrEqual, Lit(Float(l)), Lit(Float(r))) => Lit(Bool(l <= r)),
|
|
|
|
|
|
|
|
(GreaterThan, Lit(Nat(l)), Lit(Nat(r))) => Lit(Bool(l > r)),
|
|
|
|
(GreaterThan, Lit(Int(l)), Lit(Int(r))) => Lit(Bool(l > r)),
|
|
|
|
(GreaterThan, Lit(Float(l)), Lit(Float(r))) => Lit(Bool(l > r)),
|
|
|
|
|
|
|
|
(GreaterThanOrEqual, Lit(Nat(l)), Lit(Nat(r))) => Lit(Bool(l >= r)),
|
|
|
|
(GreaterThanOrEqual, Lit(Int(l)), Lit(Int(r))) => Lit(Bool(l >= r)),
|
|
|
|
(GreaterThanOrEqual, Lit(Float(l)), Lit(Float(r))) => Lit(Bool(l >= r)),
|
|
|
|
_ => return Err("No valid binop".to_string())
|
|
|
|
}.to_node(),
|
|
|
|
(prefix, &[Node::Expr(ref arg)]) => match (prefix, arg) {
|
|
|
|
(BooleanNot, Lit(Bool(true))) => Lit(Bool(false)),
|
|
|
|
(BooleanNot, Lit(Bool(false))) => Lit(Bool(true)),
|
|
|
|
(Negate, Lit(Nat(n))) => Lit(Int(-1*(*n as i64))),
|
|
|
|
(Negate, Lit(Int(n))) => Lit(Int(-1*(*n as i64))),
|
|
|
|
(Increment, Lit(Int(n))) => Lit(Int(*n)),
|
|
|
|
(Increment, Lit(Nat(n))) => Lit(Nat(*n)),
|
|
|
|
_ => return Err("No valid prefix op".to_string())
|
|
|
|
}.to_node(),
|
2018-05-12 01:18:44 -07:00
|
|
|
|
2018-05-12 03:51:42 -07:00
|
|
|
/* builtin functions */
|
2019-08-12 14:10:07 -07:00
|
|
|
(IOPrint, &[ref anything]) => {
|
2019-08-19 19:38:24 -07:00
|
|
|
let ref symbol_table = self.symbol_table_handle.borrow();
|
|
|
|
print!("{}", anything.to_repl(symbol_table));
|
2019-08-19 21:49:46 -07:00
|
|
|
Expr::Unit.to_node()
|
2018-05-12 01:18:44 -07:00
|
|
|
},
|
2019-08-12 14:10:07 -07:00
|
|
|
(IOPrintLn, &[ref anything]) => {
|
2019-08-19 19:38:24 -07:00
|
|
|
let ref symbol_table = self.symbol_table_handle.borrow();
|
|
|
|
println!("{}", anything.to_repl(symbol_table));
|
2019-08-19 21:49:46 -07:00
|
|
|
Expr::Unit.to_node()
|
2018-05-12 01:18:44 -07:00
|
|
|
},
|
2019-08-12 14:10:07 -07:00
|
|
|
(IOGetLine, &[]) => {
|
2018-05-12 02:27:54 -07:00
|
|
|
let mut buf = String::new();
|
|
|
|
io::stdin().read_line(&mut buf).expect("Error readling line in 'getline'");
|
2019-08-19 21:49:46 -07:00
|
|
|
Lit(StringLit(Rc::new(buf.trim().to_string()))).to_node()
|
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-08-13 23:28:03 -07:00
|
|
|
fn conditional(&mut self, cond: Expr, then_clause: Vec<Stmt>, else_clause: Vec<Stmt>) -> EvalResult<Node> {
|
|
|
|
let cond = self.expression(Node::Expr(cond))?;
|
2018-05-12 13:51:12 -07:00
|
|
|
Ok(match cond {
|
2018-08-13 23:28:03 -07:00
|
|
|
Node::Expr(Expr::Lit(Lit::Bool(true))) => self.block(then_clause)?,
|
|
|
|
Node::Expr(Expr::Lit(Lit::Bool(false))) => self.block(else_clause)?,
|
2018-05-12 13:51:12 -07:00
|
|
|
_ => return Err(format!("Conditional with non-boolean condition"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:46:30 -07:00
|
|
|
fn assign_expression(&mut self, val: Expr, expr: Expr) -> EvalResult<Node> {
|
|
|
|
let name = match val {
|
2019-08-30 19:10:16 -07:00
|
|
|
Expr::Sym(name) => name,
|
2018-10-18 01:46:30 -07:00
|
|
|
_ => return Err(format!("Trying to assign to a non-value")),
|
|
|
|
};
|
|
|
|
|
|
|
|
let constant = match self.values.lookup(&name) {
|
|
|
|
None => return Err(format!("Constant {} is undefined", name)),
|
|
|
|
Some(ValueEntry::Binding { constant, .. }) => constant.clone(),
|
|
|
|
};
|
|
|
|
if constant {
|
|
|
|
return Err(format!("trying to update {}, a non-mutable binding", name));
|
|
|
|
}
|
|
|
|
let val = self.expression(Node::Expr(expr))?;
|
|
|
|
self.values.insert(name.clone(), ValueEntry::Binding { constant: false, val });
|
|
|
|
Ok(Node::Expr(Expr::Unit))
|
|
|
|
}
|
|
|
|
|
2018-11-05 04:02:04 -08:00
|
|
|
fn guard_passes(&mut self, guard: &Option<Expr>, cond: &Node) -> EvalResult<bool> {
|
|
|
|
if let Some(ref guard_expr) = guard {
|
|
|
|
let guard_expr = match cond {
|
|
|
|
Node::Expr(ref e) => guard_expr.clone().replace_conditional_target_sigil(e),
|
|
|
|
_ => guard_expr.clone()
|
|
|
|
};
|
|
|
|
Ok(self.expression(guard_expr.to_node())?.is_true())
|
|
|
|
} else {
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 02:43:47 -07:00
|
|
|
fn case_match_expression(&mut self, cond: Expr, alternatives: Vec<Alternative>) -> EvalResult<Node> {
|
2018-11-05 03:17:03 -08:00
|
|
|
|
2018-11-05 04:02:04 -08:00
|
|
|
//TODO need to handle recursive subpatterns
|
|
|
|
let all_subpatterns_pass = |state: &mut State, subpatterns: &Vec<Option<Subpattern>>, items: &Vec<Node>| -> EvalResult<bool> {
|
2018-11-05 14:01:14 -08:00
|
|
|
|
|
|
|
if subpatterns.len() == 0 {
|
|
|
|
return Ok(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
if items.len() != subpatterns.len() {
|
|
|
|
return Err(format!("Subpattern length isn't correct items {} subpatterns {}", items.len(), subpatterns.len()));
|
|
|
|
}
|
|
|
|
|
2018-11-05 04:02:04 -08:00
|
|
|
for (maybe_subp, cond) in subpatterns.iter().zip(items.iter()) {
|
|
|
|
if let Some(subp) = maybe_subp {
|
|
|
|
if !state.guard_passes(&subp.guard, &cond)? {
|
|
|
|
return Ok(false)
|
|
|
|
}
|
2018-10-18 01:49:42 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-05 04:02:04 -08:00
|
|
|
Ok(true)
|
|
|
|
};
|
2018-11-01 02:43:47 -07:00
|
|
|
|
2018-11-05 04:02:04 -08:00
|
|
|
let cond = self.expression(Node::Expr(cond))?;
|
|
|
|
for alt in alternatives {
|
|
|
|
// no matter what type of condition we have, ignore alternative if the guard evaluates false
|
2019-07-30 01:33:09 -07:00
|
|
|
if !self.guard_passes(&alt.matchable.guard, &cond)? {
|
2018-11-05 04:02:04 -08:00
|
|
|
continue;
|
2018-11-05 03:41:03 -08:00
|
|
|
}
|
2018-11-01 02:43:47 -07:00
|
|
|
|
2018-11-05 03:41:03 -08:00
|
|
|
match cond {
|
|
|
|
Node::PrimObject { ref tag, ref items, .. } => {
|
2019-07-30 01:33:09 -07:00
|
|
|
if alt.matchable.tag.map(|t| t == *tag).unwrap_or(true) {
|
|
|
|
let mut inner_state = self.new_frame(items, &alt.matchable.bound_vars);
|
|
|
|
if all_subpatterns_pass(&mut inner_state, &alt.matchable.subpatterns, items)? {
|
2018-11-05 14:01:14 -08:00
|
|
|
return inner_state.block(alt.item);
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2018-11-01 02:43:47 -07:00
|
|
|
}
|
|
|
|
},
|
2018-11-03 12:53:09 -07:00
|
|
|
Node::PrimTuple { ref items } => {
|
2019-07-30 01:33:09 -07:00
|
|
|
let mut inner_state = self.new_frame(items, &alt.matchable.bound_vars);
|
|
|
|
if all_subpatterns_pass(&mut inner_state, &alt.matchable.subpatterns, items)? {
|
2018-11-05 14:01:14 -08:00
|
|
|
return inner_state.block(alt.item);
|
|
|
|
} else {
|
2018-11-05 03:17:03 -08:00
|
|
|
continue;
|
|
|
|
}
|
2018-11-01 02:43:47 -07:00
|
|
|
},
|
2018-11-02 19:54:04 -07:00
|
|
|
Node::Expr(ref _e) => {
|
2019-07-30 01:33:09 -07:00
|
|
|
if let None = alt.matchable.tag {
|
2018-11-01 02:43:47 -07:00
|
|
|
return self.block(alt.item)
|
|
|
|
}
|
2018-10-18 01:49:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-01 02:43:47 -07:00
|
|
|
Err(format!("{:?} failed pattern match", cond))
|
2018-10-18 01:49:42 -07:00
|
|
|
}
|
|
|
|
|
2019-03-11 19:36:10 -07:00
|
|
|
//TODO if I don't need to lookup by name here...
|
2019-08-30 19:10:16 -07:00
|
|
|
fn handle_sym(&mut self, name: Rc<String>) -> EvalResult<Node> {
|
2018-05-11 17:24:11 -07:00
|
|
|
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
|
|
|
|
2018-05-20 20:36:57 -07:00
|
|
|
let symbol_table = self.symbol_table_handle.borrow();
|
2019-08-30 19:05:01 -07:00
|
|
|
let value = symbol_table.lookup_by_fqsn(&fqsn!(name ; tr));
|
2018-06-12 19:37:53 -07:00
|
|
|
Ok(match value {
|
2019-09-24 18:56:53 -07:00
|
|
|
Some(Symbol { local_name, spec, .. }) => match spec {
|
2018-08-14 23:19:27 -07:00
|
|
|
//TODO I'll need this type_name later to do a table lookup
|
|
|
|
SymbolSpec::DataConstructor { type_name: _type_name, type_args, .. } => {
|
2018-05-30 23:54:24 -07:00
|
|
|
if type_args.len() == 0 {
|
2019-09-24 18:56:53 -07:00
|
|
|
Node::PrimObject { name: local_name.clone(), tag: 0, items: vec![] }
|
2018-05-30 23:54:24 -07:00
|
|
|
} else {
|
|
|
|
return Err(format!("This data constructor thing not done"))
|
|
|
|
}
|
2018-05-14 22:55:53 -07:00
|
|
|
},
|
2018-06-03 02:39:49 -07:00
|
|
|
SymbolSpec::Func(_) => match self.values.lookup(&name) {
|
2018-08-13 23:28:03 -07:00
|
|
|
Some(Binding { val: Node::Expr(Expr::Func(UserDefined { name, params, body })), .. }) => {
|
|
|
|
Node::Expr(Expr::Func(UserDefined { name: name.clone(), params: params.clone(), body: body.clone() }))
|
2018-05-14 22:55:53 -07:00
|
|
|
},
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
2019-01-25 00:57:01 -08:00
|
|
|
SymbolSpec::RecordConstructor { .. } => return Err(format!("This shouldn't be a record!")),
|
2019-03-10 17:02:01 -07:00
|
|
|
SymbolSpec::Binding => match self.values.lookup(&name) {
|
|
|
|
Some(Binding { val, .. }) => val.clone(),
|
|
|
|
None => return Err(format!("Symbol {} exists in symbol table but not in evaluator table", name))
|
|
|
|
}
|
2019-10-16 20:22:40 -07:00
|
|
|
SymbolSpec::Type { name } => return Err(format!("Symbol {} not in scope", name)),
|
2018-05-14 22:55:53 -07:00
|
|
|
},
|
2019-03-10 17:02:01 -07:00
|
|
|
//TODO ideally this should be returning a runtime error if this is ever None, but it's not
|
|
|
|
//handling all bindings correctly yet
|
|
|
|
//None => return Err(format!("Couldn't find value {}", name)),
|
2018-05-14 22:55:53 -07:00
|
|
|
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
|
|
|
}
|