diff options
author | William Carroll <wpcarro@gmail.com> | 2022-09-06T21·55-0700 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2022-09-22T17·42+0000 |
commit | 890bbf9b1fba98ac9791eaacaf594492e7ead662 (patch) | |
tree | dbb47ab0ab3a09885a5c6a12648696b7b30e2f47 /tvix/eval/src/vm.rs | |
parent | 9e16d708092e88d0013ef96ef9a33ec28b2c46ea (diff) |
feat(tvix/eval): Support builtins.lessThan r/4955
Extend and export the `cmp_op`, and this becomes trivial. Change-Id: I9c93fa4db0f5a1fc8b56928ea144676f79247de1 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6557 Autosubmit: wpcarro <wpcarro@gmail.com> Tested-by: BuildkiteCI Reviewed-by: sterni <sternenseemann@systemli.org>
Diffstat (limited to 'tvix/eval/src/vm.rs')
-rw-r--r-- | tvix/eval/src/vm.rs | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/tvix/eval/src/vm.rs b/tvix/eval/src/vm.rs index 87adfe5611a4..52627a1fea14 100644 --- a/tvix/eval/src/vm.rs +++ b/tvix/eval/src/vm.rs @@ -94,29 +94,36 @@ macro_rules! arithmetic_op { }}; } +#[macro_export] macro_rules! cmp_op { ( $self:ident, $op:tt ) => {{ let b = $self.pop(); let a = $self.pop(); + let result = fallible!($self, cmp_op!(&a, &b, $op)); + $self.push(result); + }}; + ( $a:expr, $b:expr, $op:tt ) => { // Comparable (in terms of ordering) values are numbers and // strings. Numbers need to be coerced similarly to arithmetic // ops if mixed types are encountered. - let result = match (a, b) { - (Value::Integer(i1), Value::Integer(i2)) => i1 $op i2, - (Value::Float(f1), Value::Float(f2)) => f1 $op f2, - (Value::Integer(i1), Value::Float(f2)) => (i1 as f64) $op f2, - (Value::Float(f1), Value::Integer(i2)) => f1 $op (i2 as f64), - (Value::String(s1), Value::String(s2)) => s1 $op s2, - - (lhs, rhs) => return Err($self.error(ErrorKind::Incomparable { + match ($a, $b) { + // same types + (Value::Integer(i1), Value::Integer(i2)) => Ok(Value::Bool(i1 $op i2)), + (Value::Float(f1), Value::Float(f2)) => Ok(Value::Bool(f1 $op f2)), + (Value::String(s1), Value::String(s2)) => Ok(Value::Bool(s1 $op s2)), + + // different types + (Value::Integer(i1), Value::Float(f2)) => Ok(Value::Bool((*i1 as f64) $op *f2)), + (Value::Float(f1), Value::Integer(i2)) => Ok(Value::Bool(*f1 $op (*i2 as f64))), + + // unsupported types + (lhs, rhs) => Err(ErrorKind::Incomparable { lhs: lhs.type_of(), rhs: rhs.type_of(), - })), - }; - - $self.push(Value::Bool(result)); - }}; + }), + } + } } impl<'o> VM<'o> { |