Q Liberator FOR type error

Anything QL Software or Programming Related.
EmmBee
Trump Card
Posts: 240
Joined: Fri Jan 13, 2012 5:29 pm
Location: Kent

Q Liberator FOR type error

Post by EmmBee »

Can anyone please confirm that there is an error in the Q Liberator compiler?
When I run the following program, it prints out the numbers one to ten once, and then stops with a "For type error" at line 160 Q LIB Error 38.
As I see it, the "x" is protected by being local, and so a for type error shouldn't happen. Has anyone encountered this type of error?

Code: Select all

100 REMark Q Liberator version 3.36 For type error
110 :
120 first : second 1.5
130 :
140 DEFine PROCedure first
150 LOCal x
160   FOR x = 1 TO 10 : PRINT #0, ! x !
170 END DEFine
180 :
190 DEFine PROCedure second(x)
200   first
210 END DEFine
Michael Bulford


User avatar
tofro
Font of All Knowledge
Posts: 2689
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: Q Liberator FOR type error

Post by tofro »

Same with me here.
I don't think it is related to both x as argument and LOCal.
If you unroll the single-line FOR loop into FOR....ENDFOR lines, everything is fine

Cheers,
tofro


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
RWAP
RWAP Master
Posts: 2834
Joined: Sun Nov 28, 2010 4:51 pm
Location: Stone, United Kingdom
Contact:

Re: Q Liberator FOR type error

Post by RWAP »

That's a very obscure bug -

The problem comes when the Q-lib'd program tries to LOCAL the variable x which had been passed by reference.

Add line 155 PRINT #0;'TEST - ';x

You will see what I mean - x is set to a very small number at this point, rather than being cleared.
It is set to (in SMSQ/e and QPC2):
1.092133E-617

Oddly, if you just call first in the main program (so second is not called), X is set to
1.812649E-617

After the LOCal statement - mind you, the program does run.

Why the different values ?

It suggests the stack is being corrupted by x being passed by reference and then being made a local inside a second procedure.

Mind you, some bugs have only just come to light in Turbo, but at least we have the source codes for that in order to be able to track them down and fix them :)


EmmBee
Trump Card
Posts: 240
Joined: Fri Jan 13, 2012 5:29 pm
Location: Kent

Re: Q Liberator FOR type error

Post by EmmBee »

RWAP wrote: After the LOCal statement - mind you, the program does run.
Do you mean that the compiled code actually works and without any error message in SMSQ/e and QPC2?

The error happens when I execute the Q-lib'd obj file in Q-emuLator, but I have been unable to test this in SMSQ/e because I do not have this. The demo version of QPC2 will not allow me to execute any files of my own, and I cannot even compile any Q_liberator or Turbo tasks, and so am unable to check this. However, I have run this in S*BASIC and your TEST prints out "0" and not the tiny values you give.

We may not be able to fix any errors in Q_liberator, that is unless the authors ever decide to come back into business.

Michael
EmmBee


User avatar
tofro
Font of All Knowledge
Posts: 2689
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: Q Liberator FOR type error

Post by tofro »

Have you tried unrolling the single-line FOR statement onto a multiple line statement?
That works for me on SMSQ/E. Using the original code that you posted results in the very same error that you have mentioned.

Cheers
Tofro


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
RWAP
RWAP Master
Posts: 2834
Joined: Sun Nov 28, 2010 4:51 pm
Location: Stone, United Kingdom
Contact:

Re: Q Liberator FOR type error

Post by RWAP »

EmmBee wrote:
RWAP wrote: After the LOCal statement - mind you, the program does run.
Do you mean that the compiled code actually works and without any error message in SMSQ/e and QPC2?
Ermm - the original compiled code does not work without error message in SMSQ/e and QPC2.

What I meant was that if you only call the First procedure directly from within the program, rather than calling the Second procedure (which in turn calls the First procedure).

As I say it suggests some form of stack corruption.

