Refactor: If statement
This commit is contained in:
@@ -86,8 +86,12 @@ var_decl_statement = { var_decl ~ ("=" ~ expr)? ~ ";" }
|
|||||||
|
|
||||||
assign_statement = { expr ~ "=" ~ expr ~ ";" }
|
assign_statement = { expr ~ "=" ~ expr ~ ";" }
|
||||||
|
|
||||||
|
else_if = { "else" ~ "if" ~ "(" ~ expr ~ ")" ~ statement }
|
||||||
|
|
||||||
|
else_if_list = { else_if* }
|
||||||
|
|
||||||
if_stmt = {
|
if_stmt = {
|
||||||
"if" ~ "(" ~ expr ~ ")" ~ statement ~ ("else" ~ statement)?
|
"if" ~ "(" ~ expr ~ ")" ~ statement ~ else_if_list ~ ("else" ~ statement)?
|
||||||
}
|
}
|
||||||
|
|
||||||
while_stmt = {
|
while_stmt = {
|
||||||
|
|||||||
+26
-20
@@ -196,6 +196,20 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<pest::iterators::Pair<'_, Rule>> for StatementBranch {
|
||||||
|
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||||
|
let mut inner = pair.into_inner();
|
||||||
|
|
||||||
|
let condition = Expression::from(inner.next().unwrap());
|
||||||
|
let body = Statement::from(inner.next().unwrap());
|
||||||
|
|
||||||
|
StatementBranch {
|
||||||
|
condition,
|
||||||
|
body: Box::new(body),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
|
impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
|
||||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||||
let rule = pair.as_rule();
|
let rule = pair.as_rule();
|
||||||
@@ -303,29 +317,21 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
|||||||
Rule::continue_stmt => Statement::Continue,
|
Rule::continue_stmt => Statement::Continue,
|
||||||
|
|
||||||
Rule::if_stmt => {
|
Rule::if_stmt => {
|
||||||
let condition = Expression::from(inner.next().unwrap());
|
let mut inner = inner.skip(2);
|
||||||
let then_branch = Statement::from(inner.next().unwrap());
|
|
||||||
|
|
||||||
let else_branch = inner.next().map(Statement::from);
|
Statement::If {
|
||||||
|
initial: pair.into(),
|
||||||
Statement::Break
|
else_if: inner
|
||||||
|
.next()
|
||||||
// Statement::If(IfStmt {
|
.unwrap()
|
||||||
// condition,
|
.into_inner()
|
||||||
// then_branch: Box::new(then_branch),
|
.map(StatementBranch::from)
|
||||||
// else_branch: else_branch.map(Box::new),
|
.collect(),
|
||||||
// })
|
else_branch: inner.next().map(Statement::from).map(Box::new),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Rule::while_stmt => {
|
Rule::while_stmt => Statement::While(pair.into()),
|
||||||
let condition = Expression::from(inner.next().unwrap());
|
|
||||||
let body = Statement::from(inner.next().unwrap());
|
|
||||||
|
|
||||||
Statement::While(StatementBranch {
|
|
||||||
condition,
|
|
||||||
body: Box::new(body),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
Rule::assign_statement => Statement::VarAssign(VarAssignStmt {
|
Rule::assign_statement => Statement::VarAssign(VarAssignStmt {
|
||||||
target: Expression::from(inner.next().unwrap()),
|
target: Expression::from(inner.next().unwrap()),
|
||||||
|
|||||||
Reference in New Issue
Block a user