use std::rc::Rc; use std::fmt::Write; use std::io; use crate::schala::SymbolTableHandle; use crate::util::ScopeStack; use crate::reduced_ast::{BoundVars, ReducedAST, Stmt, Expr, Lit, Func, Alternative, Subpattern}; use crate::symbol_table::{SymbolSpec, Symbol, SymbolTable, FullyQualifiedSymbolName}; use crate::builtin::Builtin; mod test; pub struct State<'a> { values: ScopeStack<'a, Rc, ValueEntry>, } impl<'a> State<'a> { pub fn new() -> State<'a> { let values = ScopeStack::new(Some(format!("global"))); State { values } } pub fn debug_print(&self) -> String { format!("Values: {:?}", self.values) } fn new_frame(&'a self, items: &'a Vec, bound_vars: &BoundVars) -> State<'a> { let mut inner_state = State { values: self.values.new_scope(None), }; 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 } } #[derive(Debug, Clone)] enum Node { Expr(Expr), PrimObject { name: Rc, tag: usize, items: Vec, }, PrimTuple { items: Vec } } fn paren_wrapped_vec(terms: impl Iterator) -> 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 { fn to_repl(&self) -> String { match self { Node::Expr(e) => e.to_repl(), Node::PrimObject { name, items, .. } if items.len() == 0 => format!("{}", name), Node::PrimObject { name, items, .. } => format!("{}{}", name, paren_wrapped_vec(items.iter().map(|x| x.to_repl()))), Node::PrimTuple { items } => format!("{}", paren_wrapped_vec(items.iter().map(|x| x.to_repl()))), } } fn is_true(&self) -> bool { match self { Node::Expr(Expr::Lit(crate::reduced_ast::Lit::Bool(true))) => true, _ => false, } } } #[derive(Debug)] enum ValueEntry { Binding { constant: bool, val: /*FullyEvaluatedExpr*/ Node, //TODO make this use a subtype to represent fully evaluatedness } } type EvalResult = Result; impl Expr { fn to_node(self) -> Node { Node::Expr(self) } fn to_repl(&self) -> String { use self::Lit::*; use self::Func::*; match self { Expr::Lit(ref l) => match l { Nat(n) => format!("{}", n), Int(i) => format!("{}", i), Float(f) => format!("{}", f), Bool(b) => format!("{}", b), StringLit(s) => format!("\"{}\"", s), }, Expr::Func(f) => match f { BuiltIn(builtin) => format!("", builtin), UserDefined { name: None, .. } => format!(""), UserDefined { name: Some(name), .. } => format!("", name), }, Expr::Constructor { type_name, arity, .. } => { format!("", type_name, arity) }, Expr::Tuple(exprs) => paren_wrapped_vec(exprs.iter().map(|x| x.to_repl())), _ => format!("{:?}", self), } } fn replace_conditional_target_sigil(self, replacement: &Expr) -> Expr { use self::Expr::*; match self { ConditionalTargetSigilValue => replacement.clone(), Unit | Lit(_) | Func(_) | Sym(_) | Constructor { .. } | CaseMatch { .. } | UnimplementedSigilValue | ReductionError(_) => self, 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"), } } } impl<'a> State<'a> { pub fn evaluate(&mut self, ast: ReducedAST, repl: bool) -> Vec> { let mut acc = vec![]; // handle prebindings for statement in ast.0.iter() { self.prebinding(statement); } for statement in ast.0 { match self.statement(statement) { Ok(Some(ref output)) if repl => { acc.push(Ok(output.to_repl())) }, Ok(_) => (), Err(error) => { acc.push(Err(format!("Runtime error: {}", error))); return acc; }, } } acc } fn prebinding(&mut self, stmt: &Stmt) { match stmt { Stmt::PreBinding { name, func } => { let v_entry = ValueEntry::Binding { constant: true, val: Node::Expr(Expr::Func(func.clone())) }; self.values.insert(name.clone(), v_entry); }, Stmt::Expr(_expr) => { //TODO have this support things like nested function defs }, _ => () } } fn statement(&mut self, stmt: Stmt) -> EvalResult> { match stmt { Stmt::Binding { name, constant, expr } => { let val = self.expression(Node::Expr(expr))?; self.values.insert(name.clone(), ValueEntry::Binding { constant, val }); Ok(None) }, Stmt::Expr(expr) => Ok(Some(self.expression(expr.to_node())?)), Stmt::PreBinding {..} | Stmt::Noop => Ok(None), } } fn block(&mut self, stmts: Vec) -> EvalResult { let mut ret = None; for stmt in stmts { ret = self.statement(stmt)?; } Ok(ret.unwrap_or(Node::Expr(Expr::Unit))) } fn expression(&mut self, node: Node) -> EvalResult { use self::Expr::*; match node { t @ Node::PrimTuple { .. } => Ok(t), obj @ Node::PrimObject { .. } => Ok(obj), Node::Expr(expr) => match expr { literal @ Lit(_) => Ok(Node::Expr(literal)), Call { box f, args } => self.call_expression(f, args), Sym(name) => Ok(match self.values.lookup(&name) { Some(ValueEntry::Binding { val, .. }) => val.clone(), None => return Err(format!("Could not look up symbol {}", name)) }), Constructor { arity, ref name, tag, .. } if arity == 0 => Ok(Node::PrimObject { name: name.clone(), tag, items: vec![] }), constructor @ Constructor { .. } => Ok(Node::Expr(constructor)), func @ Func(_) => Ok(Node::Expr(func)), Tuple(exprs) => { let nodes = exprs.into_iter().map(|expr| self.expression(Node::Expr(expr))).collect::,_>>()?; Ok(Node::PrimTuple { items: nodes }) }, Conditional { box cond, then_clause, else_clause } => self.conditional(cond, then_clause, else_clause), Assign { box val, box expr } => self.assign_expression(val, expr), Unit => Ok(Node::Expr(Unit)), CaseMatch { box cond, alternatives } => self.case_match_expression(cond, alternatives), ConditionalTargetSigilValue => Ok(Node::Expr(ConditionalTargetSigilValue)), UnimplementedSigilValue => Err(format!("Sigil value eval not implemented")), ReductionError(err) => Err(format!("Reduction error: {}", err)), } } } fn call_expression(&mut self, f: Expr, args: Vec) -> EvalResult { 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)), } } fn apply_data_constructor(&mut self, _type_name: Rc, name: Rc, tag: usize, arity: usize, args: Vec) -> EvalResult { if arity != args.len() { return Err(format!("Data constructor {} requires {} arg(s)", name, arity)); } let evaled_args = args.into_iter().map(|expr| self.expression(Node::Expr(expr))).collect::,_>>()?; //let evaled_args = vec![]; Ok(Node::PrimObject { name: name.clone(), items: evaled_args, tag }) } fn apply_function(&mut self, f: Func, args: Vec) -> EvalResult { match f { Func::BuiltIn(builtin) => Ok(self.apply_builtin(builtin, args)?), Func::UserDefined { params, body, name } => { if params.len() != args.len() { return Err(format!("calling a {}-argument function with {} args", params.len(), args.len())) } let mut func_state = State { values: self.values.new_scope(name.map(|n| format!("{}", n))), }; for (param, val) in params.into_iter().zip(args.into_iter()) { let val = func_state.expression(Node::Expr(val))?; func_state.values.insert(param, ValueEntry::Binding { constant: true, val }); } // TODO figure out function return semantics func_state.block(body) } } } fn apply_builtin(&mut self, builtin: Builtin, args: Vec) -> EvalResult { use self::Expr::*; use self::Lit::*; use Builtin::*; let evaled_args: Result, String> = args.into_iter().map(|arg| self.expression(arg.to_node())) .collect(); let evaled_args = evaled_args?; Ok(match (builtin, evaled_args.as_slice()) { (FieldAccess, &[Node::PrimObject { .. }]) => { //TODO implement field access unimplemented!() }, (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(), /* builtin functions */ (IOPrint, &[ref anything]) => { print!("{}", anything.to_repl()); Expr::Unit.to_node() }, (IOPrintLn, &[ref anything]) => { println!("{}", anything.to_repl()); Expr::Unit.to_node() }, (IOGetLine, &[]) => { let mut buf = String::new(); io::stdin().read_line(&mut buf).expect("Error readling line in 'getline'"); Lit(StringLit(Rc::new(buf.trim().to_string()))).to_node() }, (x, args) => return Err(format!("bad or unimplemented builtin {:?} | {:?}", x, args)), }) } fn conditional(&mut self, cond: Expr, then_clause: Vec, else_clause: Vec) -> EvalResult { let cond = self.expression(Node::Expr(cond))?; Ok(match cond { Node::Expr(Expr::Lit(Lit::Bool(true))) => self.block(then_clause)?, Node::Expr(Expr::Lit(Lit::Bool(false))) => self.block(else_clause)?, _ => return Err(format!("Conditional with non-boolean condition")) }) } fn assign_expression(&mut self, val: Expr, expr: Expr) -> EvalResult { let name = match val { Expr::Sym(name) => name, _ => 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)) } fn guard_passes(&mut self, guard: &Option, cond: &Node) -> EvalResult { 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) } } fn case_match_expression(&mut self, cond: Expr, alternatives: Vec) -> EvalResult { //TODO need to handle recursive subpatterns let all_subpatterns_pass = |state: &mut State, subpatterns: &Vec>, items: &Vec| -> EvalResult { 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())); } 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) } } } Ok(true) }; 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 if !self.guard_passes(&alt.matchable.guard, &cond)? { continue; } match cond { Node::PrimObject { ref tag, ref items, .. } => { 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)? { return inner_state.block(alt.item); } else { continue; } } }, Node::PrimTuple { ref items } => { let mut inner_state = self.new_frame(items, &alt.matchable.bound_vars); if all_subpatterns_pass(&mut inner_state, &alt.matchable.subpatterns, items)? { return inner_state.block(alt.item); } else { continue; } }, Node::Expr(ref _e) => { if let None = alt.matchable.tag { return self.block(alt.item) } } } } Err(format!("{:?} failed pattern match", cond)) } }