richpl / PyBasic

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

Loop variable bug #30

Closed RetiredWizard closed 3 years ago

RetiredWizard commented 3 years ago

Well my inexperience has showed up here. I screwed up my original pull request so I recreated it here. :(

As was mentioned in the original comments, it's possible the loop variable may have been used before the loop begins so it didn't seem like a good idea to remove the loop variable from the symbol table when the loop ends.

RetiredWizard commented 3 years ago

From the Original Pull Request:

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, 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


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.

richpl commented 3 years ago

Sorry, I've got some conflicts in basicparser.py, perhaps because of changes made by brickbots. Grateful if you could take a quick look, thanks

RetiredWizard commented 3 years ago

The conflicts should be resolved now :)