Repository navigation
C compiler stage 7: blocks, scoping, and alpha-renaming (task C7) - #166
Merged
Merged
Conversation
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>
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.
Compound statements and block scoping, following blog part 7. One grammar
line; the substance is in the semantic pass.
passes separate, and shadowing breaks their old interface — with two live
as, a name no longer identifies a stack slot.Resolvethereforealpha-renames: scope stack, unique names (
a#0,a#1, … —#can'toccur 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.lists and scopes open only at
{, so the statement walk is a handrecursion in QQ patterns (rebuilt with QQ construction); expressions
contain uses but never declarations, so renaming one is a single
scope-blind
everywhereMinEither— the monadic form of the task-8drewrite recipe.
declaration an error only within the same scope, out-of-scope rejected;
left-to-right scope threading gets
declare_late(use before an innershadow declaration → outer variable) right for free. Offsets grow
monotonically (the blog's scheme).
{starts no other statement;make conflict-checkstill pins dangling else + QQ dummy bracket.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.mdand updates index, status, layout, roadmap.🤖 Generated with Claude Code