Difference between revisions of "The Script Programming Language"

From SCI Wiki
Jump to navigationJump to search
 
(58 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[The Original SCI Documentation]]<br />
+
[[Official SCI Documentation]]<br />
  
 
<div align="center"><span style="font-size: 22pt">The Script Programming Language</span><br />
 
<div align="center"><span style="font-size: 22pt">The Script Programming Language</span><br />
 
''Author: [[Jeff Stephenson]]''<br />
 
''Author: [[Jeff Stephenson]]''<br />
 
Date: 4 April 1988</div>
 
Date: 4 April 1988</div>
 
&nbsp;
 
 
==<br /> 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
 
  
 
<blockquote>
 
<blockquote>
<div class="CodeBlockHeader">Code:</div>
+
*[[The Script Programming Language/Introduction | Introduction]]
<syntaxhighlight lang="sci">
+
*[[The Script Programming Language/Files | Files]]
(procedure [parameter parameter ...]).
+
*[[The Script Programming Language/Definitions | Definitions]]
</syntaxhighlight>
+
*[[The Script Programming Language/Data Types and Variables | Data Types and Variables]]
 +
*[[The Script Programming Language/Primitive Procedures | Primitive Procedures]]
 +
**[[The Script Programming Language/Primitive Procedures#Arithmetic primitives | Arithmetic primitives]]
 +
**[[The Script Programming Language/Primitive Procedures#Boolean primitives | Boolean primitives]]
 +
**[[The Script Programming Language/Primitive Procedures#Assignment primitives | Assignment primitives]]
 +
*[[The Script Programming Language/Control Flow | Control Flow]]
 +
**[[The Script Programming Language/Control Flow#Conditionals | Conditionals]]
 +
**[[The Script Programming Language/Control Flow#Iteration | Iteration]]
 +
*[[The Script Programming Language/Procedures | Procedures]]
 +
*[[The Script Programming Language/Using SC | Using SC]]
 +
*[[The Script Programming Language/Index | Index]]
 
</blockquote>
 
</blockquote>
  
The parameters to a procedure may themselves be expressions to be evaluated, and may be nested until you lose track of the parentheses.
+
<div align="center">
 +
Chapter:
 +
[[The Script Programming Language/Introduction|1]] |
 +
[[The Script Programming Language/Files|2]] |
 +
[[The Script Programming Language/Definitions|3]] |
 +
[[The Script Programming Language/Data Types and Variables|4]] |
 +
[[The Script Programming Language/Primitive Procedures|5]] |
 +
[[The Script Programming Language/Control Flow|6]] |
 +
[[The Script Programming Language/Procedures|7]] |
 +
[[The Script Programming Language/Using SC|8]] |
 +
[[The Script Programming Language/Index|Index]]
 +
</div><br />
  
Unlike Lisp, the procedure itself may NOT be the result of an evaluation. An example of an expression is
+
* [[Media:SCRIPT.pdf|Download The Script Programming Language in PDF Form]]<br />
  
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(+ (- y 2) (/ x 3))
 
</syntaxhighlight>
 
</blockquote>
 
  
which would be written in infix notation as
+
&nbsp;
  
<blockquote>
+
<span style="float: left"><span class="Inactive">&lt; Previous: </span></span><span style="float: right">[[The Script Programming Language/Introduction|Next: Introduction &gt;]]</span>
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
      (y - 2) + (x / 3).
 
</syntaxhighlight>
 
</blockquote>
 
  
All expressions are guaranteed to be evaluated from left to right. Thus,
+
&nbsp;
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(= x 4)
 
(= y (/ (+= x 4) (/= x 2)))
 
</syntaxhighlight>
 
</blockquote>
 
 
 
will result in y = 2 and x = 4.
 
 
 
Comments in Script begin with a semi-colon, ';', and continue to the end of the line.
 
 
 
 
 
== <br />Files ==
 
 
 
Source files for the script compiler have the extension .sc, header (include) files have the extension .sh. Source files may have any filename -- banner.sc and castle.sc are two examples. The output file from the compilation will have the name script.nnn where nnn is determined from the script# command (covered below) which is present in the file.
 
 
 
There are six files besides the source file and any user-defined header files which are involved in a compilation.
 
 
 
===<br /> classdef ===
 
<blockquote>This file contains the information about the structure of the classes which have been defined in the application. It is read automatically by the compiler and is rewritten by the compiler after a successful compilation in order to keep it up to date. The user need not be concerned with it.</blockquote>
 
 
 
===<br /> selector ===
 
<blockquote>This contains definitions of selectors which are used in object-oriented programming. It is automatically included in a compile and, like classdef, is rewritten after a successful compile. Any symbol in a properties or methods statement or in the selector position in a send to an object is assumed to be a selector and is assigned a selector number in included in selector.</blockquote>
 
 
 
===<br /> kernel.sh ===
 
<blockquote>This contains the definitions for interfacing with the kernel (the machine language interpreter). It is maintained by the kernel programmers and is automatically included in system.sh.</blockquote>
 
 
 
===<br /> system.sh ===
 
<blockquote>This contains the definitions for interfacing with the various system classes. It is initially provided by the kernel programmers. If you wish to tweak the system scripts yourself, you will also be responsible for maintaining your copy of system.sh. It should be included in all compiles.</blockquote>
 
 
 
===<br /> vocab.000 ===
 
<blockquote>This is the compiled output of vocab.txt, generated by the vocabulary compiler vc. It is automatically included in a compile.</blockquote>
 
 
 
===<br /> classtbl ===
 
<blockquote>This is an output file of the compiler which is used by the kernel to determine which script a given class is defined in. You needn't do anything to it other than not delete it.</blockquote>
 
 
 
There are two sc commands for dealing with source code organization:
 
 
 
===<br /> script#: ===
 
 
 
The script# command sets the script number of the output file:
 
<div class="CodeBlockHeader">Code:</div>
 
 
 
<blockquote>
 
<syntaxhighlight lang="sci">
 
(script# 4)
 
</syntaxhighlight>
 
</blockquote>
 
 
 
sets the output file name to script.004, regardless of the actual name of
 
the source file.
 
 
===<br /> include: ===
 
 
This includes a header file in the current source file at the current position.
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(include "/sc/foo.sh")
 
</syntaxhighlight>
 
</blockquote>
 
 
 
or
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(include /sc/foo.sh)
 
</syntaxhighlight>
 
</blockquote>
 
 
 
include the file /sc/foo.sh.  Include files may be nested as deeply as desired.
 
 
 
When including a file, the compiler first looks for the file in the current directory.  If it fails to find it there, it then looks for it in the directories specified in the environment variable SINCLUDE.  This variable is just like the DOS PATH variable -- the directories to search are separated by semi-colons.  Thus, if you want the compiler to look for include files in f:/games/sci/system and c:/include if it doesn't find them in the current directory, you put the line
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
set sinclude=f:/games/sci/system;c:/include
 
</syntaxhighlight>
 
</blockquote>
 
 
 
in your autoexec.bat file.
 
 
 
== <br /> Definitions ==
 
 
 
===<br /> define: ===
 
 
 
The define statement allows you to define a symbol which will stand for a
 
string of text:
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(define symbol lots of text)
 
</syntaxhighlight>
 
</blockquote>
 
 
 
will replace symbol, wherever it is encountered as a token, with lots of
 
text and then continue scanning at the beginning of the replacement text. 
 
Thus, if we write
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(define symbol some text)
 
(define some even more)
 
</syntaxhighlight>
 
</blockquote>
 
 
 
then
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(symbol)
 
</syntaxhighlight>
 
</blockquote>
 
 
 
will become
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(some text)
 
</syntaxhighlight>
 
</blockquote>
 
 
 
which then becomes
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(even more text)
 
</syntaxhighlight>
 
</blockquote>
 
 
 
===<br /> enum: ===
 
 
 
A construct for easing the definition of various states of a state-variable
 
is enum.  Say you want to walk an actor from the door of a room across the
 
floor, up the stairs, and through another door.  You have a state-variable
 
called actor-pos which will take on a number of values, which could be
 
defined with defines:
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(local    actor-pos
 
    (define at-front-door    0)
 
    (define in-room          1)
 
    (define on-stairs        2)
 
    (define top-of-stairs    3)
 
    (define upper-door      4)
 
)
 
</syntaxhighlight>
 
</blockquote>
 
 
 
or you could get the same result with enum:
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(local    actor-pos
 
    (enum
 
          at-front-door
 
          in-room
 
          on-stairs
 
          top-of-stairs
 
          upper-door
 
    )
 
)</syntaxhighlight>
 
</blockquote>
 
 
Enum defaults its first symbol to 0.  If you want a different starting
 
value, put it right after the word enum:
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(enum 7
 
    at-front-door
 
    in-room
 
    on-stairs
 
    top-of-stairs
 
    upper-door
 
)
 
</syntaxhighlight>
 
</blockquote>
 
 
 
sets at-front-door to 7, in-room to 8, etc.
 
 
 
===<br /> synonyms: ===
 
 
 
The synonyms statement defines synonyms of words.  All words must have been defined in the vocabulary file (see separate Vocabulary documentation).  The statement
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(synonyms
 
    (main-word  synonym1 synonym2 ...)
 
    ...
 
)
 
</syntaxhighlight>
 
</blockquote>
 
 
 
defines the words synonym1, synonym2, etc. to be synonyms of main-word.  In
 
input being interpreted by the script in which the synonym statement is
 
defined, user input of synonym1 will be interpreted as if the user had typed
 
main-word.
 
 
 
==<br /> Data Types and Variables ==
 
 
 
===<br /> Numbers: ===
 
 
 
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).
 
 
 
===<br /> Variables: ===
 
 
 
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:
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(global
 
    var-name var-number
 
    var-name var-number
 
    ...
 
)
 
</syntaxhighlight>
 
</blockquote>
 
 
 
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:
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(local
 
    var-name
 
    var-name
 
    ...
 
)
 
</syntaxhighlight>
 
</blockquote>
 
 
 
defines a single variables with the names var-name.
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(local [array-name n])
 
</syntaxhighlight>
 
</blockquote>
 
 
 
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:
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(local
 
    var1
 
    [array1 10]
 
    [array2 5]
 
    var2
 
          .
 
          .
 
          .
 
)
 
</syntaxhighlight>
 
</blockquote>
 
 
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.
 
 
 
 
 
===<br /> Arrays: ===
 
 
 
To access element n of the array anArray, write
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
[anArray n]
 
</syntaxhighlight>
 
</blockquote>
 
 
 
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
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(local
 
    var1
 
    var2
 
    var3
 
    var4
 
)
 
</syntaxhighlight>
 
</blockquote>
 
 
 
we can set the value of var1 to that of var4 by any of the following statements:
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(= var1 var4)
 
(= var1 [var2 2])
 
(= var1 [var3 1])
 
(= [var2 -1] [var1 3])
 
</syntaxhighlight>
 
</blockquote>
 
 
 
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
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
(global
 
    var1      23
 
    var2      24
 
                    ;10 element array
 
    var3      34
 
)
 
</syntaxhighlight>
 
</blockquote>
 
 
 
and access var2 as an array:
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
[var2 7]
 
</syntaxhighlight>
 
</blockquote>
 
 
 
=== <br /> Pointers: ===
 
 
 
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:
 
 
 
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
@ego          ;pointer to the variable ego
 
@[foo 3]      ;pointer to fourth element of array foo
 
</syntaxhighlight>
 
</blockquote>
 
 
 
Since there is currently no way in sc to dereference a pointer, this is only useful for passing pointers to kernel calls.
 
  
=== <br /> Text: ===
+
[[Category:SCI Documentation]]
 +
[[Category:Scripting]]

Latest revision as of 20:12, 2 December 2015