diff options
author | James Landrein <james.landrein@gmail.com> | 2022-10-17T21·37+0200 |
---|---|---|
committer | j4m3s <james.landrein@gmail.com> | 2022-10-25T08·46+0000 |
commit | 8425c2016c9a29008cdec8414fc647b8076a7e34 (patch) | |
tree | 1d11af3c83a0529ab44ceb925761779acb16c7fa /tvix/eval | |
parent | bd0bf6ea7d64d2b3b453db4adb94c98c0b459a24 (diff) |
feat(tvix/eval): add builtins.{floor,ceil} r/5198
Change-Id: I4e6c4f96f6f5097a5c637eb3dbbd7bb8b34b7d52 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7032 Autosubmit: j4m3s <james.landrein@gmail.com> Tested-by: BuildkiteCI Reviewed-by: tazjin <tazjin@tvl.su> Reviewed-by: grfn <grfn@gws.fyi>
Diffstat (limited to 'tvix/eval')
-rw-r--r-- | tvix/eval/src/builtins/mod.rs | 6 | ||||
-rw-r--r-- | tvix/eval/src/tests/tvix_tests/eval-okay-ceil.exp | 1 | ||||
-rw-r--r-- | tvix/eval/src/tests/tvix_tests/eval-okay-ceil.nix | 1 | ||||
-rw-r--r-- | tvix/eval/src/tests/tvix_tests/eval-okay-floor.exp | 1 | ||||
-rw-r--r-- | tvix/eval/src/tests/tvix_tests/eval-okay-floor.nix | 1 | ||||
-rw-r--r-- | tvix/eval/src/value/mod.rs | 1 |
6 files changed, 11 insertions, 0 deletions
diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 336c320e2f00..91fe27aa4695 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -134,6 +134,9 @@ fn pure_builtins() -> Vec<Builtin> { Ok(Value::List(NixList::construct(output.len(), output))) }, ), + Builtin::new("ceil", &[true], |args: Vec<Value>, _: &mut VM| { + Ok(Value::Integer(args[0].as_float()?.ceil() as i64)) + }), Builtin::new( "compareVersions", &[true, true], @@ -269,6 +272,9 @@ fn pure_builtins() -> Vec<Builtin> { .map(|list| Value::List(NixList::from(list))) .map_err(Into::into) }), + Builtin::new("floor", &[true], |args: Vec<Value>, _: &mut VM| { + Ok(Value::Integer(args[0].as_float()?.floor() as i64)) + }), Builtin::new( "foldl'", &[true, false, true], diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-ceil.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-ceil.exp new file mode 100644 index 000000000000..dffbbe59f0a0 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-ceil.exp @@ -0,0 +1 @@ +[ 4 ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-ceil.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-ceil.nix new file mode 100644 index 000000000000..5835bf829b03 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-ceil.nix @@ -0,0 +1 @@ +[ (builtins.ceil 3.4) ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-floor.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-floor.exp new file mode 100644 index 000000000000..6f98a7f48f2f --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-floor.exp @@ -0,0 +1 @@ +[ 3 ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-floor.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-floor.nix new file mode 100644 index 000000000000..c6b79c91a1b4 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-floor.nix @@ -0,0 +1 @@ +[ (builtins.floor 3.4) ] diff --git a/tvix/eval/src/value/mod.rs b/tvix/eval/src/value/mod.rs index 82be292819ae..355540402d42 100644 --- a/tvix/eval/src/value/mod.rs +++ b/tvix/eval/src/value/mod.rs @@ -293,6 +293,7 @@ impl Value { gen_cast!(as_bool, bool, "bool", Value::Bool(b), *b); gen_cast!(as_int, i64, "int", Value::Integer(x), *x); + gen_cast!(as_float, f64, "float", Value::Float(x), *x); gen_cast!(to_str, NixString, "string", Value::String(s), s.clone()); gen_cast!(to_attrs, Rc<NixAttrs>, "set", Value::Attrs(a), a.clone()); gen_cast!(to_list, NixList, "list", Value::List(l), l.clone()); |