Removed C style flow control

This commit is contained in:
2026-06-10 04:35:11 +02:00
parent 286894c5db
commit afd3d05e2f
10 changed files with 42 additions and 111 deletions
+5 -20
View File
@@ -10,32 +10,26 @@ pub struct Block {
pub soft_return: Option<Spanned<Expression>>,
}
#[derive(Debug, Clone, Serialize)]
pub enum StatementBody {
Statement(Expression),
Expression(Expression),
}
#[derive(Debug, Clone, Serialize)]
pub enum Statement {
Block(Block),
If {
initial: StatementBranch,
else_if: Vec<StatementBranch>,
else_branch: Option<StatementBody>,
else_branch: Option<Block>,
},
Loop(StatementBody),
Loop(Block),
While(StatementBranch),
CStyleFor {
init: Expression,
condition: Expression,
update: Expression,
body: StatementBody,
body: Block,
},
For {
pattern: Pattern,
iterator: Expression,
body: StatementBody,
body: Block,
},
Match(Expression, Vec<Spanned<MatchItem>>),
@@ -63,7 +57,7 @@ pub struct VarDeclStmt {
#[derive(Debug, Clone, Serialize)]
pub struct StatementBranch {
pub condition: Expression,
pub body: Box<StatementBody>,
pub body: Box<Block>,
}
impl Statement {
@@ -80,12 +74,3 @@ impl Statement {
}
}
}
impl StatementBody {
pub fn is_soft_return(&self) -> bool {
match self {
Self::Expression(_) => true,
_ => false,
}
}
}
+6 -10
View File
@@ -357,10 +357,6 @@ statement = _{
| (expr ~ semicolon)
}
statement_wrapper = { statement }
statement_body = { statement_wrapper | expr }
// ------------------------------------------------------
// BASIC STATEMENTS
// ------------------------------------------------------
@@ -402,7 +398,7 @@ control_flow = {
| block
}
statement_branch = { "(" ~ expr ~ ")" ~ statement_body }
statement_branch = { expr ~ block }
else_if = _{
"else" ~ "if" ~ statement_branch
@@ -413,7 +409,7 @@ else_if_list = {
}
if_stmt = {
"if" ~ statement_branch ~ else_if_list ~ ("else" ~ statement_body)?
"if" ~ statement_branch ~ else_if_list ~ ("else" ~ block)?
}
// ------------------------------------------------------
@@ -425,15 +421,15 @@ while_stmt = {
}
c_for_stmt = {
"for" ~ "(" ~ statement ~ statement ~ expr ~ ")" ~ statement_body
"for" ~ "(" ~ statement ~ statement ~ expr ~ ")" ~ block
}
for_stmt = {
"for" ~ "(" ~ pattern ~ ":" ~ expr ~ ")" ~ statement_body
"for" ~ pattern ~ "in" ~ expr ~ block
}
loop_stmt = {
"loop" ~ statement_body
"loop" ~ block
}
// ------------------------------------------------------
@@ -447,7 +443,7 @@ match_item = {
}
match_stmt = {
"match" ~ "(" ~ expr ~ ")" ~ "{" ~ match_item* ~ "}"
"match" ~ expr ~ "{" ~ match_item* ~ "}"
}
// ======================================================
-9
View File
@@ -100,15 +100,6 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
Rule::static_path => ast_expr!(Expression::Path(pair.try_into())),
Rule::literal => ast_expr!(Expression::Literal(pair.try_into())),
Rule::expr_path => ast_expr!(Expression::Path(pair.try_into())),
Rule::statement_wrapper => {
let i = inner.next().unwrap();
match i.as_rule() {
Rule::expr => i.try_into(),
_ => ast_expr!(Expression::Statement(
i.try_into().get_map(Box::new).map(Box::new)
)),
}
}
Rule::statement | Rule::basic_stmt | Rule::control_flow | Rule::block => ast_expr!(
Expression::Statement(pair.try_into().get_map(Box::new).map(Box::new))
),
+1 -19
View File
@@ -22,24 +22,6 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block {
}
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for StatementBody {
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.clone().into_inner();
ast_ensure!(pair, Rule::statement_body => {
let i = inner.next().unwrap();
match i.as_rule() {
Rule::expr => ast_expr!(StatementBody::Expression(i.try_into())),
Rule::statement_wrapper => ast_expr!(StatementBody::Statement(i.try_into())),
_ => AstError::bug_unimplemented(i),
}
})
}
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for StatementBranch {
type Error = AstError<'a, Self>;
@@ -85,7 +67,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
ast_expr!(Statement::If {
initial: inner.next().unwrap().try_into(),
else_if: collect_recovered(inner.next().unwrap().into_inner()),
else_branch: inner.next().map(StatementBody::try_from).transpose(),
else_branch: inner.next().map(Block::try_from).transpose(),
})
}