schala/schala-lang/language/src/tree_walk_eval/mod.rs

421 lines
15 KiB
Rust
Raw Normal View History

use crate::reduced_ir::{ReducedIR, Expression, Lookup, Callable, FunctionDefinition, Statement, Literal};
use crate::symbol_table::{DefId};
use crate::util::ScopeStack;
2021-10-24 06:36:16 -07:00
use crate::builtin::Builtin;
use crate::typechecking::TypeId;
use std::fmt::Write;
2021-10-24 06:54:48 -07:00
use std::rc::Rc;
use std::convert::From;
2021-10-24 21:54:08 -07:00
mod test;
2021-10-24 22:23:48 -07:00
type EvalResult<T> = Result<T, RuntimeError>;
#[derive(Debug)]
pub struct State<'a> {
2021-10-25 19:09:05 -07:00
environments: ScopeStack<'a, Memory, MemoryValue>,
2021-10-24 02:54:21 -07:00
}
//TODO - eh, I dunno, maybe it doesn't matter exactly how memory works in the tree-walking
//evaluator
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
enum Memory {
Index(u32)
}
2021-10-24 17:57:56 -07:00
// This is for function param lookups, and is a hack
impl From<u8> for Memory {
fn from(n: u8) -> Self {
Memory::Index(4_000_000 + (n as u32))
}
}
2021-10-24 02:54:21 -07:00
impl From<&DefId> for Memory {
fn from(id: &DefId) -> Self {
Self::Index(id.as_u32())
}
}
#[derive(Debug)]
struct RuntimeError {
msg: String
}
impl From<String> for RuntimeError {
fn from(msg: String) -> Self {
Self {
msg
}
}
}
2021-10-24 22:23:48 -07:00
impl From<&str> for RuntimeError {
fn from(msg: &str) -> Self {
Self {
msg: msg.to_string(),
}
}
}
impl RuntimeError {
2021-10-24 22:39:11 -07:00
#[allow(dead_code)]
fn get_msg(&self) -> String {
format!("Runtime error: {}", self.msg)
}
}
fn paren_wrapped(terms: impl Iterator<Item=String>) -> String {
let mut buf = String::new();
write!(buf, "(").unwrap();
for term in terms.map(Some).intersperse(None) {
match term {
Some(e) => write!(buf, "{}", e).unwrap(),
None => write!(buf, ", ").unwrap(),
};
}
write!(buf, ")").unwrap();
buf
}
2021-10-25 19:42:49 -07:00
/// Anything that can be stored in memory; that is, a function definition, or a fully-evaluated
/// program value.
#[derive(Debug)]
2021-10-25 19:09:05 -07:00
enum MemoryValue {
2021-10-24 02:54:21 -07:00
Function(FunctionDefinition),
2021-10-24 06:04:58 -07:00
Primitive(Primitive),
}
2021-10-25 19:09:05 -07:00
impl From<Primitive> for MemoryValue {
2021-10-24 06:04:58 -07:00
fn from(prim: Primitive) -> Self {
Self::Primitive(prim)
}
}
2021-10-25 19:09:05 -07:00
impl MemoryValue {
fn to_repl(&self) -> String {
match self {
2021-10-25 19:09:05 -07:00
MemoryValue::Primitive(ref prim) => prim.to_repl(),
MemoryValue::Function(..) => "<function>".to_string(),
}
}
}
2021-10-25 19:34:05 -07:00
#[derive(Debug)]
enum RuntimeValue {
Expression(Expression),
Evaluated(Primitive),
}
impl From<Expression> for RuntimeValue {
fn from(expr: Expression) -> Self {
Self::Expression(expr)
}
}
impl From<Primitive> for RuntimeValue {
fn from(prim: Primitive) -> Self {
Self::Evaluated(prim)
}
}
2021-10-24 06:04:58 -07:00
/// A fully-reduced value
#[derive(Debug, Clone)]
enum Primitive {
Tuple(Vec<Primitive>),
Literal(Literal),
Callable(Callable),
Object {
type_id: TypeId,
tag: u32,
items: Vec<Primitive>
2021-10-24 06:04:58 -07:00
},
}
2021-10-24 22:55:12 -07:00
impl Primitive {
2021-10-25 19:42:49 -07:00
fn to_repl(&self) -> String {
match self {
2021-10-25 19:57:06 -07:00
Primitive::Object { type_id, items, .. } if items.len() == 0 => format!("{}", type_id.local_name()),
Primitive::Object { type_id, items, .. } =>
format!("{}{}", type_id.local_name(), paren_wrapped(items.iter().map(|item| item.to_repl()))),
2021-10-25 19:42:49 -07:00
Primitive::Literal(lit) => match lit {
Literal::Nat(n) => format!("{}", n),
Literal::Int(i) => format!("{}", i),
Literal::Float(f) => format!("{}", f),
Literal::Bool(b) => format!("{}", b),
Literal::StringLit(s) => format!("\"{}\"", s),
}
Primitive::Tuple(terms) => paren_wrapped(terms.iter().map(|x| x.to_repl())),
Primitive::Callable(..) => "<some-callable>".to_string(),
}
}
2021-10-24 22:55:12 -07:00
fn unit() -> Self {
Primitive::Tuple(vec![])
}
}
2021-10-24 06:54:48 -07:00
impl From<Literal> for Primitive {
fn from(lit: Literal) -> Self {
Primitive::Literal(lit)
}
}
impl<'a> State<'a> {
pub fn new() -> Self {
Self {
environments: ScopeStack::new(Some("global".to_string()))
}
}
pub fn evaluate(&mut self, reduced: ReducedIR, repl: bool) -> Vec<Result<String, String>> {
let mut acc = vec![];
2021-10-24 02:54:21 -07:00
for (def_id, function) in reduced.functions.into_iter() {
let mem = (&def_id).into();
2021-10-25 19:09:05 -07:00
self.environments.insert(mem, MemoryValue::Function(function));
2021-10-24 02:54:21 -07:00
}
for statement in reduced.entrypoint.into_iter() {
match self.statement(statement) {
Ok(Some(output)) if repl => {
acc.push(Ok(output.to_repl()))
},
Ok(_) => (),
Err(error) => {
2021-10-24 22:23:48 -07:00
acc.push(Err(error.msg));
return acc;
}
}
}
acc
}
2021-10-24 17:57:56 -07:00
fn block(&mut self, statements: Vec<Statement>) -> EvalResult<Primitive> {
//TODO need to handle breaks, returns, etc.
let mut ret = None;
for stmt in statements.into_iter() {
2021-10-25 19:09:05 -07:00
if let Some(MemoryValue::Primitive(prim)) = self.statement(stmt)? {
2021-10-24 17:57:56 -07:00
ret = Some(prim);
}
}
Ok(if let Some(ret) = ret {
ret
} else {
self.expression(Expression::unit())?
})
}
2021-10-25 19:09:05 -07:00
fn statement(&mut self, stmt: Statement) -> EvalResult<Option<MemoryValue>> {
match stmt {
2021-10-24 22:39:11 -07:00
Statement::Binding { ref id, expr, constant: _ } => {
println!("eval() binding id: {}", id);
let evaluated = self.expression(expr)?;
2021-10-24 02:54:21 -07:00
self.environments.insert(id.into(), evaluated.into());
Ok(None)
},
Statement::Expression(expr) => {
let evaluated = self.expression(expr)?;
Ok(Some(evaluated.into()))
}
}
}
2021-10-24 06:04:58 -07:00
fn expression(&mut self, expression: Expression) -> EvalResult<Primitive> {
Ok(match expression {
2021-10-24 06:04:58 -07:00
Expression::Literal(lit) => Primitive::Literal(lit),
Expression::Tuple(items) => Primitive::Tuple(items.into_iter().map(|expr| self.expression(expr)).collect::<EvalResult<Vec<Primitive>>>()?),
2021-10-25 14:37:12 -07:00
Expression::Lookup(kind) => match kind {
Lookup::Function(ref id) => {
let mem = id.into();
match self.environments.lookup(&mem) {
// This just checks that the function exists in "memory" by ID, we don't
// actually retrieve it until `apply_function()`
2021-10-25 19:09:05 -07:00
Some(MemoryValue::Function(_)) => Primitive::Callable(Callable::UserDefined(id.clone())),
2021-10-25 14:37:12 -07:00
x => return Err(format!("Function not found for id: {} : {:?}", id, x).into()),
}
},
Lookup::Param(n) => {
let mem = n.into();
match self.environments.lookup(&mem) {
2021-10-25 19:09:05 -07:00
Some(MemoryValue::Primitive(prim)) => prim.clone(),
2021-10-25 14:37:12 -07:00
e => return Err(format!("Param lookup error, got {:?}", e).into()),
}
},
Lookup::LocalVar(ref id) | Lookup::GlobalVar(ref id) => {
let mem = id.into();
match self.environments.lookup(&mem) {
2021-10-25 19:09:05 -07:00
Some(MemoryValue::Primitive(expr)) => expr.clone(),
2021-10-25 14:37:12 -07:00
_ => return Err(format!("Nothing found for local/gloval variable lookup {}", id).into()),
}
},
2021-10-24 02:54:21 -07:00
},
Expression::Assign { ref lval, box rval } => {
let mem = lval.into();
2021-10-24 22:55:12 -07:00
let evaluated = self.expression(rval)?;
2021-10-25 19:09:05 -07:00
self.environments.insert(mem, MemoryValue::Primitive(evaluated));
2021-10-24 22:55:12 -07:00
Primitive::unit()
},
2021-10-24 02:54:21 -07:00
Expression::Call { box f, args } => self.call_expression(f, args)?,
2021-10-25 19:57:06 -07:00
Expression::Callable(Callable::DataConstructor { type_id, arity, tag }) if arity == 0 => Primitive::Object {
type_id, tag, items: vec![]
},
2021-10-24 06:54:48 -07:00
Expression::Callable(func) => Primitive::Callable(func),
2021-10-25 20:26:53 -07:00
Expression::Conditional { box cond, then_clause, else_clause } => {
let cond = self.expression(cond)?;
match cond {
Primitive::Literal(Literal::Bool(true)) => self.block(then_clause)?,
Primitive::Literal(Literal::Bool(false)) => self.block(else_clause)?,
v => return Err(format!("Non-boolean value {:?} in if-statement", v).into())
}
},
Expression::ReductionError(e) => return Err(e.into()),
})
}
2021-10-24 06:04:58 -07:00
fn call_expression(&mut self, f: Expression, args: Vec<Expression>) -> EvalResult<Primitive> {
2021-10-24 06:36:16 -07:00
let func = match self.expression(f)? {
Primitive::Callable(func) => func,
2021-10-24 22:23:48 -07:00
other => return Err(format!("Trying to call non-function value: {:?}", other).into()),
2021-10-24 06:36:16 -07:00
};
match func {
Callable::Builtin(builtin) => self.apply_builtin(builtin, args),
Callable::UserDefined(def_id) => {
2021-10-24 18:59:00 -07:00
let mem = (&def_id).into();
match self.environments.lookup(&mem) {
2021-10-25 19:09:05 -07:00
Some(MemoryValue::Function(FunctionDefinition { body })) => {
2021-10-24 18:59:00 -07:00
let body = body.clone(); //TODO ideally this clone would not happen
self.apply_function(body, args)
},
2021-10-24 22:23:48 -07:00
e => Err(format!("Error looking up function with id {}: {:?}", def_id, e).into())
2021-10-24 18:59:00 -07:00
}
},
Callable::Lambda { arity, body } => {
2021-10-24 18:59:00 -07:00
if arity as usize != args.len() {
2021-10-24 22:23:48 -07:00
return Err(format!("Lambda expression requries {} arguments, only {} provided", arity, args.len()).into());
2021-10-24 18:59:00 -07:00
}
let body = body.clone(); //TODO again ideally, no cloning here
self.apply_function(body, args)
}
Callable::DataConstructor { type_id, arity, tag } => {
if arity as usize != args.len() {
return Err(format!("Constructor expression requries {} arguments, only {} provided", arity, args.len()).into());
}
let mut evaluated_args: Vec<Primitive> = vec![];
for arg in args.into_iter() {
evaluated_args.push(self.expression(arg)?);
}
Ok(Primitive::Object {
type_id,
tag,
items: evaluated_args
})
}
2021-10-24 06:36:16 -07:00
}
}
fn apply_builtin(&mut self, builtin: Builtin, args: Vec<Expression>) -> EvalResult<Primitive> {
2021-10-24 06:54:48 -07:00
use Builtin::*;
use Literal::*;
2021-10-25 19:42:49 -07:00
use Primitive::Literal as Lit;
2021-10-24 06:54:48 -07:00
2021-10-25 19:42:49 -07:00
let evaled_args: EvalResult<Vec<Primitive>> =
args.into_iter().map(|arg| self.expression(arg)).collect();
2021-10-24 06:54:48 -07:00
let evaled_args = evaled_args?;
Ok(match (builtin, evaled_args.as_slice()) {
(FieldAccess, /*&[Node::PrimObject { .. }]*/ _) => {
2021-10-24 22:23:48 -07:00
return Err("Field access unimplemented".into());
2021-10-24 06:54:48 -07:00
}
2021-10-24 22:39:11 -07:00
/* builtin functions */
(IOPrint, &[ref anything]) => {
2021-10-25 19:42:49 -07:00
print!("{}", anything.to_repl());
2021-10-24 22:39:11 -07:00
Primitive::Tuple(vec![])
},
(IOPrintLn, &[ref anything]) => {
2021-10-25 19:42:49 -07:00
print!("{}", anything.to_repl());
2021-10-24 22:39:11 -07:00
Primitive::Tuple(vec![])
},
(IOGetLine, &[]) => {
let mut buf = String::new();
std::io::stdin().read_line(&mut buf).expect("Error readling line in 'getline'");
StringLit(Rc::new(buf.trim().to_string())).into()
},
/* Binops */
2021-10-24 06:54:48 -07:00
(binop, &[ref lhs, ref rhs]) => match (binop, lhs, rhs) {
(Add, Lit(Nat(l)), Lit(Nat(r))) => Nat(l + r).into(),
(Concatenate, Lit(StringLit(ref s1)), Lit(StringLit(ref s2))) => StringLit(Rc::new(format!("{}{}", s1, s2))).into(),
(Subtract, Lit(Nat(l)), Lit(Nat(r))) => Nat(l - r).into(),
(Multiply, Lit(Nat(l)), Lit(Nat(r))) => Nat(l * r).into(),
(Divide, Lit(Nat(l)), Lit(Nat(r))) => Float((*l as f64)/ (*r as f64)).into(),
(Quotient, Lit(Nat(l)), Lit(Nat(r))) => if *r == 0 {
2021-10-24 22:23:48 -07:00
return Err("Divide-by-zero error".into());
2021-10-24 06:54:48 -07:00
} else {
Nat(l / r).into()
},
2021-10-24 07:07:12 -07:00
(Modulo, Lit(Nat(l)), Lit(Nat(r))) => Nat(l % r).into(),
(Exponentiation, Lit(Nat(l)), Lit(Nat(r))) => Nat(l ^ r).into(),
(BitwiseAnd, Lit(Nat(l)), Lit(Nat(r))) => Nat(l & r).into(),
(BitwiseOr, Lit(Nat(l)), Lit(Nat(r))) => Nat(l | r).into(),
/* comparisons */
(Equality, Lit(Nat(l)), Lit(Nat(r))) => Bool(l == r).into(),
(Equality, Lit(Int(l)), Lit(Int(r))) => Bool(l == r).into(),
(Equality, Lit(Float(l)), Lit(Float(r))) => Bool(l == r).into(),
(Equality, Lit(Bool(l)), Lit(Bool(r))) => Bool(l == r).into(),
(Equality, Lit(StringLit(ref l)), Lit(StringLit(ref r))) => Bool(l == r).into(),
(LessThan, Lit(Nat(l)), Lit(Nat(r))) => Bool(l < r).into(),
(LessThan, Lit(Int(l)), Lit(Int(r))) => Bool(l < r).into(),
(LessThan, Lit(Float(l)), Lit(Float(r))) => Bool(l < r).into(),
(LessThanOrEqual, Lit(Nat(l)), Lit(Nat(r))) => Bool(l <= r).into(),
(LessThanOrEqual, Lit(Int(l)), Lit(Int(r))) => Bool(l <= r).into(),
(LessThanOrEqual, Lit(Float(l)), Lit(Float(r))) => Bool(l <= r).into(),
(GreaterThan, Lit(Nat(l)), Lit(Nat(r))) => Bool(l > r).into(),
(GreaterThan, Lit(Int(l)), Lit(Int(r))) => Bool(l > r).into(),
(GreaterThan, Lit(Float(l)), Lit(Float(r))) => Bool(l > r).into(),
(GreaterThanOrEqual, Lit(Nat(l)), Lit(Nat(r))) => Bool(l >= r).into(),
(GreaterThanOrEqual, Lit(Int(l)), Lit(Int(r))) => Bool(l >= r).into(),
(GreaterThanOrEqual, Lit(Float(l)), Lit(Float(r))) => Bool(l >= r).into(),
2021-10-24 22:23:48 -07:00
(binop, lhs, rhs) => return Err(format!("Invalid binop expression {:?} {:?} {:?}", lhs, binop, rhs).into()),
2021-10-24 06:54:48 -07:00
},
2021-10-24 07:07:12 -07:00
(prefix, &[ref arg]) => match (prefix, arg) {
(BooleanNot, Lit(Bool(true))) => Bool(false),
(BooleanNot, Lit(Bool(false))) => Bool(true),
(Negate, Lit(Nat(n))) => Int(-(*n as i64)),
(Negate, Lit(Int(n))) => Int(-(*n as i64)),
(Increment, Lit(Int(n))) => Int(*n),
(Increment, Lit(Nat(n))) => Nat(*n),
2021-10-24 22:23:48 -07:00
_ => return Err("No valid prefix op".into())
2021-10-24 07:07:12 -07:00
}.into(),
2021-10-24 22:23:48 -07:00
(x, args) => return Err(format!("bad or unimplemented builtin {:?} | {:?}", x, args).into()),
2021-10-24 06:54:48 -07:00
})
2021-10-24 06:36:16 -07:00
}
2021-10-24 18:59:00 -07:00
fn apply_function(&mut self, body: Vec<Statement>, args: Vec<Expression>) -> EvalResult<Primitive> {
let mut evaluated_args: Vec<Primitive> = vec![];
for arg in args.into_iter() {
evaluated_args.push(self.expression(arg)?);
}
2021-10-24 17:57:56 -07:00
2021-10-24 18:59:00 -07:00
let mut frame_state = State {
environments: self.environments.new_scope(None)
};
2021-10-24 17:57:56 -07:00
2021-10-24 18:59:00 -07:00
for (n, evaled) in evaluated_args.into_iter().enumerate() {
let n = n as u8;
let mem = n.into();
2021-10-25 19:09:05 -07:00
frame_state.environments.insert(mem, MemoryValue::Primitive(evaled));
2021-10-24 18:59:00 -07:00
}
2021-10-24 17:57:56 -07:00
2021-10-24 18:59:00 -07:00
frame_state.block(body)
}
}