Improve runtime performance by 7x

This commit is contained in:
Daniel M 2022-01-28 20:42:21 +01:00
parent 5bf989a640
commit 39b55b51da

View File

@ -27,11 +27,11 @@ impl Interpreter {
if print_ast {
println!("Ast:\n{:#?}", ast);
}
self.run(ast);
self.run(&ast);
}
pub fn run(&mut self, prog: Ast) {
for stmt in prog.prog {
pub fn run(&mut self, prog: &Ast) {
for stmt in &prog.prog {
match stmt {
Stmt::Expr(expr) => {
self.resolve_expr(expr);
@ -42,18 +42,18 @@ impl Interpreter {
}
Stmt::Let(name, rhs) => {
let result = self.resolve_expr(rhs);
self.vartable.insert(name, result);
self.vartable.insert(name.clone(), result);
}
Stmt::While(condition, body) => {
loop {
// Check condition
match self.resolve_expr(condition.clone()) {
match self.resolve_expr(condition) {
Value::I64(val) if val == 0 => break,
Value::I64(_) => (),
}
// Execute loop body
self.run(body.clone());
self.run(body);
}
}
Stmt::If(condition, body_if, body_else) => {
@ -67,13 +67,13 @@ impl Interpreter {
}
}
fn resolve_expr(&mut self, expr: Expr) -> Value {
fn resolve_expr(&mut self, expr: &Expr) -> Value {
match expr {
Expr::I64(val) => Value::I64(val),
Expr::BinOp(bo, lhs, rhs) => self.resolve_binop(bo, *lhs, *rhs),
Expr::UnOp(uo, val) => self.resolve_unop(uo, *val),
Expr::I64(val) => Value::I64(*val),
Expr::BinOp(bo, lhs, rhs) => self.resolve_binop(bo, &lhs, &rhs),
Expr::UnOp(uo, val) => self.resolve_unop(uo, &val),
Expr::Ident(name) => {
match self.vartable.get(&name) {
match self.vartable.get(name) {
None => panic!("Runtime error: Use of undeclared variable '{}'", name),
Some(val) => val.clone()
}
@ -81,13 +81,13 @@ impl Interpreter {
}
}
fn resolve_binop(&mut self, bo: BinOpType, lhs: Expr, rhs: Expr) -> Value {
fn resolve_binop(&mut self, bo: &BinOpType, lhs: &Expr, rhs: &Expr) -> Value {
// Treat assignment separate from the other expressions
if matches!(bo, BinOpType::Assign) {
match lhs {
Expr::Ident(name) => {
let rhs = self.resolve_expr(rhs);
self.vartable.insert(name, rhs.clone());
self.vartable.get_mut(name).map(|var| *var = rhs.clone());
return rhs;
}
_ => panic!("Runtime error: Left hand side of assignment must be an identifier"),
@ -122,7 +122,7 @@ impl Interpreter {
}
}
fn resolve_unop(&mut self, uo: UnOpType, val: Expr) -> Value {
fn resolve_unop(&mut self, uo: &UnOpType, val: &Expr) -> Value {
let val = self.resolve_expr(val);
match val {
Value::I64(val) => match uo {
@ -156,7 +156,7 @@ mod test {
let expected = Value::I64(11);
let mut interpreter = Interpreter::new();
let actual = interpreter.resolve_expr(ast);
let actual = interpreter.resolve_expr(&ast);
assert_eq!(expected, actual);
}