diff options
Diffstat (limited to 'tvix/eval/src')
-rw-r--r-- | tvix/eval/src/builtins/mod.rs | 26 | ||||
-rw-r--r-- | tvix/eval/src/compiler.rs | 11 | ||||
-rw-r--r-- | tvix/eval/src/eval.rs | 3 | ||||
-rw-r--r-- | tvix/eval/src/lib.rs | 1 | ||||
-rw-r--r-- | tvix/eval/src/value/builtin.rs | 13 |
5 files changed, 48 insertions, 6 deletions
diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs new file mode 100644 index 000000000000..62bfd145c0a9 --- /dev/null +++ b/tvix/eval/src/builtins/mod.rs @@ -0,0 +1,26 @@ +//! This module implements the builtins exposed in the Nix language. +//! +//! See //tvix/eval/docs/builtins.md for a some context on the +//! available builtins in Nix. + +use std::collections::HashMap; + +use crate::value::{Builtin, Value}; + +macro_rules! builtin { + ( $map:ident, $name:literal, $arity:literal, $body:expr ) => { + $map.insert($name, Value::Builtin(Builtin::new($name, $arity, $body))); + }; +} + +/// Set of Nix builtins that are globally available. +pub fn global_builtins() -> HashMap<&'static str, Value> { + let mut globals = HashMap::new(); + + builtin!(globals, "toString", 1, |args| { + // TODO: toString is actually not the same as Display + Ok(Value::String(format!("{}", args[0]).into())) + }); + + globals +} diff --git a/tvix/eval/src/compiler.rs b/tvix/eval/src/compiler.rs index 1bbd791876ab..cb97b61c989e 100644 --- a/tvix/eval/src/compiler.rs +++ b/tvix/eval/src/compiler.rs @@ -1029,7 +1029,11 @@ fn prepare_globals(additional: HashMap<&'static str, Value>) -> GlobalsMap { globals } -pub fn compile(expr: ast::Expr, location: Option<PathBuf>) -> EvalResult<CompilationResult> { +pub fn compile( + expr: ast::Expr, + location: Option<PathBuf>, + globals: HashMap<&'static str, Value>, +) -> EvalResult<CompilationResult> { let mut root_dir = match location { Some(dir) => Ok(dir), None => std::env::current_dir().map_err(|e| { @@ -1044,12 +1048,9 @@ pub fn compile(expr: ast::Expr, location: Option<PathBuf>) -> EvalResult<Compila root_dir.pop(); } - // TODO: accept globals as an external parameter - let globals = prepare_globals(HashMap::new()); - let mut c = Compiler { root_dir, - globals, + globals: prepare_globals(globals), contexts: vec![LambdaCtx::new()], warnings: vec![], errors: vec![], diff --git a/tvix/eval/src/eval.rs b/tvix/eval/src/eval.rs index d81744220568..8cb44b87ea08 100644 --- a/tvix/eval/src/eval.rs +++ b/tvix/eval/src/eval.rs @@ -3,6 +3,7 @@ use std::path::PathBuf; use rnix; use crate::{ + builtins::global_builtins, errors::{ErrorKind, EvalResult}, value::Value, }; @@ -28,7 +29,7 @@ pub fn interpret(code: &str, location: Option<PathBuf>) -> EvalResult<Value> { println!("{:?}", root_expr); } - let result = crate::compiler::compile(root_expr, location)?; + let result = crate::compiler::compile(root_expr, location, global_builtins())?; #[cfg(feature = "disassembler")] crate::disassembler::disassemble_chunk(&result.lambda.chunk); diff --git a/tvix/eval/src/lib.rs b/tvix/eval/src/lib.rs index f796034ded98..eebccc873956 100644 --- a/tvix/eval/src/lib.rs +++ b/tvix/eval/src/lib.rs @@ -1,3 +1,4 @@ +mod builtins; mod chunk; mod compiler; mod errors; diff --git a/tvix/eval/src/value/builtin.rs b/tvix/eval/src/value/builtin.rs index 8d7e82136125..84582e298586 100644 --- a/tvix/eval/src/value/builtin.rs +++ b/tvix/eval/src/value/builtin.rs @@ -34,6 +34,19 @@ pub struct Builtin { } impl Builtin { + pub fn new(name: &'static str, arity: usize, func: BuiltinFn) -> Self { + Builtin { + name, + arity, + func, + partials: vec![], + } + } + + pub fn name(&self) -> &'static str { + self.name + } + /// Apply an additional argument to the builtin, which will either /// lead to execution of the function or to returning a partial /// builtin. |