about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/interpreter.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-01-06T17·49+0300
committertazjin <mail@tazj.in>2021-01-06T23·02+0000
commitd47e55ba4db53445a48cdf634e10e823b1c115da (patch)
treebc0c506c595ba5a25d234e7da6879df60a6edb4c /users/tazjin/rlox/src/interpreter.rs
parent0a0335ae6cd957f48086a41832cef794998f7271 (diff)
feat(tazjin/rlox): Parse & interpret while statements r/2062
Change-Id: Iee772274de95dfd6a6d4af973402859aeda17b1d
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2325
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.rs9
1 files changed, 9 insertions, 0 deletions
diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs
index 738542b6b85b..28eb23f9948c 100644
--- a/users/tazjin/rlox/src/interpreter.rs
+++ b/users/tazjin/rlox/src/interpreter.rs
@@ -128,6 +128,7 @@ impl Interpreter {
             Statement::Var(var) => return self.interpret_var(var),
             Statement::Block(block) => return self.interpret_block(block),
             Statement::If(if_stmt) => return self.interpret_if(if_stmt),
+            Statement::While(while_stmt) => return self.interpret_while(while_stmt),
         }
 
         Ok(())
@@ -172,6 +173,14 @@ impl Interpreter {
         }
     }
 
+    fn interpret_while<'a>(&mut self, stmt: &parser::While<'a>) -> Result<(), Error> {
+        while eval_truthy(&self.eval(&stmt.condition)?) {
+            self.interpret_stmt(&stmt.body)?;
+        }
+
+        Ok(())
+    }
+
     fn eval<'a>(&mut self, expr: &Expr<'a>) -> Result<Literal, Error> {
         match expr {
             Expr::Assign(assign) => self.eval_assign(assign),