feat: stack var (WIP)

This commit is contained in:
2025-08-09 08:12:53 +08:00
parent fd182b6233
commit d8ad7fe904
36 changed files with 1521 additions and 1058 deletions

View File

@@ -5,14 +5,16 @@ use std::fmt::Debug;
use std::rc::Rc;
use derive_more::Constructor;
use hashbrown::hash_map::Entry;
use hashbrown::HashMap;
use hashbrown::hash_map::Entry;
use itertools::Itertools;
use nixjit_error::{Error, Result};
use nixjit_value::Symbol;
use nixjit_ir::ExprId;
use nixjit_value::{self as p, format_symbol};
use crate::EvalContext;
use super::Value;
/// A wrapper around a `HashMap` representing a Nix attribute set.
@@ -20,7 +22,7 @@ use super::Value;
/// It uses `#[repr(transparent)]` to ensure it has the same memory layout
/// as `HashMap<String, Value>`.
#[repr(transparent)]
#[derive(Clone, Constructor, PartialEq)]
#[derive(Clone, Constructor)]
pub struct AttrSet {
data: HashMap<String, Value>,
}
@@ -31,9 +33,9 @@ impl Debug for AttrSet {
write!(f, "{{ ")?;
for (k, v) in self.data.iter() {
match v {
List(_) => write!(f, "{k:?} = [ ... ]; ")?,
AttrSet(_) => write!(f, "{k:?} = {{ ... }}; ")?,
v => write!(f, "{k:?} = {v:?}; ")?,
List(_) => write!(f, "{} = [ ... ]; ", format_symbol(k))?,
AttrSet(_) => write!(f, "{} = {{ ... }}; ", format_symbol(k))?,
v => write!(f, "{} = {v:?}; ", format_symbol(k))?,
}
}
write!(f, "}}")
@@ -69,7 +71,7 @@ impl AttrSet {
/// Inserts an attribute, returns an error if the attribute is already defined.
pub fn push_attr(&mut self, sym: String, val: Value) -> Result<()> {
match self.data.entry(sym) {
Entry::Occupied(occupied) => Err(Error::EvalError(format!(
Entry::Occupied(occupied) => Err(Error::eval_error(format!(
"attribute '{}' already defined",
format_symbol(occupied.key())
))),
@@ -80,30 +82,32 @@ impl AttrSet {
}
}
/// Performs a deep selection of an attribute from a nested set.
///
/// It traverses the attribute path and returns the final value, or an error
/// if any intermediate attribute does not exist or is not a set.
pub fn select(
pub fn select(&self, name: &str, ctx: &mut impl EvalContext) -> Result<Value> {
self.data
.get(name)
.cloned()
.map(|attr| match attr {
Value::Thunk(id) => ctx.eval(id),
val => Ok(val),
})
.ok_or_else(|| {
Error::eval_error(format!("attribute '{}' not found", format_symbol(name)))
})?
}
pub fn select_or(
&self,
mut path: impl DoubleEndedIterator<Item = Result<String>>,
name: &str,
default: ExprId,
ctx: &mut impl EvalContext,
) -> Result<Value> {
let mut data = &self.data;
let last = path.nth_back(0).unwrap();
for item in path {
let item = item?;
let Some(Value::AttrSet(attrs)) = data.get(&item) else {
return Err(Error::EvalError(format!(
"attribute '{}' not found",
format_symbol(item)
)));
};
data = attrs.as_inner();
}
let last = last?;
data.get(&last).cloned().ok_or_else(|| {
Error::EvalError(format!("attribute '{}' not found", Symbol::from(last)))
})
self.data
.get(name)
.map(|attr| match attr {
&Value::Thunk(id) => ctx.eval(id),
val => Ok(val.clone()),
})
.unwrap_or_else(|| ctx.eval(default))
}
/// Checks if an attribute path exists within the set.
@@ -114,16 +118,14 @@ impl AttrSet {
let mut data = &self.data;
let last = path.nth_back(0).unwrap();
for item in path {
let Some(Value::AttrSet(attrs)) =
data.get(item.unwrap().coerce_to_string()?.as_ref().unwrap_string())
let Some(Value::AttrSet(attrs)) = data.get(&item.unwrap().force_string_no_ctx()?)
else {
return Ok(Value::Bool(false));
};
data = attrs.as_inner();
}
Ok(Value::Bool(
data.get(last.unwrap().coerce_to_string()?.as_ref().unwrap_string())
.is_some(),
data.get(&last.unwrap().force_string_no_ctx()?).is_some(),
))
}