Skip to content

Commit

Permalink
fix: properly handle boolean comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
vthib committed Feb 26, 2023
1 parent e639df6 commit cec439e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
16 changes: 10 additions & 6 deletions boreal/src/compiler/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,16 +498,16 @@ pub(super) fn compile_expression(
}

parser::ExpressionKind::Add(left, right) => {
compile_primary_op(compiler, *left, *right, span, Expression::Add, false)
compile_primary_op(compiler, *left, *right, span, Expression::Add, false, false)
}
parser::ExpressionKind::Sub(left, right) => {
compile_primary_op(compiler, *left, *right, span, Expression::Sub, false)
compile_primary_op(compiler, *left, *right, span, Expression::Sub, false, false)
}
parser::ExpressionKind::Mul(left, right) => {
compile_primary_op(compiler, *left, *right, span, Expression::Mul, false)
compile_primary_op(compiler, *left, *right, span, Expression::Mul, false, false)
}
parser::ExpressionKind::Div(left, right) => {
compile_primary_op(compiler, *left, *right, span, Expression::Div, false)
compile_primary_op(compiler, *left, *right, span, Expression::Div, false, false)
}

parser::ExpressionKind::Mod(left, right) => {
Expand Down Expand Up @@ -595,20 +595,22 @@ pub(super) fn compile_expression(
can_be_equal,
},
true,
false,
)?;
res.ty = Type::Boolean;
Ok(res)
}

parser::ExpressionKind::Eq(left, right) => {
let mut res = compile_primary_op(compiler, *left, *right, span, Expression::Eq, true)?;
let mut res =
compile_primary_op(compiler, *left, *right, span, Expression::Eq, true, true)?;
res.ty = Type::Boolean;
Ok(res)
}

parser::ExpressionKind::NotEq(left, right) => {
let mut res =
compile_primary_op(compiler, *left, *right, span, Expression::NotEq, true)?;
compile_primary_op(compiler, *left, *right, span, Expression::NotEq, true, true)?;
res.ty = Type::Boolean;
Ok(res)
}
Expand Down Expand Up @@ -883,6 +885,7 @@ fn compile_primary_op<F>(
span: Range<usize>,
constructor: F,
string_allowed: bool,
bool_allowed: bool,
) -> Result<Expr, CompilationError>
where
F: Fn(Box<Expression>, Box<Expression>) -> Expression,
Expand All @@ -894,6 +897,7 @@ where
(Type::Integer, Type::Integer) => Type::Integer,
(Type::Float | Type::Integer, Type::Integer | Type::Float) => Type::Float,
(Type::Bytes, Type::Bytes) if string_allowed => Type::Bytes,
(Type::Boolean, Type::Boolean) if bool_allowed => Type::Boolean,
_ => {
return Err(CompilationError::ExpressionIncompatibleTypes {
left_type: a.ty.to_string(),
Expand Down
60 changes: 59 additions & 1 deletion boreal/tests/it/evaluation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{build_rule, check, check_boreal, Checker};
use crate::utils::{build_rule, check, check_boreal, check_err, Checker};

fn build_empty_rule(condition: &str) -> String {
format!(
Expand Down Expand Up @@ -597,6 +597,64 @@ fn test_eval_not() {
);
}

#[test]
fn test_eval_eq() {
check(&build_empty_rule("var_true == var_true"), &[], true);
check(&build_empty_rule("var_true == var_false"), &[], false);

check(&build_empty_rule("1 == 2"), &[], false);
check(&build_empty_rule("-1 == -1"), &[], true);

check(&build_empty_rule("0.5 == 0.5"), &[], true);
check(&build_empty_rule("1.23 == -1.0"), &[], false);

check(&build_empty_rule("1.5 == 1"), &[], false);
check(&build_empty_rule("1.0 == 1"), &[], true);
check(&build_empty_rule("1 == 1.0"), &[], true);

check(&build_empty_rule("\"\" == \"\""), &[], true);
check(&build_empty_rule("\"anc\" == \"anc\""), &[], true);
check(&build_empty_rule("\"anc\" == \"anC\""), &[], false);

check_err(
&build_empty_rule("1 == \"a\""),
"error: expressions have invalid types",
);
check_err(
&build_empty_rule("/a/ == /a/"),
"error: expressions have invalid types",
);
}

#[test]
fn test_eval_neq() {
check(&build_empty_rule("var_true != var_true"), &[], false);
check(&build_empty_rule("var_true != var_false"), &[], true);

check(&build_empty_rule("1 != 2"), &[], true);
check(&build_empty_rule("-1 != -1"), &[], false);

check(&build_empty_rule("0.5 != 0.5"), &[], false);
check(&build_empty_rule("1.23 != -1.0"), &[], true);

check(&build_empty_rule("1.5 != 1"), &[], true);
check(&build_empty_rule("1.0 != 1"), &[], false);
check(&build_empty_rule("1 != 1.0"), &[], false);

check(&build_empty_rule("\"\" != \"\""), &[], false);
check(&build_empty_rule("\"anc\" != \"anc\""), &[], false);
check(&build_empty_rule("\"anc\" != \"anC\""), &[], true);

check_err(
&build_empty_rule("1 != \"a\""),
"error: expressions have invalid types",
);
check_err(
&build_empty_rule("/a/ != /a/"),
"error: expressions have invalid types",
);
}

#[test]
fn test_eval_matches() {
check(&build_rule("\"az\" matches /a.+z/"), &[], false);
Expand Down

0 comments on commit cec439e

Please sign in to comment.