Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Breaking loops
#1
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.
-- Baltasar
Reply
#2
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)
---
Boriel
Reply
#3
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! ;-) ).
-- Baltasar
Reply
#4
Sorry, I don’t understand. Break is not in the parser. BREAK equivalent is EXIT as in other modern Basic dialects. See docs.
---
Boriel
Reply
#5
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
-- Baltasar
Reply
#6
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.
---
Boriel
Reply
#7
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
---
Boriel
Reply
#8
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.
-- Baltasar
Reply
#9
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...
---
Boriel
Reply
#10
Try this beta an let me know:
http://www.boriel.com/files/zxb/zxbasic-...a10.tar.gz
http://www.boriel.com/files/zxb/zxbasic-...beta10.zip
http://www.boriel.com/files/zxb/zxbasic-...-win32.zip
http://www.boriel.com/files/zxb/zxbasic-...x64.tar.gz
http://www.boriel.com/files/zxb/zxbasic-...cos.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.
---
Boriel
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)