From e1147b57c74d23a3f18b1a1e7413e4d0b9d5e2ba Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Wed, 24 Aug 2022 16:03:17 +0300 Subject: 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 --- tvix/eval/src/builtins/mod.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tvix/eval/src/builtins/mod.rs (limited to 'tvix/eval/src/builtins/mod.rs') diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs new file mode 100644 index 0000000000..62bfd145c0 --- /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 +} -- cgit 1.4.1