Skip to content

C compiler stage 6: if/else and the conditional expression (task C6) - #165

Merged
prozak merged 1 commit into
masterfrom
claude/c-compiler-stage6-conditionals
Jul 3, 2026
Merged

prozak merged 1 commit into
masterfrom
claude/c-compiler-stage6-conditionals

Conversation

@prozak

@prozak prozak commented Jul 3, 2026

Copy link
Copy Markdown
Owner

if/else statements and the ternary ?:, following blog part 6. The codegen is
a reprise of stage 4's compare-and-jump shaped into diamonds; the parsing
carries the stage's ideas.

  • c.pg — statement/declaration split (pulled forward from the C7 blob:
    part 6 and the official declare_statement test both demand it): the body
    becomes BlockItemList, BlockItem = Stmt | Decl, declarations leave
    Statement, so if (5) int i = 0; is a syntax error. The split also
    flips Statement to the scalar antiquote shape (BlockItem took the list
    position) — exactly what the if/else codegen patterns need ($s1/$s2
    bind one branch each).
  • The dangling else, on purpose: If/IfElse are separate labeled
    alternatives (an inline ('else' Statement)? would synthesize unnameable
    constructors), giving the grammar its first intentional shift/reduce
    conflict — happy's shift preference binds the else to the nearest if,
    the C rule. Per the C6 gotcha, the new make conflict-check target
    (wired into make test) pins the inventory: exactly 1 s/r in CParser
    (dangling else), 1 in AsmParser (pre-existing QQ bootstrap dummy bracket),
    0 r/r anywhere.
  • CondExp between assignment and logical-or with the blog's asymmetry:
    full-Exp true branch (flag ? a = 1 : 0 assigns), right-recursive
    CondExp false branch (right associativity; ... : a = 0 is a syntax
    error per official ternary_assign, the parenthesized form works).
  • Codegen: genBlockItem/genDeclaration split off; if / if-else /
    ?: jump diamonds sharing the stage-4 label supply. asm.pg and
    Emit.hs untouched.
  • TestQQ: the mechanical churn the C7 blob predicted, plus 10 stage-6
    checks (scalar statement binders, nearest-if binding, ternary
    associativity/precedence, both syntax-error guarantees, a stage-6 pipeline
    round trip). tests/ gains 3 valid + 2 invalid programs.

Verified: tutorial suite 103/103 from a clean build; official
write_a_c_compiler stages 1–6: 107/107 (12, 11, 16, 27, 17, 24). Adds
tutorial/06-conditionals.md and updates the index, status, roadmap, and the
antiquote-shape limitation note.

🤖 Generated with Claude Code

… C6)

Add if/else statements and the ?: conditional following blog part 6. The
codegen is a reprise (the stage-4 compare-and-jump shaped into diamonds);
the parsing carries the stage's ideas:

- c.pg: the statement/declaration split, pulled forward from the C7 blob
  because part 6 (and the official suite's declare_statement) demands it:
  Function's body becomes BlockItemList, BlockItem = Stmt | Decl, and
  declarations leave Statement - `if (5) int i = 0;` is now a *syntax*
  error. The split also flips Statement's antiquote shape to scalar
  (BlockItem took the list position), which is exactly what the if/else
  codegen patterns need: $s1/$s2 bind one branch each.
- If/IfElse are separate labeled alternatives, not an inline ('else'
  Statement)? option (synthesized option alternatives cannot carry
  constructor names). The price is the grammar's first intentional LALR
  conflict, the dangling else, resolved by happy's shift preference to
  bind the else to the nearest if. The new `make conflict-check` target
  (wired into `make test`) pins the inventory: exactly 1 s/r in CParser
  (dangling else), 1 in AsmParser (the pre-existing QQ bootstrap dummy
  bracket), 0 r/r - a later grammar change cannot hide new conflicts
  behind the expected ones (the C6 gotcha in the plan).
- CondExp sits between assignment and logical-or with the blog's exact
  asymmetry: full-Exp true branch (`flag ? a = 1 : 0` assigns),
  right-recursive CondExp false branch (right-associativity, and
  `... : a = 0` is a syntax error per the official ternary_assign, while
  the parenthesized form works through Factor).
- Codegen: genBlockItem/genDeclaration split off; genStatement gains the
  if and if/else diamonds, genExp gains genCond - same diamond, but both
  branches leave the value in %eax. asm.pg and Emit.hs are untouched:
  stage 4's cmpl/je/jmp/labels suffice.
- Resolve walks [BlockItem]; nested uses under if/?: were already covered
  by the SYB whole-subtree query.
- TestQQ: the mechanical churn the C7 blob predicted ($stmts cases move to
  BlockItem, declaration quotes to the new sort) plus stage-6 checks:
  scalar $s1/$s2 patterns, nearest-if else binding, ternary associativity
  and precedence, the two syntax-error guarantees, and a stage-6 pipeline
  round trip. tests/ gains if/else, nested-else, ternary programs and the
  two invalid forms.

Verified: tutorial suite 103/103 from a clean build; official
write_a_c_compiler stages 1-6: 12, 11, 16, 27, 17, 24 - 107/107. Adds
tutorial/06-conditionals.md, updates the index, status, roadmap, the
antiquote-shape limitation note (shapes can move between stages), and a
stage-snapshot note on page 01.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@prozak
prozak merged commit d6b7115 into master Jul 3, 2026
8 checks passed
@prozak
prozak deleted the claude/c-compiler-stage6-conditionals branch July 3, 2026 02:51
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