about summary refs log tree commit diff
path: root/users/tazjin/rlox
diff options
context:
space:
mode:
Diffstat (limited to 'users/tazjin/rlox')
-rw-r--r--users/tazjin/rlox/src/interpreter.rs14
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),