Skip to content

C compiler stage 7: blocks, scoping, and alpha-renaming (task C7) - #166

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

prozak merged 1 commit into
masterfrom
claude/c-compiler-stage7-scoping

Conversation

@prozak

@prozak prozak commented Jul 3, 2026

Copy link
Copy Markdown
Owner

Compound statements and block scoping, following blog part 7. One grammar
line; the substance is in the semantic pass.

  • The design fork: the blog resolves scopes during codegen; we keep the
    passes separate, and shadowing breaks their old interface — with two live
    as, a name no longer identifies a stack slot. Resolve therefore
    alpha-renames: scope stack, unique names (a#0, a#1, … — # can't
    occur in a C identifier), every use rewritten to its declaration's unique
    name, returning the renamed tree + offsets keyed by unique names. Codegen's
    lookup stays untouched; its only new case is Compound = sequence.
  • Traversal structured by the tree: declarations occur only in item
    lists and scopes open only at {, so the statement walk is a hand
    recursion in QQ patterns (rebuilt with QQ construction); expressions
    contain uses but never declarations, so renaming one is a single
    scope-blind everywhereM in Either — the monadic form of the task-8d
    rewrite recipe.
  • Semantics per the official suite: shadowing legal, duplicate
    declaration an error only within the same scope, out-of-scope rejected;
    left-to-right scope threading gets declare_late (use before an inner
    shadow declaration → outer variable) right for free. Offsets grow
    monotonically (the blog's scheme).
  • Conflict inventory unchanged — { starts no other statement;
    make conflict-check still pins dangling else + QQ dummy bracket.
  • Tests: 4 new QQ checks (incl. a list antiquote nested inside the
    scalar statement quoter and a shadowing pipeline round trip), 2 valid + 2
    invalid local programs.

Verified: tutorial suite 112/112 from a clean build; official
write_a_c_compiler stages 1–7: 119/119 (12, 11, 16, 27, 17, 24, 12).
Adds tutorial/07-blocks.md and updates index, status, layout, roadmap.

🤖 Generated with Claude Code

Add compound statements and block scoping following blog part 7. The
grammar change is one alternative (Compound: '{' BlockItemList '}' -
stage 6 already split out BlockItem, so a block body is the same sort as
a function body, and the conflict inventory is unchanged); the stage's
substance is in the semantic pass.

- The design fork: the blog resolves scopes DURING codegen; this compiler
  keeps the passes separate, and shadowing breaks their old interface (a
  name no longer identifies a stack slot). Resolve therefore
  ALPHA-RENAMES: it walks with a scope stack, gives each declaration a
  unique name (a#0, a#1, ... - '#' cannot occur in a C identifier),
  rewrites every use to its declaration's unique name, and returns the
  renamed tree with offsets keyed by unique names. Codegen's naive
  lookup-by-name is correct again on the renamed tree; its only new case
  is Compound = sequence.
- The walk's structure follows what the tree allows: declarations occur
  only in item lists and scopes open only at '{', so BlockItem/Statement
  is a hand recursion in QQ patterns (rebuilt with QQ construction, e.g.
  [statement| { $stmts2 } |]); expressions contain uses but never
  declarations, so renaming one is a single scope-blind everywhereM (the
  monadic form of the task-8d rewrite recipe - the rewrite can fail on an
  undeclared variable). Threading the scope left to right through the
  item list gets declare_late right for free: a use before an inner
  shadow declaration resolves to the outer variable.
- Semantics per the official suite: shadowing legal, duplicate
  declaration an error only within the SAME scope, out-of-scope uses
  rejected; offsets grow monotonically across the function (the blog's
  scheme - sibling blocks do not reuse slots).
- TestQQ: compound construction, a list antiquote nested inside the
  scalar statement quoter ({ $stmts } pattern), mixed splice into a
  block, shadowing-resolves/redeclaration-rejects checks, and a
  shadowing pipeline round trip. tests/ gains shadow, sibling_scopes and
  the two invalid scoping forms.

Verified: tutorial suite 112/112 from a clean build; official
write_a_c_compiler stages 1-7: 12, 11, 16, 27, 17, 24, 12 - 119/119
(stage 7 includes declare_late, multi_nesting, and both brace syntax
errors). Adds tutorial/07-blocks.md and updates the index, status,
layout, and roadmap.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@prozak
prozak merged commit 5107ef8 into master Jul 3, 2026
8 checks passed
@prozak
prozak deleted the claude/c-compiler-stage7-scoping branch July 3, 2026 04:17
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