Page 1 of 3

Loopy bugs

Posted: Wed Jul 14, 2021 4:43 pm
by pjw
Rather than dumping on poor Dr Jim, perhaps this sort of thing should have a topic of its own.
Those of you on ql-user's will, unfortunately, have seen this one quite recently:

I dont know if this has been documented anywhere, but Im putting it out here as it caused me some grief.

It appears that Q-Liberator zeroes the loop variable on entry to a loop. The following demo, which is acceptable (although perhaps not very elegant) S*BASIC, will not work in the same way in SuperBASIC or when Qlib compiled.

Code: Select all

100 loop = 3
110 cnt = 0
120 PRINT 'Start:'! loop, cnt
130 REPeat loop
140  PRINT loop, cnt
150  cnt = cnt + 1
160  IF cnt >= loop: EXIT loop
170 END REPeat loop
180 PRINT 'End:'! loop, cnt
In the Qlib-compiled version the loop is exited after a single run as the condition cnt >= loop is met immediately, to wit 1 >= 0

The worrying part of this is that while I was figuring out what was wrong, running an embedded routine like this many times, the system crashed due to memory corruption. Whether this was due to the SBASIC or Qlib compiled version (or neither) I cant say. So just beware and keep on progging!

NB: This works across the board, though, as long as you expect loop to be 0

Code: Select all

110 cnt = 3
120 PRINT 'Start:'! loop, cnt
130 REPeat loop
140  PRINT loop, cnt
150  loop = loop + 1
160  IF loop >= cnt: EXIT loop
170 END REPeat loop
180 PRINT 'End:'! loop, cnt

Re: Loopy bugs

Posted: Thu Jul 15, 2021 4:59 am
by bwinkel67
I'm not a proficient SuperBASIC user but it's an odd example. I look at "loop" in your example as the REPEAT identifier and not a variable that I would assign anything to or use. So to me it's not like "FOR i=1 TO 10" where you then say "NEXT i" since that syntax asks to increment. With "REPEAT i" you do an "EXIT i" which says to leave the loop. Am I right to assume you an also do a "NEXT i" in a REPEAT? If so then that would be similar to "continue" in C.

Re: Loopy bugs

Posted: Thu Jul 15, 2021 6:49 am
by stevepoole
Hi Bwinkel,

On a QL, NEXT will increment in a FOR loop, but not in a REPeat loop.
The QL uses NEXT in FOR loops to activate 'loop epilogues', but not in REPeat loops :

100 FOR loop=1 TO 5
110 PRINT loop !!
120 NEXT loop: REMark increments loop and falls through to epilogue on loop=5...
130 PRINT 'end'
140 END for loop
150 :
160 loop=1
170 REPeat loop
180 PRINT loop !!
190 NEXT loop: REMark doesn't increment loop nor fall through to epilogue !!
200 PRINT 'end'
210 if loop=5: EXIT loop
220 END REPeat loop

Regards, Steve.

Re: Loopy bugs

Posted: Thu Jul 15, 2021 10:09 am
by NormanDunbar
Maybe I'm just weird (don't all agree at once!) Or maybe because I used other programming languages, but using a repeat loop name which corresponds to another variable name seems a little daft to me? Happy to discuss if you think otherwise though. I might learn something new.

@bwinkle67

Yes, you are correct, NEXT XXX in a REPeat XXX, loop is the same as continue in C. It returns to the first executable statement after the REPeat statement.


Cheers,
Norm.

Re: Loopy bugs

Posted: Thu Jul 15, 2021 10:50 am
by Martin_Head
pjw wrote:It appears that Q-Liberator zeroes the loop variable on entry to a loop. The following demo, which is acceptable (although perhaps not very elegant) S*BASIC, will not work in the same way in SuperBASIC or when Qlib compiled.
Yes it does. If you decompile it, you will find a variable assignment of zero.

SuperCharge and Turbo does not produce any compiled code for a REPEAT statement.

Re: Loopy bugs

Posted: Thu Jul 15, 2021 11:14 am
by dilwyn
Martin_Head wrote:
pjw wrote:It appears that Q-Liberator zeroes the loop variable on entry to a loop. The following demo, which is acceptable (although perhaps not very elegant) S*BASIC, will not work in the same way in SuperBASIC or when Qlib compiled.
Yes it does. If you decompile it, you will find a variable assignment of zero.

SuperCharge and Turbo does not produce any compiled code for a REPEAT statement.
Having noticed this in the past and avoided using the "feature" in compiled programs over the years, I'm glad to find it wasn't me doing something wrong after all these years.

Thanks for the explanation. Useful to have this documented.

Re: Loopy bugs

Posted: Thu Jul 15, 2021 3:47 pm
by RichardCGH
The online SuperBasic manual has some interesting comments on FOR loops -
including how different ROMs will process some FOR statements differently.
And some compilers may also have issues.
And SMS.

https://superbasic-manual.readthedocs.i ... F/for.html

Re: Loopy bugs

Posted: Fri Jul 16, 2021 4:11 am
by bwinkel67
NormanDunbar wrote:Maybe I'm just weird (don't all agree at once!) Or maybe because I used other programming languages, but using a repeat loop name which corresponds to another variable name seems a little daft to me? Happy to discuss if you think otherwise though. I might learn something new.
That's what I was trying to articulate and likely too because I've used other languages.

Re: Loopy bugs

Posted: Fri Jul 16, 2021 6:39 am
by stevepoole
Hi Bwinkel,

You can use use a specific name for the EXIT variable with a REPeat loop, if you wish.

Indeed, REPeat under SMSQ/E can work without a loop variable ! (Loop variables were retained for backward compatibility).

But the fact is that NEXT never increments REPeat counts, as it would using the FOR variable control list.

NEXT was ONLY retained by Jan Jones in BASIC to allow 'loop epilogues'. (This was why she adopted END loop statements).

In some cases, you can neatly GOTO the END line to increment the FOR variable to avoid the epilogue, (but some programmeurs will get riled up....).

Transcoding QL Basics to Pascal or 'C++' can be very cumbersome where the FOR control lists are complex, (as in EmmBees examples). But Basic is SO easy to program !

So, if you want transcodability, write your programs with REPeat loops only.... Regards, Steve.

Re: Loopy bugs

Posted: Fri Jul 16, 2021 12:41 pm
by pjw
First of all lets not get hung up in collateral issues when discussing what are meant to be illustrative examples. A lot of these anomalies were discovered through mistakes or blue sky experimentation, and Im just pointing out what I found, not necessarily endorsing any particular quirk or method. Some parts of the illustrative code may contain now meaningless remnants of the code they were snipped from. Etc, etc. I hope this collectively deals with a number of objections and comments above.