Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions implants/lib/eldritchv2/eldritch-core/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,27 @@ impl Value {
visited.insert(pair);
}

// Special case for Int vs Float comparison to make them behave numerically
// This must be done before discriminant check because they have different discriminants
// but we want them to compare by value.
match (self, other) {
(Value::Int(i), Value::Float(f)) => {
if p1 != 0 && p2 != 0 {
let pair = (p1, p2);
visited.remove(&pair);
}
return (*i as f64).total_cmp(f);
}
(Value::Float(f), Value::Int(i)) => {
if p1 != 0 && p2 != 0 {
let pair = (p1, p2);
visited.remove(&pair);
}
return f.total_cmp(&(*i as f64));
}
_ => {}
}

// Define an ordering between types:
// None < Bool < Int < Float < String < Bytes < List < Tuple < Dict < Set < Function < Native < Bound < Foreign
let self_discriminant = self.discriminant_value();
Expand Down
62 changes: 62 additions & 0 deletions implants/lib/eldritchv2/eldritch-core/tests/max_min_mixed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#[cfg(test)]
mod tests {
use eldritch_core::{Interpreter, Value};

#[test]
fn test_max_min_mixed_types() {
let code = r#"
tests = [
[1, 0.8, 1],
[1, 1.8, 1.8],
[10, 9.999, 10],
[10, 19.999, 19.999],
[1, 1, 1],
[1, 10, 10],
[11, 0, 11],
[.1, 0, .1],
[.2, .1, .2],
[.2, 2, 2],
]
errors = []
for a, b, c in tests:
r = max(a, b)
if r != c:
errors.append(f"FAIL max({a}, {b}) != '{c}' got '{r}'")

tests = [
[1, 0.8, 0.8],
[1, 1.8, 1],
[10, 9.999, 9.999],
[10, 19.999, 10],
[1, 1, 1],
[1, 10, 1],
[11, 0, 0],
[.1, 0, 0],
[.2, .1, .1],
[.2, 2, .2],
]
for a, b, c in tests:
r = min(a, b)
if r != c:
errors.append(f"FAIL min({a}, {b}) != '{c}' got '{r}'")

errors
"#;
let mut interp = Interpreter::new();
match interp.interpret(code) {
Ok(val) => match val {
Value::List(l) => {
let errors = l.read();
if !errors.is_empty() {
for err in errors.iter() {
println!("{}", err);
}
panic!("Test failed with {} errors", errors.len());
}
}
_ => panic!("Expected list of errors"),
},
Err(e) => panic!("Interpreter error: {}", e),
}
}
}
Loading