skx / gobasic

A BASIC interpreter written in golang.

Home Page:https://blog.steve.fi/tags/basic/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Improve the handling of impossible FOR loops

skx opened this issue · comments

The following example code was emailed to me:

10 FOR I = 1 TO -20 
20 PRINT I, "\n"
30 NEXT I

Output? Constant numbers.

Expected output? zero numbers. There shouldn't even be a single iteration.

A similar situation is:

10 FOR I = 1 TO 10 STEP 7
20 PRINT I, "\n"
30 NEXT I

On my system that never terminates. On a real spectrum it produces:

  1
  8
10 FOR I = 1 TO 10 STEP 2
20 PRINT I, "\n"
30 NEXT I

Prints 1, 3, 5, 7, 9.

I guess we test if the loop is "up" or "down" and test for >= END on each iteration.

Of course ">=" doesn't matter if START==END we must run one iteration. Hrm.