richpl / PyBasic

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

loop variable bug #27

Closed RetiredWizard closed 3 years ago

RetiredWizard commented 3 years ago

if a for loop is terminated by a goto outside the extent of the loop and then the loop is initiated again through normal program flow, in this case the loop variable will not initalize but pick up where it was when the previous loop was exited.

This pull request will initialize the loop variable anytime the for statement is reached from a statement other than an active next statement.

There is still a problem when loops are exited abnormally since the loop variable is not cleaned up properly upon the termination of the loop

RetiredWizard commented 3 years ago

I dug into the loop variable cleanup concern I had and it looks like it shouldn't be a problem because the set ".add" function doesn't create duplicates.

I did notice one interesting thing in that in most BASIC versions I've encountered the loop variable is accessible after the loop has completed, it's usually documented as undefined out side of the loop but typically ends up as endvalue+step. I haven't run into any vintage programs that depended on this behavior yet so I'm not sure it should be changed.

This does mean that if a loop is exited using a goto the loop variable will be available outside the loop which is different that the behavior if the loop exits normally.

RetiredWizard commented 3 years ago

I messed up my original pull request and couldn't seem to add new files to this one so I created a new pull for this issue. I'll move the info from this one to the new pull and try and close this one. Sorry :(

RetiredWizard commented 3 years ago

See the new Loop variable bug #30