The Script Programming Language

From SCI Wiki
Jump to navigationJump to search

The Original SCI Documentation

The Script Programming Language

Author: Jeff Stephenson

Date: 4 April 1988

 


Introduction

The Script adventure game language is an object-oriented language with a Lisp-like syntax. It is compiled by the sc compiler into the pseudo-code which is used by the interpreter, sci.

We will begin our discussion of the language with its basic Lisp-like characteristics, then go on to the object-oriented parts of the language.

As is Lisp, Script is based on parenthesized expressions which return values. An expression is of the form

Code:
(procedure [parameter parameter ...]).

The parameters to a procedure may themselves be expressions to be evaluated, and may be nested until you lose track of the parentheses.

Unlike Lisp, the procedure itself may NOT be the result of an evaluation. An example of an expression is

Code:
(+ (- y 2) (/ x 3))

which would be written in infix notation as

Code:
      (y - 2) + (x / 3).

All expressions are guaranteed to be evaluated from left to right. Thus,

Code:
(= x 4)
(= y (/ (+= x 4) (/= x 2)))

will result in y = 2 and x = 4.

Comments in Script begin with a semi-colon, ';', and continue to the end of the line.


Files


Definitions


Data Types and Variables


Characters:

Characters are single ASCII characters, and are denoted by preceding the character with the reverse single quote ("tick") character:

`A represents uppercase A and `? represents the question mark

Several character sequences represent special key combinations:

