Refactored if statement AST
This commit is contained in:
+7
-10
@@ -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
@@ -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),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user