diff options
author | Vincent Ambo <mail@tazj.in> | 2022-08-07T23·16+0300 |
---|---|---|
committer | tazjin <tazjin@tvl.su> | 2022-08-12T13·24+0000 |
commit | d35ecc0caf2d6ba54b5934f606796687303275b0 (patch) | |
tree | 82d6c303eb0846a053b332656205eade95d3c72a /tvix/eval/src/value.rs | |
parent | e96a2934adadab633b6522367a4e1d768c8b5a87 (diff) |
feat(tvix/eval): implement simple arithmetic binary operations r/4410
Implements simple arithmetic operations (+, -, *, /). There is some scaffolding included to pop and coerce pairs of numbers, as the Nix language will let arithmetic operators apply to arbitrary pairs of number types (always resulting in floats if the types are mixed). Change-Id: I5f62c363bdea8baa6ef812cc64c5406759d257cf Reviewed-on: https://cl.tvl.fyi/c/depot/+/6074 Tested-by: BuildkiteCI Reviewed-by: grfn <grfn@gws.fyi>
Diffstat (limited to 'tvix/eval/src/value.rs')
-rw-r--r-- | tvix/eval/src/value.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/tvix/eval/src/value.rs b/tvix/eval/src/value.rs index 037284b144b2..cbfff55b49e1 100644 --- a/tvix/eval/src/value.rs +++ b/tvix/eval/src/value.rs @@ -8,3 +8,28 @@ pub enum Value { Integer(i64), Float(f64), } + +impl Value { + pub fn is_number(&self) -> bool { + match self { + Value::Integer(_) => true, + Value::Float(_) => true, + _ => false, + } + } + + pub fn type_of(&self) -> &'static str { + match self { + Value::Null => "null", + Value::Bool(_) => "bool", + Value::Integer(_) => "int", + Value::Float(_) => "float", + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum NumberPair { + Floats(f64, f64), + Integer(i64, i64), +} |