chore: merge codegen::compile and codegen::compile_scoped

This commit is contained in:
2026-02-22 22:25:42 +08:00
parent d7351e907b
commit c24d6a8bb3
2 changed files with 18 additions and 52 deletions

View File

@@ -26,32 +26,17 @@ macro_rules! code {
};
}
pub(crate) fn compile(expr: &Ir, ctx: &impl CodegenContext) -> String {
pub(crate) fn compile<const SCOPED: bool>(expr: &Ir, ctx: &impl CodegenContext) -> String {
let mut buf = CodeBuffer::with_capacity(8192);
code!(&mut buf, ctx; "(()=>{");
code!(&mut buf, ctx;
"const _d="
quoted(&ctx.get_current_dir().display().to_string())
",_w=null;return "
expr
"})()");
buf.into_string()
}
pub(crate) fn compile_scoped(expr: &Ir, ctx: &impl CodegenContext) -> String {
let mut buf = CodeBuffer::with_capacity(8192);
code!(&mut buf, ctx; "((_s)=>{");
code!(&mut buf, ctx;
"const _d="
quoted(&ctx.get_current_dir().display().to_string())
",_w=null;return "
expr
"})"
code!(
&mut buf, ctx;
"((" { if SCOPED { "_s" } else { "" } } ")=>{"
"const _d="
quoted(&ctx.get_current_dir().display().to_string())
",_w=null;"
"return " expr
"})" { if SCOPED { "" } else { "()" } }
);
buf.into_string()
@@ -244,11 +229,7 @@ impl<Ctx: CodegenContext> Compile<Ctx> for Ir<'_> {
}
&Ir::Builtin(Builtin { inner: name, .. }) => {
// Nix.builtins
code!(buf, ctx;
"$b.get("
ctx.get_sym(name)
")"
);
code!(buf, ctx; "$b.get(" ctx.get_sym(name) ")");
}
Ir::ConcatStrings(x) => x.compile(ctx, buf),
Ir::HasAttr(x) => x.compile(ctx, buf),
@@ -273,35 +254,19 @@ impl<Ctx: CodegenContext> Compile<Ctx> for Ir<'_> {
}
Ir::CurPos(cur_pos) => {
// Nix.mkPos
code!(buf, ctx;
"$mp("
cur_pos.span
")"
);
code!(buf, ctx; "$mp(" cur_pos.span ")");
}
&Ir::ReplBinding(ReplBinding { inner: name, .. }) => {
// Nix.getReplBinding
code!(buf, ctx;
"$gb("
ctx.get_sym(name)
")"
);
code!(buf, ctx; "$gb(" ctx.get_sym(name) ")");
}
&Ir::ScopedImportBinding(ScopedImportBinding { inner: name, .. }) => {
code!(buf, ctx;
"_s.get("
ctx.get_sym(name)
")"
);
code!(buf, ctx; "_s.get(" ctx.get_sym(name) ")");
}
Ir::With(x) => x.compile(ctx, buf),
&Ir::WithLookup(WithLookup { inner: name, .. }) => {
// Nix.lookupWith
code!(buf, ctx;
"$l("
ctx.get_sym(name)
",_w)"
);
code!(buf, ctx; "$l(" ctx.get_sym(name) ",_w)");
}
}
}
@@ -385,6 +350,7 @@ impl<Ctx: CodegenContext> Compile<Ctx> for UnOp<'_> {
let rhs = self.rhs;
match self.kind {
Neg => {
// 0 - rhs
code!(buf, ctx; "$os(0n," rhs ")");
}
Not => {

View File

@@ -6,7 +6,7 @@ use hashbrown::{DefaultHashBuilder, HashMap, HashSet};
use rnix::TextRange;
use string_interner::DefaultStringInterner;
use crate::codegen::{CodegenContext, compile, compile_scoped};
use crate::codegen::{CodegenContext, compile};
use crate::downgrade::*;
use crate::error::{Error, Result, Source};
use crate::ir::{
@@ -354,7 +354,7 @@ impl Ctx {
) -> Result<String> {
let root = self.downgrade(source, extra_scope)?;
tracing::debug!("Generating JavaScript code");
let code = compile(root.as_ref(), self);
let code = compile::<false>(root.as_ref(), self);
tracing::debug!("Generated code: {}", &code);
Ok(code)
}
@@ -368,7 +368,7 @@ impl Ctx {
);
let root = self.downgrade(source, Some(scope))?;
tracing::debug!("Generating JavaScript code for scoped import");
let code = compile_scoped(root.as_ref(), self);
let code = compile::<true>(root.as_ref(), self);
tracing::debug!("Generated scoped code: {}", &code);
Ok(code)
}