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

@@ -135,10 +135,10 @@ impl<Ctx: EvalContext> Evaluate<Ctx> for ir::BinOp {
let mut lhs = self.lhs.eval(ctx)?;
let mut rhs = self.rhs.eval(ctx)?;
match self.kind {
Add => lhs.add(rhs),
Add => lhs.add(rhs)?,
Sub => {
rhs.neg();
lhs.add(rhs);
lhs.add(rhs)?;
}
Mul => lhs.mul(rhs),
Div => lhs.div(rhs)?,

View File

@@ -1,4 +1,5 @@
use std::hash::Hash;
use std::process::abort;
use std::rc::Rc;
use std::fmt::{write, Debug};
@@ -7,7 +8,7 @@ use derive_more::{IsVariant, Unwrap};
use func::FuncApp;
use hashbrown::HashSet;
use nixjit_ir::ExprId;
use replace_with::replace_with_or_abort;
use replace_with::{replace_with_and_return, replace_with_or_abort};
use nixjit_error::{Error, Result};
use nixjit_value::Const;
@@ -307,21 +308,24 @@ impl<Ctx: EvalContext> Value<Ctx> {
}
}
pub fn add(&mut self, other: Self) {
pub fn add(&mut self, other: Self) -> Result<()> {
use Value::*;
replace_with_or_abort(self, |a| match (a, other) {
(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),
(String(mut a), String(b)) => {
a.push_str(&b);
String(a)
}
(a @ Value::Catchable(_), _) => a,
(_, x @ Value::Catchable(_)) => x,
_ => todo!(),
});
replace_with_and_return(self, || abort(), |a| {
let val = match (a, other) {
(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),
(String(mut a), String(b)) => {
a.push_str(&b);
String(a)
}
(a @ Value::Catchable(_), _) => a,
(_, x @ Value::Catchable(_)) => x,
_ => return (Err(Error::EvalError(format!(""))), Value::Null),
};
(Ok(()), val)
})
}
pub fn mul(&mut self, other: Self) {