Skip to content

Commit

Permalink
comparisons for tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
kuviman committed Dec 6, 2024
1 parent b636ebb commit 2663226
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,15 +740,23 @@ impl Cache {
})
}
macro_rules! binary_cmp_op {
($op:tt) => {
insert_named(binary_cmp_op(stringify!($op), |lhs, rhs| {
($op:tt, $name:ident) => {
fn $name(lhs: Value, rhs: Value) -> eyre::Result<bool> {
Ok(match (lhs, rhs) {
(Value::Bool(lhs), Value::Bool(rhs)) => lhs $op rhs,
(Value::Int32(lhs), Value::Int32(rhs)) => lhs $op rhs,
(Value::Int64(lhs), Value::Int64(rhs)) => lhs $op rhs,
(Value::Float64(lhs), Value::Float64(rhs)) => lhs $op rhs,
(Value::Char(lhs), Value::Char(rhs)) => lhs $op rhs,
(Value::String(lhs), Value::String(rhs)) => lhs $op rhs,
(Value::Tuple(lhs), Value::Tuple(rhs)) => 'result: {
for (_name, (lhs, rhs)) in lhs.zip(rhs).unwrap() {
if lhs != rhs {
break 'result $name(lhs, rhs)?;
}
}
stringify!($op).contains('=')
},
(lhs, rhs) => {
eyre::bail!(
"{:?} doesnt work for {} and {}",
Expand All @@ -758,15 +766,16 @@ impl Cache {
)
}
})
}));
}
insert_named(binary_cmp_op(stringify!($op), $name));
};
}
binary_cmp_op!(<);
binary_cmp_op!(<=);
binary_cmp_op!(==);
binary_cmp_op!(!=);
binary_cmp_op!(>=);
binary_cmp_op!(>);
binary_cmp_op!(<, lt);
binary_cmp_op!(<=, le);
binary_cmp_op!(==, eq);
binary_cmp_op!(!=, ne);
binary_cmp_op!(>=, gt);
binary_cmp_op!(>, ge);
insert_named(unary_op("unary -", |value| {
Ok(match value {
Value::Int32(value) => Value::Int32(-value),
Expand Down

0 comments on commit 2663226

Please sign in to comment.