VAL function in SuperBasic

Anything QL Software or Programming Related.
User avatar
Andrea.C
ROM Dongle
Posts: 8
Joined: Sat Aug 22, 2015 4:21 pm

VAL function in SuperBasic

Post by Andrea.C »

Hello all

In SuperBasic the VAL function does not exist because it is replaced by coercion which however is not as efficient as VAL.
Is there any toolkit that provides the VAL function on the QL?

Thank you

Andrea


Andrea from Italy :ugeek:
User avatar
tofro
Font of All Knowledge
Posts: 2685
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: VAL function in SuperBasic

Post by tofro »

Andrea,

in case you have Minerva or SMSQ/E, you can pipe the VAL "evaluation line" as a program line to an S*Basic child job and have the result piped back from there. Proven to work. Standard QL ROMs unfortunately can't do that.

I am not aware of any Toolkit command that would support VAL or similar.

Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
Andrea.C
ROM Dongle
Posts: 8
Joined: Sat Aug 22, 2015 4:21 pm

Re: VAL function in SuperBasic

Post by Andrea.C »

in case you have Minerva or SMSQ/E, you can pipe the VAL "evaluation line"
Yes but I need somothing similar to ZX Spectrum VAL function in QDOS.

I find this:

EVAL (6K) http://www.dilwyn.me.uk/tk/eval.zip - Two machine code functions allowing you to calculate numeric expressions from BASIC - many of the standard QL functions and up to three numeric variables may be used in the expressions. Assembler source included.

Can be usefull?

Andrea


Andrea from Italy :ugeek:
User avatar
Andrea.C
ROM Dongle
Posts: 8
Joined: Sat Aug 22, 2015 4:21 pm

Re: VAL function in SuperBasic

Post by Andrea.C »

EVAL (6K) http://www.dilwyn.me.uk/tk/eval.zip - Two machine code functions allowing you to calculate numeric expressions from BASIC - many of the standard QL functions and up to three numeric variables may be used in the expressions. Assembler source included.
I tried this software is that it is precisely the solution to the absence of the VAL function.
Thanks to the work of Thierry Godefroy

First you have to "compile" the string to evaluate with COMPIL$ and then you get the result with EVAL
function. For ex:

100 expr$="3+4*(5-2)/((342-1)*3+4*(7/2))"
110 err%=0
120 comp_exp$=COMPIL$(expr$)
130 result=EVAL(err%,comp_exp$,0)
140 IF err%=0
150 PRINT "Result: ";result
160 ELSE
170 PRINT "Error n. ";err%
180 END IF


Andrea from Italy :ugeek:
User avatar
dilwyn
Mr QL
Posts: 2753
Joined: Wed Dec 01, 2010 10:39 pm

Re: VAL function in SuperBasic

Post by dilwyn »

Andrea.C wrote:
EVAL (6K) http://www.dilwyn.me.uk/tk/eval.zip - Two machine code functions allowing you to calculate numeric expressions from BASIC - many of the standard QL functions and up to three numeric variables may be used in the expressions. Assembler source included.
I tried this software is that it is precisely the solution to the absence of the VAL function.
Thanks to the work of Thierry Godefroy

First you have to "compile" the string to evaluate with COMPIL$ and then you get the result with EVAL
function. For ex:

100 expr$="3+4*(5-2)/((342-1)*3+4*(7/2))"
110 err%=0
120 comp_exp$=COMPIL$(expr$)
130 result=EVAL(err%,comp_exp$,0)
140 IF err%=0
150 PRINT "Result: ";result
160 ELSE
170 PRINT "Error n. ";err%
180 END IF
Thank you Andrea. Although this function is on my website, I have never really used it. I notice from the instructions it can take up to three numeric variables x,y,z too. Not quite a full VAL like Spectrum BASIC but still potentially very useful.

I looked in Quanta Library Guide http://quanta.org.uk/software-library/q ... e-version/ just now, there are the following expression evaluators there too which might help if you are a member of Quanta.

Disk UT01 - "QL VAL" by Manuel Garcia
The function VAL added to S/basic. Evaluates a string containing an arithmetic expression. This overcomes the weakness of “coercion” in S/Basic, & adds an useful new facility. 2 examples supplied, & a version compatible with the Turbo & Supercharge compilers from D.P. Ltd. Associated files : Readme_doc, Qlval_boot, Qlval_cde, Examples_bas, Qlval_asm, Qlval2_cde, Qval2_boot, Cmp_doc, Runtime_exts, Runtime_boot, Numer_boot, Numeric_integration, Translat_doc.

