Control flow

This commit is contained in:
2026-05-21 07:28:19 +02:00
parent 197afbfb35
commit 70e6eb6276
4 changed files with 19 additions and 8 deletions
+4
View File
@@ -5,5 +5,9 @@ void main() {
x++; x++;
if (1 == 1) {
println!("Yes!");
}
println!("hello, world {x}"); println!("hello, world {x}");
} }
+11 -1
View File
@@ -343,7 +343,8 @@ block = {
} }
statement = _{ statement = _{
(expr ~ semicolon) control_flow
| (expr ~ semicolon)
} }
// ------------------------------------------------------ // ------------------------------------------------------
@@ -377,6 +378,15 @@ var_decl_statement = {
// CONDITIONALS // CONDITIONALS
// ------------------------------------------------------ // ------------------------------------------------------
control_flow = {
if_stmt
| while_stmt
| c_for_stmt
| for_stmt
| match_stmt
| block
}
statement_branch = { "(" ~ expr ~ ")" ~ statement } statement_branch = { "(" ~ expr ~ ")" ~ statement }
else_if = { else_if = {
+3 -6
View File
@@ -78,12 +78,9 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
Rule::static_path => ast_expr!(Expression::Path(pair.try_into())), Rule::static_path => ast_expr!(Expression::Path(pair.try_into())),
Rule::literal => ast_expr!(Expression::Literal(pair.try_into())), Rule::literal => ast_expr!(Expression::Literal(pair.try_into())),
Rule::expr_path => ast_expr!(Expression::Path(pair.try_into())), Rule::expr_path => ast_expr!(Expression::Path(pair.try_into())),
Rule::statement => ast_expr!(Expression::Statement( Rule::statement | Rule::basic_stmt | Rule::control_flow => ast_expr!(
pair.try_into().get_map(Box::new).map(Box::new) Expression::Statement(pair.try_into().get_map(Box::new).map(Box::new))
)), ),
Rule::basic_stmt => ast_expr!(Expression::Statement(
pair.try_into().get_map(Box::new).map(Box::new)
)),
_ => AstError::bug_unimplemented(pair), _ => AstError::bug_unimplemented(pair),
} }
+1 -1
View File
@@ -39,7 +39,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
let mut inner = pair.clone().into_inner(); let mut inner = pair.clone().into_inner();
match rule { match rule {
Rule::statement | Rule::basic_stmt => Statement::try_from(inner.next().unwrap()), Rule::statement | Rule::basic_stmt | Rule::control_flow => Statement::try_from(inner.next().unwrap()),
Rule::block => ast_expr!(Statement::Block(pair.try_into())), Rule::block => ast_expr!(Statement::Block(pair.try_into())),