Skip to content

Fix: Refactor loop_stmt to prevent RecursionError#25

Merged
Pallavrai merged 1 commit into
Pallavrai:mainfrom
Sansi-28:fix-recursion-error-in-loop
Oct 6, 2025
Merged

Fix: Refactor loop_stmt to prevent RecursionError#25
Pallavrai merged 1 commit into
Pallavrai:mainfrom
Sansi-28:fix-recursion-error-in-loop

Conversation

@Sansi-28

@Sansi-28 Sansi-28 commented Oct 6, 2025

Copy link
Copy Markdown
Contributor

This pull request resolves a critical bug where the interpreter crashes with a RecursionError: maximum recursion depth exceeded when executing a wagtail loop with a large number of iterations (typically >1000).

The root cause was the recursive implementation of the loop_stmt method in the interpreter, where each loop iteration resulted in another function call, eventually overflowing the call stack. This fix makes the language more robust and capable of handling common, long-running loops.

Implementation Details

I have refactored the loop_stmt function in doglang/main.py to use an iterative approach instead of a recursive one.

  • The recursive call self.loop_stmt(children) has been replaced with a standard Python while loop.
  • The loop's condition is evaluated using self.expression_stmt() at the beginning of each iteration.
  • The body of the loop is then executed by visiting each of the body nodes.

This change eliminates the risk of stack overflow, allowing wagtail loops to run for a virtually unlimited number of iterations without crashing the interpreter.

How This Was Tested

Manual Verification: I created a test file (long_loop.doggy) containing a wagtail loop set to run 2,000 iterations.
- Before the fix: The program crashed with a RecursionError.
- After the fix: The program now runs to completion successfully.

Screenshots:

Screenshot From 2025-10-06 21-05-50 Screenshot From 2025-10-06 21-05-18

Closes: #24
Ready for review. Thank you!

@Pallavrai Pallavrai merged commit 5f625d7 into Pallavrai:main Oct 6, 2025
1 check 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.

Bug: Interpreter crashes with RecursionError on long-running loops

2 participants