Improved for loops
This commit is contained in:
@@ -94,6 +94,7 @@ impl GenRust for Statement {
|
|||||||
Statement::While(StatementBranch { condition, body }) => {
|
Statement::While(StatementBranch { condition, body }) => {
|
||||||
cg.add("while ");
|
cg.add("while ");
|
||||||
condition.gen_rust(ctx, cg);
|
condition.gen_rust(ctx, cg);
|
||||||
|
cg.add(" ");
|
||||||
cg.ensure_brackets(ctx, body);
|
cg.ensure_brackets(ctx, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,17 +104,25 @@ impl GenRust for Statement {
|
|||||||
update,
|
update,
|
||||||
body,
|
body,
|
||||||
} => {
|
} => {
|
||||||
cg.add_indentedln("{");
|
cg.addln("{");
|
||||||
cg.indent += 1;
|
cg.indent += 1;
|
||||||
|
|
||||||
|
ctx.expr_ensure_semicolon = true;
|
||||||
|
|
||||||
|
cg.add_indented("");
|
||||||
|
|
||||||
init.gen_rust(ctx, cg);
|
init.gen_rust(ctx, cg);
|
||||||
|
|
||||||
cg.add("while ");
|
cg.addln("");
|
||||||
|
|
||||||
|
cg.add_indented("while ");
|
||||||
|
|
||||||
|
ctx.expr_ensure_semicolon = false;
|
||||||
condition.gen_rust(ctx, cg);
|
condition.gen_rust(ctx, cg);
|
||||||
|
|
||||||
cg.add("{");
|
cg.add(" ");
|
||||||
|
|
||||||
|
cg.add("{");
|
||||||
cg.indent += 1;
|
cg.indent += 1;
|
||||||
|
|
||||||
ctx.expr_ensure_semicolon = true;
|
ctx.expr_ensure_semicolon = true;
|
||||||
@@ -122,11 +131,13 @@ impl GenRust for Statement {
|
|||||||
|
|
||||||
update.gen_rust(ctx, cg);
|
update.gen_rust(ctx, cg);
|
||||||
|
|
||||||
|
cg.addln("");
|
||||||
|
|
||||||
cg.indent -= 1;
|
cg.indent -= 1;
|
||||||
cg.add_indentedln("}");
|
cg.add_indentedln("}");
|
||||||
|
|
||||||
cg.indent -= 1;
|
cg.indent -= 1;
|
||||||
cg.add_indentedln("}");
|
cg.add_indented("}");
|
||||||
}
|
}
|
||||||
|
|
||||||
Statement::For {
|
Statement::For {
|
||||||
|
|||||||
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -19,10 +19,10 @@ pub enum Statement {
|
|||||||
},
|
},
|
||||||
While(StatementBranch),
|
While(StatementBranch),
|
||||||
CStyleFor {
|
CStyleFor {
|
||||||
init: Box<Statement>,
|
init: Expression,
|
||||||
condition: Expression,
|
condition: Expression,
|
||||||
update: Box<Statement>,
|
update: Expression,
|
||||||
body: Box<Statement>,
|
body: Expression,
|
||||||
},
|
},
|
||||||
For {
|
For {
|
||||||
mutable: bool,
|
mutable: bool,
|
||||||
|
|||||||
@@ -405,11 +405,11 @@ while_stmt = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c_for_stmt = {
|
c_for_stmt = {
|
||||||
"for" ~ "(" ~ statement ~ expr ~ semicolon ~ statement ~ ")" ~ statement
|
"for" ~ "(" ~ statement ~ statement ~ expr ~ ")" ~ statement
|
||||||
}
|
}
|
||||||
|
|
||||||
for_stmt = {
|
for_stmt = {
|
||||||
"for" ~ "(" ~ mutable? ~ pattern ~ "in" ~ expr ~ ")" ~ statement
|
"for" ~ "(" ~ mutable? ~ pattern ~ ":" ~ expr ~ ")" ~ statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------
|
// ------------------------------------------------------
|
||||||
|
|||||||
@@ -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::while_stmt => ast_expr!(Statement::While(inner.next().unwrap().try_into())),
|
||||||
|
|
||||||
Rule::c_for_stmt => ast_expr!(Statement::CStyleFor {
|
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(),
|
condition: inner.next().unwrap().try_into(),
|
||||||
update: inner.next().unwrap().try_into().map(Box::new),
|
update: inner.next().unwrap().try_into(),
|
||||||
body: inner.next().unwrap().try_into().map(Box::new),
|
body: inner.next().unwrap().try_into(),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
Rule::for_stmt => ast_expr!(Statement::For {
|
Rule::for_stmt => ast_expr!(Statement::For {
|
||||||
|
|||||||
Reference in New Issue
Block a user