Use new looping construct in examples

This commit is contained in:
Dane Johnson 2024-11-18 10:41:51 -06:00
parent 08cab74fb5
commit 7f241523d4
3 changed files with 14 additions and 9 deletions

View File

@ -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()

View File

@ -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(())
}

View File

@ -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++) {