DISK MA02 - "J_EVALUATOR" by J Haftke
The description reads: "An excellent calculator which copes with complex expressions. Assoc: Files: j_evaluator, j_eval_bin, qlib_run, ramdisc, j_boot."

I'll have to look into the copyright staus of these to see if they are restricted to Quanta library or not, to see if I can make them available.


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

Re: VAL function in SuperBasic

Post by EmmBee »

tofro wrote:
in case you have Minerva or SMSQ/E, you can pipe the VAL "evaluation line" as a program line to an S*Basic child job and have the result piped back from there.
Hi tofro,

Can you give an example of how this can be done in QPC2, please.

EmmBee


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

Re: VAL function in SuperBasic

Post by EmmBee »

Hi all,
I can recommend this attempt at an expression evaluator ...

Code: Select all

DEFine FuNction VAL(x$)
LOCal result
  result = 0
  IF x$ <> FILL$(" ",LEN(x$))
    COMMAND_LINE
    TYPE_IN "result = " & x$ & CHR$(10)
    TYPE_IN "CONTINUE" & CHR$(10)
    STOP
  END IF
  RETurn result
END DEFine VAL
:
REPeat loop
  INPUT "Expn? "; e$
  PRINT " = "; VAL((e$))
END REPeat loop
The above needs the Turbo toolkit and works by typing into the Interpreter's command line. It is only after the temporary STOP that the typed in commands can be read and acted upon. The CONTINUE statement causes the VAL function to resume and then returns the result of the expression entered. Everything tidies up nicely. Any and all current variables can be used, along with any currently defined functions. This code only works in S*BASIC - not Turbo compiled, and there is no error trapping, except for the error messages given by the Interpreter. Any errors give a result of 0.

EmmBee


stevepoole
Super Gold Card
Posts: 712
Joined: Mon Nov 24, 2014 2:03 pm

Re: VAL function in SuperBasic

Post by stevepoole »

Hi,
EmmBee's program works well on my SGC QL, but not under SMSQ, nor therefore with QPC2, as stated.
This is because of the way SMSQ handles the keyboard queues. (Under SMSQ, ESCape from the program by hitting the 'windows' key...)

To improve error messages :
in place of result=0 let result$=x$
Change TYPE_In "result"... to "result$"...
append END IF : result=result$

This will at least halt the program when something is amiss, in two different ways, (ANd allow expressions to evaluate to zero).

