Interpreter - Support for logical or / and

This commit is contained in:
Emmanuel BENOîT 2023-01-01 18:27:45 +01:00
parent 79c594afa9
commit a671836655

View file

@ -132,6 +132,11 @@ impl Interpretable for ast::ExprNode {
let value = value.interprete(environment)?; let value = value.interprete(environment)?;
environment.borrow_mut().assign(name, value) environment.borrow_mut().assign(name, value)
} }
ast::ExprNode::Logical {
left,
operator,
right,
} => self.on_logic(environment, left, operator, right),
ast::ExprNode::Binary { ast::ExprNode::Binary {
left, left,
operator, operator,
@ -146,6 +151,24 @@ impl Interpretable for ast::ExprNode {
} }
impl 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. /// Evaluate a binary operator.
fn on_binary( fn on_binary(
&self, &self,