C compiler stage 5: local variables and the first semantic pass (task C5) - #158
Merged
Conversation
… C5) Add local variables -- declarations, assignment, references -- following blog part 5. Two genuinely new things: a stack frame, and the compiler's first semantic pass. - c.pg: assignment enters the cascade at the top (lowest precedence, right-recursive). Its LHS is a bare Ident, which keeps the grammar LALR(1)-clean (an Ident is an assignment target only when '=' follows, else it reduces to a Factor -- no conflict) and makes `a + 3 = 4` a syntax error, as the blog wants. Factor gains VarRef; Statement gains expression statements and the two declaration forms. - Resolve.hs (new): the first semantic pass. Walks the statement list in order, assigns each local a stack offset, and rejects undeclared/ use-before-declaration/duplicate-declaration with Either String. The "variables referenced anywhere in this node" query is one SYB listify over the derived Data instances rather than a hand recursion over the Exp cascade -- QQ for targeted matching, generic programming for whole-tree queries. - Main.hs: runs resolve between parse and codegen; semantic errors reject with the same no-artifacts contract as parse errors. - asm.pg: 64-bit movq/subq, %rbp/%rsp, a Mem operand (signed disp(base)), and signed num so -4(%rbp) lexes as one displacement. - Codegen.hs: now ReaderT VarMap (State Int) -- Reader for the offset map, State for labels. Prologue/epilogue frame every function; references load and assignments store (the value stays in %eax, so assignment is an expression). - TestQQ.hs: assignment (incl. right-assoc and value), VarRef, declaration, the Mem operand, and a parse->resolve->codegen->emit->parse round trip. tests/ gains variable programs and four semantic-error cases. Verified: tutorial suite 88/88; official write_a_c_compiler stage 5 17/17, stages 1-3 still green, and stage 4 rose 24 -> 27 (its skip_on_failure short-circuit tests need variables, so they now compile and pass -- a second check that stage 4's jumps are right). Adds tutorial/05-variables.md and updates the index, status, layout, and roadmap. https://claude.ai/code/session_01TXSeYgnZJZWC6dczN6d5E9
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.
Add local variables -- declarations, assignment, references -- following blog
part 5. Two genuinely new things: a stack frame, and the compiler's first
semantic pass.
right-recursive). Its LHS is a bare Ident, which keeps the grammar
LALR(1)-clean (an Ident is an assignment target only when '=' follows, else
it reduces to a Factor -- no conflict) and makes
a + 3 = 4a syntax error,as the blog wants. Factor gains VarRef; Statement gains expression
statements and the two declaration forms.
order, assigns each local a stack offset, and rejects undeclared/
use-before-declaration/duplicate-declaration with Either String. The
"variables referenced anywhere in this node" query is one SYB listify over
the derived Data instances rather than a hand recursion over the Exp
cascade -- QQ for targeted matching, generic programming for whole-tree
queries.
with the same no-artifacts contract as parse errors.
signed num so -4(%rbp) lexes as one displacement.
State for labels. Prologue/epilogue frame every function; references load
and assignments store (the value stays in %eax, so assignment is an
expression).
the Mem operand, and a parse->resolve->codegen->emit->parse round trip.
tests/ gains variable programs and four semantic-error cases.
Verified: tutorial suite 88/88; official write_a_c_compiler stage 5 17/17,
stages 1-3 still green, and stage 4 rose 24 -> 27 (its skip_on_failure
short-circuit tests need variables, so they now compile and pass -- a second
check that stage 4's jumps are right). Adds tutorial/05-variables.md and
updates the index, status, layout, and roadmap.
https://claude.ai/code/session_01TXSeYgnZJZWC6dczN6d5E9