Fix #894: reset mathcnt when a call statement closes an if-block#906
Merged
Conversation
…lock Two consecutive `if <cond with && or ||> then <function/event call> end` blocks compiled incorrectly: the first block's trailing OP_AND/OP_OR was relocated into the second block, so both conditions evaluated wrong and both bodies ran regardless of the condition. The corruption is present in the parser output (before bc_assign_slots), so it is not a slot-assignment bug. Root cause: the assignment-statement path resets the temporary slot counter (mathcnt = 0) but the call/ expression-statement path does not. When a call statement closes an if-block, the leftover mathcnt makes the next sibling if-condition's first operand get a slot index != 1, so bc_parse_math_order's backward search for the start of the current expression overshoots past the previous block's call body and JMP into that block's first OP_GETVAL, pulling its trailing logical operator into the new block. Reset mathcnt in the statement terminator's TEND branch, i.e. only when a call/expression statement is the last in its block. Doing it after every statement would change slot reuse for multi-statement bodies; scoping it to block close keeps previously-correct bytecode byte-identical. Validated against the upstream CurlyMoo/rules unit-test suite (all 526 tests pass, no regressions) and a standalone bytecode harness covering the &&/||, multi-statement-body, nested-if and value-returning-call cases. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two consecutive
if <cond with && or ||> then <function/event call> endblocks compiled incorrectly: the first block's trailing OP_AND/OP_OR was relocated into the second block, so both conditions evaluated wrong and both bodies ran regardless of the condition.The corruption is present in the parser output (before bc_assign_slots), so it is not a slot-assignment bug. Root cause: the assignment-statement path resets the temporary slot counter (mathcnt = 0) but the call/ expression-statement path does not. When a call statement closes an if-block, the leftover mathcnt makes the next sibling if-condition's first operand get a slot index != 1, so bc_parse_math_order's backward search for the start of the current expression overshoots past the previous block's call body and JMP into that block's first OP_GETVAL, pulling its trailing logical operator into the new block.
Reset mathcnt in the statement terminator's TEND branch, i.e. only when a call/expression statement is the last in its block. Doing it after every statement would change slot reuse for multi-statement bodies; scoping it to block close keeps previously-correct bytecode byte-identical.
Validated against the upstream CurlyMoo/rules unit-test suite (all 526 tests pass, no regressions) and a standalone bytecode harness covering the &&/||, multi-statement-body, nested-if and value-returning-call cases.