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·35+0300
committertazjin <mail@tazj.in>2021-01-06T23·02+0000
commit0a0335ae6cd957f48086a41832cef794998f7271 (patch)
tree54e59e6c3b88989f7d913aa7aaf8e4cb3a750a69 /users/tazjin/rlox/src/interpreter.rs
parent93122a212e1f27cb0a941b11e342458fef97ed25 (diff)
feat(tazjin/rlox): Parse & interpret logical operators r/2061
Change-Id: I1a7d0eda61f7f077b820dc0d2c2516e204966962
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2324
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.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs
index cfef5548ec..738542b6b8 100644
--- a/users/tazjin/rlox/src/interpreter.rs
+++ b/users/tazjin/rlox/src/interpreter.rs
@@ -180,6 +180,7 @@ impl Interpreter {
             Expr::Unary(unary) => self.eval_unary(unary),
             Expr::Binary(binary) => self.eval_binary(binary),
             Expr::Variable(var) => self.get_var(var),
+            Expr::Logical(log) => self.eval_logical(log),
         }
     }
 
@@ -249,6 +250,20 @@ impl Interpreter {
         self.assign_var(&assign.name, value.clone())?;
         Ok(value)
     }
+
+    fn eval_logical<'a>(&mut self, logical: &parser::Logical<'a>) -> Result<Literal, Error> {
+        let left = eval_truthy(&self.eval(&logical.left)?);
+        let right = eval_truthy(&self.eval(&logical.right)?);
+
+        match &logical.operator.kind {
+            TokenKind::And => Ok(Literal::Boolean(left && right)),
+            TokenKind::Or => Ok(Literal::Boolean(left || right)),
+            kind => Err(Error {
+                line: logical.operator.line,
+                kind: ErrorKind::InternalError(format!("Invalid logical operator: {:?}", kind)),
+            }),
+        }
+    }
 }
 
 // Interpreter functions not dependent on interpreter-state.