Skip to content

Implement goto statement and labeled statements - #115

Merged
praeclarum merged 3 commits into
masterfrom
copilot/implement-goto-statement
Mar 31, 2026
Merged

Implement goto statement and labeled statements#115
praeclarum merged 3 commits into
masterfrom
copilot/implement-goto-statement

Conversation

Copilot AI commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

goto was parsed by the grammar but had no action code — no AST nodes were created. Labeled statements (label:) were similarly unimplemented.

Changes

  • AST nodes: GotoStatement emits Jump to a resolved label; LabeledStatement defines a label and wraps the subsequent statement
  • Grammar actions in CParser.jay for GOTO IDENTIFIER ';' and IDENTIFIER ':' statement
  • Label resolution: Function-scoped Dictionary<string, Label> in FunctionContext. Both goto and label definitions resolve through ResolveGotoLabel/DefineGotoLabel on EmitContext, so forward references work naturally via shared Label objects.
  • Validation: CheckLabels() after function emission reports undefined labels (error 9999) and duplicate labels (error 140)
  • AGENTS.md: Removed "No goto" from Known Limitations

Example

void main() {
    int x = 0;
    goto skip;
    x = 42;        // skipped
skip:
    // x == 0

    int i = 0;
loop:
    i = i + 1;
    if (i < 5)
        goto loop;
    // i == 5
}

Tests

9 cases: forward goto, backward goto, multi-label chains, nested block jumps, loop breakout, and error cases for undefined/duplicate labels.

Copilot AI linked an issue Mar 31, 2026 that may be closed by this pull request
Copilot AI and others added 2 commits March 31, 2026 21:08
- Add GotoStatement and LabeledStatement AST nodes
- Add grammar actions in CParser.jay for both rules
- Add function-scoped label registry in FunctionContext
- Add ResolveGotoLabel/DefineGotoLabel methods to EmitContext
- Add CheckLabels validation after function compilation
- Add GotoTests with 9 test cases (forward, backward, nested, errors)
- Update AGENTS.md to remove "No goto" limitation

Agent-Logs-Url: https://github.com/praeclarum/CLanguage/sessions/ba96b912-1cf8-4ca6-99e6-532546744425

Co-authored-by: praeclarum <323548+praeclarum@users.noreply.github.com>
Agent-Logs-Url: https://github.com/praeclarum/CLanguage/sessions/ba96b912-1cf8-4ca6-99e6-532546744425

Co-authored-by: praeclarum <323548+praeclarum@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement goto statement in C parser Implement goto statement and labeled statements Mar 31, 2026
Copilot AI requested a review from praeclarum March 31, 2026 21:13
@praeclarum
praeclarum marked this pull request as ready for review March 31, 2026 21:23
@praeclarum
praeclarum merged commit c8e4440 into master Mar 31, 2026
1 check passed
@praeclarum
praeclarum deleted the copilot/implement-goto-statement branch March 31, 2026 21:27
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.

goto statement not implemented

2 participants