function, object, parser, and executer rewrites

This commit is contained in:
2024-10-18 02:21:31 -04:00
parent 34569248d3
commit f2cfb03fa1
6 changed files with 401 additions and 454 deletions

View File

@@ -1,11 +1,10 @@
use super::{Value, Type, Object, Evaluation, Function}; use super::{Value, Type, Object};
use super::parser::{ParseTree, ParseError}; use super::parser::{ParseTree, ParseError};
use std::collections::HashMap; use std::collections::HashMap;
use std::borrow::Cow;
use std::fmt::Display; use std::fmt::Display;
use std::error::Error; use std::error::Error;
use std::io::{self, Read, Write}; use std::io;
#[derive(Debug)] #[derive(Debug)]
pub enum RuntimeError { pub enum RuntimeError {
@@ -47,52 +46,62 @@ impl Error for RuntimeError {}
/// Executes an input of ParseTrees /// Executes an input of ParseTrees
pub struct Executor<'a, I> pub struct Executor<'a, I>
where where
I: Iterator<Item = Result<ParseTree, ParseError>>, I: Iterator<Item = Result<ParseTree, ParseError>>
{ {
exprs: I, exprs: &'a mut I,
globals: HashMap<String, Object>, globals: HashMap<String, Object>,
stdout: Box<dyn Write + 'a>, locals: HashMap<String, Object>,
stdin: Box<dyn Read + 'a>,
} }
impl<'a, I> Executor<'a, I> impl<'a, I> Executor<'a, I>
where where
I: Iterator<Item = Result<ParseTree, ParseError>>, I: Iterator<Item = Result<ParseTree, ParseError>>,
{ {
pub fn new(exprs: I) -> Self { pub fn new(exprs: &'a mut I) -> Self {
Self { Self {
exprs, exprs,
globals: HashMap::new(), globals: HashMap::new(),
stdout: Box::new(io::stdout()), locals: HashMap::new(),
stdin: Box::new(io::stdin()),
} }
} }
pub fn stdout(self, writer: impl Write + 'a) -> Self { pub fn globals(mut self, globals: HashMap<String, Object>) -> Self {
Self { self.globals = globals;
exprs: self.exprs, self
globals: self.globals,
stdout: Box::new(writer),
stdin: self.stdin,
}
} }
pub fn stdin(self, reader: impl Read + 'a) -> Self { pub fn _add_global(mut self, k: String, v: Object) -> Self {
Self { self.globals.insert(k, v);
exprs: self.exprs, self
globals: self.globals,
stdout: self.stdout,
stdin: Box::new(reader),
}
} }
fn exec( pub fn locals(mut self, locals: HashMap<String, Object>) -> Self {
&mut self, self.locals = locals;
tree: Box<ParseTree>, self
locals: &mut Cow<Box<HashMap<String, Object>>>) -> Result<Value, RuntimeError> }
{
pub fn add_local(mut self, k: String, v: Object) -> Self {
self.locals.insert(k, v);
self
}
fn _get_object(&self, ident: &String) -> Result<&Object, RuntimeError> {
self.locals.get(ident).or(self.globals.get(ident))
.ok_or(RuntimeError::VariableUndefined(ident.clone()))
}
fn get_object_mut(&mut self, ident: &String) -> Result<&mut Object, RuntimeError> {
self.locals.get_mut(ident).or(self.globals.get_mut(ident))
.ok_or(RuntimeError::VariableUndefined(ident.clone()))
}
fn variable_exists(&self, ident: &String) -> bool {
self.locals.contains_key(ident) || self.globals.contains_key(ident)
}
pub fn exec(&mut self, tree: Box<ParseTree>) -> Result<Value, RuntimeError> {
match *tree { match *tree {
ParseTree::Add(x, y) => match (self.exec(x, locals)?, self.exec(y, locals)?) { ParseTree::Add(x, y) => match (self.exec(x)?, self.exec(y)?) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Int(x + y)), (Value::Int(x), Value::Int(y)) => Ok(Value::Int(x + y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Float(x + y as f64)), (Value::Float(x), Value::Int(y)) => Ok(Value::Float(x + y as f64)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Float(x as f64 + y)), (Value::Int(x), Value::Float(y)) => Ok(Value::Float(x as f64 + y)),
@@ -129,14 +138,14 @@ where
}, },
(x, y) => Err(RuntimeError::NoOverloadForTypes("+".into(), vec![x, y])) (x, y) => Err(RuntimeError::NoOverloadForTypes("+".into(), vec![x, y]))
}, },
ParseTree::Sub(x, y) => match (self.exec(x, locals)?, self.exec(y, locals)?) { ParseTree::Sub(x, y) => match (self.exec(x)?, self.exec(y)?) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Int(x - y)), (Value::Int(x), Value::Int(y)) => Ok(Value::Int(x - y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Float(x - y as f64)), (Value::Float(x), Value::Int(y)) => Ok(Value::Float(x - y as f64)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Float(x as f64 - y)), (Value::Int(x), Value::Float(y)) => Ok(Value::Float(x as f64 - y)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(x - y)), (Value::Float(x), Value::Float(y)) => Ok(Value::Float(x - y)),
(x, y) => Err(RuntimeError::NoOverloadForTypes("-".into(), vec![x, y])) (x, y) => Err(RuntimeError::NoOverloadForTypes("-".into(), vec![x, y]))
}, },
ParseTree::Mul(x, y) => match (self.exec(x, locals)?, self.exec(y, locals)?) { ParseTree::Mul(x, y) => match (self.exec(x)?, self.exec(y)?) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Int(x * y)), (Value::Int(x), Value::Int(y)) => Ok(Value::Int(x * y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Float(x * y as f64)), (Value::Float(x), Value::Int(y)) => Ok(Value::Float(x * y as f64)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Float(x as f64 * y)), (Value::Int(x), Value::Float(y)) => Ok(Value::Float(x as f64 * y)),
@@ -144,28 +153,28 @@ where
(Value::String(x), Value::Int(y)) => Ok(Value::String(x.repeat(y as usize))), (Value::String(x), Value::Int(y)) => Ok(Value::String(x.repeat(y as usize))),
(x, y) => Err(RuntimeError::NoOverloadForTypes("*".into(), vec![x, y])) (x, y) => Err(RuntimeError::NoOverloadForTypes("*".into(), vec![x, y]))
}, },
ParseTree::Div(x, y) => match (self.exec(x, locals)?, self.exec(y, locals)?) { ParseTree::Div(x, y) => match (self.exec(x)?, self.exec(y)?) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Int(x / y)), (Value::Int(x), Value::Int(y)) => Ok(Value::Int(x / y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Float(x / y as f64)), (Value::Float(x), Value::Int(y)) => Ok(Value::Float(x / y as f64)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Float(x as f64 / y)), (Value::Int(x), Value::Float(y)) => Ok(Value::Float(x as f64 / y)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(x / y)), (Value::Float(x), Value::Float(y)) => Ok(Value::Float(x / y)),
(x, y) => Err(RuntimeError::NoOverloadForTypes("*".into(), vec![x, y])) (x, y) => Err(RuntimeError::NoOverloadForTypes("*".into(), vec![x, y]))
}, },
ParseTree::Exp(x, y) => match (self.exec(x, locals)?, self.exec(y, locals)?) { ParseTree::Exp(x, y) => match (self.exec(x)?, self.exec(y)?) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Int(x.pow(y as u32))), (Value::Int(x), Value::Int(y)) => Ok(Value::Int(x.pow(y as u32))),
(Value::Int(x), Value::Float(y)) => Ok(Value::Float((x as f64).powf(y))), (Value::Int(x), Value::Float(y)) => Ok(Value::Float((x as f64).powf(y))),
(Value::Float(x), Value::Int(y)) => Ok(Value::Float(x.powf(y as f64))), (Value::Float(x), Value::Int(y)) => Ok(Value::Float(x.powf(y as f64))),
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(x.powf(y))), (Value::Float(x), Value::Float(y)) => Ok(Value::Float(x.powf(y))),
(x, y) => Err(RuntimeError::NoOverloadForTypes("**".into(), vec![x, y])), (x, y) => Err(RuntimeError::NoOverloadForTypes("**".into(), vec![x, y])),
}, },
ParseTree::Mod(x, y) => match (self.exec(x, locals)?, self.exec(y, locals)?) { ParseTree::Mod(x, y) => match (self.exec(x)?, self.exec(y)?) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Int(x % y)), (Value::Int(x), Value::Int(y)) => Ok(Value::Int(x % y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Float(x % y as f64)), (Value::Float(x), Value::Int(y)) => Ok(Value::Float(x % y as f64)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Float(x as f64 % y)), (Value::Int(x), Value::Float(y)) => Ok(Value::Float(x as f64 % y)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(x % y)), (Value::Float(x), Value::Float(y)) => Ok(Value::Float(x % y)),
(x, y) => Err(RuntimeError::NoOverloadForTypes("%".into(), vec![x, y])), (x, y) => Err(RuntimeError::NoOverloadForTypes("%".into(), vec![x, y])),
}, },
ParseTree::EqualTo(x, y) => match (self.exec(x, locals)?, self.exec(y, locals)?) { ParseTree::EqualTo(x, y) => match (self.exec(x)?, self.exec(y)?) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x == y)), (Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x == y)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool(x as f64 == y)), (Value::Int(x), Value::Float(y)) => Ok(Value::Bool(x as f64 == y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(x == y as f64)), (Value::Float(x), Value::Int(y)) => Ok(Value::Bool(x == y as f64)),
@@ -174,7 +183,7 @@ where
(Value::String(x), Value::String(y)) => Ok(Value::Bool(x == y)), (Value::String(x), Value::String(y)) => Ok(Value::Bool(x == y)),
(x, y) => Err(RuntimeError::NoOverloadForTypes("==".into(), vec![x, y])), (x, y) => Err(RuntimeError::NoOverloadForTypes("==".into(), vec![x, y])),
}, },
ParseTree::NotEqualTo(x, y) => match (self.exec(x, locals)?, self.exec(y, locals)?) { ParseTree::NotEqualTo(x, y) => match (self.exec(x)?, self.exec(y)?) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x != y)), (Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x != y)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool(x as f64 != y)), (Value::Int(x), Value::Float(y)) => Ok(Value::Bool(x as f64 != y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(x != y as f64)), (Value::Float(x), Value::Int(y)) => Ok(Value::Bool(x != y as f64)),
@@ -183,80 +192,83 @@ where
(Value::String(x), Value::String(y)) => Ok(Value::Bool(x != y)), (Value::String(x), Value::String(y)) => Ok(Value::Bool(x != y)),
(x, y) => Err(RuntimeError::NoOverloadForTypes("!=".into(), vec![x, y])), (x, y) => Err(RuntimeError::NoOverloadForTypes("!=".into(), vec![x, y])),
}, },
ParseTree::GreaterThan(x, y) => match (self.exec(x, locals)?, self.exec(y, locals)?) { ParseTree::GreaterThan(x, y) => match (self.exec(x)?, self.exec(y)?) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x > y)), (Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x > y)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool(x as f64 > y)), (Value::Int(x), Value::Float(y)) => Ok(Value::Bool(x as f64 > y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(x > y as f64)), (Value::Float(x), Value::Int(y)) => Ok(Value::Bool(x > y as f64)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x > y)), (Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x > y)),
(x, y) => Err(RuntimeError::NoOverloadForTypes(">".into(), vec![x, y])), (x, y) => Err(RuntimeError::NoOverloadForTypes(">".into(), vec![x, y])),
}, },
ParseTree::GreaterThanOrEqualTo(x, y) => match (self.exec(x, locals)?, self.exec(y, locals)?) { ParseTree::GreaterThanOrEqualTo(x, y) => match (self.exec(x)?, self.exec(y)?) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x >= y)), (Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x >= y)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool(x as f64 >= y)), (Value::Int(x), Value::Float(y)) => Ok(Value::Bool(x as f64 >= y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(x >= y as f64)), (Value::Float(x), Value::Int(y)) => Ok(Value::Bool(x >= y as f64)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x >= y)), (Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x >= y)),
(x, y) => Err(RuntimeError::NoOverloadForTypes(">=".into(), vec![x, y])), (x, y) => Err(RuntimeError::NoOverloadForTypes(">=".into(), vec![x, y])),
}, },
ParseTree::LessThan(x, y) => match (self.exec(x, locals)?, self.exec(y, locals)?) { ParseTree::LessThan(x, y) => match (self.exec(x)?, self.exec(y)?) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x < y)), (Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x < y)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool((x as f64) < y)), (Value::Int(x), Value::Float(y)) => Ok(Value::Bool((x as f64) < y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(x < y as f64)), (Value::Float(x), Value::Int(y)) => Ok(Value::Bool(x < y as f64)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x < y)), (Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x < y)),
(x, y) => Err(RuntimeError::NoOverloadForTypes("<".into(), vec![x, y])), (x, y) => Err(RuntimeError::NoOverloadForTypes("<".into(), vec![x, y])),
}, },
ParseTree::LessThanOrEqualTo(x, y) => match (self.exec(x, locals)?, self.exec(y, locals)?) { ParseTree::LessThanOrEqualTo(x, y) => match (self.exec(x)?, self.exec(y)?) {
(Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x <= y)), (Value::Int(x), Value::Int(y)) => Ok(Value::Bool(x <= y)),
(Value::Int(x), Value::Float(y)) => Ok(Value::Bool(x as f64 <= y)), (Value::Int(x), Value::Float(y)) => Ok(Value::Bool(x as f64 <= y)),
(Value::Float(x), Value::Int(y)) => Ok(Value::Bool(x <= y as f64)), (Value::Float(x), Value::Int(y)) => Ok(Value::Bool(x <= y as f64)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x <= y)), (Value::Float(x), Value::Float(y)) => Ok(Value::Bool(x <= y)),
(x, y) => Err(RuntimeError::NoOverloadForTypes("<=".into(), vec![x, y])), (x, y) => Err(RuntimeError::NoOverloadForTypes("<=".into(), vec![x, y])),
}, },
ParseTree::Not(x) => match self.exec(x, locals)? { ParseTree::Not(x) => match self.exec(x)? {
Value::Bool(x) => Ok(Value::Bool(!x)), Value::Bool(x) => Ok(Value::Bool(!x)),
x => Err(RuntimeError::NoOverloadForTypes("not".into(), vec![x])) x => Err(RuntimeError::NoOverloadForTypes("not".into(), vec![x]))
}, },
ParseTree::And(x, y) => match (self.exec(x, locals)?, self.exec(y, locals)?) { ParseTree::And(x, y) => match (self.exec(x)?, self.exec(y)?) {
(Value::Bool(x), Value::Bool(y)) => Ok(Value::Bool(x && y)), (Value::Bool(x), Value::Bool(y)) => Ok(Value::Bool(x && y)),
(x, y) => Err(RuntimeError::NoOverloadForTypes("&&".into(), vec![x, y])) (x, y) => Err(RuntimeError::NoOverloadForTypes("&&".into(), vec![x, y]))
}, },
ParseTree::Or(x, y) => match (self.exec(x, locals)?, self.exec(y, locals)?) { ParseTree::Or(x, y) => match (self.exec(x)?, self.exec(y)?) {
(Value::Bool(x), Value::Bool(y)) => Ok(Value::Bool(x || y)), (Value::Bool(x), Value::Bool(y)) => Ok(Value::Bool(x || y)),
(x, y) => Err(RuntimeError::NoOverloadForTypes("||".into(), vec![x, y])) (x, y) => Err(RuntimeError::NoOverloadForTypes("||".into(), vec![x, y]))
}, },
ParseTree::Equ(ident, body, scope) => { ParseTree::Equ(ident, body, scope) => {
if self.globals.contains_key(&ident) || locals.contains_key(&ident) { if self.variable_exists(&ident) {
Err(RuntimeError::ImmutableError(ident.clone())) Err(RuntimeError::ImmutableError(ident.clone()))
} else { } else {
let locals = locals.to_mut(); let value = self.exec(body)?;
let value = self.exec(body, &mut Cow::Borrowed(&locals))?;
locals.insert(ident.clone(), Object::Variable(Evaluation::Computed(value)));
self.exec(scope, &mut Cow::Borrowed(&locals)) Executor::new(self.exprs)
.globals(self.globals.clone())
.locals(self.locals.clone())
.add_local(ident, Object::value(value, self.globals.to_owned(), self.locals.to_owned()))
.exec(scope)
} }
}, },
ParseTree::LazyEqu(ident, body, scope) => { ParseTree::LazyEqu(ident, body, scope) => {
if self.globals.contains_key(&ident) || locals.contains_key(&ident) { if self.variable_exists(&ident) {
Err(RuntimeError::ImmutableError(ident.clone())) Err(RuntimeError::ImmutableError(ident.clone()))
} else { } else {
let locals = locals.to_mut(); Executor::new(self.exprs)
locals.insert(ident.clone(), Object::Variable(Evaluation::Uncomputed(body))); .globals(self.globals.clone())
.locals(self.locals.clone())
self.exec(scope, &mut Cow::Borrowed(&locals)) .add_local(ident, Object::variable(*body, self.globals.to_owned(), self.locals.to_owned()))
.exec(scope)
} }
}, },
ParseTree::FunctionDefinition(func, scope) => { ParseTree::FunctionDefinition(func, scope) => {
let locals = locals.to_mut(); Executor::new(self.exprs)
.globals(self.globals.clone())
locals.insert(func.name.clone().unwrap(), Object::Function(func)); .locals(self.locals.clone())
.add_local(func.name().unwrap().to_string(), Object::function(func, self.globals.clone(), self.locals.clone()))
self.exec(scope, &mut Cow::Borrowed(&locals)) .exec(scope)
}, },
ParseTree::Compose(x, y) => { ParseTree::Compose(x, y) => {
self.exec(x, locals)?; self.exec(x)?;
self.exec(y, locals) self.exec(y)
}, },
ParseTree::Id(x) => self.exec(x, locals), ParseTree::Id(x) => self.exec(x),
ParseTree::If(cond, body) => if match self.exec(cond, locals)? { ParseTree::If(cond, body) => if match self.exec(cond)? {
Value::Float(f) => f != 0.0, Value::Float(f) => f != 0.0,
Value::Int(i) => i != 0, Value::Int(i) => i != 0,
Value::Bool(b) => b, Value::Bool(b) => b,
@@ -265,85 +277,42 @@ where
Value::Nil => false, Value::Nil => false,
x => return Err(RuntimeError::NoOverloadForTypes("?".into(), vec![x])), x => return Err(RuntimeError::NoOverloadForTypes("?".into(), vec![x])),
} { } {
self.exec(body, locals) self.exec(body)
} else { } else {
Ok(Value::Nil) Ok(Value::Nil)
}, },
ParseTree::IfElse(cond, istrue, isfalse) => if match self.exec(cond, locals)? { ParseTree::IfElse(cond, istrue, isfalse) => if match self.exec(cond)? {
Value::Float(f) => f != 0.0, Value::Float(f) => f != 0.0,
Value::Int(i) => i != 0, Value::Int(i) => i != 0,
Value::Bool(b) => b, Value::Bool(b) => b,
Value::String(s) => !s.is_empty(), Value::String(s) => !s.is_empty(),
Value::Array(_, vec) => !vec.is_empty(), Value::Array(_, vec) => !vec.is_empty(),
Value::Nil => false, Value::Nil => false,
x => return Err(RuntimeError::NoOverloadForTypes("?".into(), vec![x])), x => return Err(RuntimeError::NoOverloadForTypes("??".into(), vec![x])),
} { } {
self.exec(istrue, locals) self.exec(istrue)
} else { } else {
self.exec(isfalse, locals) self.exec(isfalse)
}, },
ParseTree::FunctionCall(ident, args) => { ParseTree::FunctionCall(ident, args) => {
let obj = locals.get(&ident).or(self.globals.get(&ident)).cloned(); let args = args.into_iter().map(|x| Object::variable(x, self.globals.clone(), self.locals.clone())).collect();
let obj = self.get_object_mut(&ident)?;
let v = obj.eval()?;
match obj { match v {
Some(Object::Function(f)) => { Value::Function(mut f) => f.call(obj.globals(), obj.locals(), args),
assert!(f.arg_names.is_some());
let loc = std::iter::zip(std::iter::zip(f.t.1.clone(), f.arg_names.clone().unwrap()), args)
.map(|((t, name), tree)| {
let v = self.exec(Box::new(tree), locals)?;
if t != v.get_type() {
return Err(RuntimeError::TypeError(t, v.get_type()));
}
match v {
Value::Function(f) => Ok((Object::Function(f), name)),
v => Ok((Object::Variable(Evaluation::Computed(v)), name)),
}
}).collect::<Result<Vec<(Object, String)>, RuntimeError>>()?;
let mut locals = f.locals.clone();
for (obj, name) in loc.into_iter() {
locals.insert(name, obj);
}
// the parser previously placed a copy of this function with the same name and type
// into it's locals, however it doesn't have a body. This would cause a
// panic later when attempting to execute the function during recursive calls.
// we fix this by replacing it with a *complete* copy of the function.
// also only do this if the function has a name in the first place, otherwise it panics with lambdas.
if let Some(name) = f.name.clone() {
locals.insert(name, Object::Function(f.clone()));
}
self.exec(f.body.unwrap(), &mut Cow::Borrowed(&Box::new(locals)))
}
_ => Err(RuntimeError::FunctionUndefined(ident.clone())) _ => Err(RuntimeError::FunctionUndefined(ident.clone()))
} }
}, },
ParseTree::Variable(ident) => { ParseTree::Variable(ident) => {
let locals = locals.to_mut(); let obj = self.get_object_mut(&ident)?;
let obj = locals.get(&ident).or(self.globals.get(&ident)).cloned(); let v = obj.eval()?;
if let Some(Object::Variable(eval)) = obj { Ok(v)
match eval {
Evaluation::Computed(v) => Ok(v),
Evaluation::Uncomputed(tree) => {
let v = self.exec(tree, &mut Cow::Borrowed(&locals))?;
locals.insert(ident, Object::Variable(Evaluation::Computed(v.clone())));
Ok(v)
}
}
} else {
Err(RuntimeError::VariableUndefined(ident.clone()))
}
}, },
ParseTree::Constant(value) => Ok(value), ParseTree::Constant(value) => Ok(value),
ParseTree::IntCast(x) => match self.exec(x, locals)? { ParseTree::IntCast(x) => match self.exec(x)? {
Value::Int(x) => Ok(Value::Int(x)), Value::Int(x) => Ok(Value::Int(x)),
Value::Float(x) => Ok(Value::Int(x as i64)), Value::Float(x) => Ok(Value::Int(x as i64)),
Value::Bool(x) => Ok(Value::Int(if x { 1 } else { 0 })), Value::Bool(x) => Ok(Value::Int(if x { 1 } else { 0 })),
@@ -353,7 +322,7 @@ where
} }
x => Err(RuntimeError::NoOverloadForTypes("int".into(), vec![x])), x => Err(RuntimeError::NoOverloadForTypes("int".into(), vec![x])),
}, },
ParseTree::FloatCast(x) => match self.exec(x, locals)? { ParseTree::FloatCast(x) => match self.exec(x)? {
Value::Int(x) => Ok(Value::Float(x as f64)), Value::Int(x) => Ok(Value::Float(x as f64)),
Value::Float(x) => Ok(Value::Float(x)), Value::Float(x) => Ok(Value::Float(x)),
Value::Bool(x) => Ok(Value::Float(if x { 1.0 } else { 0.0 })), Value::Bool(x) => Ok(Value::Float(if x { 1.0 } else { 0.0 })),
@@ -363,7 +332,7 @@ where
} }
x => Err(RuntimeError::NoOverloadForTypes("float".into(), vec![x])), x => Err(RuntimeError::NoOverloadForTypes("float".into(), vec![x])),
}, },
ParseTree::BoolCast(x) => match self.exec(x, locals)? { ParseTree::BoolCast(x) => match self.exec(x)? {
Value::Int(x) => Ok(Value::Bool(x != 0)), Value::Int(x) => Ok(Value::Bool(x != 0)),
Value::Float(x) => Ok(Value::Bool(x != 0.0)), Value::Float(x) => Ok(Value::Bool(x != 0.0)),
Value::Bool(x) => Ok(Value::Bool(x)), Value::Bool(x) => Ok(Value::Bool(x)),
@@ -371,49 +340,38 @@ where
Value::Array(_, vec) => Ok(Value::Bool(!vec.is_empty())), Value::Array(_, vec) => Ok(Value::Bool(!vec.is_empty())),
x => Err(RuntimeError::NoOverloadForTypes("bool".into(), vec![x])), x => Err(RuntimeError::NoOverloadForTypes("bool".into(), vec![x])),
}, },
ParseTree::StringCast(x) => Ok(Value::String(format!("{}", self.exec(x, locals)?))), ParseTree::StringCast(x) => Ok(Value::String(format!("{}", self.exec(x)?))),
ParseTree::Print(x) => match self.exec(x, locals)? { ParseTree::Print(x) => match self.exec(x)? {
Value::String(s) => { Value::String(s) => {
writeln!(self.stdout, "{s}").map_err(|e| RuntimeError::IO(e))?; println!("{s}");
Ok(Value::Nil) Ok(Value::Nil)
} }
x => { x => {
writeln!(self.stdout, "{x}").map_err(|e| RuntimeError::IO(e))?; println!("{x}");
Ok(Value::Nil) Ok(Value::Nil)
} }
} }
ParseTree::LambdaDefinition(func) => Ok(Value::Function(func)), ParseTree::LambdaDefinition(func) => Ok(Value::Function(func)),
ParseTree::NonCall(name) => { ParseTree::NonCall(name) => {
let locals = locals.to_mut(); let obj = self.get_object_mut(&name)?;
let func = locals.get(&name).ok_or(RuntimeError::FunctionUndefined(name.clone())).cloned()?; let v = obj.eval()?;
match func { Ok(v)
Object::Function(func) => Ok(Value::Function(func.clone())),
Object::Variable(var) => match var {
Evaluation::Computed(value) => Ok(value.clone()),
Evaluation::Uncomputed(tree) => {
let v = self.exec(tree, &mut Cow::Borrowed(&locals))?;
locals.insert(name, Object::Variable(Evaluation::Computed(v.clone())));
Ok(v)
}
}
}
} }
ParseTree::Head(x) => match self.exec(x, locals)? { ParseTree::Head(x) => match self.exec(x)? {
Value::Array(_, x) => Ok(x.first().ok_or(RuntimeError::EmptyArray)?.clone()), Value::Array(_, x) => Ok(x.first().ok_or(RuntimeError::EmptyArray)?.clone()),
t => Err(RuntimeError::NoOverloadForTypes("head".into(), vec![t])) t => Err(RuntimeError::NoOverloadForTypes("head".into(), vec![t]))
}, },
ParseTree::Tail(x) => match self.exec(x, locals)? { ParseTree::Tail(x) => match self.exec(x)? {
Value::Array(t, x) => Ok(Value::Array(t, if x.len() > 0 { x[1..].to_vec() } else { vec![] })), Value::Array(t, x) => Ok(Value::Array(t, if x.len() > 0 { x[1..].to_vec() } else { vec![] })),
t => Err(RuntimeError::NoOverloadForTypes("tail".into(), vec![t])) t => Err(RuntimeError::NoOverloadForTypes("tail".into(), vec![t]))
}, },
ParseTree::Init(x) => match self.exec(x, locals)? { ParseTree::Init(x) => match self.exec(x)? {
Value::Array(t, x) => Ok(Value::Array(t, if x.len() > 0 { x[..x.len() - 1].to_vec() } else { vec![] })), Value::Array(t, x) => Ok(Value::Array(t, if x.len() > 0 { x[..x.len() - 1].to_vec() } else { vec![] })),
t => Err(RuntimeError::NoOverloadForTypes("init".into(), vec![t])) t => Err(RuntimeError::NoOverloadForTypes("init".into(), vec![t]))
}, },
ParseTree::Fini(x) => match self.exec(x, locals)? { ParseTree::Fini(x) => match self.exec(x)? {
Value::Array(_, x) => Ok(x.last().ok_or(RuntimeError::EmptyArray)?.clone()), Value::Array(_, x) => Ok(x.last().ok_or(RuntimeError::EmptyArray)?.clone()),
t => Err(RuntimeError::NoOverloadForTypes("fini".into(), vec![t])) t => Err(RuntimeError::NoOverloadForTypes("fini".into(), vec![t]))
}, },
@@ -428,7 +386,7 @@ impl<'a, I: Iterator<Item = Result<ParseTree, ParseError>>> Iterator for Executo
let expr = self.exprs.next(); let expr = self.exprs.next();
match expr { match expr {
Some(Ok(expr)) => Some(self.exec(Box::new(expr), &mut Cow::Borrowed(&Box::new(HashMap::new())))), Some(Ok(expr)) => Some(self.exec(Box::new(expr))),
Some(Err(e)) => Some(Err(RuntimeError::ParseError(e))), Some(Err(e)) => Some(Err(RuntimeError::ParseError(e))),
None => None, None => None,
} }

