From a6718366550d1fb8c6dd9a90a3b5545a1e6c9f0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sun, 1 Jan 2023 18:27:45 +0100 Subject: [PATCH] Interpreter - Support for logical or / and --- src/interpreter/interpretable.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/interpreter/interpretable.rs b/src/interpreter/interpretable.rs index 61cae64..c3fa21d 100644 --- a/src/interpreter/interpretable.rs +++ b/src/interpreter/interpretable.rs @@ -132,6 +132,11 @@ impl Interpretable for ast::ExprNode { let value = value.interprete(environment)?; environment.borrow_mut().assign(name, value) } + ast::ExprNode::Logical { + left, + operator, + right, + } => self.on_logic(environment, left, operator, right), ast::ExprNode::Binary { left, operator, @@ -146,6 +151,24 @@ impl Interpretable for ast::ExprNode { } impl ast::ExprNode { + /// Evaluate a logical operator. + fn on_logic( + &self, + environment: &EnvironmentRef, + left: &ast::ExprNode, + operator: &Token, + right: &ast::ExprNode, + ) -> InterpreterResult { + let left_value = left.interprete(environment)?; + if operator.token_type == TokenType::Or && left_value.is_truthy() { + Ok(left_value) + } else if operator.token_type == TokenType::And && !left_value.is_truthy() { + Ok(left_value) + } else { + right.interprete(environment) + } + } + /// Evaluate a binary operator. fn on_binary( &self,