From f21cd5a9277bf1627694459cd92cb217e5bab3ff Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Sun, 24 May 2026 14:43:09 +0200 Subject: [PATCH] Fixed statement bodies --- crates/codegen/src/expr.rs | 2 +- crates/codegen/src/lib.rs | 10 +++++++--- crates/codegen/src/statement.rs | 2 +- crates/parser/src/ast/expr.rs | 4 ++-- crates/parser/src/ast/statement.rs | 29 ++++++++--------------------- mist-test/src/main.mist | 2 +- 6 files changed, 20 insertions(+), 29 deletions(-) diff --git a/crates/codegen/src/expr.rs b/crates/codegen/src/expr.rs index 0eddd32..c6fa873 100644 --- a/crates/codegen/src/expr.rs +++ b/crates/codegen/src/expr.rs @@ -103,7 +103,7 @@ impl GenRust for Expression { if ensure_semicolon { ctx.expr_ensure_semicolon = true; - if !self.is_soft_return() { + if !self.is_block() { cg.add(";"); } } diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index 951f928..ff34768 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -95,10 +95,14 @@ impl RustCodegen { } } - pub fn ensure_brackets_body(&mut self, ctx: &mut Context, expr: &StatementBody) { - match expr { - StatementBody::Statement(expr) => self.ensure_brackets_expr(ctx, expr), + pub fn ensure_brackets_body(&mut self, ctx: &mut Context, body: &StatementBody) { + match body { StatementBody::Expression(expr) => self.ensure_brackets_expr(ctx, expr), + + StatementBody::Statement(expr) => { + ctx.expr_ensure_semicolon = true; + self.ensure_brackets_expr(ctx, expr); + } } } } diff --git a/crates/codegen/src/statement.rs b/crates/codegen/src/statement.rs index f98bc0e..e55bf4c 100644 --- a/crates/codegen/src/statement.rs +++ b/crates/codegen/src/statement.rs @@ -31,8 +31,8 @@ impl GenRust for StatementBody { match self { Self::Expression(expr) => expr.gen_rust(ctx, cg), Self::Statement(stmt) => { + ctx.expr_ensure_semicolon = true; stmt.gen_rust(ctx, cg); - cg.add(";"); } } } diff --git a/crates/parser/src/ast/expr.rs b/crates/parser/src/ast/expr.rs index c3cc4de..1c6eb6b 100644 --- a/crates/parser/src/ast/expr.rs +++ b/crates/parser/src/ast/expr.rs @@ -84,9 +84,9 @@ pub enum Generic { } impl Expression { - pub fn is_soft_return(&self) -> bool { + pub fn is_block(&self) -> bool { if let Expression::Statement(stmt) = self { - stmt.is_soft_return() + stmt.is_block() } else { false } diff --git a/crates/parser/src/ast/statement.rs b/crates/parser/src/ast/statement.rs index e7fbc21..d4102ea 100644 --- a/crates/parser/src/ast/statement.rs +++ b/crates/parser/src/ast/statement.rs @@ -64,28 +64,15 @@ pub struct StatementBranch { } impl Statement { - pub fn is_soft_return(&self) -> bool { + pub fn is_block(&self) -> bool { match self { - Self::Block(_) | Self::Match(_, _) => true, - Self::While(branch) => branch.body.is_soft_return(), - Self::For { body, .. } | Self::Loop(body) | Self::CStyleFor { body, .. } => { - body.is_soft_return() - } - Self::If { - initial, - else_if, - else_branch, - } => { - else_branch - .as_ref() - .map(|v| v.is_soft_return()) - .unwrap_or_default() - || else_if - .last() - .map(|b| b.body.is_soft_return()) - .unwrap_or_default() - || initial.body.is_soft_return() - } + Self::Block(_) + | Self::Match(_, _) + | Self::While(_) + | Self::For { .. } + | Self::Loop(..) + | Self::CStyleFor { .. } + | Self::If { .. } => true, _ => false, } } diff --git a/mist-test/src/main.mist b/mist-test/src/main.mist index 47de144..59ecadb 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -14,7 +14,7 @@ void main() { void do_stuff() { vec!(1, 2, 3).into_iter().map(Option |var v| {Some(v)}).collect::>(); - if (43=69) testing1(); + if (43==69) testing1(); } i32 testing1() {