diff options
author | Vincent Ambo <mail@tazj.in> | 2020-12-31T14·59+0300 |
---|---|---|
committer | tazjin <mail@tazj.in> | 2020-12-31T15·33+0000 |
commit | 3c979acdf367ea18ec2248bbe8e0befa15966bb4 (patch) | |
tree | 2878c92786c4462164b24065c200354944ff6468 /users/tazjin/rlox/src/interpreter.rs | |
parent | 26ed836e1d12ce7a3205bdfbe20e4e5a59f5062b (diff) |
refactor(tazjin/rlox): Unify parser::Statement & parser::Declaration r/2036
Change-Id: I6f21b246eb9d3bf760edb3220ce6be5de5b05b08 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2302 Reviewed-by: tazjin <mail@tazj.in> Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin/rlox/src/interpreter.rs')
-rw-r--r-- | users/tazjin/rlox/src/interpreter.rs | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs index c0af7d047652..edaa5e28f234 100644 --- a/users/tazjin/rlox/src/interpreter.rs +++ b/users/tazjin/rlox/src/interpreter.rs @@ -1,5 +1,5 @@ use crate::errors::{Error, ErrorKind}; -use crate::parser::{self, Declaration, Expr, Literal, Program, Statement}; +use crate::parser::{self, Expr, Literal, Program, Statement}; use crate::scanner::{self, TokenKind}; use std::collections::HashMap; use std::rc::Rc; @@ -78,6 +78,14 @@ pub struct Interpreter { } impl Interpreter { + pub fn interpret<'a>(&mut self, program: &Program<'a>) -> Result<(), Error> { + for stmt in program { + self.interpret_stmt(stmt)?; + } + + Ok(()) + } + fn interpret_stmt<'a>(&mut self, stmt: &Statement<'a>) -> Result<(), Error> { match stmt { Statement::Expr(expr) => { @@ -87,6 +95,7 @@ impl Interpreter { let result = self.eval(expr)?; println!("{:?}", result) } + Statement::Var(var) => return self.interpret_var(var), } Ok(()) @@ -102,17 +111,6 @@ impl Interpreter { return Ok(()); } - pub fn interpret<'a>(&mut self, program: &Program<'a>) -> Result<(), Error> { - for decl in program { - match decl { - Declaration::Stmt(stmt) => self.interpret_stmt(stmt)?, - Declaration::Var(var) => self.interpret_var(var)?, - } - } - - Ok(()) - } - fn eval<'a>(&mut self, expr: &Expr<'a>) -> Result<Literal, Error> { match expr { Expr::Assign(assign) => self.eval_assign(assign), |