diff --git a/demo/real.dee b/demo/real.dee index 4509197..623c9da 100644 --- a/demo/real.dee +++ b/demo/real.dee @@ -1,11 +1,10 @@ ## Obligatory fizzbuzz <- -> - loop(range(100), x -> + loop over x in range(100) if x % 15 = 0 print("fizzbuzz") elif x % 3 = 0 print("fizz") elif x % 5 = 0 print("buzz") else print(x) - ) fizzbuzz() diff --git a/src/emitter.rs b/src/emitter.rs index cb1772f..055f563 100644 --- a/src/emitter.rs +++ b/src/emitter.rs @@ -100,7 +100,19 @@ pub fn emit(w: &mut dyn Write, stmt: &parser::Stmt, ctx: &mut LexicalContext) -> writeln!(w, "}}")?; } } - parser::Stmt::Loop(_) => todo!() + parser::Stmt::Loop(eloop) => match &eloop { + parser::Loop::Over(id, expr, block) => { + let is_tail = ctx.is_tail; + ctx.is_tail = false; + write!(w, "for (let {} of ", id)?; + emit_expr(w, expr, ctx)?; + writeln!(w, ") {{")?; + emit_all(w, block, ctx)?; + writeln!(w, "}}")?; + ctx.is_tail = is_tail; + } + _ => todo!() + } } Ok(()) } diff --git a/src/js/deeinject.js b/src/js/deeinject.js index 824c909..67f81bb 100644 --- a/src/js/deeinject.js +++ b/src/js/deeinject.js @@ -1,11 +1,5 @@ const print = console.log -function loop(seq, f) { - for (el of seq) { - f(el) - } -} - function range(x) { const ret = [] for (let i = 0; i < x; i++) {