|
By Andrew J. Wozniewicz
Milwaukee, August 8, 2008
Somewhat counterintuitive to a Pascal programmer is the fact that the order
of declarations does not matter in WANTScript.
For example, variables can be used in statements before they are declared
(in terms of their position in the script file). This is possible, because
WANTScript interpreter - unlike the Pascal compiler - is multi-pass and can
resolve the names that are defined further down in the source file, as long
as they are defined at all.
Here is a simple example that illustrates this point:
project Test040
WriteLn("The answer is: ",X)
WriteLn("What was the question?")
var X := TheUltimateAnswer
const TheUltimateAnswer = 42
end
Notice how the declarations occur in exactly the opposite order to what you
would expect in a Pascal program. The variable X is referred to in the first statement (WriteLn), even though it is declared two statements below. WANTScript behaves more like C/C++ in
this regard.
The output, of course, is:
The answer is: 42
What was the question?
In general, identifiers declared in a module have visibility in the entire
module, including any embedded modules.
Technically, at this point of the language evolution (this will undoubtedly change), all identifiers have public visibility, that is, they are also visible outside the module in which they are defined, but the references to them must either be fully qualified (prefixed with the complete module path), or their enclosing module must be explicitly imported.
Here is another example that involves an embedded module (a procedure):
program Test044
TestInner
WriteLn(H," and ", G)
procedure TestInner
H := H + '!'
WriteLn('Variables can be defined after they are used: ',H)
WriteLn('As long as they are in scope: ',G)
end
var H = "Hello"
var G = "Goodbye!"
end
The output of this program is
Variables can be defined after they are used: Hello!
As long as they are in scope: Goodbye!
Hello! and Goodbye!
-Andrew
|