feat(tvix/eval): introduce mechanism for defining builtins
Adds a new builtins module in which builtins can be constructed. The functions in this module should return a correctly structured value to be passed to the compiler's `globals`. This is wired up all the way to the compiler with an example `toString` builtin, available as a global. Note that this does not yet actually behave like the real toString, which has some differences from `Display`. Change-Id: Ibb5f6fbe6207782fdf2434435567fc1bd80039a5 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6254 Tested-by: BuildkiteCI Reviewed-by: sterni <sternenseemann@systemli.org>
This commit is contained in:
parent
ca90c0f45a
commit
e1147b57c7
5 changed files with 48 additions and 6 deletions
26
tvix/eval/src/builtins/mod.rs
Normal file
26
tvix/eval/src/builtins/mod.rs
Normal file
|
@ -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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue