feat(builtins): macro

This commit is contained in:
2025-08-05 23:54:10 +08:00
parent 64f650b695
commit 32c602f21c
12 changed files with 426 additions and 132 deletions

View File

@@ -0,0 +1,10 @@
[package]
name = "nixjit_builtins"
version = "0.1.0"
edition = "2024"
[dependencies]
nixjit_error = { path = "../nixjit_error" }
nixjit_eval = { path = "../nixjit_eval" }
nixjit_macros = { path = "../nixjit_macros" }
nixjit_value = { path = "../nixjit_value" }

View File

@@ -0,0 +1,38 @@
use nixjit_macros::builtins;
use nixjit_eval::EvalContext;
pub trait BuiltinsContext: EvalContext {}
#[builtins]
mod builtins {
use nixjit_error::{Error, Result};
use nixjit_eval::Value;
use nixjit_value::Const;
use super::BuiltinsContext;
const TRUE: Const = Const::Bool(true);
const FALSE: Const = Const::Bool(false);
const NULL: Const = Const::Null;
fn add<Ctx: BuiltinsContext>(a: Value<Ctx>, b: Value<Ctx>) -> Result<Value<Ctx>> {
use Value::*;
Ok(match (a, b) {
(Int(a), Int(b)) => Int(a + b),
(Int(a), Float(b)) => Float(a as f64 + b),
(Float(a), Int(b)) => Float(a + b as f64),
(Float(a), Float(b)) => Float(a + b),
(a @ Value::Catchable(_), _) => a,
(_, b @ Value::Catchable(_)) => b,
_ => return Err(Error::EvalError(format!(""))),
})
}
pub fn import<Ctx: BuiltinsContext>(ctx: &mut Ctx, path: Value<Ctx>) -> Result<Value<Ctx>> {
todo!()
}
fn elem_at<Ctx: BuiltinsContext>(list: Value<Ctx>, idx: Value<Ctx>) -> Result<Value<Ctx>> {
todo!()
}
}