feat: support the ternary conditional operator (?:) - #11
Merged
Conversation
- Question/Colon tokens and a ConditionalExpr AST node - Parsed above logical-or with C's right associativity (a ? b : c ? d : e groups as a ? b : (c ? d : e)) - Codegen branches lazily and merges with a phi, mirroring the existing short-circuit lowering; branch types must match - Also removes a stray clippy allow attribute left on LoopContext - Parser unit test plus e2e tests covering chaining, laziness (untaken division by zero), and use with calls; 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 ternary conditional operator
cond ? a : b.Question/Colontokens and aConditionalExprnode.logical-orasparse_conditional, with C's right associativity —a ? b : c ? d : eparses asa ? b : (c ? d : e), which makes chained selection ladders work (see theclampexample below).&&/||short-circuit lowering (including re-fetching the insert block for phi incoming edges). Branch types are checked to match.Tests include an e2e case proving laziness (
0 ? 5 / zero : 7returns 7 rather than trapping on the division) and right-associative chaining. 34 unit + 40 e2e green; differentially verified against clang with aclamp(v, lo, hi)built from nested ternaries.Also removes a stray
#[allow(clippy::too_many_arguments)]that landed onLoopContextin #10.cc @hand-burger
🤖 Generated with Claude Code