A FOR body runs once with the overshot counter, and FOR I = 1 TO 1 does not run at all
#5
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Source: TODO.md §6 items 19 and 20 (at
9151438)Three defects in one place, and two of them cancel out, which is why none was noticed.
1. The body runs with the overshot counter. The loop condition is tested against the counter
before the increment, so the increment's result reaches the body before anything compares it to
the limit.
FOR I = 1 TO 9 STEP 3runs its body with 1, 4, 7 and then 10. Invisible wheneverthe step lands exactly on the limit, which is every case in the golden corpus and every case in
the reference's own tests.
2.
FOR I = 1 TO 1does not run its body at all, where every BASIC ever written runs it once.The two errors compensate for a step of 1, which is why neither was noticed. Chapter 18 had to
gain a rule about it: every list in the chapter writes its one-item case out beside the loop.
3. The counter does not survive the loop. It is created in the environment the loop pushes,
which pops when the loop ends, so reading it afterwards finds a fresh variable holding zero. A
C128 leaves it at the value that ended the loop and plenty of published listings read it there.
That is a scoping decision about where a counter is created, not an ordering one.
Not fixed because the corpus pins it.
tests/reference/language/flowcontrol/forloopwaitingforcommand.basdepends onFOR I# = 1 TO 1skipping its body, and
tests/reference/README.mdforbids editing an expectation to suit thisinterpreter.
tests/for_semantics.cis registered inAKBASIC_KNOWN_FAILING_TESTSasserting the correctcontract. Fixing it means deciding what to do with that golden file, and that is a decision
rather than a patch -- though the bar for diverging dropped when the Go reference was
deprecated, so the decision is now available to take.
Files:
src/runtime_commands.c(akbasic_cmd_for,akbasic_cmd_next),tests/for_semantics.c,tests/reference/language/flowcontrol/forloopwaitingforcommand.basFiled by Tachikoma (Claude Code, Opus 5, 1M context)