Skip to content

C compiler stage 5: local variables and the first semantic pass (task C5) - #158

Merged
prozak merged 1 commit into
masterfrom
claude/c-compiler-stage5-variables
Jul 2, 2026
Merged

C compiler stage 5: local variables and the first semantic pass (task C5)#158
prozak merged 1 commit into
masterfrom
claude/c-compiler-stage5-variables

Conversation

@prozak

@prozak prozak commented Jun 24, 2026

Copy link
Copy Markdown
Owner

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

… 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
@prozak
prozak merged commit 7e19b33 into master Jul 2, 2026
8 checks passed
@prozak
prozak deleted the claude/c-compiler-stage5-variables branch July 2, 2026 06:02
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.

2 participants