From 93122a212e1f27cb0a941b11e342458fef97ed25 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 3 Jan 2021 19:01:55 +0300 Subject: feat(tazjin/rlox): Interpret if statements Change-Id: Ic6aed29bec42098eb07e1ba9eb01dbcaae8d11e3 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2322 Tested-by: BuildkiteCI Reviewed-by: tazjin --- users/tazjin/rlox/src/interpreter.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'users/tazjin') 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 { match expr { Expr::Assign(assign) => self.eval_assign(assign), -- cgit 1.4.1