However, changing the FOR loop into multiple lines does fix the issue - did anyone try adding the NEXT x or END FOR x to the single line FOR loop (I haven't had chance)


EmmBee
Trump Card
Posts: 240
Joined: Fri Jan 13, 2012 5:29 pm
Location: Kent

Re: Q Liberator FOR type error

Post by EmmBee »

tofro wrote:Have you tried unrolling the single-line FOR statement onto a multiple line statement?
That works for me on SMSQ/E. Using the original code that you posted results in the very same error that you have mentioned.
I have tried this - unrolling into the full FOR - END FOR construct, but can only try this on Q-emuLator, and I have the same problem. Unrolling doesn't solve the issue.

Michael
EmmBee


EmmBee
Trump Card
Posts: 240
Joined: Fri Jan 13, 2012 5:29 pm
Location: Kent

Re: Q Liberator FOR type error

Post by EmmBee »

RWAP wrote:What I meant was that if you only call the First procedure directly from within the program, rather than calling the Second procedure (which in turn calls the First procedure).
I have written this particular test code to illustrate what can happen. In a large program with many procedures and functions, one simply cannot remember all the variable names that have been used as parameters. I do everything correctly, according to the book, yet Q_liberator lets me down. It reports FOR loop errors when there are none.
However, changing the FOR loop into multiple lines does fix the issue -
This may fix the issue in SMSQ/E and QPC2 - but unfortunately I cannot check this myself. Could I please ask you to try this in Q-emuLator? I only have Q-emuLator to try things out on.

Michael Bulford


RWAP
RWAP Master
Posts: 2834
Joined: Sun Nov 28, 2010 4:51 pm
Location: Stone, United Kingdom
Contact:

Re: Q Liberator FOR type error

Post by RWAP »

No - I have now tried this and it doesn't fix the issue in smsq/e or q-emulator, which just underlines the fact that it is a q-emulator bug.

Interestingly, the bug remains if a second parameter is passed to the Second procedure, and also the removing the LOCal statement in First does not help.

If x is undefined when Second is called then the program runs OK.
Even more oddly is that if you change line 120 to read:

Code: Select all

120 x=1.5:first : second x
The program also works

I wonder if there is anything in the qliberator manual to help suggest what is causing this?


EmmBee
Trump Card
Posts: 240
Joined: Fri Jan 13, 2012 5:29 pm
Location: Kent

Re: Q Liberator FOR type error

Post by EmmBee »

Many thanks for checking. I have made an in-depth study of the Qliberator Manual. It says ... The control variable of a FOR..END FOR loop cannot be a formal procedure parameter or an error will occur.

Apparently the idea dates back to 1984 in the days of the AH and JM ROMs. In those days a SELect variable and also a FOR control loop variable had to be of type floating point. If this were not the case the program would not work as intended. To demonstrate this, consider the following program ...

Code: Select all

100 main 7 DIV 1
110 :
120 DEFine PROCedure main(i)
130   PRINT "i = "; i
140   SELect ON i = 7 : = REMAINDER : PRINT "That's not a 7"
150   FOR i = 6 TO 8 : PRINT "i = "; i
160 END DEFine 
Now, all SuperBASIC procedure and function parameters take their type from the incoming argument. The use of the SuperBASIC infix operator "DIV" in "7 DIV 1" makes that expression an integer type. When "main" receives this "7 DIV 1", the "i" will be instantiated to that value and will get to be of type integer, and not of type floating point. From thence forward we can expect trouble. Running this code on a JM QL produces the following output ...

i = 7
That's not a 7
i = 2051
i = 2051
i = 2052

This output is clearly incorrect!

We can now see what Qliberator has done. In order to prevent incorrect results from happening, Qliberator has deliberately stopped the program. When this happens, the line number and the error report of FOR type error appears on the screen. The idea is that the programmer would recognise was has happened and correct the code. So, Qliberator is doing a very good thing and is enhancing the QL experience! Owners of QLs with either an AH or a JM ROM would do well to get this compiler.

In my opinion, it is the implementation of this idea which is at fault: it goes too far and also stops programs that have been correctly coded.

Michael Bulford
EmmBee


Post Reply