From b3097de75d50a018665823e28e0ee4d7a3878160 Mon Sep 17 00:00:00 2001 From: William Carroll Date: Mon, 29 Aug 2022 12:57:31 -0700 Subject: feat(tvix/eval): Support builtins.{add,sub} First pass at supporting `builtins` for tvix. The following tests appear to be WAI: ```shell $ cd tvix/eval $ cargo build $ cargo test ``` Change-Id: I27cce23d503b17a886d1109e285e8b4be4264977 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6405 Tested-by: BuildkiteCI Reviewed-by: sterni --- tvix/eval/src/builtins/mod.rs | 12 ++++++++++++ tvix/eval/src/tests/tvix_tests/eval-okay-builtins-add.exp | 1 + tvix/eval/src/tests/tvix_tests/eval-okay-builtins-add.nix | 7 +++++++ tvix/eval/src/tests/tvix_tests/eval-okay-builtins-sub.exp | 1 + tvix/eval/src/tests/tvix_tests/eval-okay-builtins-sub.nix | 7 +++++++ tvix/eval/src/vm.rs | 1 + 6 files changed, 29 insertions(+) create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-builtins-add.exp create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-builtins-add.nix create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-builtins-sub.exp create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-builtins-sub.nix (limited to 'tvix/eval/src') diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 49f047543994..a0ada0233c59 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -13,8 +13,15 @@ use crate::{ value::{Builtin, NixAttrs, NixList, NixString, Value}, }; +use crate::arithmetic_op; + fn pure_builtins() -> Vec { vec![ + Builtin::new("add", 2, |mut args| { + let b = args.pop().unwrap(); + let a = args.pop().unwrap(); + Ok(arithmetic_op!(a, b, +)) + }), Builtin::new("abort", 1, |mut args| { return Err( ErrorKind::Abort(args.pop().unwrap().to_string()?.as_str().to_owned()).into(), @@ -63,6 +70,11 @@ fn pure_builtins() -> Vec { Builtin::new("isString", 1, |args| { Ok(Value::Bool(matches!(args[0], Value::String(_)))) }), + Builtin::new("sub", 2, |mut args| { + let b = args.pop().unwrap(); + let a = args.pop().unwrap(); + Ok(arithmetic_op!(a, b, -)) + }), Builtin::new("throw", 1, |mut args| { return Err( ErrorKind::Throw(args.pop().unwrap().to_string()?.as_str().to_owned()).into(), diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-add.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-add.exp new file mode 100644 index 000000000000..25c0a9cc0fba --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-add.exp @@ -0,0 +1 @@ +[ 18 18.9 18.9 19.1 19 ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-add.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-add.nix new file mode 100644 index 000000000000..6b2a29d7982c --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-add.nix @@ -0,0 +1,7 @@ +[ + (builtins.add 7 11) + (builtins.add 7.9 11) + (builtins.add 7 11.9) + (builtins.add 7.2 11.9) + (builtins.add 7.1 11.9) +] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-sub.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-sub.exp new file mode 100644 index 000000000000..c47a17fa2709 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-sub.exp @@ -0,0 +1 @@ +[ -4 -3.1 -4.9 -4.7 -4 ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-sub.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-sub.nix new file mode 100644 index 000000000000..68b1913a0097 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-sub.nix @@ -0,0 +1,7 @@ +[ + (builtins.sub 7 11) + (builtins.sub 7.9 11) + (builtins.sub 7 11.9) + (builtins.sub 7.2 11.9) + (builtins.sub 7.9 11.9) +] diff --git a/tvix/eval/src/vm.rs b/tvix/eval/src/vm.rs index ec8c0bc506e2..a2b370ab9288 100644 --- a/tvix/eval/src/vm.rs +++ b/tvix/eval/src/vm.rs @@ -37,6 +37,7 @@ pub struct VM { with_stack: Vec, } +#[macro_export] macro_rules! arithmetic_op { ( $self:ident, $op:tt ) => {{ let b = $self.pop(); -- cgit 1.4.1