Refactored if statement AST

This commit is contained in:
2026-05-05 15:23:51 +02:00
parent f604b52384
commit 6125ecfde4
4 changed files with 36 additions and 26 deletions
+7 -10
View File
@@ -102,8 +102,12 @@ pub enum Statement {
VarDecl(VarDeclStmt),
VarAssign(VarAssignStmt),
If(IfStmt),
While(WhileStmt),
If {
initial: StatementBranch,
else_if: Vec<StatementBranch>,
else_branch: Option<Box<Statement>>,
},
While(StatementBranch),
For(ForStmt),
Return(Option<Expression>),
@@ -131,14 +135,7 @@ pub struct VarAssignStmt {
}
#[derive(Debug, Clone, Serialize)]
pub struct IfStmt {
pub condition: Expression,
pub then_branch: Box<Statement>,
pub else_branch: Option<Box<Statement>>,
}
#[derive(Debug, Clone, Serialize)]
pub struct WhileStmt {
pub struct StatementBranch {
pub condition: Expression,
pub body: Box<Statement>,
}
+8 -6
View File
@@ -308,18 +308,20 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
let else_branch = inner.next().map(Statement::from);
Statement::If(IfStmt {
condition,
then_branch: Box::new(then_branch),
else_branch: else_branch.map(Box::new),
})
Statement::Break
// Statement::If(IfStmt {
// condition,
// then_branch: Box::new(then_branch),
// else_branch: else_branch.map(Box::new),
// })
}
Rule::while_stmt => {
let condition = Expression::from(inner.next().unwrap());
let body = Statement::from(inner.next().unwrap());
Statement::While(WhileStmt {
Statement::While(StatementBranch {
condition,
body: Box::new(body),
})