Repository navigation
feat(compiler): support bitwise shift operators (<< and >>) - #41
Merged
Merged
Conversation
- 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
approved these changes
Oct 25, 2026
sql-hkr
left a comment
Owner
There was a problem hiding this comment.
LGTM! Thank you for the contribution!
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 #38
Changes
<<and>>added to theOPpattern, ahead of<=/>=and the single-character operators.parse_shiftlevel between relational and additive, following C precedence:x << 2 + 1isx << 3,y << 1 < 7is(y << 1) < 7, and shifts are left-associative._fold_binaryfolds<<and>>on literals to 8-bit results (0x81 << 1gives2).gen_expr):LSL R16/LSR R16. A count of 8 or more loads0directly instead of emitting 8+ instructions (e.g.x << 200).CPI R17, 0/BREQ), shifts once, and doesDEC R17. A count of 0 leaves the value unchanged.>>usesLSR(logical), as the issue asks, so0x81 >> 7is1.Tests
New tests in
tests/test_compiler.pycompile, assemble and run programs on the CPU. They cover:>= 8short cutThe tests write their results to
0xC0and up, because the compiler allocates variables from0x60.pytest: 282 passed (276 before, plus 6)ruff checkandruff format --checkare clean on both changed files.ruff check .still reports the 2 issues that are already onmainin other files.ASRin place ofLSR>>fold>= 8short cutDEC R17