feat: support bitwise operators (&, |, ^, <<, >>, ~) - #12
Merged
Conversation
- 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>
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.
PR Description
Adds the bitwise operator family: binary
&,|,^,<<,>>and unary~.Pipe,Caret,Tilde,LessLess,GreaterGreatertokens; a lone|is now a real token instead of a lex error, and</>grew one more character of lookahead.&>^>|>&&. This gets the classic C gotchas right:4 & 3 == 3is4 & (3 == 3)and1 << 3 > 7is(1 << 3) > 7(both covered by tests). Binary&coexists with address-of by parse position:x & &yparses asBitAnd(x, AddrOf(y))(also tested).and/or/xor/shland, notably,ashrfor>>so right-shifting negative ints sign-extends like C (-8 >> 1 == -4, covered by a dedicated e2e test);~lowers to LLVMnot.37 unit + 44 e2e green; differentially verified against clang including a
popcountprogram 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