skx / gobasic

A BASIC interpreter written in golang.
https://blog.steve.fi/tags/basic/
GNU General Public License v2.0
323 stars 27 forks source link

Improve the handling of impossible FOR loops #73

Closed skx closed 5 years ago

skx commented 5 years ago

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
skx commented 5 years ago
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.

skx commented 5 years ago

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