Repository navigation
C compiler stage 6: if/else and the conditional expression (task C6) - #165
Merged
Merged
Conversation
… 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>
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.
if/else statements and the ternary
?:, following blog part 6. The codegen isa reprise of stage 4's compare-and-jump shaped into diamonds; the parsing
carries the stage's ideas.
part 6 and the official
declare_statementtest both demand it): the bodybecomes
BlockItemList,BlockItem = Stmt | Decl, declarations leaveStatement, soif (5) int i = 0;is a syntax error. The split alsoflips
Statementto the scalar antiquote shape (BlockItemtook the listposition) — exactly what the if/else codegen patterns need (
$s1/$s2bind one branch each).
If/IfElseare separate labeledalternatives (an inline
('else' Statement)?would synthesize unnameableconstructors), giving the grammar its first intentional shift/reduce
conflict — happy's shift preference binds the
elseto the nearestif,the C rule. Per the C6 gotcha, the new
make conflict-checktarget(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.
CondExpbetween assignment and logical-or with the blog's asymmetry:full-
Exptrue branch (flag ? a = 1 : 0assigns), right-recursiveCondExpfalse branch (right associativity;... : a = 0is a syntaxerror per official
ternary_assign, the parenthesized form works).genBlockItem/genDeclarationsplit off; if / if-else /?:jump diamonds sharing the stage-4 label supply.asm.pgandEmit.hsuntouched.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.mdand updates the index, status, roadmap, and theantiquote-shape limitation note.
🤖 Generated with Claude Code