Skip to content

feat(compiler): support do-while loop statement (#40) - #49

Merged
sql-hkr merged 1 commit into
mainfrom
feat/do-while-loop
Oct 27, 2026
Merged

sql-hkr merged 1 commit into
mainfrom
feat/do-while-loop

Conversation

@sql-hkr

@sql-hkr sql-hkr commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Closes #40

What changed

  • Lexer: Added do keyword to Lexer.TOKEN_SPEC.
  • AST: Added DoWhileStmt(body: Block, condition: ASTNode) node.
  • Parser: Added parsing of do <block | stmt> while (<condition>); in Parser.parse_stmt.
  • CodeGenerator:
    • Implemented code generation for DoWhileStmt with loop start (dowhile_start_*), condition (dowhile_cond_*), and end (dowhile_end_*) labels.
    • Pushed condition label to loop_continue_stack so continue correctly routes to condition evaluation.
    • Pushed end label to loop_end_stack so break cleanly exits the loop.
    • Evaluated condition with CPI R16, 0 and looped back with BRNE <start>.

How tested

  • Added unit tests in tests/test_compiler.py:
    • test_compiler_do_while_basic: verify repeated execution while condition holds true.
    • test_compiler_do_while_false_condition_executes_once: verify body executes exactly once when initial condition is false.
    • test_compiler_do_while_break_and_continue: verify break exits the loop and continue jumps to condition evaluation.
    • test_compiler_do_while_single_statement: verify brace-less single statement do ... while (...);.
    • test_compiler_do_while_ast_and_lexer: verify tokenization and AST parsing into DoWhileStmt.
  • uv run pytest: 307 passed (all tests passed).
  • uv run ruff check src/tiny8/compiler.py tests/test_compiler.py: passed.
  • uv run ruff format --check src/tiny8/compiler.py tests/test_compiler.py: passed.

- Add do to keywords in Lexer.TOKEN_SPEC
- Add DoWhileStmt(body: Block, condition: ASTNode) AST node
- Update Parser.parse_stmt to parse do <block> while (<condition>);
- Implement code generation for do-while loops with start, cond, and end labels
- Add unit tests verifying multiple iterations, false condition executing once, break/continue, single statement syntax, AST and lexer

Closes #40
@codecov

codecov Bot commented Oct 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@sql-hkr
sql-hkr merged commit f55d0c1 into main Oct 27, 2026
5 checks passed
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.

feat(compiler): support do-while loop statement

1 participant