Skip to content

Commit

Permalink
Fix logical expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
lgyanf committed Oct 8, 2023
1 parent 0f33f92 commit ca786fc
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ impl<'a> Interpreter<'a> {
right: &Expr,
position: &PositionRange,
) -> Result<Value, LoxError> {
// TOOD: right value should be calculated lazily
let left_value = self.visit_expr(left)?;
let right_value = self.visit_expr(right)?;
match (&left_value, &op, &right_value) {
(_, BinaryOp::And, _) => Ok(if !Self::is_truthy(&left_value) { left_value } else { right_value }),
(_, BinaryOp::Or, _) => Ok(if Self::is_truthy(&left_value) { left_value } else { right_value }),
(Value::Number(l), _, Value::Number(r)) => {
let result = Self::binary_arithmentic_op(*l, op, *r, position);
Ok(result)
Expand All @@ -91,8 +94,6 @@ impl<'a> Interpreter<'a> {
}
(_, BinaryOp::EqualEqual, _) => Ok(Value::Boolean(left_value == right_value)),
(_, BinaryOp::BangEqual, _) => Ok(Value::Boolean(left_value != right_value)),
(_, BinaryOp::And, _) => Ok(Value::Boolean(Self::is_truthy(&left_value) && Self::is_truthy(&right_value))),
(_, BinaryOp::Or, _) => Ok(Value::Boolean(Self::is_truthy(&left_value) || Self::is_truthy(&right_value))),
_ => Err(LoxError {
kind: LoxErrorKind::Runtime,
position: position.clone(),
Expand Down Expand Up @@ -123,8 +124,8 @@ impl<'a> Interpreter<'a> {
BinaryOp::LessEqual => Value::Boolean(left <= right),
BinaryOp::EqualEqual => Value::Boolean(left == right),
BinaryOp::BangEqual => Value::Boolean(left != right),
BinaryOp::And => Value::Boolean((left > 0.) && (right > 0.)),
BinaryOp::Or => Value::Boolean((left > 0.) || (right > 0.)),
BinaryOp::And => unreachable!(),
BinaryOp::Or => unreachable!(),
}
}

Expand Down Expand Up @@ -461,5 +462,15 @@ global c
Ok(()),
"false\n",
),
print_hi_or_2: (
"print \"hi\" or 2;",
Ok(()),
"hi\n",
),
print_nil_or_yes: (
"print nil or \"yes\";",
Ok(()),
"yes\n",
),
);
}

0 comments on commit ca786fc

Please sign in to comment.