Posts: 27
Threads: 10
Joined: Nov 2024
Reputation:
0
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
Posts: 1,838
Threads: 56
Joined: Aug 2019
Reputation:
25
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
Posts: 27
Threads: 10
Joined: Nov 2024
Reputation:
0
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
Posts: 1,838
Threads: 56
Joined: Aug 2019
Reputation:
25
Sorry, I don’t understand. Break is not in the parser. BREAK equivalent is EXIT as in other modern Basic dialects. See docs.
---
Boriel
Posts: 27
Threads: 10
Joined: Nov 2024
Reputation:
0
02-10-2025, 09:23 AM
(This post was last modified: 02-10-2025, 09:43 AM by baltasarq.)
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.
-- Baltasar
Posts: 1,838
Threads: 56
Joined: Aug 2019
Reputation:
25
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.
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
Posts: 1,838
Threads: 56
Joined: Aug 2019
Reputation:
25
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:
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
Posts: 27
Threads: 10
Joined: Nov 2024
Reputation:
0
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
Posts: 1,838
Threads: 56
Joined: Aug 2019
Reputation:
25
02-16-2025, 09:29 PM
(This post was last modified: 02-16-2025, 09:31 PM by boriel.)
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
Posts: 1,838
Threads: 56
Joined: Aug 2019
Reputation:
25
|