From b7cfa9fbe6b898babd5137d6f30716dbc7b8de4a Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Fri, 22 May 2026 02:52:22 +0200 Subject: [PATCH] Improved for loops --- codegen/src/statement.rs | 19 +++++++++++++++---- mist-test/src/app.mist | 14 ++++++++++++++ parser/src/ast/statement.rs | 6 +++--- parser/src/grammar.pest | 4 ++-- parser/src/parser/common/statement.rs | 6 +++--- 5 files changed, 37 insertions(+), 12 deletions(-) diff --git a/codegen/src/statement.rs b/codegen/src/statement.rs index 5ac4681..f4f18d3 100644 --- a/codegen/src/statement.rs +++ b/codegen/src/statement.rs @@ -94,6 +94,7 @@ impl GenRust for Statement { Statement::While(StatementBranch { condition, body }) => { cg.add("while "); condition.gen_rust(ctx, cg); + cg.add(" "); cg.ensure_brackets(ctx, body); } @@ -103,17 +104,25 @@ impl GenRust for Statement { update, body, } => { - cg.add_indentedln("{"); + cg.addln("{"); cg.indent += 1; + ctx.expr_ensure_semicolon = true; + + cg.add_indented(""); + init.gen_rust(ctx, cg); - cg.add("while "); + cg.addln(""); + cg.add_indented("while "); + + ctx.expr_ensure_semicolon = false; condition.gen_rust(ctx, cg); - cg.add("{"); + cg.add(" "); + cg.add("{"); cg.indent += 1; ctx.expr_ensure_semicolon = true; @@ -122,11 +131,13 @@ impl GenRust for Statement { update.gen_rust(ctx, cg); + cg.addln(""); + cg.indent -= 1; cg.add_indentedln("}"); cg.indent -= 1; - cg.add_indentedln("}"); + cg.add_indented("}"); } Statement::For { diff --git a/mist-test/src/app.mist b/mist-test/src/app.mist index 5c09745..f6af528 100644 --- a/mist-test/src/app.mist +++ b/mist-test/src/app.mist @@ -91,4 +91,18 @@ pub class Application { } } } + + pub void loops_test() { + for (x : 0..10) { + println!("{x}"); + } + + for (usize i = 0; i < 10; i++) { + println!("{i}"); + } + + while (true) { + println!("hi"); + } + } } \ No newline at end of file diff --git a/parser/src/ast/statement.rs b/parser/src/ast/statement.rs index a95429c..4ba6aa8 100644 --- a/parser/src/ast/statement.rs +++ b/parser/src/ast/statement.rs @@ -19,10 +19,10 @@ pub enum Statement { }, While(StatementBranch), CStyleFor { - init: Box, + init: Expression, condition: Expression, - update: Box, - body: Box, + update: Expression, + body: Expression, }, For { mutable: bool, diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index 8e03ebc..50b7127 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -405,11 +405,11 @@ while_stmt = { } c_for_stmt = { - "for" ~ "(" ~ statement ~ expr ~ semicolon ~ statement ~ ")" ~ statement + "for" ~ "(" ~ statement ~ statement ~ expr ~ ")" ~ statement } for_stmt = { - "for" ~ "(" ~ mutable? ~ pattern ~ "in" ~ expr ~ ")" ~ statement + "for" ~ "(" ~ mutable? ~ pattern ~ ":" ~ expr ~ ")" ~ statement } // ------------------------------------------------------ diff --git a/parser/src/parser/common/statement.rs b/parser/src/parser/common/statement.rs index 6c8bfa2..f532b39 100644 --- a/parser/src/parser/common/statement.rs +++ b/parser/src/parser/common/statement.rs @@ -75,10 +75,10 @@ impl<'a> TryFrom> for Statement { Rule::while_stmt => ast_expr!(Statement::While(inner.next().unwrap().try_into())), Rule::c_for_stmt => ast_expr!(Statement::CStyleFor { - init: inner.next().unwrap().try_into().map(Box::new), + init: inner.next().unwrap().try_into(), condition: inner.next().unwrap().try_into(), - update: inner.next().unwrap().try_into().map(Box::new), - body: inner.next().unwrap().try_into().map(Box::new), + update: inner.next().unwrap().try_into(), + body: inner.next().unwrap().try_into(), }), Rule::for_stmt => ast_expr!(Statement::For {