The Script Programming Language/Data Types and Variables
Data Types and Variables
Data Types and Variables
All numbers in Script are 16 bit integers, giving a range of -32768 to +32767. Numbers may be written as decimal (1024), hex ($400), or binary (%10000000000).
Variables hold numbers. Variables can be either global, local, or temporary, depending on when they are created and destroyed:
Global variables are created when the program starts and destroyed when it ends, and are thus accessible to all scripts at all times.
Local variables are created when a logic script is loaded and destroyed when it is purged. They are thus only available when the logic script is loaded and will not retain a value through a purge-reload cycle. You will find that, as your programming takes on a more object-oriented flavor, you will use fewer and fewer local variables.
Temporary variables are created when a procedure or method is entered and destroyed when it is left. They are thus only available to the declaring procedure and do not retain a value between calls to the procedure.
In order to throw the 'link' out of the traditional 'edit-compile- link-test' cycle of software development, YOU, rather than the linker, must define the address (i.e. variable number) of global variables. This is done with the global definition:
Code:(global var-name var-number var-name var-number ... )
This defines var-name to be global variable number var-number.
Local variables, not being accessible outside of the scripts in which they are declared and thus not requiring linking, can have their addresses set by the Script compiler. There are two ways of defining locals:
Code:(local var-name var-name ... )
defines a single variables with the names var-name.
Code:(local [array-name n])
defines an array of n elements with the name array-name (the brackets in this do NOT mean 'optional' -- they are required).
Multiple local variable definitions may be combined in one statement:
Code:(local var1 [array1 10] [array2 5] var2 . . . )
Temporary variables will be discussed in the section on user-defined procedures.
Define and enum statements may be included within both global and local variable definitions.
To access element n of the array anArray, write
Despite the syntactic difference between local variable declarations and local array declarations, there really is no distinction between variables and arrays -- any variable may be indexed as an array. Thus, if we have the local variable declarations
Code:(local var1 var2 var3 var4 )
we can set the value of var1 to that of var4 by any of the following statements:
Code:(= var1 var4) (= var1 [var2 2]) (= var1 [var3 1]) (= [var2 -1] [var1 3])
The first method is obviously the preferred method for clarity, but this array property of all variables allows access to variable numbers of parameters in a user-defined procedure (see section on user-defined procedures).
This property of variables is also the basis of the method by which you declare global arrays -- you simply leave an array-sized gap in the global variable numbering sequence. To declare var2 as a global array of 10 elements, write
Code:(global var1 23 var2 24 ;10 element array var3 34 )
and access var2 as an array:
Some kernel calls require pointers to variables, rather than the value of a variable. A pointer to a variable is created by preceding a variable reference with the '@' sign. Pointers may be created to array elements as well as to simple variables:
Code:@ego ;pointer to the variable ego @[foo 3] ;pointer to fourth element of array foo
Since there is currently no way in sc to dereference a pointer, this is only useful for passing pointers to kernel calls.
Text strings are strings of characters enclosed in double quotes, and may be used anywhere you like:
Code:(Print "This is immediate text.")
prints the text string,
Code:(= textToPrint "This text is referenced through a variable.")
sets the variable to a pointer to the text string, and
Code:(instance foo of Bar (properties name:"fooBar" ) )
sets the name property of foo to be a pointer to the text string.
When sc goes to squirrel a text string away, it first checks to see if it has seen the string before. If so, it just uses the previous text, rather than duplicating the text. For long text strings which are used in several places, however, the likelihood that you will manage to type the text identically in each case is small. In this case you can simply put the text in a define statement
Code:(define lotsOftext "This is a long text string. I am using a define statement to avoid having to type it repeatedly.")
This introduces another aspect of text strings: If text is too long fit on a single line, you may enter it on several lines. Multiple white-space (spaces, tabs, and newlines) gets converted to a single space, so the text above ends up with just one space between the words on each line. If you want multiple spaces, enter them as underbars, '_'. These are converted to spaces in the string, but are not compacted.
To include a '_' in text, type '\_', where '\' is the escape character. Explicit newlines are entered just as in C: '\n'. A CR/LF pair is entered as '\r' (the '\r' should be used in place of '\n' in all strings destined for a file). Characters which are not on the keyboard, but are defined in a font (such as the Sierra symbol in the menubar) can be included in the string by preceding the two-digit hex value of the character with the '\'. Thus,
"This is the Sierra symbol: \01" would put the value 1 at the end of the string, and this character in the font is the Sierra symbol.
The maximum length of a text string is 2000 bytes.
Word-strings are used to represent templates for user input in Said statements. A word-string is a string enclosed in single quotes which contains meta-characters describing the content of a sentence. The meta-characters and their meanings are described in the separate Vocabulary documentation.
Code:(if (Said 'give/pirate/gold coins<#' @howMany) (Print "Get lost creep.") )
As with text strings, identical strings are stored only once.
Characters are single ASCII characters, and are denoted by preceding the character with the reverse single quote ("tick") character:
`Arepresents uppercase A and
`?represents the question mark
Several character sequences represent special key combinations:
`#4represents the F4 key
Sometimes, as in the code
Code:(cast eachElementDo: #showSelf:)
you want to send the value of selector rather than use the selector as the start of another message to an object (these terms will be described in Object Oriented Programming in Script). Preceding the selector with a '#' produces the literal value of the selector rather than using it as a message.