use a box in executor

This commit is contained in:
2024-10-14 21:55:34 -04:00
parent e52d71a2c0
commit a1b76956dc

View File

@@ -75,11 +75,11 @@ impl<I: Iterator<Item = Result<ParseTree, ParseError>>> Executor<I> {
fn exec(
&mut self,
tree: ParseTree,
tree: Box<ParseTree>,
locals: &mut Cow<HashMap<String, Object>>) -> Result<Value, RuntimeError>
{
match tree {
ParseTree::Add(x, y) => match (self.exec(*x, locals)?, self.exec(*y, locals)?) {
match *tree {
ParseTree::Add(x, y) => match (self.exec(x, locals)?, self.exec(y, locals)?) {
(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::Int(x), Value::Float(y)) => Ok(Value::Float(x as f64 + y)),
@@ -87,14 +87,14 @@ impl<I: Iterator<Item = Result<ParseTree, ParseError>>> Executor<I> {
(Value::String(x), Value::String(y)) => Ok(Value::String(format!("{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, locals)?, self.exec(y, locals)?) {
(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::Int(x), Value::Float(y)) => Ok(Value::Float(x as f64 - y)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(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, locals)?, self.exec(y, locals)?) {
(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::Int(x), Value::Float(y)) => Ok(Value::Float(x as f64 * y)),
@@ -102,28 +102,28 @@ impl<I: Iterator<Item = Result<ParseTree, ParseError>>> Executor<I> {
(Value::String(x), Value::Int(y)) => Ok(Value::String(x.repeat(y as usize))),
(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, locals)?, self.exec(y, locals)?) {
(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::Int(x), Value::Float(y)) => Ok(Value::Float(x as f64 / y)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(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, locals)?, self.exec(y, locals)?) {
(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::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))),
(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, locals)?, self.exec(y, locals)?) {
(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::Int(x), Value::Float(y)) => Ok(Value::Float(x as f64 % y)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Float(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, locals)?, self.exec(y, locals)?) {
(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::Float(x), Value::Int(y)) => Ok(Value::Bool(x == y as f64)),
@@ -132,35 +132,35 @@ impl<I: Iterator<Item = Result<ParseTree, ParseError>>> Executor<I> {
(Value::String(x), Value::String(y)) => Ok(Value::Bool(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, locals)?, self.exec(y, locals)?) {
(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::Float(x), Value::Int(y)) => Ok(Value::Bool(x > y as f64)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(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, locals)?, self.exec(y, locals)?) {
(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::Float(x), Value::Int(y)) => Ok(Value::Bool(x >= y as f64)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(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, locals)?, self.exec(y, locals)?) {
(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::Float(x), Value::Int(y)) => Ok(Value::Bool(x < y as f64)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(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, locals)?, self.exec(y, locals)?) {
(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::Float(x), Value::Int(y)) => Ok(Value::Bool(x <= y as f64)),
(Value::Float(x), Value::Float(y)) => Ok(Value::Bool(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, locals)? {
Value::Bool(x) => Ok(Value::Bool(!x)),
x => Err(RuntimeError::NoOverloadForTypes("not".into(), vec![x]))
},
@@ -169,10 +169,10 @@ impl<I: Iterator<Item = Result<ParseTree, ParseError>>> Executor<I> {
Err(RuntimeError::ImmutableError(ident.clone()))
} else {
let locals = locals.to_mut();
let value = self.exec(*body, &mut Cow::Borrowed(&locals))?;
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))
self.exec(scope, &mut Cow::Borrowed(&locals))
}
},
ParseTree::LazyEqu(ident, body, scope) => {
@@ -182,7 +182,7 @@ impl<I: Iterator<Item = Result<ParseTree, ParseError>>> Executor<I> {
let locals = locals.to_mut();
locals.insert(ident.clone(), Object::Variable(Evaluation::Uncomputed(body)));
self.exec(*scope, &mut Cow::Borrowed(&locals))
self.exec(scope, &mut Cow::Borrowed(&locals))
}
},
ParseTree::FunctionDefinition(ident, args, r, body, scope) => {
@@ -198,36 +198,36 @@ impl<I: Iterator<Item = Result<ParseTree, ParseError>>> Executor<I> {
body: Some(body)
}));
self.exec(*scope, &mut Cow::Borrowed(&locals))
self.exec(scope, &mut Cow::Borrowed(&locals))
}
}
},
ParseTree::Compose(x, y) => {
self.exec(*x, locals)?;
self.exec(*y, locals)
self.exec(x, locals)?;
self.exec(y, locals)
},
ParseTree::Id(x) => self.exec(*x, locals),
ParseTree::If(cond, body) => if match self.exec(*cond, locals)? {
ParseTree::Id(x) => self.exec(x, locals),
ParseTree::If(cond, body) => if match self.exec(cond, locals)? {
Value::Float(f) => f != 0.0,
Value::Int(i) => i != 0,
Value::Bool(b) => b,
Value::String(s) => !s.is_empty(),
Value::Nil => false,
} {
self.exec(*body, locals)
self.exec(body, locals)
} else {
Ok(Value::Nil)
},
ParseTree::IfElse(cond, istrue, isfalse) => if match self.exec(*cond, locals)? {
ParseTree::IfElse(cond, istrue, isfalse) => if match self.exec(cond, locals)? {
Value::Float(f) => f != 0.0,
Value::Int(i) => i != 0,
Value::Bool(b) => b,
Value::String(s) => !s.is_empty(),
Value::Nil => false,
} {
self.exec(*istrue, locals)
self.exec(istrue, locals)
} else {
self.exec(*isfalse, locals)
self.exec(isfalse, locals)
},
ParseTree::FunctionCall(ident, args) => {
let obj = locals.get(&ident).or(self.globals.get(&ident)).cloned();
@@ -237,10 +237,10 @@ impl<I: Iterator<Item = Result<ParseTree, ParseError>>> Executor<I> {
let body = f.body.ok_or(RuntimeError::FunctionUndefined(ident.clone()))?;
for ((name, _), tree) in std::iter::zip(f.decl.args, args) {
locals.insert(name.clone(), Object::Variable(Evaluation::Computed(self.exec(tree, &mut Cow::Borrowed(locals))?)));
locals.insert(name.clone(), Object::Variable(Evaluation::Computed(self.exec(Box::new(tree), &mut Cow::Borrowed(locals))?)));
}
self.exec(*body, &mut Cow::Borrowed(&locals))
self.exec(body, &mut Cow::Borrowed(&locals))
} else {
Err(RuntimeError::FunctionUndeclared(ident.clone()))
}
@@ -254,7 +254,7 @@ impl<I: Iterator<Item = Result<ParseTree, ParseError>>> Executor<I> {
match eval {
Evaluation::Computed(v) => Ok(v),
Evaluation::Uncomputed(tree) => {
let v = self.exec(*tree, &mut Cow::Borrowed(&locals))?;
let v = self.exec(tree, &mut Cow::Borrowed(&locals))?;
locals.insert(ident, Object::Variable(Evaluation::Computed(v.clone())));
Ok(v)
@@ -265,7 +265,7 @@ impl<I: Iterator<Item = Result<ParseTree, ParseError>>> Executor<I> {
}
},
ParseTree::Constant(value) => Ok(value),
ParseTree::ToInt(x) => match self.exec(*x, locals)? {
ParseTree::ToInt(x) => match self.exec(x, locals)? {
Value::Int(x) => Ok(Value::Int(x)),
Value::Float(x) => Ok(Value::Int(x as i64)),
Value::Bool(x) => Ok(Value::Int(if x { 1 } else { 0 })),
@@ -275,7 +275,7 @@ impl<I: Iterator<Item = Result<ParseTree, ParseError>>> Executor<I> {
}
x => Err(RuntimeError::NoOverloadForTypes("int".into(), vec![x])),
},
ParseTree::ToFloat(x) => match self.exec(*x, locals)? {
ParseTree::ToFloat(x) => match self.exec(x, locals)? {
Value::Int(x) => Ok(Value::Float(x as f64)),
Value::Float(x) => Ok(Value::Float(x)),
Value::Bool(x) => Ok(Value::Float(if x { 1.0 } else { 0.0 })),
@@ -285,14 +285,14 @@ impl<I: Iterator<Item = Result<ParseTree, ParseError>>> Executor<I> {
}
x => Err(RuntimeError::NoOverloadForTypes("float".into(), vec![x])),
},
ParseTree::ToBool(x) => match self.exec(*x, locals)? {
ParseTree::ToBool(x) => match self.exec(x, locals)? {
Value::Int(x) => Ok(Value::Bool(x != 0)),
Value::Float(x) => Ok(Value::Bool(x != 0.0)),
Value::Bool(x) => Ok(Value::Bool(x)),
Value::String(x) => Ok(Value::Bool(!x.is_empty())),
x => Err(RuntimeError::NoOverloadForTypes("bool".into(), vec![x])),
},
ParseTree::ToString(x) => Ok(Value::String(format!("{}", self.exec(*x, locals)?))),
ParseTree::ToString(x) => Ok(Value::String(format!("{}", self.exec(x, locals)?))),
}
}
}
@@ -304,7 +304,7 @@ impl<I: Iterator<Item = Result<ParseTree, ParseError>>> Iterator for Executor<I>
let expr = self.exprs.next();
match expr {
Some(Ok(expr)) => Some(self.exec(expr, &mut Cow::Borrowed(&HashMap::new()))),
Some(Ok(expr)) => Some(self.exec(Box::new(expr), &mut Cow::Borrowed(&HashMap::new()))),
Some(Err(e)) => Some(Err(RuntimeError::ParseError(e))),
None => None,
}