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 <sternenseemann@systemli.org>
This commit is contained in:
William Carroll 2022-08-29 12:57:31 -07:00 committed by tazjin
parent 55d21a1389
commit b3097de75d
6 changed files with 29 additions and 0 deletions

View file

@ -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(),

View file

@ -0,0 +1 @@
[ 18 18.9 18.9 19.1 19 ]

View file

@ -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)
]

View file

@ -0,0 +1 @@
[ -4 -3.1 -4.9 -4.7 -4 ]

View file

@ -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)
]

View file

@ -37,6 +37,7 @@ pub struct VM {
with_stack: Vec<usize>,
}
#[macro_export]
macro_rules! arithmetic_op {
( $self:ident, $op:tt ) => {{
let b = $self.pop();