79
src/function.rs Normal file
View File

@@ -0,0 +1,79 @@
use crate::parser::ParseTree;
use crate::executor::{Executor, RuntimeError};
use crate::{Type, Object, Value};
use std::collections::HashMap;
use std::fmt::{self, Display};
#[derive(Clone, Debug, PartialEq)]
pub struct FunctionType(pub Box<Type>, pub Vec<Type>);
impl Display for FunctionType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Function({}, {})", self.0, self.1.iter().map(|x| format!("{x}")).collect::<Vec<_>>().join(", "))
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Function {
name: Option<String>,
t: FunctionType,
arg_names: Vec<String>,
body: Box<ParseTree>,
}
impl Function {
pub(crate) fn lambda(t: FunctionType, arg_names: Vec<String>, body: Box<ParseTree>) -> Self {
Self {
name: None,
t,
arg_names,
body
}
}
pub(crate) fn named(name: &str, t: FunctionType, arg_names: Vec<String>, body: Box<ParseTree>) -> Self {
Self {
name: Some(name.to_string()),
t,
arg_names,
body
}
}
pub fn name(&self) -> Option<&str> {
self.name.as_ref().map(|x| x.as_str())
}
pub fn get_type(&self) -> FunctionType {
self.t.clone()
}
pub(crate) fn call(&mut self,
globals: HashMap<String, Object>,
locals: HashMap<String, Object>,
args: Vec<Object>) -> Result<Value, RuntimeError>
{
let mut tree = vec![Ok(*self.body.clone())].into_iter();
let mut exec = Executor::new(&mut tree)
.locals(locals.clone())
.globals(globals.clone());
for (obj, name) in std::iter::zip(args.into_iter(), self.arg_names.clone().into_iter()) {
exec = exec.add_local(name.clone(), obj);
}
if let Some(name) = self.name().map(|x| x.to_string()) {
exec = exec.add_local(name, Object::function(self.clone(), globals, locals));
}
exec.next().unwrap()
}
}
impl Display for Function {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.t)
}
}

