Skip to content

Conversation

@Jayking40
Copy link

Fix: Re-evaluate Function Calls in Until Loop Conditions

Problem
In goboscript, when a function call (e.g., random(1, 10)) is used in the condition of an until loop, the function was only evaluated once at the start of the loop. This meant that the loop would not re-evaluate the condition on each iteration, leading to unexpected behavior.

Solution
We modified the code generation logic for the until loop in src/codegen/stmt.rs to duplicate the condition evaluation code at the end of the loop body. This ensures that the condition (and any function calls within it) is re-evaluated on each iteration, matching the expected behavior in Scratch.

Testing
A test case was added in playground/stage.gs that uses an until loop with a random(1, 10) function call in its condition. The test verifies that the loop continues until the random function returns 5, and the count is incremented and displayed on each iteration.

Impact
This fix ensures that goboscript's until loop behaves correctly, making it more reliable for projects that rely on dynamic conditions within loops.

This commit fixes an issue where function calls inside until loop conditions were only evaluated once at the start of the loop. Now, the condition is re-evaluated at the end of each iteration, ensuring that functions (like random()) are called on every iteration. This change makes the until loop behavior match the expected Scratch semantics.
@aspizu aspizu closed this May 29, 2025
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.

2 participants