(Nearly) solved: FuNction which tests if a variable is set

Anything QL Software or Programming Related.
User avatar
ql_freak
Gold Card
Posts: 353
Joined: Sun Jan 18, 2015 1:29 am

(Nearly) solved: FuNction which tests if a variable is set

Post by ql_freak »

Is there a S(uper)BASIC-function which tests if a variable is set?

e.g.:

Code: Select all

REMark Turbo Toolkit required for END_CMD
IF NOT variableSet(a_%):END_CMD
REMark Alternative: IF NOT variableSet("a_%"):END_CMD
REMark Continue with command file only if a_% is set
REMark ... (your commands)
END_CMD
In SBASIC (SMSQ/E on QPC2) you can use:

IF NOT a_%:END_CMD

But this does unfortunately not (even) work on Minerva :-( It is also not what I want, cause if a_% is 0 the command file will always stop. But perhaps the command file should continue and use the value 0.

EDIT:

Found a function from Turbo Toolkit: BASIC_INDEX%(name$) does it. Returns a value <0 if no such name exists.

A note in the Keyword Reference Page says there is a fix (TurboFix_bin), which allows using BASIC_INDEX%() even with MultiBASICs. Where can I obtain Turbofix_bin? Without BASIC_INDEX%() always references the name table of the initial S(uper)BASIC interpreter (Job-ID 0).


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 :-)
RWAP
RWAP Master
Posts: 2834
Joined: Sun Nov 28, 2010 4:51 pm
Location: Stone, United Kingdom
Contact:

Re: (Nearly) solved: FuNction which tests if a variable is set

Post by RWAP »

ql_freak wrote: A note in the Keyword Reference Page says there is a fix (TurboFix_bin), which allows using BASIC_INDEX%() even with MultiBASICs. Where can I obtain Turbofix_bin? Without BASIC_INDEX%() always references the name table of the initial S(uper)BASIC interpreter (Job-ID 0).
TurboFix_bin was released by me to help users of MasterBASIC - see http://www.dilwyn.me.uk/turbo/index.html


User avatar
Dave
SandySuperQDave
Posts: 2765
Joined: Sat Jan 22, 2011 6:52 am
Location: Austin, TX
Contact:

Re: (Nearly) solved: FuNction which tests if a variable is set

Post by Dave »

I think there's an ambiguity in the question.

You might want to check if a variable name is set (though, is there a use case for this?)

You might want to check if a variable has had a value set. Is it a value or null (aka "*")?

If a variable has ever had a value set, is there ever a way to reset it to no value set? Is there even a use case for this?


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

Re: (Nearly) solved: FuNction which tests if a variable is set

Post by ql_freak »

Dave wrote:I think there's an ambiguity in the question.

You might want to check if a variable name is set (though, is there a use case for this?)
Of course! E.g. in command files (without line numbers) where you want to pass parameters:

Code: Select all

IF BASIC_INDEX%("x_%")<0:PRINT#0,"x_% (or y_%) not set! Aborting":END_CMD
IF BASIC_INDEX%("y_%")<0:PRINT#0,"y_% (or x_%) not set! Aborting":END_CMD
IF x_%<0 OR x_%>512:PRINT#0,"x_% must be in range 0..512!":END_CMD
IF y_%<0 OR y_%>510:Print#0,"y_% must be in range 0..510!":END_CMD
PRINT#0,"Reset SuperBASIC origin to "&x_%&", "&y_%&"?";
CURSEN#0:a_$=INKEY$(#0,-1):CURDIS#0
IF a_$<>'y'AND a_$<>'Y':PRINT#0,"User abort":END_CMD
BORDER#0,2:BORDER#1,0:BORDER#0,0
PAPER#2,0,2:PAPER#1,0:PAPER#0,0:CLS#2:CLS#1:CLS#1
REMark x%=0:y%=0
BORDER#2,0:BORDER#1,0:BORDER#0,0:CLS#2:CLS#1:CLS#0
WINDOW#2,256,202,x_%,y_%:WINDOW#1,256,202,x_%+256,y_%:WINDOW#0,512,54,x_%,y_%+202
CLS#0:WINDOW#0,512,54,x_%,y_%+204
BORDER#2,1,255:BORDER#1,1,255:BORDER#0,1,255
PAPER#2,7:PAPER#1,2:PAPER#0,0:CLS#2:CLS#1:CLS#0
END_CMD
If you use this with the following hotkey:

Code: Select all

ERT HOT_KEY("r","x_%=:y_%=:DO'resetSB_bat'"&FILL$(CHR$(192),21))
Gives:

Code: Select all

x_%=<Cursor is here>:y_%=:DO'resetSB_bat'
You can easily move SuperBASIC around by setting values to x_% and y_%, without loosing the currently loaded program, variables, ...