View File

@@ -1,15 +1,19 @@
mod tokenizer; mod tokenizer;
mod parser; mod parser;
mod executor; mod executor;
mod function;
use executor::{Executor, RuntimeError}; use executor::{Executor, RuntimeError};
use parser::{ParseTree, Parser}; use parser::{ParseTree, Parser};
use tokenizer::Tokenizer; use tokenizer::Tokenizer;
use function::{FunctionType, Function};
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::Display; use std::fmt::Display;
use std::io::{Write, Read, BufRead}; use std::io::BufRead;
use std::fmt; use std::fmt;
use std::iter::Peekable;
use std::marker::PhantomData;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Type { pub enum Type {
@@ -71,7 +75,7 @@ impl Value {
Self::String(_) => Type::String, Self::String(_) => Type::String,
Self::Array(t, _) => Type::Array(Box::new(t.clone())), Self::Array(t, _) => Type::Array(Box::new(t.clone())),
Self::Nil => Type::Nil, Self::Nil => Type::Nil,
Self::Function(f) => Type::Function(f.t.clone()), Self::Function(f) => Type::Function(f.get_type()),
} }
} }
} }
@@ -84,98 +88,96 @@ impl Display for Value {
Self::Bool(x) => write!(f, "{}", if *x { "true" } else { "false" }), Self::Bool(x) => write!(f, "{}", if *x { "true" } else { "false" }),
Self::String(x) => write!(f, "\"{x}\""), Self::String(x) => write!(f, "\"{x}\""),
Self::Array(_t, v) => write!(f, "[{}]", v.iter().map(|x| format!("{x}")).collect::<Vec<_>>().join(" ")), Self::Array(_t, v) => write!(f, "[{}]", v.iter().map(|x| format!("{x}")).collect::<Vec<_>>().join(" ")),
Self::Function(func) => { Self::Function(func) => write!(f, "{func}"),
if let Some(name) = &func.name {
write!(f, "Function({}, {}, {})", name, func.t.0, func.t.1.iter().map(|x| format!("{x}")).collect::<Vec<_>>().join(", "))
} else {
write!(f, "{}", func.t)
}
}
Self::Nil => write!(f, "nil"), Self::Nil => write!(f, "nil"),
} }
} }
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct FunctionType(Box<Type>, Vec<Type>); enum Cache {
Cached(Value),
impl Display for FunctionType { Uncached(ParseTree),
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Function({}, {})", self.0, self.1.iter().map(|x| format!("{x}")).collect::<Vec<_>>().join(", "))
}
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
enum Evaluation { struct Object {
// at this point, it's type is set in stone
Computed(Value),
// at this point, it's type is unknown, and may contradict a variable's type
// or not match the expected value of the expression, this is a runtime error
Uncomputed(Box<ParseTree>),
}
#[derive(Clone, Debug, PartialEq)]
enum Object {
Variable(Evaluation),
Function(Function),
}
#[derive(Clone, Debug, PartialEq)]
pub struct Function {
name: Option<String>,
t: FunctionType,
locals: HashMap<String, Object>, locals: HashMap<String, Object>,
arg_names: Option<Vec<String>>, globals: HashMap<String, Object>,
body: Option<Box<ParseTree>>, value: Cache,
} }
impl Function { impl Object {
fn lambda(t: FunctionType, arg_names: Vec<String>, locals: HashMap<String, Object>, body: Option<Box<ParseTree>>) -> Self { pub fn variable(tree: ParseTree, globals: HashMap<String, Object>, locals: HashMap<String, Object>) -> Self {
Self { Self {
name: None,
t,
locals, locals,
arg_names: Some(arg_names), globals,
body value: Cache::Uncached(tree),
} }
} }
fn named(name: &str, t: FunctionType, arg_names: Option<Vec<String>>, locals: HashMap<String, Object>, body: Option<Box<ParseTree>>) -> Self { pub fn value(v: Value, globals: HashMap<String, Object>, locals: HashMap<String, Object>) -> Self {
Self { Self {
name: Some(name.to_string()),
t,
locals, locals,
arg_names, globals,
body value: Cache::Cached(v),
} }
} }
pub fn function(func: Function, globals: HashMap<String, Object>, locals: HashMap<String, Object>) -> Self {
Self {
locals,
globals,
value: Cache::Cached(Value::Function(func)),
}
}
/// evaluate the tree inside of an object if it isn't evaluated yet, returns the value
pub fn eval(&mut self) -> Result<Value, RuntimeError> {
match self.value.clone() {
Cache::Cached(v) => Ok(v),
Cache::Uncached(tree) => {
let mut tree = vec![Ok(tree)].into_iter();
let mut exec = Executor::new(&mut tree)
.locals(self.locals.clone())
.globals(self.globals.clone());
let v = exec.next().unwrap()?;
self.value = Cache::Cached(v.clone());
Ok(v)
}
}
}
pub fn locals(&self) -> HashMap<String, Object> {
self.locals.clone()
}
pub fn globals(&self) -> HashMap<String, Object> {
self.globals.clone()
}
} }
pub struct Runtime<'a, R: BufRead> { pub struct Runtime<'a, R: BufRead> {
inner: executor::Executor<'a, parser::Parser<tokenizer::Tokenizer<R>>> tokenizer: Peekable<Tokenizer<R>>,
parser: Option<Parser<'a, Tokenizer<R>>>,
phantom: PhantomData<Executor<'a, Parser<'a, Tokenizer<R>>>>,
} }
impl<'a, R: BufRead + 'a> Runtime<'a, R> { impl<'a, R: BufRead> Runtime<'a, R> {
pub fn new(reader: R) -> Self { pub fn new(reader: R) -> Self {
Self { Self {
inner: Executor::new(Parser::new(Tokenizer::new(reader))) tokenizer: Tokenizer::new(reader).peekable(),
parser: None,
phantom: PhantomData,
} }
} }
pub fn stdout(self, stdout: impl Write + 'a) -> Self { pub fn values(&'a mut self) -> impl Iterator<Item = Result<Value, RuntimeError>> + 'a {
Self { self.parser = Some(Parser::new(&mut self.tokenizer));
inner: self.inner.stdout(stdout) Executor::new(self.parser.as_mut().unwrap())
}
}
pub fn stdin(self, stdin: impl Read + 'a) -> Self {
Self {
inner: self.inner.stdin(stdin)
}
}
pub fn values(self) -> impl Iterator<Item = Result<Value, RuntimeError>> + 'a {
self.inner
} }
} }

