Improve runtime performance by 7x
This commit is contained in:
parent
5bf989a640
commit
39b55b51da
@ -27,11 +27,11 @@ impl Interpreter {
|
|||||||
if print_ast {
|
if print_ast {
|
||||||
println!("Ast:\n{:#?}", ast);
|
println!("Ast:\n{:#?}", ast);
|
||||||
}
|
}
|
||||||
self.run(ast);
|
self.run(&ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(&mut self, prog: Ast) {
|
pub fn run(&mut self, prog: &Ast) {
|
||||||
for stmt in prog.prog {
|
for stmt in &prog.prog {
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::Expr(expr) => {
|
Stmt::Expr(expr) => {
|
||||||
self.resolve_expr(expr);
|
self.resolve_expr(expr);
|
||||||
@ -42,18 +42,18 @@ impl Interpreter {
|
|||||||
}
|
}
|
||||||
Stmt::Let(name, rhs) => {
|
Stmt::Let(name, rhs) => {
|
||||||
let result = self.resolve_expr(rhs);
|
let result = self.resolve_expr(rhs);
|
||||||
self.vartable.insert(name, result);
|
self.vartable.insert(name.clone(), result);
|
||||||
}
|
}
|
||||||
Stmt::While(condition, body) => {
|
Stmt::While(condition, body) => {
|
||||||
loop {
|
loop {
|
||||||
// Check condition
|
// Check condition
|
||||||
match self.resolve_expr(condition.clone()) {
|
match self.resolve_expr(condition) {
|
||||||
Value::I64(val) if val == 0 => break,
|
Value::I64(val) if val == 0 => break,
|
||||||
Value::I64(_) => (),
|
Value::I64(_) => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute loop body
|
// Execute loop body
|
||||||
self.run(body.clone());
|
self.run(body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Stmt::If(condition, body_if, body_else) => {
|
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 {
|
match expr {
|
||||||
Expr::I64(val) => Value::I64(val),
|
Expr::I64(val) => Value::I64(*val),
|
||||||
Expr::BinOp(bo, lhs, rhs) => self.resolve_binop(bo, *lhs, *rhs),
|
Expr::BinOp(bo, lhs, rhs) => self.resolve_binop(bo, &lhs, &rhs),
|
||||||
Expr::UnOp(uo, val) => self.resolve_unop(uo, *val),
|
Expr::UnOp(uo, val) => self.resolve_unop(uo, &val),
|
||||||
Expr::Ident(name) => {
|
Expr::Ident(name) => {
|
||||||
match self.vartable.get(&name) {
|
match self.vartable.get(name) {
|
||||||
None => panic!("Runtime error: Use of undeclared variable '{}'", name),
|
None => panic!("Runtime error: Use of undeclared variable '{}'", name),
|
||||||
Some(val) => val.clone()
|
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
|
// Treat assignment separate from the other expressions
|
||||||
if matches!(bo, BinOpType::Assign) {
|
if matches!(bo, BinOpType::Assign) {
|
||||||
match lhs {
|
match lhs {
|
||||||
Expr::Ident(name) => {
|
Expr::Ident(name) => {
|
||||||
let rhs = self.resolve_expr(rhs);
|
let rhs = self.resolve_expr(rhs);
|
||||||
self.vartable.insert(name, rhs.clone());
|
self.vartable.get_mut(name).map(|var| *var = rhs.clone());
|
||||||
return rhs;
|
return rhs;
|
||||||
}
|
}
|
||||||
_ => panic!("Runtime error: Left hand side of assignment must be an identifier"),
|
_ => 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);
|
let val = self.resolve_expr(val);
|
||||||
match val {
|
match val {
|
||||||
Value::I64(val) => match uo {
|
Value::I64(val) => match uo {
|
||||||
@ -156,7 +156,7 @@ mod test {
|
|||||||
let expected = Value::I64(11);
|
let expected = Value::I64(11);
|
||||||
|
|
||||||
let mut interpreter = Interpreter::new();
|
let mut interpreter = Interpreter::new();
|
||||||
let actual = interpreter.resolve_expr(ast);
|
let actual = interpreter.resolve_expr(&ast);
|
||||||
|
|
||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user