Improved loop bodies
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)) }),
|
||||
|
||||
Reference in New Issue
Block a user