Page 2 of 2 <12
Topic Options
#76833 - 2003-09-23 10:33 AM Re: SetOption options for UDFs
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Sure sounds right to me.

The script is interpreted, not compiled. This means that variables will be defined as they are parsed.

The variable which is assigned the value of the SetOption() is fully parsed before the SetOption(). This means that the variable will be automatically declared as a global before the SetOption() is called.

For similar reasons, the following will choke on $Undeclared, $gExplicitState will however be a valid global variable:
code:
$gExplicitState=SetOption("Explicit","ON")
$Undeclared=SetOption("Explicit",$gExplicitState)

And you shouldn't be surprised that the "OFF" in the following works, but the "ON" fails.
code:
$ON="ON"
$OFF="OFF"
$gNull=SetOption("NoVarsInStrings","$OFF")
$gNull=SetOption("NoVarsInStrings","$ON")



[ 23. September 2003, 10:38: Message edited by: Richard H. ]

Top
#76834 - 2003-09-23 02:09 PM Re: SetOption options for UDFs
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
My thoughts ...

The line would have been parsed.

The right side of the equation is evaluted to get a value and value type (string, double, etc)

Then the Left side evaluted and the proper variable created.

In my mind the setoption would have been completed before the $Explicit variable was created. Obviously I was wrong.

[ 23. September 2003, 14:09: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#76835 - 2003-09-23 04:01 PM Re: SetOption options for UDFs
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
I've confused things by writing as I think and mixing terminology.

The parsing phase happens early on, presumably converting to bytecode (tokenising). We know this happens because things like functions are identified and stored in memory.

At this parse stage the state of variables and the "explicit" setting are unknown. No variable definition occurs during this initial phase.

The interpreter then runs through the code. The interpreter will see the variable and will need to put a reference to it on the internal stack - at this stage it will determine whether the variable exists. The expression will then be added to the stack.

Now the expression is evaluated and the result pushed onto the stack. The result is popped, the next operator (assignment) is popped and the variable reference popped.

So, while the process evaluates the expression before assigning the result to the variable, the variable is declared when it is initially pushed onto the stack.

A simple test confirms that variables are only declared when needed, i.e. when "executed". This test will not error as the code with the undelcared variable is never executed:
code:
$gNull=SetOption("Explicit","ON")
If "true" Exit 0 Else $Undeclared=1 EndIf

Another test to confirm this - the messagebox will not appear as the variable has not been declared, and this is validated before the assignment:
code:
$gNull=SetOption("Explicit","ON")
$Undeclared=MessageBox("Test Message","Test Message")

A final test - there are two undeclared variables, but the one on the LHS of the assignment is the one that causes the error:
code:
$gNull=SetOption("Explicit","ON")
$Undeclared1=$Undeclared2


Top
#76836 - 2003-09-23 04:12 PM Re: SetOption options for UDFs
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
This is actually what I've seen, too. The variable on the LHS gets initialized before the variable(s) on the RHS get initialized. This might just be how KiXtart interprets/analyzes a line of code (left-to-right). The would also be consistent with the way number values are defined in numerical calculations.

[ 23. September 2003, 16:12: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.

Top
#76837 - 2003-09-23 04:17 PM Re: SetOption options for UDFs
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Thanks for the details. This morph'ed into an educational experience. Generally, I do not concern myself with the low level workings of KiX, but somehow caught the bug on this one.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#76838 - 2003-09-23 04:58 PM Re: SetOption options for UDFs
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
There is an exception of course [Roll Eyes]

The IsDeclared() function is an "impossible" function - it shouldn't be able to work as the expression (variable name) should be evaluated before the IsDeclared() function gets called. This would cause an error when "Explicit" is on and the variable is undeclared.

My guess is Ruud does a little bit of internal magic and that IsDeclared() is more like a language construct or operator rather than a function.

Top
#76839 - 2003-09-23 05:04 PM Re: SetOption options for UDFs
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
IIRC, IsDeclared does not work when using EXPLICIT=ON.
_________________________
There are two types of vessels, submarines and targets.

Top
#76840 - 2003-09-23 05:10 PM Re: SetOption options for UDFs
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
No, it works fine:
code:
$gNull=SetOption("Explicit","ON")
If IsDeclared($foo)
"Foo is a variable" ?
Else
"Foo is not a variable" ?
EndIf

This will print "Foo is not a variable"

Top
#76841 - 2003-09-24 05:05 PM Re: SetOption options for UDFs
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
Yes, kind-of. However, when using ISDECLARED to check whether a variable has been declared or not and then using this distinction to DIM a variable you end up having to use a GLOBAL due to scope issues.
code:
$gNull=SetOption("Explicit","ON")
If IsDeclared($foo)
"Foo is a variable" ?
dim $foo
Else
"Foo is not a variable" ?
EndIf
"Foo is a "+vartypename($foo)

_________________________
There are two types of vessels, submarines and targets.

Top
#76842 - 2003-09-24 05:25 PM Re: SetOption options for UDFs
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
No, you don't *have* to declare it as a global so long as you don't mind incurring the wrath of the Goto Police:
quote:
$gNull=SetOption("Explicit","ON")

If IsDeclared($foo) Goto FOO_IS_DECLARED EndIf
Dim $foo

:FOO_IS_DECLARED
"Foo is "+VarTypeName($foo) ?


Top
#76843 - 2003-09-24 05:28 PM Re: SetOption options for UDFs
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Argh! Yuck!

Need a new If construct (Like Perl)

code:
Dim $foo if not IsDeclared($foo)

_________________________
Home page: http://www.kixhelp.com/hb/

Top
#76844 - 2003-09-24 05:46 PM Re: SetOption options for UDFs
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
Sorry but I hacked my KiXtart version to no longer support GOTO. I also blackened out the particular section in the KiXtart Manual [Big Grin]
_________________________
There are two types of vessels, submarines and targets.

Top
Page 2 of 2 <12


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 370 anonymous users online.
Newest Members
Timothy, Jojo67, MaikSimon, kvn317, kixtarts2025
17874 Registered Users

Generated in 0.063 seconds in which 0.024 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org