Skip to content

feat: add compound assignment operators (+=, -=, *=, /=, %=) - #18

Merged
zombocoder merged 2 commits into
zombocoder:mainfrom
Craftegy-NextGen:feature/compound-assignment
Oct 27, 2026
Merged

zombocoder merged 2 commits into
zombocoder:mainfrom
Craftegy-NextGen:feature/compound-assignment

Conversation

@sashml

@sashml sashml commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds the compound assignment operators +=, -=, *=, /= and %= for local variables and
this.property targets.

Design

  • Lexer: recognizes the two-character tokens +=, -=, *=, /=, %=. Only adjacent
    characters form the token, so + = and the existing single-character 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 O²L's left-to-right evaluation intact (a += 2 + 3 adds 5).
  • No interpreter or semantic changes: desugaring reuses the existing BinaryOpNode and assignment
    nodes.

Happy to adjust the syntax or semantics if you see the language differently, for example if you would
rather not support this.property targets.

Tests

  • tests/test_compound_assignment.cpp, 14 cases: lexer tokens, desugar shape for variables and
    this.property, each operator end to end, whole-expression right-hand side, loop accumulation, and
    plain assignment unchanged.
  • Runnable example examples/test_compound_assignment.obq.
  • CHANGELOG entry under [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.ConsecutiveOperators asserted the old
split; the second commit updates its two affected expectations (the token count is unchanged) and
explains why in the test.

Verification

  • Windows 10, MSVC 2022, Release, Ninja, GoogleTest from vcpkg: build OK, o2l_tests 523/523 passing.
  • Linux and macOS: covered by CI.

svaiml and others added 2 commits October 25, 2026 02:47
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>
@zombocoder
zombocoder merged commit 10b3f7a into zombocoder:main Oct 27, 2026
6 checks passed
@sashml
sashml deleted the feature/compound-assignment branch October 27, 2026 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants