Skip to content

feat: support the ternary conditional operator (?:) - #11

Merged
AdamHameed merged 1 commit into
mainfrom
feature/ternary
Jul 15, 2026
Merged

feat: support the ternary conditional operator (?:)#11
AdamHameed merged 1 commit into
mainfrom
feature/ternary

Conversation

@AdamHameed

Copy link
Copy Markdown
Owner

PR Description

Adds the ternary conditional operator cond ? a : b.

  • Lexer/AST: Question/Colon tokens and a ConditionalExpr node.
  • Parser: sits above logical-or as parse_conditional, with C's right associativity — a ? b : c ? d : e parses as a ? b : (c ? d : e), which makes chained selection ladders work (see the clamp example below).
  • Codegen: lazy evaluation via conditional branches and a phi merge, following the same pattern as the &&/|| 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 : 7 returns 7 rather than trapping on the division) and right-associative chaining. 34 unit + 40 e2e green; differentially verified against clang with a clamp(v, lo, hi) built from nested ternaries.

Also removes a stray #[allow(clippy::too_many_arguments)] that landed on LoopContext in #10.

cc @hand-burger

🤖 Generated with Claude Code

- 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>
@AdamHameed
AdamHameed merged commit d108b61 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