Compare commits

..

No commits in common. "9291e65c1150d265a5d48cf5e696fab24e16f14b" and "26bff70b1ffd6b21bae6b3055f0c821248f3ab07" have entirely different histories.

4 changed files with 10 additions and 29 deletions

View File

@ -2,7 +2,7 @@
fizzbuzz <- x -> fizzbuzz <- x ->
if x % 15 = 0 print("fizzbuzz") if x % 15 = 0 print("fizzbuzz")
elif x % 3 = 0 print("fizz") elif x % 3 = 0 print("fizz")
elif x % 5 = 0 print("buzz") # elif x % 5 = 0 print("buzz")
else print(x) else print(x)
if x < 100 fizzbuzz(x + 1) ## TODO add looping construct(s) if x < 100 fizzbuzz(x + 1) ## TODO add looping construct(s)

View File

@ -37,13 +37,6 @@ 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;
@ -120,18 +113,15 @@ 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) => {
if let Some((last, butlast)) = args.split_last() { write!(w, "{}", munge(id))?;
write!(w, "T({}, ", munge(id))?; if args.is_empty() {
for expr in butlast {
emit_expr(w, expr, ctx)?;
write!(w, ", ")?;
}
emit_expr(w, last, ctx)?;
write!(w, ")")?;
} else {
write!(w, "{}", munge(id))?;
write!(w, "()")?; write!(w, "()")?;
} }
for arg in args {
write!(w, "(")?;
emit_expr(w, arg, ctx)?;
write!(w, ")")?;
}
} }
parser::Expr::Funcdef(arg, body) => { parser::Expr::Funcdef(arg, body) => {
if let Some(arg) = arg { if let Some(arg) = arg {

View File

@ -1,10 +0,0 @@
const print = console.log
function T(f, ...args) {
let res = f;
for (arg of args) {
res = res(arg);
}
return res;
}

View File

@ -53,7 +53,8 @@ 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();
emitter::emit_injector(&mut out).ok(); // TODO remove this hack
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 {