Improved loop bodies

This commit is contained in:
2026-05-22 03:25:05 +02:00
parent a26c1c31fb
commit c5c5e885bf
6 changed files with 51 additions and 21 deletions
+2 -6
View File
@@ -100,14 +100,10 @@ impl GenRust for Expression {
} }
} }
if let Expression::Statement(stmt) = self {
if !stmt.is_block() && ensure_semicolon {
ctx.expr_ensure_semicolon = true;
cg.add(";");
}
} else {
if ensure_semicolon { if ensure_semicolon {
ctx.expr_ensure_semicolon = true; ctx.expr_ensure_semicolon = true;
if !self.is_block() {
cg.add(";"); cg.add(";");
} }
} }
+5 -5
View File
@@ -76,18 +76,18 @@ impl GenRust for Statement {
cg.add("if "); cg.add("if ");
initial.condition.gen_rust(ctx, cg); initial.condition.gen_rust(ctx, cg);
cg.add(" "); cg.add(" ");
cg.ensure_brackets(ctx, &initial.body); cg.ensure_brackets_expr(ctx, &initial.body);
for else_if_branch in else_if { for else_if_branch in else_if {
cg.add("else if"); cg.add("else if");
else_if_branch.condition.gen_rust(ctx, cg); else_if_branch.condition.gen_rust(ctx, cg);
cg.add(" "); cg.add(" ");
cg.ensure_brackets(ctx, &else_if_branch.body); cg.ensure_brackets_expr(ctx, &else_if_branch.body);
} }
if let Some(else_br) = else_branch { if let Some(else_br) = else_branch {
cg.add(" else "); cg.add(" else ");
cg.ensure_brackets(ctx, else_br); cg.ensure_brackets_expr(ctx, else_br);
} }
} }
@@ -95,7 +95,7 @@ impl GenRust for Statement {
cg.add("while "); cg.add("while ");
condition.gen_rust(ctx, cg); condition.gen_rust(ctx, cg);
cg.add(" "); cg.add(" ");
cg.ensure_brackets(ctx, body); cg.ensure_brackets_expr(ctx, body);
} }
Statement::Loop(body) => { Statement::Loop(body) => {
@@ -156,7 +156,7 @@ impl GenRust for Statement {
pattern.gen_rust(ctx, cg); pattern.gen_rust(ctx, cg);
cg.add(" in "); cg.add(" in ");
iterator.gen_rust(ctx, cg); iterator.gen_rust(ctx, cg);
cg.ensure_brackets(ctx, body); cg.ensure_brackets_expr(ctx, body);
} }
Statement::Return(expr) => { Statement::Return(expr) => {
+10 -1
View File
@@ -92,7 +92,7 @@ pub class Application {
} }
} }
pub void loops_test() { pub i32 loops_test() {
for (x : 0..10) { for (x : 0..10) {
println!("{x}"); println!("{x}");
} }
@@ -110,5 +110,14 @@ pub class Application {
loop { loop {
println!("loop!"); println!("loop!");
} }
if (x == 0) bro();
else bro();
0
} }
} }
pub i32 bro() {
0
}
+10
View File
@@ -81,3 +81,13 @@ pub enum Generic {
Lifetime(Identifier), Lifetime(Identifier),
Type(TypeExpr), Type(TypeExpr),
} }
impl Expression {
pub fn is_block(&self) -> bool {
if let Expression::Statement(stmt) = self {
stmt.is_block()
} else {
false
}
}
}
+20 -5
View File
@@ -14,7 +14,7 @@ pub enum Statement {
If { If {
initial: StatementBranch, initial: StatementBranch,
else_if: Vec<StatementBranch>, else_if: Vec<StatementBranch>,
else_branch: Option<Box<Statement>>, else_branch: Option<Box<Expression>>,
}, },
Loop(Expression), Loop(Expression),
While(StatementBranch), While(StatementBranch),
@@ -28,7 +28,7 @@ pub enum Statement {
mutable: bool, mutable: bool,
pattern: Pattern, pattern: Pattern,
iterator: Expression, iterator: Expression,
body: Box<Statement>, body: Box<Expression>,
}, },
Match(Expression, Vec<(Vec<Pattern>, Expression)>), Match(Expression, Vec<(Vec<Pattern>, Expression)>),
@@ -54,14 +54,29 @@ pub struct VarDeclStmt {
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct StatementBranch { pub struct StatementBranch {
pub condition: Expression, pub condition: Expression,
pub body: Box<Statement>, pub body: Box<Expression>,
} }
impl Statement { impl Statement {
pub fn is_block(&self) -> bool { pub fn is_block(&self) -> bool {
match self { match self {
Self::VarDecl(_) | Self::Return(_) | Self::Break | Self::Continue => false, Self::Block(_) => true,
_ => true, Self::If {
initial,
else_if,
else_branch,
} => {
else_branch
.as_ref()
.map(|v| v.is_block())
.unwrap_or_default()
|| else_if
.last()
.map(|b| b.body.is_block())
.unwrap_or_default()
|| initial.body.is_block()
}
_ => false,
} }
} }
} }
+1 -1
View File
@@ -65,7 +65,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
else_if: collect_recovered(inner.next().unwrap().into_inner()), else_if: collect_recovered(inner.next().unwrap().into_inner()),
else_branch: inner else_branch: inner
.next() .next()
.map(Statement::try_from) .map(Expression::try_from)
.transpose() .transpose()
.map(|v| v.map(Box::new)) .map(|v| v.map(Box::new))
.get_map(|v| { Some(Box::new(v)) }), .get_map(|v| { Some(Box::new(v)) }),