Compound assign statement

This commit is contained in:
2026-05-16 22:40:01 +02:00
parent 60e5995dc6
commit c9030809fd
5 changed files with 52 additions and 8 deletions
+9 -1
View File
@@ -181,7 +181,15 @@ pub enum Statement {
Block(Block),
VarDecl(VarDeclStmt),
VarAssign(VarAssignStmt),
Assign {
target: Expression,
value: Expression,
},
CompoundAssign {
target: Expression,
compound: String,
value: Expression,
},
If {
initial: StatementBranch,
else_if: Vec<StatementBranch>,
+17
View File
@@ -314,6 +314,7 @@ statement = _{
expr_stmt
| match_stmt
| assign_statement
| compound_assign_statement
| if_stmt
| while_stmt
| c_for_stmt
@@ -353,6 +354,22 @@ assign_statement = {
expr ~ "=" ~ expr ~ semicolon
}
compound_assign = {
"+="
| "-="
| "/="
| "%="
| "&="
| "|="
| "^="
| "<<="
| ">>="
}
compound_assign_statement = {
expr ~ compound_assign ~ expr ~ semicolon
}
// ------------------------------------------------------
// CONDITIONALS
// ------------------------------------------------------
+11 -5
View File
@@ -88,13 +88,10 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
body: inner.next().unwrap().try_into().map(Box::new),
}),
Rule::assign_statement => ast_expr!(VarAssignStmt {
Rule::assign_statement => ast_expr!(Statement::Assign {
target: inner.next().unwrap().try_into(),
value: inner.next().unwrap().try_into(),
})
.map(Statement::VarAssign)
.get_map(Statement::VarAssign),
}),
Rule::match_stmt => ast_expr!(Statement::Match(
inner.next().unwrap().try_into(),
inner
@@ -108,6 +105,15 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
.collect::<AstResult<'a, Vec<_>>>(),
)),
Rule::compound_assign_statement => {
ast_expr!(Statement::CompoundAssign {
target: inner.next().unwrap().try_into(),
compound: Ok(inner.next().unwrap().as_str().trim().to_string())
as AstResult<'_, String>,
value: inner.next().unwrap().try_into(),
})
}
Rule::unexpected_statement => {
return Err(AstError {
span: pair.as_span(),