Skip to content

feat(compiler): support bitwise shift operators (<< and >>) - #41

Merged
sql-hkr merged 1 commit into
sql-hkr:mainfrom
am-official:feat/shift-operators
Oct 25, 2026
Merged

sql-hkr merged 1 commit into
sql-hkr:mainfrom
am-official:feat/shift-operators

Conversation

@am-official

Copy link
Copy Markdown
Contributor

Closes #38

Changes

  • Lexer: << and >> added to the OP pattern, ahead of <=/>= and the single-character operators.
  • Parser: a new parse_shift level between relational and additive, following C precedence: x << 2 + 1 is x << 3, y << 1 < 7 is (y << 1) < 7, and shifts are left-associative.
  • Constant folding: _fold_binary folds << and >> on literals to 8-bit results (0x81 << 1 gives 2).
  • Code generation (gen_expr):
    • Literal count: unrolled LSL R16 / LSR R16. A count of 8 or more loads 0 directly instead of emitting 8+ instructions (e.g. x << 200).
    • Variable count: a small loop that tests R17 (CPI R17, 0 / BREQ), shifts once, and does DEC R17. A count of 0 leaves the value unchanged.
    • >> uses LSR (logical), as the issue asks, so 0x81 >> 7 is 1.
  • README: shifts listed among the C features.

Tests

New tests in tests/test_compiler.py compile, assemble and run programs on the CPU. They cover:

  • literal shifts, including 0, 7, and 8 or more
  • variable shifts, including counts of 0 and 8
  • precedence and associativity
  • constant folding
  • the unrolled output and the >= 8 short cut

The tests write their results to 0xC0 and up, because the compiler allocates variables from 0x60.

  • pytest: 282 passed (276 before, plus 6)
  • ruff check and ruff format --check are clean on both changed files. ruff check . still reports the 2 issues that are already on main in other files.
  • Mutation checks: each of these makes tests fail:
    • ASR in place of LSR
    • dropping the new precedence level
    • a wrong >> fold
    • removing the >= 8 short cut
    • dropping DEC R17

- Lexer: add `<<` and `>>` to the OP token pattern, ahead of `<=`/`>=`
  and the single-character operators.
- Parser: new parse_shift level between relational and additive, so
  `x << 2 + 1` is `x << 3` and `y << 1 < 7` is `(y << 1) < 7`, as in C.
- Constant folding: fold `<<` and `>>` on literals to 8-bit results.
- Code generation: a literal shift count unrolls into LSL/LSR R16, and a
  count of 8 or more loads 0 instead of emitting the instructions. A
  variable count runs a small loop that shifts once and decrements R17
  until it reaches 0. `>>` is a logical shift (LSR), so 0x81 >> 7 is 1.
- Tests for literal and variable counts, zero and >= 8 counts,
  precedence and associativity, folding, and the unrolled output.
- README: list the shift operators among the C features.

Closes sql-hkr#38

@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! Thank you for the contribution!

@codecov

codecov Bot commented Oct 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.22222% with 1 line in your changes missing coverage. Please review.

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

📢 Thoughts on this report? Let us know!

@sql-hkr
sql-hkr merged commit 7f6c3d9 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 bitwise shift operators (<< and >>)

2 participants