The Script Programming Language/Procedures

From SCI Wiki
Revision as of 17:25, 29 November 2015 by Andrew Branscom (talk | contribs) (Created page with "==<br /> Procedures == Procedures are created with the procedure construct: <blockquote> <div class="CodeBlockHeader">Code:</div> <syntaxhighlight lang="sci"> (procedure (p...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search


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).