Skip to content

feat: support bitwise operators (&, |, ^, <<, >>, ~) - #12

Merged
AdamHameed merged 1 commit into
mainfrom
feature/bitwise-operators
Jul 15, 2026
Merged

feat: support bitwise operators (&, |, ^, <<, >>, ~)#12
AdamHameed merged 1 commit into
mainfrom
feature/bitwise-operators

Conversation

@AdamHameed

Copy link
Copy Markdown
Owner

PR Description

Adds the bitwise operator family: binary &, |, ^, <<, >> and unary ~.

  • Lexer: Pipe, Caret, Tilde, LessLess, GreaterGreater tokens; a lone | is now a real token instead of a lex error, and </> grew one more character of lookahead.
  • Parser: full C precedence — shift sits between additive and relational, and below equality the chain is & > ^ > | > &&. This gets the classic C gotchas right: 4 & 3 == 3 is 4 & (3 == 3) and 1 << 3 > 7 is (1 << 3) > 7 (both covered by tests). Binary & coexists with address-of by parse position: x & &y parses as BitAnd(x, AddrOf(y)) (also tested).
  • Codegen: and/or/xor/shl and, notably, ashr for >> so right-shifting negative ints sign-extends like C (-8 >> 1 == -4, covered by a dedicated e2e test); ~ lowers to LLVM not.

37 unit + 44 e2e green; differentially verified against clang including a popcount program and a mixed-precedence expression torture case.

cc @hand-burger — that wraps the operator set: with #7-#12 the compiler now has comments, %, compound assignment, ++/--, break/continue, ?:, and bitwise ops on top of your function support. Natural next steps if you want to grab one: bitwise/shift compound assignments (&=, <<=, ...), else if-friendly diagnostics with source spans, or pointer arithmetic.

🤖 Generated with Claude Code

- New Pipe/Caret/Tilde/LessLess/GreaterGreater tokens; a single | is now
  valid instead of a lex error
- BinaryOp gains BitAnd/BitOr/BitXor/ShiftLeft/ShiftRight, UnaryOp gains
  BitNot, with C's precedence: shift sits between additive and
  relational, and equality > & > ^ > | > &&
- Binary & is disambiguated from address-of naturally by parse position;
  x & &y parses as BitAnd(x, AddrOf(y))
- >> lowers to an arithmetic shift (ashr) to match C's sign-preserving
  behavior on negative ints; ~ lowers to LLVM not
- Lexer/parser/precedence unit tests and e2e coverage including a
  popcount program; verified against clang

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@AdamHameed
AdamHameed merged commit 29ee70a into main Jul 15, 2026
1 check 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.

1 participant