Forum
Breaking loops - Printable Version

+- Forum (https://www.boriel.com/forum)
+-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11)
+---- Forum: Bug Reports (https://www.boriel.com/forum/forumdisplay.php?fid=15)
+---- Thread: Breaking loops (/showthread.php?tid=2608)



Breaking loops - baltasarq - 02-07-2025

Out of simple routine, I created a for loop like this:
Code:
for i = 0 to ubound(m)
    if m( i ) = 0
        break
    end if
next

At first, I did not understand what was happening. break was not working. But there was not any error or even a warning. Then I realized: break is not a part of Basic. It never was.
The strange point is the absence of any error or warning. Either this was included in the grammar by error, or maybe because it is planned for inclusion.
I don't know.


RE: Breaking loops - boriel - 02-08-2025

Boriel ZX Basic does implement CONTINUE and BREAK as in Visual Basic / FreeBasic syntax.
In this case the syntax is EXIT FOR.

I suggest to consult the documentation:
https://zxbasic.readthedocs.io/en/docs/exit/ (break equivalent)
https://zxbasic.readthedocs.io/en/docs/continue/ (continue)


RE: Breaking loops - baltasarq - 02-08-2025

I see... then you should remove "break" from the parser. Also, the ZXBasic wiki should mention "break" somewhere in the "exit" pages so when looking up it you're pointed in the right direction (yep, I looked it up before writing the post! ;-) ).


RE: Breaking loops - boriel - 02-09-2025

Sorry, I don’t understand. Break is not in the parser. BREAK equivalent is EXIT as in other modern Basic dialects. See docs.


RE: Breaking loops - baltasarq - 02-10-2025

What I'm trying to tell you is that "break" is compiled without warning or error. You can even run the program, and then, "break" is not executed as "exit loop" or whatever... it' actually as it was a nop.

Try this:

Code:
dim m(10) as byte => { 11, 22, 33, 44, 55, 66, 77, 88, 99, 100, 110 }

for i = 0 to 10
    if m( i ) = 66
        break
    end if
next

print "i = "; i

Compile it with this:

Code:
python ~/bin/zxbasic/zxbc.py -f tap --BASIC --autorun --debug-array sample.bas

And then it compiles okay (not even warnings), then you run it in the emulator, and the output is 11.

Code:
i = 11



RE: Breaking loops - boriel - 02-10-2025

baltasarq Wrote:What I'm trying to tell you is that "break" is compiled without warning or error. You can even run the program, and then, "break" is not executed as "exit loop" or whatever... it' actually as it was a nop.

Try this:

Code:
dim m(10) as byte => { 11, 22, 33, 44, 55, 66, 77, 88, 99, 100, 110 }

for i = 0 to 10
    if m( i ) = 66
        break
    end if
next

print "i = "; i

Compile it with this:

Code:
python ~/bin/zxbasic/zxbc.py -f tap --BASIC --autorun --debug-array sample.bas

And then it compiles okay (not even warnings), then you run it in the emulator, and the output is 11.

Code:
i = 11

Oh, then this is obiously a bug.
I'll review the code. As you said it should either EXIT the current (inner) loop or throw syntax error.


RE: Breaking loops - boriel - 02-16-2025

Have reported it here: https://github.com/boriel-basic/zxbasic/issues/953 if you want to track it.
The thing is that this is not exactly a "bug", but a feature of the (inherently) ambiguous modern BASIC grammar:
Traditionally BASIC line listings were numbered:
Code:
10 PRINT "HELLO WORLD"
20 GOTO 10

These are indeed labels. Boriel BASIC allows these for backward compatibilty, but also omtting them, or using normal (identifiers) labels instead:
Code:
START
   PRINT "HELLO WORLD"
   GOTO START

On top of that, the language grammar allows calling Sub and functions with no parenthesis. So:

Code:
START

Could be: Declaring the START label or calling START() function!
Since the compiler does not know anything about START it will try to best-guess
In your example, BREAK is not a reserved word, so your code is the same as an empty loop, with a label BREAK: declared in the center of the loop.

So it's like:
Code:
dim m(10) as byte => { 11, 22, 33, 44, 55, 66, 77, 88, 99, 100, 110 }

for i = 0 to 10
    if m( i ) = 66
        break ' This declares the label "break"
    end if
next

print "i = "; i



RE: Breaking loops - baltasarq - 02-16-2025

Mmm... I see. Shouldn't labels had a distinct syntax, such as "label:", I mean with a colon behind?
Oh, well, your strategy to allow them at top level could also be another path.


RE: Breaking loops - boriel - 02-16-2025

The only upgrade to this is allowing them in left-most position of every row. But still the example above is valid.
I will look how FreeBasic manages this though (Visual Basic doesn´t seem to allow labels at all).

EDIT: In fact, FreeBasic *REQUIRES* a colon after a label declaration: https://www.freebasic.net/wiki/ProPgLabels
I will enforce it so...


RE: Breaking loops - boriel - 02-16-2025

Try this beta an let me know:
http://www.boriel.com/files/zxb/zxbasic-v1.18.0-beta10.tar.gz
http://www.boriel.com/files/zxb/zxbasic-v1.18.0-beta10.zip
http://www.boriel.com/files/zxb/zxbasic-v1.18.0-beta10-win32.zip
http://www.boriel.com/files/zxb/zxbasic-v1.18.0-beta10-linux64.tar.gz
http://www.boriel.com/files/zxb/zxbasic-v1.18.0-beta10-macos.tar.gz

It will enforce declaring a label only at the beginning of the line, and followed by a colon.
So in your case, it won't compile and will show an error about BREAK() function not being defined.