View File

@@ -1,7 +1,7 @@
use std::io::{self, BufReader}; use std::io::{self, BufReader};
fn main() { fn main() {
let runtime = lamm::Runtime::new(BufReader::new(io::stdin())); let mut runtime = lamm::Runtime::new(BufReader::new(io::stdin()));
for value in runtime.values() { for value in runtime.values() {
match value { match value {

View File

@@ -1,12 +1,10 @@
use crate::Evaluation;
use super::{Value, Type, Object, Function, FunctionType}; use super::{Value, Type, Function, FunctionType};
use super::tokenizer::{Token, TokenizeError, Op}; use super::tokenizer::{Token, TokenizeError, Op};
use std::error; use std::error;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::Display; use std::fmt::Display;
use std::borrow::Cow;
use std::iter::Peekable; use std::iter::Peekable;
#[derive(Debug)] #[derive(Debug)]
@@ -93,149 +91,135 @@ pub(crate) enum ParseTree {
Print(Box<ParseTree>), Print(Box<ParseTree>),
} }
macro_rules! one_arg { /// Parses input tokens and produces ParseTrees for an Executor
($op:ident, $tokens:ident, $globals:ident, $locals:ident) => { pub(crate) struct Parser<'a, I: Iterator<Item = Result<Token, TokenizeError>>> {
Ok(ParseTree::$op( tokens: &'a mut Peekable<I>,
Box::new(ParseTree::parse($tokens, $globals, $locals)?) globals: HashMap<String, Type>,
))} locals: HashMap<String, Type>,
} }
macro_rules! two_arg { impl<'a, I: Iterator<Item = Result<Token, TokenizeError>>> Parser<'a, I> {
($op:ident, $tokens:ident, $globals:ident, $locals:ident) => { pub fn new(tokens: &'a mut Peekable<I>) -> Self {
Ok(ParseTree::$op( Self {
Box::new(ParseTree::parse($tokens, $globals, $locals)?), tokens: tokens,
Box::new(ParseTree::parse($tokens, $globals, $locals)?) globals: HashMap::new(),
))} locals: HashMap::new()
} }
}
macro_rules! three_arg { pub fn globals(mut self, globals: HashMap<String, Type>) -> Self {
($op:ident, $tokens:ident, $globals:ident, $locals:ident) => { self.globals = globals;
Ok(ParseTree::$op( self
Box::new(ParseTree::parse($tokens, $globals, $locals)?), }
Box::new(ParseTree::parse($tokens, $globals, $locals)?),
Box::new(ParseTree::parse($tokens, $globals, $locals)?)
))}
}
impl ParseTree { pub fn _add_global(mut self, k: String, v: Type) -> Self {
fn parse<I>( self.globals.insert(k, v);
tokens: &mut Peekable<I>, self
globals: &HashMap<String, Object>, }
locals: &mut Cow<HashMap<String, Object>>) -> Result<Self, ParseError>
where pub fn locals(mut self, locals: HashMap<String, Type>) -> Self {
I: Iterator<Item = Result<Token, TokenizeError>>, self.locals = locals;
{ self
match tokens.next() { }
pub fn add_local(mut self, k: String, v: Type) -> Self {
self.locals.insert(k, v);
self
}
fn get_object_type(&self, ident: &String) -> Result<&Type, ParseError> {
self.locals.get(ident).or(self.globals.get(ident))
.ok_or(ParseError::IdentifierUndefined(ident.clone()))
}
fn parse(&mut self) -> Result<ParseTree, ParseError> {
match self.tokens.next() {
Some(Ok(token)) => { Some(Ok(token)) => {
match token { match token {
Token::Constant(c) => Ok(Self::Constant(c)), Token::Constant(c) => Ok(ParseTree::Constant(c)),
Token::Identifier(ident) => { Token::Identifier(ident) => {
if let Some(obj) = locals.clone().get(&ident).or(globals.clone().get(&ident)) { match self.get_object_type(&ident)? {
match obj { Type::Function(f) => {
Object::Function(f) => { let args = f.1.clone().iter()
let args = f.t.1.iter() .map(|_| self.parse()).collect::<Result<Vec<_>, ParseError>>()?;
.map(|_| ParseTree::parse(tokens, globals, locals)).collect::<Result<Vec<_>, ParseError>>()?;
Ok(ParseTree::FunctionCall(ident, args))
Ok(ParseTree::FunctionCall(ident, args))
}
Object::Variable(e) => Ok(ParseTree::Variable(ident)),
} }
} else { _ => Ok(ParseTree::Variable(ident)),
Err(ParseError::IdentifierUndefined(ident))
} }
} }
Token::Operator(op) => { Token::Operator(op) => {
match op { match op {
Op::Add => two_arg!(Add, tokens, globals, locals), Op::Add => Ok(ParseTree::Add(Box::new(self.parse()?), Box::new(self.parse()?))),
Op::Sub => two_arg!(Sub, tokens, globals, locals), Op::Sub => Ok(ParseTree::Sub(Box::new(self.parse()?), Box::new(self.parse()?))),
Op::Mul => two_arg!(Mul, tokens, globals, locals), Op::Mul => Ok(ParseTree::Mul(Box::new(self.parse()?), Box::new(self.parse()?))),
Op::Div => two_arg!(Div, tokens, globals, locals), Op::Div => Ok(ParseTree::Div(Box::new(self.parse()?), Box::new(self.parse()?))),
Op::Exp => two_arg!(Exp, tokens, globals, locals), Op::Exp => Ok(ParseTree::Exp(Box::new(self.parse()?), Box::new(self.parse()?))),
Op::Mod => two_arg!(Mod, tokens, globals, locals), Op::Mod => Ok(ParseTree::Mod(Box::new(self.parse()?), Box::new(self.parse()?))),
Op::Equ | Op::LazyEqu => { Op::Equ | Op::LazyEqu => {
let token = tokens.next() let token = self.tokens.next()
.ok_or(ParseError::UnexpectedEndInput)? .ok_or(ParseError::UnexpectedEndInput)?
.map_err(|e| ParseError::TokenizeError(e))?; .map_err(|e| ParseError::TokenizeError(e))?;
let body = Box::new(ParseTree::parse(tokens, globals, locals)?); let body = Box::new(self.parse()?);
if let Token::Identifier(ident) = token { if let Token::Identifier(ident) = token {
let locals = locals.to_mut();
locals.insert(ident.clone(), Object::Variable(Evaluation::Computed(Value::Nil)));
match op { match op {
Op::Equ => Ok(ParseTree::Equ(ident, Op::Equ => Ok(ParseTree::Equ(ident.clone(),
body, body.clone(),
Box::new(ParseTree::parse(tokens, globals, &mut Cow::Borrowed(&locals))?) Box::new(Parser::new(self.tokens.by_ref())
)), .locals(self.locals.clone())
Op::LazyEqu => Ok(ParseTree::LazyEqu(ident, .globals(self.globals.clone())
body, .add_local(ident, Type::Any)
Box::new(ParseTree::parse(tokens, globals, &mut Cow::Borrowed(&locals))?) .parse()?))
)), ),
_ => panic!("Operator literally changed under your nose"), Op::LazyEqu => Ok(ParseTree::LazyEqu(ident.clone(),
body.clone(),
Box::new(Parser::new(self.tokens.by_ref())
.locals(self.locals.clone())
.globals(self.globals.clone())
.add_local(ident, Type::Any)
.parse()?))
),
_ => unreachable!(),
} }
} else { } else {
Err(ParseError::InvalidIdentifier(token)) Err(ParseError::InvalidIdentifier(token))
} }
} }
Op::FunctionDefine(arg_count) => { Op::FunctionDefine(arg_count) => {
let f = { let f = self.parse_function(arg_count)?;
let mut f = ParseTree::parse_function(tokens, arg_count)?;
if locals.contains_key(&f.name.clone().unwrap()) { Ok(ParseTree::FunctionDefinition(f.clone(),
return Err(ParseError::ImmutableError(f.name.unwrap())); Box::new(
} Parser::new(self.tokens)
.globals(self.globals.clone())
f.locals = locals.to_mut().clone(); .locals(self.locals.clone())
.add_local(f.name().unwrap().to_string(), Type::Function(f.get_type()))
// recursion requires that f's prototype is present in locals .parse()?
f.locals.insert(f.name.clone().unwrap(), Object::Function(f.clone())); )))
// we also need any function parameters in local scope
for (name, t) in std::iter::zip(f.arg_names.clone().unwrap(), f.t.1.clone()) {
match t {
Type::Function(t) => {
f.locals.insert(name.clone(), Object::Function(Function::named(&name, t, None, HashMap::new(), None)));
}
_ => {
// the value isn't important, just that the identifier is there
f.locals.insert(name.clone(), Object::Variable(Evaluation::Computed(Value::Nil)));
}
}
}
f.body = Some(Box::new(ParseTree::parse(tokens, globals, &mut Cow::Borrowed(&f.locals))?));
f
};
let locals = locals.to_mut();
locals.insert(f.name.clone().unwrap(), Object::Function(f.clone()));
Ok(ParseTree::FunctionDefinition(f, Box::new(ParseTree::parse(tokens, globals, &mut Cow::Borrowed(&locals))?)))
}, },
Op::Compose => two_arg!(Compose, tokens, globals, locals), Op::Compose => Ok(ParseTree::Compose(Box::new(self.parse()?), Box::new(self.parse()?))),
Op::Id => one_arg!(Id, tokens, globals, locals), Op::Id => Ok(ParseTree::Id(Box::new(self.parse()?))),
Op::If => two_arg!(If, tokens, globals, locals), Op::IfElse => Ok(ParseTree::IfElse(Box::new(self.parse()?), Box::new(self.parse()?), Box::new(self.parse()?))),
Op::IfElse => three_arg!(IfElse, tokens, globals, locals), Op::If => Ok(ParseTree::If(Box::new(self.parse()?), Box::new(self.parse()?))),
Op::EqualTo => two_arg!(EqualTo, tokens, globals, locals), Op::EqualTo => Ok(ParseTree::EqualTo(Box::new(self.parse()?), Box::new(self.parse()?))),
Op::GreaterThan => two_arg!(GreaterThan, tokens, globals, locals), Op::GreaterThan => Ok(ParseTree::GreaterThan(Box::new(self.parse()?), Box::new(self.parse()?))),
Op::LessThan => two_arg!(LessThan, tokens, globals, locals), Op::LessThan => Ok(ParseTree::LessThan(Box::new(self.parse()?), Box::new(self.parse()?))),
Op::GreaterThanOrEqualTo => two_arg!(GreaterThanOrEqualTo, tokens, globals, locals), Op::GreaterThanOrEqualTo => Ok(ParseTree::GreaterThanOrEqualTo(Box::new(self.parse()?), Box::new(self.parse()?))),
Op::LessThanOrEqualTo => two_arg!(LessThanOrEqualTo, tokens, globals, locals), Op::LessThanOrEqualTo => Ok(ParseTree::LessThanOrEqualTo(Box::new(self.parse()?), Box::new(self.parse()?))),
Op::Not => one_arg!(Not, tokens, globals, locals), Op::Not => Ok(ParseTree::Not(Box::new(self.parse()?))),
Op::IntCast => one_arg!(IntCast, tokens, globals, locals), Op::IntCast => Ok(ParseTree::IntCast(Box::new(self.parse()?))),
Op::FloatCast => one_arg!(FloatCast, tokens, globals, locals), Op::FloatCast => Ok(ParseTree::FloatCast(Box::new(self.parse()?))),
Op::BoolCast => one_arg!(BoolCast, tokens, globals, locals), Op::BoolCast => Ok(ParseTree::BoolCast(Box::new(self.parse()?))),
Op::StringCast => one_arg!(StringCast, tokens, globals, locals), Op::StringCast => Ok(ParseTree::StringCast(Box::new(self.parse()?))),
Op::Print => one_arg!(Print, tokens, globals, locals), Op::Print => Ok(ParseTree::Print(Box::new(self.parse()?))),
Op::OpenArray => { Op::OpenArray => {
let mut depth = 1; let mut depth = 1;
// take tokens until we reach the end of this array // take tokens until we reach the end of this array
// if we don't collect them here it causes rust to overflow computing the types // if we don't collect them here it causes rust to overflow computing the types
let array_tokens = tokens.by_ref().take_while(|t| match t { let array_tokens = self.tokens.by_ref().take_while(|t| match t {
Ok(Token::Operator(Op::OpenArray)) => { Ok(Token::Operator(Op::OpenArray)) => {
depth += 1; depth += 1;
true true
@@ -247,11 +231,16 @@ impl ParseTree {
_ => true, _ => true,
}).collect::<Result<Vec<_>, TokenizeError>>().map_err(|e| ParseError::TokenizeError(e))?; }).collect::<Result<Vec<_>, TokenizeError>>().map_err(|e| ParseError::TokenizeError(e))?;
let array_tokens: Vec<Result<Token, TokenizeError>> = array_tokens.into_iter().map(|t| Ok(t)).collect(); let mut array_tokens = array_tokens
.into_iter()
.map(|t| Ok(t))
.collect::<Vec<Result<Token, TokenizeError>>>()
.into_iter()
.peekable();
let trees: Vec<ParseTree> = Parser::new(array_tokens.into_iter()) let trees: Vec<ParseTree> = Parser::new(&mut array_tokens)
.globals(globals.clone()) .globals(self.globals.to_owned())
.locals(locals.to_mut().to_owned()) .locals(self.locals.to_owned())
.collect::<Result<_, ParseError>>()?; .collect::<Result<_, ParseError>>()?;
let tree = trees.into_iter().fold( let tree = trees.into_iter().fold(
@@ -263,44 +252,21 @@ impl ParseTree {
} }
Op::Empty => Ok(ParseTree::Constant(Value::Array(Type::Any, vec![]))), Op::Empty => Ok(ParseTree::Constant(Value::Array(Type::Any, vec![]))),
Op::CloseArray => Err(ParseError::UnmatchedArrayClose), Op::CloseArray => Err(ParseError::UnmatchedArrayClose),
Op::NotEqualTo => two_arg!(NotEqualTo, tokens, globals, locals), Op::NotEqualTo => Ok(ParseTree::NotEqualTo(Box::new(self.parse()?), Box::new(self.parse()?))),
Op::And => two_arg!(And, tokens, globals, locals), Op::And => Ok(ParseTree::And(Box::new(self.parse()?), Box::new(self.parse()?))),
Op::Or => two_arg!(Or, tokens, globals, locals), Op::Or => Ok(ParseTree::Or(Box::new(self.parse()?), Box::new(self.parse()?))),
Op::LambdaDefine(arg_count) => { Op::LambdaDefine(arg_count) => {
let f = { let f = self.parse_lambda(arg_count)?;
let mut f = ParseTree::parse_lambda(tokens, arg_count)?;
let locals = locals.to_mut();
f.locals = locals.clone();
// we need any function parameters in local scope
for (name, t) in std::iter::zip(f.arg_names.clone().unwrap(), f.t.1.clone()) {
match t {
Type::Function(t) => {
f.locals.insert(name.clone(), Object::Function(Function::named(&name, t, None, HashMap::new(), None)));
}
_ => {
// the value isn't important, just that the identifier is there
f.locals.insert(name.clone(), Object::Variable(Evaluation::Computed(Value::Nil)));
}
}
}
f.body = Some(Box::new(ParseTree::parse(tokens, globals, &mut Cow::Borrowed(&f.locals))?));
f
};
Ok(ParseTree::LambdaDefinition(f)) Ok(ParseTree::LambdaDefinition(f))
} }
Op::NonCall => { Op::NonCall => {
let name = Self::get_identifier(tokens.next())?; let name = Self::get_identifier(self.tokens.next())?;
Ok(ParseTree::NonCall(name)) Ok(ParseTree::NonCall(name))
}, },
Op::Head => one_arg!(Head, tokens, globals, locals), Op::Head => Ok(ParseTree::Head(Box::new(self.parse()?))),
Op::Tail => one_arg!(Tail, tokens, globals, locals), Op::Tail => Ok(ParseTree::Tail(Box::new(self.parse()?))),
Op::Init => one_arg!(Init, tokens, globals, locals), Op::Init => Ok(ParseTree::Init(Box::new(self.parse()?))),
Op::Fini => one_arg!(Fini, tokens, globals, locals), Op::Fini => Ok(ParseTree::Fini(Box::new(self.parse()?))),
op => Err(ParseError::UnwantedToken(Token::Operator(op))), op => Err(ParseError::UnwantedToken(Token::Operator(op))),
} }
} }
@@ -312,28 +278,40 @@ impl ParseTree {
} }
} }
fn parse_lambda<I>(tokens: &mut Peekable<I>, arg_count: usize) -> Result<Function, ParseError> fn parse_lambda(&mut self, arg_count: usize) -> Result<Function, ParseError> {
where let (t, args) = Self::parse_function_declaration(self.tokens, arg_count)?;
I: Iterator<Item = Result<Token, TokenizeError>>,
{ let mut locals = self.locals.clone();
let (t, args) = Self::parse_function_declaration(tokens, arg_count)?;
Ok(Function::lambda(t, args, HashMap::new(), None)) for (name, t) in std::iter::zip(args.iter(), t.1.iter()) {
locals.insert(name.clone(), t.clone());
}
Ok(Function::lambda(t, args, Box::new(
Parser::new(self.tokens)
.globals(self.globals.clone())
.locals(locals).parse()?)))
} }
fn parse_function<I>(tokens: &mut Peekable<I>, arg_count: usize) -> Result<Function, ParseError> fn parse_function(&mut self, arg_count: usize) -> Result<Function, ParseError> {
where let name = Self::get_identifier(self.tokens.next())?;
I: Iterator<Item = Result<Token, TokenizeError>>, let (t, args) = Self::parse_function_declaration(self.tokens, arg_count)?;
{
let name = Self::get_identifier(tokens.next())?;
let (t, args) = Self::parse_function_declaration(tokens, arg_count)?;
Ok(Function::named(&name, t, Some(args), HashMap::new(), None)) let mut locals = self.locals.clone();
for (name, t) in std::iter::zip(args.iter(), t.1.iter()) {
locals.insert(name.clone(), t.clone());
}
locals.insert(name.clone(), Type::Function(t.clone()));
Ok(Function::named(&name, t, args, Box::new(
Parser::new(self.tokens)
.globals(self.globals.clone())
.locals(locals).parse()?)))
} }
fn parse_function_declaration<I>(tokens: &mut Peekable<I>, arg_count: usize) -> Result<(FunctionType, Vec<String>), ParseError> fn parse_function_declaration(tokens: &mut Peekable<I>, arg_count: usize) -> Result<(FunctionType, Vec<String>), ParseError> {
where
I: Iterator<Item = Result<Token, TokenizeError>>
{
let args: Vec<(Type, String)> = (0..arg_count) let args: Vec<(Type, String)> = (0..arg_count)
.map(|_| Self::parse_function_declaration_parameter(tokens)) .map(|_| Self::parse_function_declaration_parameter(tokens))
.collect::<Result<_, _>>()?; .collect::<Result<_, _>>()?;
@@ -342,22 +320,14 @@ impl ParseTree {
let (types, names): (Vec<_>, Vec<_>) = args.into_iter().unzip(); let (types, names): (Vec<_>, Vec<_>) = args.into_iter().unzip();
let mut ret = Type::Any; let mut ret = Type::Any;
if let Some(t) = tokens.next_if(|x| matches!(x, Ok(Token::Operator(Op::Arrow)))) if tokens.next_if(|x| matches!(x, Ok(Token::Operator(Op::Arrow)))).is_some() {
{
if let Err(e) = t {
return Err(ParseError::TokenizeError(e));
}
ret = Self::parse_type(tokens)?; ret = Self::parse_type(tokens)?;
} }
Ok((FunctionType(Box::new(ret), types), names)) Ok((FunctionType(Box::new(ret), types), names))
} }
fn parse_function_declaration_parameter<I>(mut tokens: &mut Peekable<I>) -> Result<(Type, String), ParseError> fn parse_function_declaration_parameter(mut tokens: &mut Peekable<I>) -> Result<(Type, String), ParseError> {
where
I: Iterator<Item = Result<Token, TokenizeError>>
{
match tokens.next() { match tokens.next() {
// untyped variable // untyped variable
Some(Ok(Token::Identifier(x))) => Ok((Type::Any, x)), Some(Ok(Token::Identifier(x))) => Ok((Type::Any, x)),
@@ -406,10 +376,7 @@ impl ParseTree {
} }
} }
fn parse_type<I>(tokens: &mut I) -> Result<Type, ParseError> fn parse_type(tokens: &mut Peekable<I>) -> Result<Type, ParseError> {
where
I: Iterator<Item = Result<Token, TokenizeError>>,
{
match tokens.next() { match tokens.next() {
Some(Ok(Token::Type(t))) => Ok(t), Some(Ok(Token::Type(t))) => Ok(t),
Some(Ok(Token::Operator(Op::FunctionDefine(n)))) => { Some(Ok(Token::Operator(Op::FunctionDefine(n)))) => {
@@ -447,48 +414,11 @@ impl ParseTree {
} }
} }
/// Parses input tokens and produces ParseTrees for an Executor impl<'a, I: Iterator<Item = Result<Token, TokenizeError>>> Iterator for Parser<'a, I> {
pub(crate) struct Parser<I: Iterator<Item = Result<Token, TokenizeError>>> {
tokens: I,
// These are used to keep track of functions in the current context
// by the parser. otherwise the parser would have no way to tell
// if the program `* a b 12` is supposed to be ((* a b) (12)) or (* (a b) 12)
globals: HashMap<String, Object>,
locals: HashMap<String, Object>,
}
impl<I: Iterator<Item = Result<Token, TokenizeError>>> Parser<I> {
pub fn new(tokens: I) -> Self {
Self {
tokens,
globals: HashMap::new(),
locals: HashMap::new()
}
}
pub fn globals(self, globals: HashMap<String, Object>) -> Self {
Self {
tokens: self.tokens,
globals,
locals: self.locals,
}
}
pub fn locals(self, locals: HashMap<String, Object>) -> Self {
Self {
tokens: self.tokens,
globals: self.globals,
locals,
}
}
}
impl<I: Iterator<Item = Result<Token, TokenizeError>>> Iterator for Parser<I> {
type Item = Result<ParseTree, ParseError>; type Item = Result<ParseTree, ParseError>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let tree = ParseTree::parse(&mut self.tokens.by_ref().peekable(), &self.globals, &mut Cow::Borrowed(&self.locals)); let tree = self.parse();
match tree { match tree {
Ok(tree) => Some(Ok(tree)), Ok(tree) => Some(Ok(tree)),

View File

@@ -42,7 +42,7 @@ impl Display for TokenizeError {
impl error::Error for TokenizeError {} impl error::Error for TokenizeError {}
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub(crate) enum Op { pub enum Op {
Add, Add,
Sub, Sub,
Mul, Mul,
@@ -85,7 +85,7 @@ pub(crate) enum Op {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) enum Token { pub enum Token {
Identifier(String), Identifier(String),
Operator(Op), Operator(Op),
Constant(Value), Constant(Value),
@@ -408,26 +408,4 @@ impl<R: BufRead> std::iter::Iterator for Tokenizer<R> {
Err(e) => Some(Err(TokenizeError::IO(e))), Err(e) => Some(Err(TokenizeError::IO(e))),
} }
} }
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use crate::parser::{Parser, ParseTree, ParseError};
use super::*;
#[test]
fn uwu() {
let program = ":. map ?: f Any -> Any ?. x [Any] -> [Any] ?? bool x + f head x map 'f tail x empty map ;x ** x 2 [1 2 3 4 5]";
let tokens: Vec<Token> = Tokenizer::from_str(program).unwrap().collect::<Result<_, TokenizeError>>().unwrap();
println!("{tokens:?}");
let trees: Result<Vec<ParseTree>, ParseError> = Parser::new(tokens.into_iter().map(|x| Ok(x))).collect();
println!("{trees:?}");
}
} }