| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (3 out of 25)

Procedure Syntax

The syntax of a G2 procedure is similar to the syntax of a procedure in any ordinary programming language. You can write G2 procedures without knowing every detail of their syntax, because the text editor warns you of any syntactic error as soon as it occurs.

A G2 procedure consists of four major parts:

For example:


The formal syntax of a G2 procedure is:

The rest of this section describes procedure syntax in detail.

Local Names in Procedures

A local name in a procedure is a name that represents an item or value while the procedure executes. A local name has no attributes, collection time, or expiration time, and need not be explicitly deleted when a procedure returns: it has no properties except its type and the item or value that it represents.

Procedure Header Syntax

The procedure header names the procedure and specifies what arguments it accepts and the types of values (if any) that it returns. The syntax is:

For example, the following header begins a procedure that takes two arguments, a mixer and the name of a part used in it, and returns a truth-value that tells whether that part is currently in stock:


Tip: Omitting the keyword class when specifying an argument or return value is one of the most common errors in defining procedures.

Duplicate Procedure Names

If a procedure name is not unique, G2 posts a warning in the Notes attribute of every procedure that shares the name. Invoking the duplicated procedure invokes an arbitrarily selected instance of it. The selection may differ from one invocation to the next.

Procedures used as methods can have duplicate names provided that the number of arguments differs in each method that shares a name, as described under Duplicate Methods.

Local Declarations Syntax

A procedure's argument definitions (if any) define local names to hold the arguments. Any additional local names needed in the procedure are defined in the local declarations section. If no additional local names are needed, the local declarations section is omitted.

Local declarations specify local names, their associated types, and their initial values (if any). The syntax is:

If more than one local name is of the same type, you can combine the declarations. The following declares three local names of type integer, each with the initial value 0.

Terminology

In other programming languages, declared names that hold values during procedure execution are called variables. In G2, the term "variable" refers exclusively to a G2-VARIABLE, as described in Chapter 15, Variables and Parameters.

Variables and parameters can have initial values, but these are not the same as the initial values of local names. Be careful not to confuse the two meanings of "initial value." The context always clarifies which is intended.

Procedure Body Syntax

The procedure body contains one or more procedure statements, which specify the operations that the procedure performs. All of the statements in a procedure are enclosed in a begin-end block. The syntax is:

If a statement-label appears, the statement can be the target of a go to, as described under go to.

Statements

Every statement in a procedure is either an action statement or a procedure statement. An action statement specifies some action, using the same syntax that the action uses when specified in a rule. For information on the various actions that a procedure can contain, see Chapter 19, Actions.

Procedure statements make assignments, control the flow of execution, and do other things typical of statements in any computer language. The following table summarizes all G2 procedure statements. The Dictionary of Procedure Statements provides complete information about all procedure statements.

Statement Description
allow other processing
Lets G2 interrupt a procedure to perform other tasks with the same priority.
Assignment (=)
Associates a value with a given local name without causing data-seeking.
begin ... end
Defines a compound statement, including the body of a procedure.
case
Specifies a series of alternative statements to be executed based on the value of an expression
call
Invokes a procedure and transfers control to it.
collect data
Associates a value with a given name by causing data-seeking. Procedures must use this statement when referencing variable values.
do in parallel
Executes two or more statements at the same time.
exit if
Transfers control outside a loop, based on the value of a specified logical expression.
for
Specifies a loop, that is, a sequence of one or more statements to be executed repeatedly until a specified condition has been fulfilled.
go to
Transfers control explicitly to a specified label.
if-then
Specifies conditional execution of statements based on the value of an expression.
on error
Accepts control of the procedure when an error occurs.
repeat
Explicitly iterates over a sequence of one or more statements.
return
Returns control to the calling procedure.
signal
Signals errors that you have named.
wait
Suspends procedure execution for a specified time or based on the value of some expression.

Error Handler Syntax

G2 provides error handlers. When errors occur, G2 searches for and invokes the error handler. The default error handler prints a message to the logbook describing the error.

You can also define your own error handlers within procedures by using the on error statement. The syntax is:

For details, see on error. For information on all aspects of G2 error handling, see Chapter 41, Error Handling.

Comments

You can include comments within the text of a procedure body by enclosing comments within braces:

You can place a comment anywhere that whitespace is allowed. Comments are saved with the procedure, and do not affect its compilation.

| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (3 out of 25)

Copyright © 1997 Gensym Corporation, Inc.