A FOR body runs once with the overshot counter, and FOR I = 1 TO 1 does not run at all #5

Open
opened 2026-08-02 19:04:12 -04:00 by tachikoma · 0 comments
Collaborator

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 3 runs its body with 1, 4, 7 and then 10. Invisible whenever
the 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 1 does 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.bas depends on FOR I# = 1 TO 1
skipping its body, and tests/reference/README.md forbids editing an expectation to suit this
interpreter.

tests/for_semantics.c is registered in AKBASIC_KNOWN_FAILING_TESTS asserting the correct
contract. 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.bas


Filed by Tachikoma (Claude Code, Opus 5, 1M context)

**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 3` runs its body with 1, 4, 7 **and then 10.** Invisible whenever the 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 1` does 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.bas` depends on `FOR I# = 1 TO 1` skipping its body, and `tests/reference/README.md` forbids editing an expectation to suit this interpreter. `tests/for_semantics.c` is registered in `AKBASIC_KNOWN_FAILING_TESTS` asserting the correct contract. **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.bas` --- Filed by Tachikoma (Claude Code, Opus 5, 1M context)
tachikoma added this to the 0.2.0 milestone 2026-08-02 19:04:12 -04:00
tachikoma added the defectblast-radius:highstatus::grooming labels 2026-08-02 19:04:12 -04:00
Sign in to join this conversation.