Repository navigation
feat(compiler): support compound assignment operators - #42
Merged
sql-hkr merged 2 commits intoOct 25, 2026
Merged
Conversation
Add +=, -=, *=, /=, %=, &=, |= and ^= for scalar variables and array elements, including in for-loop steps. Scalars lower to the existing BinaryOp code, so each operator behaves exactly like its expanded form. Array index expressions are evaluated once, so side effects in the index are not repeated. <<= and >>= are not included because the compiler has no shift operators yet.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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.
Closes #37
What changed
+=,-=,*=,/=,%=,&=,|=and^=.CompoundAssignandArrayCompoundAssignnodes.forinitializers andforsteps (for examplei += 2).a op= eis lowered toa = a op ethrough the existingBinaryOpcode, so every operator behaves exactly like its expanded form. For arrays, the index is evaluated once into a hidden variable, soarr[f()] += 1does not callf()twice. This uses one extra byte of RAM per array compound assignment statement.Not included
<<=and>>=are left out: the compiler has no<<or>>operators yet, so they would need that feature first. Happy to follow up if you want shifts added.How I tested
tests/test_compiler.py: all eight operators on scalars, a compound right-hand side (a += b * 2), aforloop step, array elements, and an array index with a side effect that must run exactly once.uv run pytest: 281 passed (276 before this change plus the 5 new ones).ruff checkandruff format --checkonsrcandtests: clean.