OK, so I'm converting this program and I get this error after playing it for a while. Now I'm pretty sure that this is caused by some of the FOR...NEXT loops not cleaning up properly, but I need to know how to do it in BASIC Engine.
There are several FOR...NEXT loops that use a GOTO to exit it prematurely. The original BASIC may have been more tolerant of this, but BASIC Engine does not detect and clean up the short circuit.
My question is, how can I cleanly exit the FOR...NEXT loop early?
For example:
10 FOR I=1 TO 10
20 FOR J=1 TO 20
30 <do something>
40 IF <conditional> THEN 60
50 NEXT J
60 <do something else>
70 NEXT I

If I remember correctly, exiting a FOR NEXT loop early leaves a pointer on the stack. Doing it too many times will fill the stack completely causing your problem. Any leftover pointers reduces the stack size.

Back in the day or at least for the C-64, the way to clear the FOR NEXT loop for an early exit is to set the FOR variable to the max value of the loop before exiting.

Line 40 should be changed to this:

40 IF <conditional> THEN J=20 : GOTO60

A better option for BASIC Engine is to use a DO LOOP and exit when the <condition> or the max value occurs.

    Willard Thanks for your response. What you suggested is indeed what I ended up implementing, except the GOTO was to the NEXT of the inner loop. It seems to have solved the problem.
    I may continue to restructure this program I am playing with to improve it further as you suggested, or I may move on to a new project.

    Yes the GOTO should go to the Next statement so it can execute to clear the pointer. I thought of that after I made the post.

    I am waiting for Prime to deliver my hardware to start using BASIC Engine. Maybe I'll get it by the end of the week.

    I've been using the SDL version of BASIC Engine for development. It's pretty good, although the pixels are REALLY small. Changing the SCREEN doesn't make them any bigger.

    I have just pushed commits that implement EXIT loop commands. You should be able to write your example like this now (or rather, from tomorrow's build):

    10 FOR I=1 TO 10
    20 FOR J=1 TO 20
    30 <do something>
    40 IF <conditional> THEN EXIT FOR
    50 NEXT J
    60 <do something else>
    70 NEXT I

    There is also EXIT DO and EXIT WHILE. See HELP EXIT for details. Be aware, however, that this is hot off the press and probably buggy...

      uli Wow, that was really responsive.

      So my understanding is that it will go to the line after the NEXT statement and continue execution from there. This is a much cleaner, more generic solution to setting the loop variable to the exit value and jumping to the NEXT statement.
      Can it be executed anywhere, or only within an IF…THEN statement?
      I’m assuming that this will introduce a new error if it is called not within a FOR…NEXT loop.

      • uli replied to this.

        Hawk So my understanding is that it will go to the line after the NEXT statement and continue execution from there.

        It will go to the statement after NEXT.

        Hawk Can it be executed anywhere, or only within an IF…THEN statement?

        It will work anywhere.

        Hawk I’m assuming that this will introduce a new error if it is called not within a FOR…NEXT loop.

        There will be an error ("EXIT failed") if there are no active loops (due to a bug this currently results in "NEXT without FOR"; I'll have to fix that) or if the corresponding looping statement (NEXT, LOOP or WEND) is not found.

        Powered by: FreeFlarum.
        (remove this footer)