`^a represents ctrl-A `@b represents alt-B `#4 represents the F4 key


Literal selectors:

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.


Primitive Procedures


Arithmetic primitives:

In the following, e1, e2, ... are arbitrary expressions.


(+ e1 e2 [e3...])

Evaluates to e1 + e2 [+ e3 ...]


(* e1 e2 [e3...])

Evaluates to e1 * e2 [* e3 ...]


(- e1 e2)

Evaluates to e1 - e2


(/ e1 e2)

Evaluates to e1 / e2


(mod e1 e2)

Evaluates to the remainder of e1 when divided by e2.


(<< e1 e2)

Evaluates to e1 << e2 where the << operation shifts its left hand side left by the number of bits specified by its right hand side. (As in C).


(>> e1 e2)

Evaluates to e1 >> e2 as in << except a right shift.


(^ e1 e2 [e3 ...])

Evaluates to e1 ^ e2 [^ e3 ^ ...] where '^' is the bitwise exclusive-or operator.


(& e1 e2 [e3 ...])

Evaluates to e1 & e2 [& e3 & ...] where '&' is the bitwise and operator.


(| e1 e2 [e3])

Evaluates to e1 | e2 [| e3 | ...] where '|' is the bitwise or operator.


(! e1)

Evaluates to TRUE if e1 == 0, else FALSE.


(~ e1)

Evaluates to the bit-wise not of e1, i.e. all 1 bits are changed to 0 and all 0 bits are changed to 1.


Boolean primitives:

These procedures are always guaranteed to evaluate their parameters left to right and to terminate the moment the truth value of the expression is determined. If the truth value of the boolean is determined before an expression is reached, the expression is never evaluated.



(> e1 e2 [e3...])

Evaluates to TRUE if e1 > e2 [> e3 ...], else FALSE.


(>= e1 e2 [e3...])

Evaluates to TRUE if e1 >= e2 [>= e3 ...], else FALSE.


(< e1 e2 [e3...])

Evaluates to TRUE if e1 < e2 [< e3 ...], else FALSE.


(<= e1 e2 [e3...])

Evaluates to TRUE if e1 <= e2 [<= e3 ...], else FALSE.


(== e1 e2 [e3...])

Evaluates to TRUE if e1 == e2 [== e3 ...], else FALSE.


(!= e1 e2 [e3...])

Evaluates to TRUE if e1 != e1 [!= e3 ...], else FALSE.


(and e1 e2 [e3...])

Evaluates to TRUE if all the expressions are non-zero, else FALSE.


(or e1 e2 [e3...])

Evaluates to TRUE if any of the expressions are non-zero, else FALSE.


(not e)

Evaluates to TRUE if the expression is zero, else FALSE.


Assignment primitives:

All assignment procedures store a value in a variable and return that value as the result of the assignment. In the following, v is a variable and e an expression.


(= v e)

v = e


(+= v e)

v = v + e


(-= v e)

v = v - e


(*= v e)

v = v * e


(/= v e)

v = v / e


(|= v e)

v = v | e


(&= v e)

v = v & e


(^= v e)

v = v ^ e


(>>= v e)

v = v >> e


(<<= v e)

v = v << e


(++ v)

v = v + 1


(-- v)

v = v - 1


Control Flow

In the following, code1, ..., codeN are arbitrary sequences of expressions. There are no BEGIN ... END blocks as in Pascal or progn forms as in Lisp.

The value of a control flow expression is the value of the last expression in the control body which was evaluated. Thus, if we execute the following code:

Code:
(= x 3)
     (= y 2)
     (= y (if (> x y)
          (- x y)
     else
          (+ x y)
     )
)

y will have the value 1.


Return:

Code:
(return [expression])

The return statement returns control to the procedure which called the currently executing procedure. If the optional expression is present, that value is returned as the value of the current procedure. There is an implicit return at the end of all procedures, and the value returned in that case is the value of the last expression evaluated. A return from the main procedure of script 0 returns to the operating system.


Conditionals:

Code:
(if expression code1 [else code2])

If expression is not FALSE, execute code1, else execute code2. (The else clause is optional).

Code:
(cond (e1 code1) (e2 code2) ... [(else codeN)])

Evaluate e1. If it is not FALSE, execute code1 and exit the cond clause. If it is FALSE, evaluate e2 and continue. If all of the expressions are FALSE and the optional else clause is present, execute codeN.

Code:
(switch expression (exp1 code1) (exp2 code2) ... [(else codeN)])

Evaluate expression. If it is equal to exp1, execute code1 and exit the switch. If it is equal to exp2, execute code2 and exit. If it doesn't equal any of the expressions and the optional else clause is present, execute codeN.


Iteration:

Code:
(for (initialization) condition (re-initialization) code)

Evaluate the expressions comprising initialization. Then evaluate condition. If the result is FALSE, exit the loop. Otherwise, execute code, then the expressions comprising re-initialization, and loop back to condition.

Code:
(while condition code)

Evaluate condition. If not FALSE, execute code and loop back to evaluate condition again. Exit the loop when condition is FALSE. (Note that this means that the value of a while condition is always FALSE.) This is equivalent to

Code:
(for () condition () code)
Code:
(repeat code)

Continually execute the code until some condition in the code (a break) causes the loop to be exited. This is equivalent to

Code:
(while TRUE code)

or

Code:
(for () TRUE () code)


Supporting constructs for iteration:

Code:
(break [n])

Break out of n levels of loops. If n is not specified break out of the innermost loop.

Code:
(breakif expression [n])

If expression is not FALSE, break out of n levels of loops. If n is not specified, break out of the innermost loop.

Code:
(continue [n])

Loop back to the beginning of the nth level loop. If n is not specified, loop to the beginning of the innermost loop.

Code:
(contif expression [n])

If expression is not FALSE, loop back to the beginning of the nth level loop. If n is not specified, loop to the beginning of the innermost loop.


Procedures

Procedures are created with the procedure construct:

Code:
(procedure (proc-name [p1 p2 ...] [&tmp t1 t2...])
     code
)

This defines the procedure with the name proc-name. This procedure takes parameters p1, p2, ... and allocates temporary variables (which disappear on exit from the procedure) t1, t2, .... Note that the procedure may take no parameters and have no automatic variables. In this case, the definition would be

Code:
(procedure (proc-name)
     code
)

You can define temporary arrays in the same way as you would local arrays:

Code:
(procedure (proc-name &tmp [array n])
     code
)

Code in these examples is any list of valid expressions.

All procedures have at least one parameter, the compiler defined variable argc (argument count), which gives the number of parameters passed to the procedure.

For example, you might define the procedure square, to square a number, as follows:

Code:
(procedure (square n)
     (* n n)
)

or the procedure max to find the maximum of an arbitrary number of numbers passed to the procedure:

Code:
(procedure
     (max
          p         ;parameters (will be accessed as an array)
          &tmp
          biggest   ;temporary variable containing maximum
          i         ;index into parameter array
     )

     (for ((= i 0) (= biggest 0))
          (< i argc)     ;compare to number of parameters passed.
          ((++ i))       ;note that this is a LIST of expressions,
                         ;not a single expression.

          (if (> [p i] biggest)
               (= biggest [p i])
          )

     )

     (return biggest)
)


The call

Code:
(max 3 -4 -9 0 -2 7 12 4 3 5)

will return the value 12.

In order to use a procedure before it has been defined in a source file (for example making a call to max before the actual definition of max), the compiler must be told that the procedure's name corresponds to a procedure, not an object. This is done with another form of the procedure statement:

Code:
(procedure
     procedure-name
     procedure-name
     ...
)

Tells the compiler to compile code for procedure calls when it encounters procedure-name, rather than code for send messages to an object.


&rest:

Argc, as discussed above, makes it easy to write procedures (or methods, discussed in Object Oriented Programming in Script) which handle a variable number of arguments. If a procedure or method has received a variable number of arguments and wants to pass them on to another procedure or method, things get messy. The only way to do this given only argc is to build a switch statement on argc which looks like

Code:
(procedure (foo arg)
     (switch argc
          (0   (mumble))
          (1   (mumble arg))
          (2   (mumble arg [arg 1]))
          (3   (mumble arg [arg 1] [arg 2]))
          ...
     )
)

This not only involves a lot of typing and generates a lot of code, it limits the number of arguments which can be passed to the next function (you can only type a finite number of clauses in the switch statement).

&rest exists to solve this problem -- it stands for the rest of the parameters not specified in the procedure or method definition. The above procedure could then be written simply as

Code:
(procedure (foo)
     (mumble &rest)
)

which is not only easier to type and read but also produces smaller, faster object code.

A variant of &rest allows you to specify all parameters starting at any point in a parameter list of a procedure or method definition:

Code:
(procedure (foo arg1 arg2 arg3 arg4)
     (mumble (&rest arg3))
)

passes all arguments starting with arg3 to procedure mumble.


Extern:

Calling a procedure in another script is another matter. Since there is no link phase in the development cycle, one procedure cannot know the address of a procedure in a different script. The extern statement allows a script to know where the external procedure is:

Code:
(extern
     procedure-name script-number entry-number
     ...
)

This says that the procedure referred to by the symbol procedure-name in this script is to be found in script number script-number at entry number entry-number in the script's dispatch table. kernel.sh and base.sh both use the extern statement to let all other scripts know where their public procedures are.

The dispatch table for a script is defined by the public statement:


Public:

All procedures within a script which are to be accessed from outside the script must be entered in the dispatch table for the script with the public statement:

Code:
(public 
     procedure-name entry-number 
     ...
)

puts the procedure procedure-name in the dispatch table at entry number entry-number. The entries need not be in numeric order, nor do the numbers need to be continuous (though if they're not continuous the table will be larger than it needs to be).


Using SC

The sc compiler is invoked with the command

Code:
sc file_spec [file_spec] [options]

Any number of file specifications may be entered on the command line, and a file specification may include wild-card names.


Options:


-l

Generate an assembly language code listing for the file. This is useful when using the built-in debugger of sci, which lists only the assembly language code, not the source. When compiling filename.sc, the list file is named filename.sl


-n

Turns off 'auto-naming' of objects. As described in Script Classes for Adventure Games, each object has a name, or 'print-string' property, which is how to represent the object textually. Unless the property is explicitly set, the compiler will generate the value for this property automatically, using the object's symbol string for the name. The object names, however, take up space in the heap. While they are useful (almost vital) for debugging, if you're running out of heap in a room, it might help to compile with the -n option to leave the names out.


-oout-dir

Set the directory for the output file (script.nnn) to out-dir.


-v

Turns on verbose mode, which prints the number of bytes occupied by various parts of the output file (code, objects, text, etc.).


-z

Turn off optimization. Not a particularly useful option except for those of us who must maintain the compiler.


Index

! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
!= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
+= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
-- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
-= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
*= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
/ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
/= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
^ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
^= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
< . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
<< . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
<<= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
<= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
== . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
>= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
>> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
>>= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
& . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
&= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
&rest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
|= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
~ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
and . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
arithmetic primitives . . . . . . . . . . . . . . . . . . . . . . . . . 13
array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
assignment primitives . . . . . . . . . . . . . . . . . . . . . . . . . 15
base.sh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
boolean primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
breakif . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
classdef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
classtbl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
cond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
contif . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
enum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
extern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
global . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
kernal.sh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
local . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
mod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
not . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
or . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19, 20
public . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
repeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
sc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
script# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
selector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
literal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
SINCLUDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
synonyms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
global . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
local . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
temporary . . . . . . . . . . . . . . . . . . . . . . . . . . . 8, 19
vocab.000 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
word strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11