diff options
author | Vincent Ambo <mail@tazj.in> | 2021-01-06T17·35+0300 |
---|---|---|
committer | tazjin <mail@tazj.in> | 2021-01-06T23·02+0000 |
commit | 0a0335ae6cd957f48086a41832cef794998f7271 (patch) | |
tree | 54e59e6c3b88989f7d913aa7aaf8e4cb3a750a69 /users/tazjin/rlox/src/interpreter.rs | |
parent | 93122a212e1f27cb0a941b11e342458fef97ed25 (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.rs | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs index cfef5548ecdf..738542b6b85b 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. |