Finish emitter

This commit is contained in:
Dane Johnson 2024-11-18 21:16:02 -06:00
parent 7f241523d4
commit c345ac4fe5

View File

@ -100,18 +100,35 @@ pub fn emit(w: &mut dyn Write, stmt: &parser::Stmt, ctx: &mut LexicalContext) ->
writeln!(w, "}}")?;
}
}
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;
parser::Stmt::Loop(eloop) => {
let is_tail = ctx.is_tail;
ctx.is_tail = false;
match &eloop {
parser::Loop::Over(id, expr, block) => {
write!(w, "for (let {} of ", id)?;
emit_expr(w, expr, ctx)?;
writeln!(w, ") {{")?;
let mut block_ctx = LexicalContext::new(ctx);
emit_all(w, block, &mut block_ctx)?;
writeln!(w, "}}")?;
ctx.is_tail = is_tail;
}
parser::Loop::Until(guarded_block) => {
write!(w, "while (!(")?;
emit_expr(w, &guarded_block.guard, ctx)?;
write!(w, ")) {{")?;
let mut block_ctx = LexicalContext::new(ctx);
emit_all(w, &guarded_block.block, &mut block_ctx)?;
writeln!(w, "}}")?;
}
parser::Loop::Bare(block) => {
write!(w, "for(;;){{")?;
let mut block_ctx = LexicalContext::new(ctx);
emit_all(w, block, &mut block_ctx)?;
writeln!(w, "}}")?;
}
}
_ => todo!()
ctx.is_tail = is_tail;
}
}
Ok(())
@ -195,7 +212,11 @@ pub fn emit_expr(w: &mut dyn Write, expr: &parser::Expr, ctx: &mut LexicalContex
write!(w, " {} ", op)?;
emit_expr(w, e2.as_ref(), ctx)?;
}
_ => todo!(),
parser::Expr::UnaryMinus(e) => {
write!(w, "-(")?;
emit_expr(w, e.as_ref(), ctx)?;
write!(w, ")")?;
}
}
Ok(())
}