Repository navigation
feat: add compound assignment operators (+=, -=, *=, /=, %=) - #18
Merged
zombocoder merged 2 commits intoOct 27, 2026
Merged
Conversation
Lexer: recognize the two-character tokens +=, -=, *=, /=, %= (adjacent only, so spaced `+ =` and existing single operators are unaffected). Parser: compound assignment desugars at parse time: x op= expr -> x = x op (expr) this.p op= expr -> this.p = this.p op (expr) The right-hand side is parsed as one complete expression and used as-is as the right operand, which keeps O2L's intentional left-to-right evaluation intact. Desugaring reuses the existing BinaryOpNode and assignment nodes, so no interpreter or semantic changes are needed. Tests: tests/test_compound_assignment.cpp (lexer tokens, desugar shape for variables and this.property, and end-to-end arithmetic/loop/ accumulator behavior) plus examples/test_compound_assignment.obq. Signed-off-by: sashml <sashml@users.noreply.github.com> Co-authored-by: sashml <sashml@users.noreply.github.com>
With the compound assignment tokens, "**==" lexes as '*', '*=', '=' (the second '*' takes the following '=' as '*=', longest match, as in C, C++ and Java) instead of '*', '*', '=='. The token count stays at 11; update the two affected expectations and explain why in the test. Signed-off-by: sashml <sashml@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds the compound assignment operators
+=,-=,*=,/=and%=for local variables andthis.propertytargets.Design
+=,-=,*=,/=,%=. Only adjacentcharacters form the token, so
+ =and the existing single-character operators are unaffected.keeps O²L's left-to-right evaluation intact (
a += 2 + 3adds 5).BinaryOpNodeand assignmentnodes.
Happy to adjust the syntax or semantics if you see the language differently, for example if you would
rather not support
this.propertytargets.Tests
tests/test_compound_assignment.cpp, 14 cases: lexer tokens, desugar shape for variables andthis.property, each operator end to end, whole-expression right-hand side, loop accumulation, andplain assignment unchanged.
examples/test_compound_assignment.obq.[Unreleased],Added.Note: one lexer expectation changes
Multi-character operators lex by longest match, so a run like
**==now tokenizes as*,*=,=(as in C, C++ and Java) instead of
*,*,==.LexerTest.ConsecutiveOperatorsasserted the oldsplit; the second commit updates its two affected expectations (the token count is unchanged) and
explains why in the test.
Verification
o2l_tests523/523 passing.