As for SMSQ, I can find no solution to the problem... as even QPC2 gets confused with keyboard queues occasionally! (eg Quill data getting into superbasic #0 ).
Regards,
Steve Poole.


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

Re: VAL function in SuperBasic

Post by EmmBee »

Perhaps experimenting with the keyboard settings on QPC2's front panel could make a difference to the keyboard queue handling.

Taking Steve's idea a bit further, we can implement a more robust version of VAL by using S*BASIC's coercion ability, to return any data type, coerced into a string. If our new function is called VAL$, we could do things such as ...
n = VAL$("2+3+4+5")
x$ = VAL$("'ABC' & '___' & 'xyz'")
n% = VAL$("33 DIV 2")
Here's the new function ...

Code: Select all

100 DEFine FuNction VAL$(expn$)
110   IF expn$ = FILL$(" ",LEN(expn$)) : RETurn expn$
120   COMMAND_LINE
130   TYPE_IN "RETurn " & expn$ & CHR$(10)
140   STOP
150 END DEFine VAL$
160 :
170 REPeat loop
180   INPUT "Expn? "; e$
190   PRINT " = "; VAL$((e$))
200 END REPeat loop
The STOP command causes a return to the command interpreter, and allows the TYPED_IN command to do its work. The RETurn statement causes VAL$ to continue, and returns the result of the expression.

When developing some S*BASIC program, I may wish to stop at same place in the code to check that all is going as expected. Usually it is the value of variables that will need to be checked, but also the value of some expressions can also be useful. This is where these expression evaluators can come in very handy. The actual values may not need to be be recorded, but just need to be displayed on the screen. Helpfully, what could be displayed is something along these lines ...
x = 123
y = 200
dev$ = ram5_
Displaying this info on the screen can avoid the need of having write them down on paper. Using the same technique as used in VAL$, here is Vprint ...

Code: Select all

220 DEFine PROCedure Vprint
230 LOCal lp, Lin$(1024), qt$(1)
240   REPeat lp
250     INPUT #0,"Vprint "; Lin$ : IF Lin$ = "" : RETurn
260     IF "'" INSTR Lin$ : qt$ = '"' : ELSE qt$ = "'"
270     IF qt$ INSTR Lin$
280       PRINT #0," - Can only use one quote mark - not both" : NEXT lp
290     END IF
300     COMMAND_LINE
310     TYPE_IN "PRINT ! " & qt$ & Lin$ & " =" & qt$ & " ! " & Lin$ & CHR$(10)
320     IF COMPILED
330       PAUSE 50 : REMark Really need to wait until SB completes with no bad line
340     ELSE
350       TYPE_IN "CONTINUE" & CHR$(10) : STOP
360     END IF
370   END REPeat lp
380 END DEFine Vprint
Notice that the STOP command is there, as with VAL$, which is necessary for this code to work. This procedure can be Turbo compiled or Interpreted. It would be a better performer compiled, since there would be no local variables possibly interfering and producing incorrect results. The code would need to be added to, to cope with the multi-tasking of the Turbo code and S*BASIC. Possibly a HOT_DO command may be needed. Also, as noted, Turbo would need to wait until the interpreter's cursor is at the left edge of the window, with no bad line, before continuing and displaying the prompt again. This additional work could be quite involved.

Best regards,
EmmBee


User avatar
ql_freak
Gold Card
Posts: 353
Joined: Sun Jan 18, 2015 1:29 am

Re: VAL function in SuperBasic

Post by ql_freak »

stevepoole wrote:Hi,
EmmBee's program works well on my SGC QL, but not under SMSQ, nor therefore with QPC2, as stated.
It worked here on my QPC2, but the problem is, that it doesn't return an error, if there is an "error in expression". In addition it does not work with MultiBASIC jobs.

So I have improved the program. It is now compatible with MultiBASIC and it needs a second string parameter, which must be a string variable, as it is assigned an error message, so that you can test afterwards if there was an error. If there was no error (a correct expression was passed in the first parameter) this variable is set to the empty string ("").

I also have renamed the function, cause it is no BASIC VAL() function. Look what VAL in Visual BASIC, C64 BASIC, ... does. The Spectrum VAL() function is special. So I choosed the name EVAL(expr$,error_result$). This is the same function name as in Python (which also has such a nice function). BTW: A normal BASIC compatible VAL() function (not Spectrum VAL) would be useful too.

Here is the code together with a simple test program, Turbo Toolkit Version 3.40 or higher(!) and SuperToolkit II is required:

Code: Select all

100 REMark Test for EVAL(string$,error_result$) SuperBASIC function
110 :
120 a$="10":rorre$="":REMark default no error
130 a=EVAL("3*4+a$",rorre$)
140 IF (rorre$ <> "")
150   PRINT rorre$
160 ELSE
170   PRINT a
180 END IF
30000 :
32100 REMark Support PROCedures AND FuNctions
32110 :
32120 DEFine FuNction EVAL(x$,err$)
32130   LOCal result,test$
32140   err$="":pi%=FOPEN("CON")
32150   po%=FOPEN("CON"):CLOSE#po%:CLOSE#pi%
32160   IF FOP_NEW(#po%,"PIPE_1024"):err$="Cannot open PIPE":RETurn 0
32170   CONNECT po% TO pi%
32180   result = 0
32190   test$="PRINT#po%,"&x$
32210   IF x$ <> FILL$(" ",LEN(x$))
32220     COMMAND_LINE JOBID()
32260     TYPE_IN test$&CHR$(10)
32270     TYPE_IN FILL$(CHR$(194),LEN(test$))&"CONTINUE"&CHR$(10):STOP
32280     CLOSE#po%:IF EOF(#pi%):CLOSE#pi%:err$="Error in expression":RETurn 0
32290     CLOSE#pi%
32300     TYPE_IN "result = " & x$ & CHR$(10)
32340     TYPE_IN "CONTINUE" & CHR$(10)
32380     STOP
32420   END IF
32460   RETurn result
32500 END DEFine EVAL


http://peter-sulzer.bplaced.net
GERMAN! QL-Download page also available in English: GETLINE$() function, UNIX-like "ls" command, improved DIY-Toolkit function EDLINE$ - All with source. AND a good Python 3 Tutorial (German) for Win/UNIX :-)
Post Reply