diff --git a/crates/codegen/src/class_decl.rs b/crates/codegen/src/class_decl.rs index ce0fd09..b53e7e9 100644 --- a/crates/codegen/src/class_decl.rs +++ b/crates/codegen/src/class_decl.rs @@ -362,7 +362,6 @@ impl ClassProcessedData { if *i > 0 { cg.add(", "); } - ctx.expr_ensure_semicolon = false; param.name.gen_rust(ctx, cg); } cg.addln(");"); @@ -599,7 +598,6 @@ pub fn gen_method_point(method: &FunctionDecl, ctx: &mut Context, cg: &mut RustC for (_, param) in ¶ms { cg.add(", "); - ctx.expr_ensure_semicolon = false; param.name.gen_rust(ctx, cg); } diff --git a/crates/codegen/src/expr.rs b/crates/codegen/src/expr.rs index 2a41a91..38f4e3b 100644 --- a/crates/codegen/src/expr.rs +++ b/crates/codegen/src/expr.rs @@ -68,14 +68,6 @@ impl GenRust for Literal { impl GenRust for Expression { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { - let ensure_semicolon = if ctx.expr_ensure_semicolon { - ctx.expr_ensure_semicolon = false; - - true - } else { - false - }; - match self { Expression::Path(path) => cg.add(&path.get_rust_ctx(ctx)), Expression::Literal(literal) => literal.gen_rust(ctx, cg), @@ -152,14 +144,6 @@ impl GenRust for Expression { } } } - - if ensure_semicolon { - ctx.expr_ensure_semicolon = true; - - if !self.is_block() { - cg.add(";"); - } - } } } diff --git a/crates/codegen/src/fmt/expr.rs b/crates/codegen/src/fmt/expr.rs index f787346..a73605b 100644 --- a/crates/codegen/src/fmt/expr.rs +++ b/crates/codegen/src/fmt/expr.rs @@ -4,13 +4,6 @@ use crate::fmt::{Context, GenMist, GetMist, MistCodegen}; impl GenMist for Expression { fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) { - let ensure_semicolon = if ctx.expr_ensure_semicolon { - ctx.expr_ensure_semicolon = false; - true - } else { - false - }; - match self { Expression::Path(path) => cg.add(&path.get_mist()), Expression::Literal(literal) => literal.gen_mist(ctx, cg), @@ -75,13 +68,6 @@ impl GenMist for Expression { } } } - - if ensure_semicolon { - ctx.expr_ensure_semicolon = true; - if !self.is_block() { - cg.add(";"); - } - } } } diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index 3e0610a..6642487 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -12,7 +12,6 @@ use mist_parser::{ }; pub struct Context { - pub expr_ensure_semicolon: bool, pub expr_super: Option, } @@ -81,7 +80,6 @@ impl RustCodegen { pub fn generate(&mut self, toplevels: Vec) -> String { let mut ctx = Context { - expr_ensure_semicolon: true, expr_super: None, }; diff --git a/crates/codegen/src/statement.rs b/crates/codegen/src/statement.rs index 98dfaf7..e428011 100644 --- a/crates/codegen/src/statement.rs +++ b/crates/codegen/src/statement.rs @@ -10,14 +10,15 @@ impl GenRust for Block { cg.indent += 1; for stmt in &self.statements { - ctx.expr_ensure_semicolon = true; cg.add_indented(""); stmt.gen_rust(ctx, cg); + if stmt.item.is_semicolon_required() { + cg.add(";"); + } cg.addln(""); } if let Some(soft_return) = &self.soft_return { - ctx.expr_ensure_semicolon = false; cg.add_indented(""); soft_return.gen_rust(ctx, cg); cg.addln(""); @@ -86,14 +87,12 @@ impl GenRust for Statement { else_branch, } => { cg.add("if "); - ctx.expr_ensure_semicolon = false; initial.condition.gen_rust(ctx, cg); cg.add(" "); initial.body.gen_rust(ctx, cg); for else_if_branch in else_if { cg.add(" else if "); - ctx.expr_ensure_semicolon = false; else_if_branch.condition.gen_rust(ctx, cg); cg.add(" "); else_if_branch.body.gen_rust(ctx, cg); @@ -126,17 +125,18 @@ impl GenRust for Statement { cg.addln("{"); cg.indent += 1; - ctx.expr_ensure_semicolon = true; - cg.add_indented(""); init.gen_rust(ctx, cg); + if init.is_semicolon_required() { + cg.add(";"); + } + cg.addln(""); cg.add_indented("while "); - ctx.expr_ensure_semicolon = false; condition.gen_rust(ctx, cg); cg.add(" "); @@ -144,12 +144,14 @@ impl GenRust for Statement { cg.add("{"); cg.indent += 1; - ctx.expr_ensure_semicolon = true; - body.gen_rust(ctx, cg); update.gen_rust(ctx, cg); + if update.is_semicolon_required() { + cg.add(";"); + } + cg.addln(""); cg.indent -= 1; diff --git a/crates/parser/src/ast/expr.rs b/crates/parser/src/ast/expr.rs index 10e7129..ad814c3 100644 --- a/crates/parser/src/ast/expr.rs +++ b/crates/parser/src/ast/expr.rs @@ -96,16 +96,6 @@ pub enum Generic { Type(TypeExpr), } -impl Expression { - pub fn is_block(&self) -> bool { - if let Expression::Statement(stmt) = self { - stmt.is_block() - } else { - false - } - } -} - impl From for ExprPath { fn from(path: Path) -> Self { Self( @@ -125,9 +115,17 @@ impl Expression { match self { Self::Statement(v) => match &**v { Statement::TopLevel(_) => false, - _ => true, + stmt => !stmt.is_block(), }, _ => true, } } + + pub fn is_block(&self) -> bool { + if let Expression::Statement(stmt) = self { + stmt.is_block() + } else { + false + } + } }