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),
|
||||
})
|
||||
|
||||
+15
-10
@@ -1,7 +1,7 @@
|
||||
use parser::ast::{
|
||||
Attribute, BinaryOp, Block, Expression, IfStmt, Literal, Path, Postfix, Prefix, Statement,
|
||||
TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl,
|
||||
VarDeclStmt, WhileStmt,
|
||||
Attribute, BinaryOp, Block, Expression, Literal, Path, Postfix, Prefix, Statement,
|
||||
StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt,
|
||||
VarDecl, VarDeclStmt,
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -353,13 +353,18 @@ impl ToRust for Statement {
|
||||
cg.add_indentedln(&format!("{} = {};", target.get_rust(), value.get_rust(),));
|
||||
}
|
||||
|
||||
Statement::If(IfStmt {
|
||||
condition,
|
||||
then_branch,
|
||||
Statement::If {
|
||||
initial,
|
||||
else_if,
|
||||
else_branch,
|
||||
}) => {
|
||||
cg.add_indentedln(&format!("if {}", condition.get_rust()));
|
||||
then_branch.to_rust(cg);
|
||||
} => {
|
||||
cg.add_indentedln(&format!("if {}", initial.condition.get_rust()));
|
||||
initial.body.to_rust(cg);
|
||||
|
||||
for else_if_branch in else_if {
|
||||
cg.add_indentedln(&format!("else if {}", else_if_branch.condition.get_rust()));
|
||||
else_if_branch.body.to_rust(cg);
|
||||
}
|
||||
|
||||
if let Some(else_br) = else_branch {
|
||||
cg.add_indentedln("else");
|
||||
@@ -367,7 +372,7 @@ impl ToRust for Statement {
|
||||
}
|
||||
}
|
||||
|
||||
Statement::While(WhileStmt { condition, body }) => {
|
||||
Statement::While(StatementBranch { condition, body }) => {
|
||||
cg.add_indentedln(&format!("while {} {{", condition.get_rust()));
|
||||
cg.indent += 1;
|
||||
body.to_rust(cg);
|
||||
|
||||
@@ -11,6 +11,12 @@ void main() {
|
||||
|
||||
if (call.x == 20) {
|
||||
println("= 20");
|
||||
} else if (call.x == 10) {
|
||||
println("= 10");
|
||||
} else if (call.x == 5) {
|
||||
println("= 5");
|
||||
} else {
|
||||
println("= unknown");
|
||||
}
|
||||
|
||||
println!("{call:?}");
|
||||
|
||||
Reference in New Issue
Block a user