feat: WIP

This commit is contained in:
2025-08-23 23:47:24 +08:00
parent 65bcfcb47b
commit 2fbd2a26a9
8 changed files with 284 additions and 238 deletions

View File

@@ -1,15 +1,10 @@
use std::cell::Cell;
use std::marker::PhantomPinned;
use std::ops::{Deref, DerefMut};
use std::ptr::NonNull;
use bumpalo::{Bump, boxed::Box};
use hashbrown::HashMap;
use itertools::Itertools;
use petgraph::{
dot::{Config, Dot},
graphmap::DiGraphMap,
};
use petgraph::graphmap::DiGraphMap;
use nixjit_builtins::{
Builtins, BuiltinsContext,
@@ -19,7 +14,7 @@ use nixjit_error::{Error, Result};
use nixjit_eval::{Args, EvalContext, Value};
use nixjit_hir::{DowngradeContext, Hir};
use nixjit_ir::{AttrSet, ExprId, Param, PrimOpId, StackIdx};
use nixjit_lir::{Lir, ResolveContext};
use nixjit_lir::Lir;
use crate::downgrade::DowngradeCtx;
use crate::eval::EvalCtx;
@@ -29,40 +24,6 @@ mod downgrade;
mod eval;
mod resolve;
#[derive(Debug)]
struct Pin<'bump, T> {
ptr: Box<'bump, T>,
_marker: PhantomPinned,
}
impl<T> Pin<'_, T> {
fn into_inner(self) -> T {
Box::into_inner(self.ptr)
}
}
impl<T> Deref for Pin<'_, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.ptr.as_ref()
}
}
impl<T> DerefMut for Pin<'_, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.ptr.as_mut()
}
}
impl<'bump, T> Pin<'bump, T> {
fn new_in(x: T, bump: &'bump Bump) -> Self {
Self {
ptr: Box::new_in(x, bump),
_marker: PhantomPinned,
}
}
}
/// The main evaluation context.
///
/// This struct orchestrates the entire Nix expression evaluation process,
@@ -70,7 +31,7 @@ impl<'bump, T> Pin<'bump, T> {
pub struct Context<'bump> {
ir_count: usize,
hirs: Vec<Hir>,
lirs: Vec<Pin<'bump, Lir>>,
lirs: Vec<Box<'bump, Lir>>,
/// Maps a function's body `ExprId` to its parameter definition.
funcs: HashMap<ExprId, Param>,
@@ -112,17 +73,17 @@ impl<'bump> Context<'bump> {
.chain(scoped.iter().map(|&(_, arity, f)| (arity, f)))
.collect_array()
.unwrap();
let lirs = (0..global.len()).map(|idx| Lir::PrimOp(unsafe { PrimOpId::from_raw(idx) }))
let lirs = (0..global.len())
.map(|idx| Lir::PrimOp(unsafe { PrimOpId::from_raw(idx) }))
.chain(
(0..scoped.len())
.map(|idx| Lir::PrimOp(unsafe { PrimOpId::from_raw(idx + GLOBAL_LEN) })),
)
.chain(core::iter::once(Lir::AttrSet(AttrSet {
stcs: global.into_iter().enumerate().map(|(idx, (name, ..))| {
(name.to_string(), unsafe {
ExprId::from_raw(idx)
})
})
stcs: global
.into_iter()
.enumerate()
.map(|(idx, (name, ..))| (name.to_string(), unsafe { ExprId::from_raw(idx) }))
.chain(scoped.into_iter().enumerate().map(|(idx, (name, ..))| {
(name.to_string(), unsafe {
ExprId::from_raw(idx + GLOBAL_LEN)
@@ -137,10 +98,10 @@ impl<'bump> Context<'bump> {
.chain(core::iter::once(Lir::Thunk(unsafe {
ExprId::from_raw(GLOBAL_LEN + SCOPED_LEN)
})))
.map(|lir| Pin::new_in(lir, bump))
.collect();
.map(|lir| Box::new_in(lir, bump))
.collect_vec();
Self {
ir_count: 0,
ir_count: lirs.len(),
hirs: Vec::new(),
lirs,
funcs: HashMap::new(),
@@ -159,8 +120,8 @@ impl<'bump> Context<'bump> {
DowngradeCtx::new(self)
}
pub fn resolve_ctx<'a>(&'a mut self) -> ResolveCtx<'a, 'bump> {
ResolveCtx::new(self)
pub fn resolve_ctx<'a>(&'a mut self, root: ExprId) -> ResolveCtx<'a, 'bump> {
ResolveCtx::new(self, root)
}
pub fn eval_ctx<'a>(&'a mut self) -> EvalCtx<'a, 'bump> {
@@ -183,27 +144,19 @@ impl<'bump> Context<'bump> {
let root = self
.downgrade_ctx()
.downgrade_root(root.tree().expr().unwrap())?;
self.resolve_ctx().resolve_root(root)?;
println!(
"{:?}",
Dot::with_config(&self.graph, &[Config::EdgeNoLabel])
);
for (idx, ir) in self.lirs.iter().enumerate() {
println!("{:?} {:#?}", unsafe { ExprId::from_raw(idx) }, &ir);
}
self.resolve_ctx(root).resolve_root()?;
Ok(self.eval_ctx().eval_root(root)?.to_public())
}
pub fn add_binding(&mut self, ident: &str, expr: &str) -> Result<()> {
let root = rnix::Root::parse(expr);
if !root.errors().is_empty() {
return Err(Error::parse_error(
root.errors().iter().map(|err| err.to_string()).join("; "),
));
}
let root_expr = root.tree().expr().unwrap();
let root_expr = root
.ok()
.map_err(|err| Error::parse_error(err.to_string()))?
.expr()
.unwrap();
let expr_id = self.downgrade_ctx().downgrade_root(root_expr)?;
self.resolve_ctx().resolve_root(expr_id)?;
self.resolve_ctx(expr_id).resolve_root()?;
unsafe { self.repl_scope.as_mut() }.insert(ident.to_string(), expr_id);
Ok(())
}