From c345ac4fe58e95244096ae6396f46cc3bfc9e47b Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Mon, 18 Nov 2024 21:16:02 -0600 Subject: [PATCH] Finish emitter --- src/emitter.rs | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/src/emitter.rs b/src/emitter.rs index 055f563..8644259 100644 --- a/src/emitter.rs +++ b/src/emitter.rs @@ -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(()) }