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
+10
View File
@@ -81,3 +81,13 @@ pub enum Generic {
Lifetime(Identifier),
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 {
initial: StatementBranch,
else_if: Vec<StatementBranch>,
else_branch: Option<Box<Statement>>,
else_branch: Option<Box<Expression>>,
},
Loop(Expression),
While(StatementBranch),
@@ -28,7 +28,7 @@ pub enum Statement {
mutable: bool,
pattern: Pattern,
iterator: Expression,
body: Box<Statement>,
body: Box<Expression>,
},
Match(Expression, Vec<(Vec<Pattern>, Expression)>),
@@ -54,14 +54,29 @@ pub struct VarDeclStmt {
#[derive(Debug, Clone, Serialize)]
pub struct StatementBranch {
pub condition: Expression,
pub body: Box<Statement>,
pub body: Box<Expression>,
}
impl Statement {
pub fn is_block(&self) -> bool {
match self {
Self::VarDecl(_) | Self::Return(_) | Self::Break | Self::Continue => false,
_ => true,
Self::Block(_) => 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_branch: inner
.next()
.map(Statement::try_from)
.map(Expression::try_from)
.transpose()
.map(|v| v.map(Box::new))
.get_map(|v| { Some(Box::new(v)) }),