Skip to content

Fix for(;;) NullReferenceException in parser - #113

Merged
praeclarum merged 3 commits into
masterfrom
copilot/fix-nullreferenceexception-in-parser
Mar 31, 2026
Merged

Fix for(;;) NullReferenceException in parser#113
praeclarum merged 3 commits into
masterfrom
copilot/fix-nullreferenceexception-in-parser

Conversation

Copilot AI commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

for(;;) and other for-loops with empty expressions crash with NullReferenceException because expression_statement: ';' returns null, and the FOR grammar rules blindly cast to ExpressionStatement.

Changes

  • CParser.jay — All 4 FOR rules use as with null-conditional operator instead of direct cast:
    // before
    ((ExpressionStatement)$4).Expression
    // after
    ($4 as ExpressionStatement)?.Expression
  • ForStatement.csContinueExpression made nullable; DoEmit skips the conditional branch when null (infinite loop until break)
  • CParser.cs — Regenerated via prebuild.sh
  • Tests — Added for(;;), for(i=0;;i++), for(i=0;i<10;), for(;i<5;i++)

…in FOR rules

- Update 4 FOR grammar rules in CParser.jay to use reference type null
  checking (`$4 is ExpressionStatement es ? es.Expression : null`)
- Make ForStatement.ContinueExpression nullable and skip condition
  emission when null (infinite loop until break)
- Regenerate parser via prebuild.sh
- Add tests for for(;;), for(i=0;;i++), for(i=0;i<10;), for(;i<5;i++)

Agent-Logs-Url: https://github.com/praeclarum/CLanguage/sessions/4fd85394-830c-4edd-b5a1-71623366c29b

Co-authored-by: praeclarum <323548+praeclarum@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix NullReferenceException in parser for infinite for loops Fix for(;;) NullReferenceException in parser Mar 31, 2026
Copilot AI requested a review from praeclarum March 31, 2026 20:19
@praeclarum
praeclarum marked this pull request as ready for review March 31, 2026 20:29

@praeclarum praeclarum left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of $4 is ExpressionStatement es4a ? es4a.Expression : null etc I would prefer the simpler ($4 as ExpressionStatement)?.Expression. Unless that doesn't work for some reason...

Use ($4 as ExpressionStatement)?.Expression instead of pattern matching.

Agent-Logs-Url: https://github.com/praeclarum/CLanguage/sessions/3f96ebb4-83a6-41af-bf99-d72fe14ad18f

Co-authored-by: praeclarum <323548+praeclarum@users.noreply.github.com>

Copilot AI commented Mar 31, 2026

Copy link
Copy Markdown
Contributor Author

Instead of $4 is ExpressionStatement es4a ? es4a.Expression : null etc I would prefer the simpler ($4 as ExpressionStatement)?.Expression. Unless that doesn't work for some reason...

Updated all 4 rules to use ($4 as ExpressionStatement)?.Expression in 43fd50f.

Copilot AI requested a review from praeclarum March 31, 2026 20:34
@praeclarum
praeclarum merged commit 6904f49 into master Mar 31, 2026
1 check passed
@praeclarum
praeclarum deleted the copilot/fix-nullreferenceexception-in-parser branch March 31, 2026 20:36
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.

for(;;) crashes with NullReferenceException in parser

2 participants