Skip to content

feat(compiler): support compound assignment operators - #42

Merged
sql-hkr merged 2 commits into
sql-hkr:mainfrom
eyadstein:feat/compound-assignment-operators
Oct 25, 2026
Merged

sql-hkr merged 2 commits into
sql-hkr:mainfrom
eyadstein:feat/compound-assignment-operators

Conversation

@eyadstein

Copy link
Copy Markdown
Contributor

Closes #37

What changed

  • Lexer: the operator pattern now also matches +=, -=, *=, /=, %=, &=, |= and ^=.
  • AST: new CompoundAssign and ArrayCompoundAssign nodes.
  • Parser: recognized in statements, for initializers and for steps (for example i += 2).
  • Code generation: a op= e is lowered to a = a op e through the existing BinaryOp code, so every operator behaves exactly like its expanded form. For arrays, the index is evaluated once into a hidden variable, so arr[f()] += 1 does not call f() 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

  • Five new tests in tests/test_compiler.py: all eight operators on scalars, a compound right-hand side (a += b * 2), a for loop 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 check and ruff format --check on src and tests: clean.
  • Run locally on Windows with Python 3.13 only. The 3.11 and 3.12 jobs in CI are the check for the other versions.

eyadstein and others added 2 commits October 25, 2026 05:21
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.

@sql-hkr sql-hkr left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Excellent implementation. Resolved the minor conflict with #41. Thank you for the contribution!

@codecov

codecov Bot commented Oct 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.09524% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/tiny8/compiler.py 88.09% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@sql-hkr
sql-hkr merged commit 6b19677 into sql-hkr:main Oct 25, 2026
5 checks passed
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.

feat(compiler): support compound assignment operators (+=, -=, *=, etc.)

2 participants