chore: merge codegen::compile and codegen::compile_scoped
This commit is contained in:
@@ -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);
|
let mut buf = CodeBuffer::with_capacity(8192);
|
||||||
|
|
||||||
code!(&mut buf, ctx; "(()=>{");
|
code!(
|
||||||
|
&mut buf, ctx;
|
||||||
code!(&mut buf, ctx;
|
"((" { if SCOPED { "_s" } else { "" } } ")=>{"
|
||||||
"const _d="
|
"const _d="
|
||||||
quoted(&ctx.get_current_dir().display().to_string())
|
quoted(&ctx.get_current_dir().display().to_string())
|
||||||
",_w=null;return "
|
",_w=null;"
|
||||||
expr
|
"return " expr
|
||||||
"})()");
|
"})" { if SCOPED { "" } else { "()" } }
|
||||||
|
|
||||||
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
|
|
||||||
"})"
|
|
||||||
);
|
);
|
||||||
|
|
||||||
buf.into_string()
|
buf.into_string()
|
||||||
@@ -244,11 +229,7 @@ impl<Ctx: CodegenContext> Compile<Ctx> for Ir<'_> {
|
|||||||
}
|
}
|
||||||
&Ir::Builtin(Builtin { inner: name, .. }) => {
|
&Ir::Builtin(Builtin { inner: name, .. }) => {
|
||||||
// Nix.builtins
|
// Nix.builtins
|
||||||
code!(buf, ctx;
|
code!(buf, ctx; "$b.get(" ctx.get_sym(name) ")");
|
||||||
"$b.get("
|
|
||||||
ctx.get_sym(name)
|
|
||||||
")"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
Ir::ConcatStrings(x) => x.compile(ctx, buf),
|
Ir::ConcatStrings(x) => x.compile(ctx, buf),
|
||||||
Ir::HasAttr(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) => {
|
Ir::CurPos(cur_pos) => {
|
||||||
// Nix.mkPos
|
// Nix.mkPos
|
||||||
code!(buf, ctx;
|
code!(buf, ctx; "$mp(" cur_pos.span ")");
|
||||||
"$mp("
|
|
||||||
cur_pos.span
|
|
||||||
")"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
&Ir::ReplBinding(ReplBinding { inner: name, .. }) => {
|
&Ir::ReplBinding(ReplBinding { inner: name, .. }) => {
|
||||||
// Nix.getReplBinding
|
// Nix.getReplBinding
|
||||||
code!(buf, ctx;
|
code!(buf, ctx; "$gb(" ctx.get_sym(name) ")");
|
||||||
"$gb("
|
|
||||||
ctx.get_sym(name)
|
|
||||||
")"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
&Ir::ScopedImportBinding(ScopedImportBinding { inner: name, .. }) => {
|
&Ir::ScopedImportBinding(ScopedImportBinding { inner: name, .. }) => {
|
||||||
code!(buf, ctx;
|
code!(buf, ctx; "_s.get(" ctx.get_sym(name) ")");
|
||||||
"_s.get("
|
|
||||||
ctx.get_sym(name)
|
|
||||||
")"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
Ir::With(x) => x.compile(ctx, buf),
|
Ir::With(x) => x.compile(ctx, buf),
|
||||||
&Ir::WithLookup(WithLookup { inner: name, .. }) => {
|
&Ir::WithLookup(WithLookup { inner: name, .. }) => {
|
||||||
// Nix.lookupWith
|
// Nix.lookupWith
|
||||||
code!(buf, ctx;
|
code!(buf, ctx; "$l(" ctx.get_sym(name) ",_w)");
|
||||||
"$l("
|
|
||||||
ctx.get_sym(name)
|
|
||||||
",_w)"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -385,6 +350,7 @@ impl<Ctx: CodegenContext> Compile<Ctx> for UnOp<'_> {
|
|||||||
let rhs = self.rhs;
|
let rhs = self.rhs;
|
||||||
match self.kind {
|
match self.kind {
|
||||||
Neg => {
|
Neg => {
|
||||||
|
// 0 - rhs
|
||||||
code!(buf, ctx; "$os(0n," rhs ")");
|
code!(buf, ctx; "$os(0n," rhs ")");
|
||||||
}
|
}
|
||||||
Not => {
|
Not => {
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use hashbrown::{DefaultHashBuilder, HashMap, HashSet};
|
|||||||
use rnix::TextRange;
|
use rnix::TextRange;
|
||||||
use string_interner::DefaultStringInterner;
|
use string_interner::DefaultStringInterner;
|
||||||
|
|
||||||
use crate::codegen::{CodegenContext, compile, compile_scoped};
|
use crate::codegen::{CodegenContext, compile};
|
||||||
use crate::downgrade::*;
|
use crate::downgrade::*;
|
||||||
use crate::error::{Error, Result, Source};
|
use crate::error::{Error, Result, Source};
|
||||||
use crate::ir::{
|
use crate::ir::{
|
||||||
@@ -354,7 +354,7 @@ impl Ctx {
|
|||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
let root = self.downgrade(source, extra_scope)?;
|
let root = self.downgrade(source, extra_scope)?;
|
||||||
tracing::debug!("Generating JavaScript code");
|
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);
|
tracing::debug!("Generated code: {}", &code);
|
||||||
Ok(code)
|
Ok(code)
|
||||||
}
|
}
|
||||||
@@ -368,7 +368,7 @@ impl Ctx {
|
|||||||
);
|
);
|
||||||
let root = self.downgrade(source, Some(scope))?;
|
let root = self.downgrade(source, Some(scope))?;
|
||||||
tracing::debug!("Generating JavaScript code for scoped import");
|
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);
|
tracing::debug!("Generated scoped code: {}", &code);
|
||||||
Ok(code)
|
Ok(code)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user