diff options
author | Vincent Ambo <mail@tazj.in> | 2021-01-03T16·01+0300 |
---|---|---|
committer | tazjin <mail@tazj.in> | 2021-01-06T17·14+0000 |
commit | 93122a212e1f27cb0a941b11e342458fef97ed25 (patch) | |
tree | f7e0161cbd0bb3c7536ae5cffd0ec161b17460d8 | |
parent | a89777b34f52ee5af51d93c66e1d9f92e830ca6a (diff) |
feat(tazjin/rlox): Interpret if statements r/2060
Change-Id: Ic6aed29bec42098eb07e1ba9eb01dbcaae8d11e3 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2322 Tested-by: BuildkiteCI Reviewed-by: tazjin <mail@tazj.in>
-rw-r--r-- | users/tazjin/rlox/src/interpreter.rs | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs index 3645e884ddf9..cfef5548ecdf 100644 --- a/users/tazjin/rlox/src/interpreter.rs +++ b/users/tazjin/rlox/src/interpreter.rs @@ -127,7 +127,7 @@ impl Interpreter { } Statement::Var(var) => return self.interpret_var(var), Statement::Block(block) => return self.interpret_block(block), - Statement::If(_) => unimplemented!(), + Statement::If(if_stmt) => return self.interpret_if(if_stmt), } Ok(()) @@ -160,6 +160,18 @@ impl Interpreter { return result; } + fn interpret_if<'a>(&mut self, if_stmt: &parser::If<'a>) -> Result<(), Error> { + let condition = self.eval(&if_stmt.condition)?; + + if eval_truthy(&condition) { + self.interpret_stmt(&if_stmt.then_branch) + } else if let Some(else_branch) = &if_stmt.else_branch { + self.interpret_stmt(else_branch) + } else { + Ok(()) + } + } + fn eval<'a>(&mut self, expr: &Expr<'a>) -> Result<Literal, Error> { match expr { Expr::Assign(assign) => self.eval_assign(assign), |