richpl / PyBasic

Simple interactive BASIC interpreter written in Python
GNU General Public License v3.0
165 stars 45 forks source link

NEXT clause ignores its parameter #52

Closed sblendorio closed 2 years ago

sblendorio commented 2 years ago

This (intentionally byzantine) example should print 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 but it ignores the parameter of NEXT clause and prints "1" three times:

10 FOR I = 1 TO 10
20 FOR J = 1 TO 3
30 PRINT I
40 NEXT I
50 END
60 NEXT J
> run
1
1
1
RetiredWizard commented 2 years ago

This certainly is something that could be fixed, PyBasic does ignore the NEXT parameter but as you pointed out, don't you have to break the coding rules of FOR/NEXT loops for it to matter?

GWBasic actually won't run your example code, it displays a "NEXT without FOR in 60" message.

RetiredWizard commented 2 years ago

Okay here's an example that will run on GWBasic as well:

          10 FOR I = 1 TO 10
          20 FOR J = 1 TO 3
          30 PRINT I
          40 GOTO 60
          50 NEXT J
          60 NEXT I

I'll start thinking about it and propose a PR if I come up with a reasonable fix.

richpl commented 2 years ago

So I think any code which overlaps loops is pretty scary and the programmer might deserve all they get if the interpreter behaves unpredictably! But @RetiredWizard has produced a fix in record time, so I'll close this.

sblendorio commented 2 years ago

No doubt it's a misuse. But, there is a lot of pre-existent code that does it ;)

Thank you for the Pull Request, @RetiredWizard !