If there are commands (I don't know any), which could read the current PAPER, INK, STRIP, SIZE, BORDER of a window, it would even be possible to move the whole SuperBASIC windows #0 to #2 without resetting them to the default.

Without checking that x_% and y_% are set, this command file will have strange effects (if x_% or y_% doesn't exist) and leaves the command file open, because END_CMD will never be called (well known bug, which is even present in Minerva).
Dave wrote:You might want to check if a variable has had a value set. Is it a value or null (aka "*")?
Yes, I must be able to detect if a variable exists and in case of an integer variable it always has a value, it cannot be "nothing" (empty) like a string variable.
Dave wrote:If a variable has ever had a value set, is there ever a way to reset it to no value set? Is there even a use case for this?
I don't think this is possible.
Last edited by ql_freak on Tue Apr 18, 2017 8:19 pm, edited 1 time in total.


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 :-)
User avatar
tofro
Font of All Knowledge
Posts: 2685
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: (Nearly) solved: FuNction which tests if a variable is set

Post by tofro »

Surely a nice and entertaining exercise trying to move SB windows about - But, the WMOV command that is part of the now free EasyPtr package (see Marcel's site) does that using a machine code extension nicely and interactively, if you want.

Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
ql_freak
Gold Card
Posts: 353
Joined: Sun Jan 18, 2015 1:29 am

Re: (Nearly) solved: FuNction which tests if a variable is set

Post by ql_freak »

Yes it's nice but it has one disadvantage: After a WMOV I cannot set the windows beyond the outline.After a WMOV my script doesn't run anymore. Error: out of range. At least on uQLx with Minerva 1.98 the SuperBASIC behaves strange when trying to open Windows after a WMOV. I can then only open a CON-channel with maximum size of 512x256a0x0, but it's origin is not relative to screen, but relative to top left of channel #2.

I think WMOV is nice for real programs (compiled or run as MultiBASIC) but not so good for normal SuperBASIC (Job 0), which serves most time as a command shell.

BTW: I have edited the script (my message). The maximum y position is now 510 (bigger values for x_% give out of range). It is for a 1024x768 screen.


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 :-)
User avatar
janbredenbeek
Super Gold Card
Posts: 629
Joined: Wed Jan 21, 2015 4:54 pm
Location: Hilversum, The Netherlands

Re: (Nearly) solved: FuNction which tests if a variable is set

Post by janbredenbeek »

Dave wrote:I think there's an ambiguity in the question.

You might want to check if a variable name is set (though, is there a use case for this?)
A variable always has a name as soon as it's entered into the program.
You might want to check if a variable has had a value set. Is it a value or null (aka "*")?
If it has a value, it's name type (first byte of NT entry) will be 2, else 0. Also, the longword at bytes 4-8 in the NT entry will be a pointer (offset) to the value in the VV area. If it has no value this longword will hold -1.
If a variable has ever had a value set, is there ever a way to reset it to no value set? Is there even a use case for this?
Probably, by setting the name type to 0, VV pointer to -1 and releasing the space in the VV area. Don't know if this would have undesired side-effects though.

Jan.


User avatar
janbredenbeek
Super Gold Card
Posts: 629
Joined: Wed Jan 21, 2015 4:54 pm
Location: Hilversum, The Netherlands

Re: (Nearly) solved: FuNction which tests if a variable is set

Post by janbredenbeek »

ql_freak wrote:Is there a S(uper)BASIC-function which tests if a variable is set?
It just occurred to me that the TK2 PARUSE function (not to be confused with PAR_USE!) does the job.
From the TK2 manual:

Code: Select all

16  Procedure Parameters

In QL SuperBASIC procedure parameters are handled by substitution: on
calling a procedure (or function), the dummy parameters in the
procedure definition become the actual parameters in the procedure
call. The type and usage of procedure parameters may be found with two
functions:

     PARTYP (name)                      find type of parameter
     PARUSE (name)                      find usage of parameter

     the type is  0 null            the usage is  0 unset
                  1 string                        1 variable
                  2 floating point                2 array
                  3 integer
Jan.


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

Re: (Nearly) solved: FuNction which tests if a variable is set

Post by EmmBee »

Is there a S(uper)BASIC-function which tests if a variable is set?
The DIY toolkit volume "P" has a function called "UNSET". See: http://www.dilwyn.me.uk/tk/index.html
This returns 1 if no value has been set, otherwise 0.
E.g. okay = NOT UNSET(var%)
If there are commands (I don't know any), which could read the current PAPER, INK, STRIP, SIZE, BORDER of a window, it would even be possible to move the whole SuperBASIC windows #0 to #2 without resetting them to the default.
Again, the DIY toolkit comes to the rescue here.
See Volume "Q" for the CHBASE function, which returns base addresses of Window Definition Blocks,
and Volume "C" to read the CHANDATA_DOC. This will detail how to read the current values of PAPER, INK, STRIP, etc.
This DIY toolkit is all good stuff.

EmmBee


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

Re: (Nearly) solved: FuNction which tests if a variable is set

Post by ql_freak »

janbredenbeek wrote:
ql_freak wrote:Is there a S(uper)BASIC-function which tests if a variable is set?
It just occurred to me that the TK2 PARUSE function (not to be confused with PAR_USE!) does the job.
I have found this function before I asked here. But I'm afraid that works only in functions and procedures. I need a function which can be used in Command files. As stated BASIC_INDEX% from Turbo Toolkit is fine :-)


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