Improved for loops

This commit is contained in:
2026-05-22 02:52:22 +02:00
parent 960cc40886
commit b7cfa9fbe6
5 changed files with 37 additions and 12 deletions
+15 -4
View File
@@ -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 {
+14
View File
@@ -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");
}
}
}
+3 -3
View File
@@ -19,10 +19,10 @@ pub enum Statement {
},
While(StatementBranch),
CStyleFor {
init: Box<Statement>,
init: Expression,
condition: Expression,
update: Box<Statement>,
body: Box<Statement>,
update: Expression,
body: Expression,
},
For {
mutable: bool,
+2 -2
View File
@@ -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
}
// ------------------------------------------------------
+3 -3
View File
@@ -75,10 +75,10 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> 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 {