#76833 - 2003-09-23 10:33 AM
Re: SetOption options for UDFs
|
Richard H.
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
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 ]
|
Top
|
|
|
|
#76835 - 2003-09-23 04:01 PM
Re: SetOption options for UDFs
|
Richard H.
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
|
|
|
|
#76837 - 2003-09-23 04:17 PM
Re: SetOption options for UDFs
|
Howard Bullock
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.
|
Top
|
|
|
|
#76838 - 2003-09-23 04:58 PM
Re: SetOption options for UDFs
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
There is an exception of course
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
|
|
|
|
#76840 - 2003-09-23 05:10 PM
Re: SetOption options for UDFs
|
Richard H.
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
|
|
|
|
#76842 - 2003-09-24 05:25 PM
Re: SetOption options for UDFs
|
Richard H.
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
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)
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 528 anonymous users online.
|
|
|