Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement Short-Circuit Evaluation for AND Expressions to Fix Issue #117 #118

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions interpreter/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,12 @@ impl<'a> Value {
}
Expression::And(left, right) => {
let left = Value::resolve(left, ctx)?;
let right = Value::resolve(right, ctx)?;
Value::Bool(left.to_bool() && right.to_bool()).into()
if !left.to_bool() {
left.into()
Masterlvng marked this conversation as resolved.
Show resolved Hide resolved
} else {
let right = Value::resolve(right, ctx)?;
right.into()
Masterlvng marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expression::Unary(op, expr) => {
let expr = Value::resolve(expr, ctx)?;
Expand Down Expand Up @@ -973,4 +977,18 @@ mod tests {
)))]))
);
}

#[test]
fn test_short_curcuit_and() {
let mut context = Context::default();
let data: HashMap<String, String> = HashMap::new();
context.add_variable_from_value("data", data);

let program = Program::compile("has(data.x) && data.x.startsWith(\"foo\")").unwrap();
let value = program.execute(&context);
assert!(
value.is_ok(),
"The AND expression should support short-circuit evaluation."
);
}
}
Loading