Refactored if statement AST
This commit is contained in:
+7
-10
@@ -102,8 +102,12 @@ pub enum Statement {
|
|||||||
|
|
||||||
VarDecl(VarDeclStmt),
|
VarDecl(VarDeclStmt),
|
||||||
VarAssign(VarAssignStmt),
|
VarAssign(VarAssignStmt),
|
||||||
If(IfStmt),
|
If {
|
||||||
While(WhileStmt),
|
initial: StatementBranch,
|
||||||
|
else_if: Vec<StatementBranch>,
|
||||||
|
else_branch: Option<Box<Statement>>,
|
||||||
|
},
|
||||||
|
While(StatementBranch),
|
||||||
For(ForStmt),
|
For(ForStmt),
|
||||||
|
|
||||||
Return(Option<Expression>),
|
Return(Option<Expression>),
|
||||||
@@ -131,14 +135,7 @@ pub struct VarAssignStmt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub struct IfStmt {
|
pub struct StatementBranch {
|
||||||
pub condition: Expression,
|
|
||||||
pub then_branch: Box<Statement>,
|
|
||||||
pub else_branch: Option<Box<Statement>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
|
||||||
pub struct WhileStmt {
|
|
||||||
pub condition: Expression,
|
pub condition: Expression,
|
||||||
pub body: Box<Statement>,
|
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);
|
let else_branch = inner.next().map(Statement::from);
|
||||||
|
|
||||||
Statement::If(IfStmt {
|
Statement::Break
|
||||||
condition,
|
|
||||||
then_branch: Box::new(then_branch),
|
// Statement::If(IfStmt {
|
||||||
else_branch: else_branch.map(Box::new),
|
// condition,
|
||||||
})
|
// then_branch: Box::new(then_branch),
|
||||||
|
// else_branch: else_branch.map(Box::new),
|
||||||
|
// })
|
||||||
}
|
}
|
||||||
|
|
||||||
Rule::while_stmt => {
|
Rule::while_stmt => {
|
||||||
let condition = Expression::from(inner.next().unwrap());
|
let condition = Expression::from(inner.next().unwrap());
|
||||||
let body = Statement::from(inner.next().unwrap());
|
let body = Statement::from(inner.next().unwrap());
|
||||||
|
|
||||||
Statement::While(WhileStmt {
|
Statement::While(StatementBranch {
|
||||||
condition,
|
condition,
|
||||||
body: Box::new(body),
|
body: Box::new(body),
|
||||||
})
|
})
|
||||||
|
|||||||
+15
-10
@@ -1,7 +1,7 @@
|
|||||||
use parser::ast::{
|
use parser::ast::{
|
||||||
Attribute, BinaryOp, Block, Expression, IfStmt, Literal, Path, Postfix, Prefix, Statement,
|
Attribute, BinaryOp, Block, Expression, Literal, Path, Postfix, Prefix, Statement,
|
||||||
TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl,
|
StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt,
|
||||||
VarDeclStmt, WhileStmt,
|
VarDecl, VarDeclStmt,
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -353,13 +353,18 @@ impl ToRust for Statement {
|
|||||||
cg.add_indentedln(&format!("{} = {};", target.get_rust(), value.get_rust(),));
|
cg.add_indentedln(&format!("{} = {};", target.get_rust(), value.get_rust(),));
|
||||||
}
|
}
|
||||||
|
|
||||||
Statement::If(IfStmt {
|
Statement::If {
|
||||||
condition,
|
initial,
|
||||||
then_branch,
|
else_if,
|
||||||
else_branch,
|
else_branch,
|
||||||
}) => {
|
} => {
|
||||||
cg.add_indentedln(&format!("if {}", condition.get_rust()));
|
cg.add_indentedln(&format!("if {}", initial.condition.get_rust()));
|
||||||
then_branch.to_rust(cg);
|
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 {
|
if let Some(else_br) = else_branch {
|
||||||
cg.add_indentedln("else");
|
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.add_indentedln(&format!("while {} {{", condition.get_rust()));
|
||||||
cg.indent += 1;
|
cg.indent += 1;
|
||||||
body.to_rust(cg);
|
body.to_rust(cg);
|
||||||
|
|||||||
@@ -11,6 +11,12 @@ void main() {
|
|||||||
|
|
||||||
if (call.x == 20) {
|
if (call.x == 20) {
|
||||||
println("= 20");
|
println("= 20");
|
||||||
|
} else if (call.x == 10) {
|
||||||
|
println("= 10");
|
||||||
|
} else if (call.x == 5) {
|
||||||
|
println("= 5");
|
||||||
|
} else {
|
||||||
|
println("= unknown");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("{call:?}");
|
println!("{call:?}");
|
||||||
|
|||||||
Reference in New Issue
Block a user