Use injected js, trampolines for curried functions
This commit is contained in:
parent
d0de70eb54
commit
9291e65c11
@ -37,6 +37,13 @@ impl <'a> LexicalContext<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Embeds the pre-written js
|
||||||
|
pub fn emit_injector(w: &mut dyn Write) -> std::io::Result<()>{
|
||||||
|
let bytes = include_bytes!("./js/deeinject.js");
|
||||||
|
write!(w, "{}", String::from_utf8_lossy(bytes))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn emit_all(w: &mut dyn Write, ast: &Vec<parser::Stmt>, ctx: &mut LexicalContext) -> std::io::Result<()> {
|
pub fn emit_all(w: &mut dyn Write, ast: &Vec<parser::Stmt>, ctx: &mut LexicalContext) -> std::io::Result<()> {
|
||||||
let is_tail = ctx.is_tail;
|
let is_tail = ctx.is_tail;
|
||||||
ctx.is_tail = false;
|
ctx.is_tail = false;
|
||||||
@ -113,14 +120,17 @@ pub fn emit_expr(w: &mut dyn Write, expr: &parser::Expr, ctx: &mut LexicalContex
|
|||||||
write!(w, "{}", atom)?;
|
write!(w, "{}", atom)?;
|
||||||
}
|
}
|
||||||
parser::Expr::Funcall(id, args) => {
|
parser::Expr::Funcall(id, args) => {
|
||||||
write!(w, "{}", munge(id))?;
|
if let Some((last, butlast)) = args.split_last() {
|
||||||
if args.is_empty() {
|
write!(w, "T({}, ", munge(id))?;
|
||||||
write!(w, "()")?;
|
for expr in butlast {
|
||||||
}
|
emit_expr(w, expr, ctx)?;
|
||||||
for arg in args {
|
write!(w, ", ")?;
|
||||||
write!(w, "(")?;
|
}
|
||||||
emit_expr(w, arg, ctx)?;
|
emit_expr(w, last, ctx)?;
|
||||||
write!(w, ")")?;
|
write!(w, ")")?;
|
||||||
|
} else {
|
||||||
|
write!(w, "{}", munge(id))?;
|
||||||
|
write!(w, "()")?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
parser::Expr::Funcdef(arg, body) => {
|
parser::Expr::Funcdef(arg, body) => {
|
||||||
|
10
src/js/deeinject.js
Normal file
10
src/js/deeinject.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
const print = console.log
|
||||||
|
|
||||||
|
function T(f, ...args) {
|
||||||
|
let res = f;
|
||||||
|
for (arg of args) {
|
||||||
|
res = res(arg);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
@ -53,8 +53,7 @@ fn script(cli: &Cli) {
|
|||||||
println!("{:#?}", tree);
|
println!("{:#?}", tree);
|
||||||
} else if cli.ecmascript {
|
} else if cli.ecmascript {
|
||||||
let mut out = io::stdout();
|
let mut out = io::stdout();
|
||||||
// TODO remove this hack
|
emitter::emit_injector(&mut out).ok();
|
||||||
writeln!(out, "const print = console.log;").ok();
|
|
||||||
let mut toplevel = emitter::LexicalContext::toplevel();
|
let mut toplevel = emitter::LexicalContext::toplevel();
|
||||||
emitter::emit_all(&mut out, &tree, &mut toplevel).ok();
|
emitter::emit_all(&mut out, &tree, &mut toplevel).ok();
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user