v4 Parsing: for loops and short-circuit logic

Work through examples, try the reasoning yourself, then put it into practice.

View Slides

Before you start

  • Parse v3 postfix expressions and blocks.
  • Predict v4 loop events and boolean precedence.

What you will practice

  • Assign for-header delimiters to the correct parser.
  • Represent missing clauses without losing loop structure.
  • Parse loop-control syntax without enforcing analyzer context.
In this lesson

Lesson

3 min read

v4 Parsing: for loops and short-circuit logic

for (init; cond; post) { body } has three optional clauses and two mandatory semicolons. Its AST stores Init and Post statements, Cond expression, and Body statements. The header parser owns its separators; clause helpers must not consume statement semicolons independently.

Worked reconstruction

For for (let i=0; i<3; i=i+1) { continue; }:

AST field Shape Delimiter owned by outer parser
Init variable declaration i initialized to 0 first ;
Cond Lt(Ident(i),Number(3)) second ;
Post assignment to i with Add(i,1) )
Body one Continue statement closing }

For for (;;) { break; }, all three clause fields are nil. Compilation later supplies true for the absent condition; parsing should preserve absence rather than inventing a source expression. The body still requires braces.

Boolean tree shape

The expression entry is now or → and → equality → comparison → term → factor → unary → postfix → primary. Each binary level loops, preserving left associativity. a || b && c becomes Or(a,And(b,c)); (a || b) && c has And at the root. Short-circuit execution is not performed by the parser.

Break and continue parse as standalone statements with a semicolon. Even a top-level break; has valid syntax and reaches the analyzer for placement checking. They are not expressions: let x=break; fails parsing. This API returns a first parse error; it does not promise synchronization and continued recovery after a malformed header.

Reuse the NoSemi variants of declaration/assignment parsing for the appropriate header positions. Post accepts assignment or expression, not a new declaration. If a helper consumes the wrong delimiter, a correct-looking final AST may hide that the next statement was swallowed.

Debugging lab

A header parser calls the ordinary semicolon-consuming declaration parser for init and then consumes another semicolon. Explain why a valid for header fails and which routine should own that token.

Compare your diagnosis after attempting the lab

The inner helper consumes the first separator, so the outer parser now expects another at the start of the condition. Parse the initializer without its normal terminator; let parseFor consume each of the two required separators exactly once.

Transfer task

Draw the AST for for (;ready || a && b;) { break; }, marking nil fields and the logical root. Produce one missing-semicolon error and one syntactically valid but semantically illegal break outside a loop.

Carry forward

Carry a faithful for AST into analysis. Keep clause absence, parse validity, and loop placement as three separate decisions.

Build program-parser-v4. Keep a before/after trace of one failing test. Use the checkpoint to diagnose specific skills; passing it does not replace implementing and testing the artifact.


Practice sequence

Use the order below for a guided path, or choose an item to revisit. A pass records a successful attempt; try a fresh example later to check what you can reconstruct.

Join Discord