about summary refs log tree commit diff
path: root/tvix/eval/src/builtins/mod.rs
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2022-08-29T19·57-0700
committertazjin <tazjin@tvl.su>2022-09-07T20·56+0000
commitb3097de75d50a018665823e28e0ee4d7a3878160 (patch)
tree2aac78701beec7b02b4e7d8537922bede2e5f335 /tvix/eval/src/builtins/mod.rs
parent55d21a1389504d6d9872144077196bac9ee17feb (diff)
feat(tvix/eval): Support builtins.{add,sub} r/4738
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 <sternenseemann@systemli.org>
Diffstat (limited to 'tvix/eval/src/builtins/mod.rs')
-rw-r--r--tvix/eval/src/builtins/mod.rs12
1 files changed, 12 insertions, 0 deletions
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<Builtin> {
     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> {
         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(),