The programming statements supported by Treescript are described in the following subsections.
Method Invocation
A method invocation invokes a method for an object or class. Method invocations may be used within an expression or as a separate statement. If used as a separate statement, the enclosing parentheses are omitted. Method invocations take the following forms:
· ( method-name argument-list )
· ( : object-name method-name argument-list )
· ( : class-name method-name argument-list )
The argument-list consists of a list of zero or more expressions that are consistent with the method’s parameters.
Allocation Statements
When an object is allocated, it is typically assigned to a variable. However, it is not required to be assigned when it is allocated. An allocation statement is of the following form:
new constructor argument-list
The new operator is used to allocate an object of the class specified by the constructor. The constructor is then invoked to initialize the object using the arguments specified in the argument-list.
Assignment Statements
The assignment statement assigns an object or value to a variable. Its general form is
= variable-name expression
where the expression yields a value that is consistent with the variable’s type.
Other assignment operators may be used in addition to the = operator. See the section titled Operators.
The if Statement
The if statement is used to select among alternative paths of execution. It is written as follows:
if boolean-expression then block elseif-clause else-clause
The elseif and else clauses are optional. The elseif clause may occur zero or more times. They are written as follows:
· elseif boolean-expression then block
· else block
Each block is only executed if the preceding boolean expression is true. At most one block in the if statement is executed. The block inside the else clause is only executed if the preceding boolean expression is false.
The switch Statement
The switch statement is similar to the if statement in that it enables a selection from alternative paths of execution. It is written as follows:
switch expression ( case-clause default-clause )
The expression must evaluate to a byte, char, short, or int value. Control is transferred to the block following the constant list containing a value matching the expression.
The case clause may occur one or more times. If the constant list contains only one value, the enclosing parentheses are optional. The default clause is optional. They are written as follows:
· case ( constant-list ) do block
· default block
If none of the values contained in any of the constant lists match the expression, and a default clause exists, control is transferred to the default clause block.
The for Statement
The for statement is used to iteratively execute a block. It takes the following forms:
· for index-variable ( initial-expression limit-expression ) by-clause do block
· for index-variable list-variable by-clause do block
The by-clause is optional. It is written as follows:
· by step-expression
· by ( step-expression from-expression )
The index variable must be a local variable of ordinal type (byte, short, int, or long). In regards to the first form of the for statement, each of the 2 expressions contained within the parentheses, as well as the step expression, must evaluate to the same type as the index variable. The initial expression is assigned to the index variable at the beginning of the for statement. The step expression defaults to 1. At the beginning of each loop, if the value of the index variable is greater than the limit expression (or less than the limit expression if the step expression is negative), then the for statement terminates. Otherwise the block is executed, and then the step expression is added to the index variable, another comparison between the index variable and the limit expression takes place, and so on.
In regards to the other form of the for statement, the loop iterates through the list defined by the list variable. If the step expression is negative, the for statement iterates from the end of the list to the beginning of the list. The step expression must be of ordinal type. A step expression of 2 iterates through every other element of the list, a step expression of 3 iterates through every third element of the list, and so on. The from-expression defaults to 0. The current list index is initially either the from-expression or, in case of a negative step expression, the no. of elements in the list minus 1 minus the from-expression.
The while Statement
The while statement is used to execute a block while a boolean expression is true. It is written as follows:
while boolean-expression do block
The boolean expression is evaluated; if it is true, the block is executed. It continues to execute until the boolean expression is false.
The do-while Statement
The do while statement is used to execute a block until a boolean expression becomes false. Unlike the while statement, the block is always executed at least once, since the boolean expression is evaluated at the bottom of the loop. The do while statement is written as follows:
do block while boolean-expression
The break Statement
The break statement is used to transfer control out of a loop block to the statement immediately following the for, while, or do-while statement.
The continue Statement
Like the break statement, the continue statement is used to transfer control out of a loop block, but then instead of terminating the loop, the boolean expression is evaluated (or in the case of a for statement, the step expression is added to the index variable and then compared to the limit expression), and the loop statement terminates if the boolean expression evaluates to the appropriate value. Otherwise, the for, while, or do-while statement continues executing.
The return Statement
The return statement is used to return an object or value as the result of a method’s invocation. It is written as follows:
return expression
The value of the expression must match the return value in the method’s declaration. If the method is a procedure, the expression is omitted, since procedures don’t return anything, and the method terminates.
The with Statement
The with statement (borrowed from Object Pascal) is used instead of the colon operator (:) as a means of abbreviating numerous and/or lengthy colon expressions. The following 2 colon expressions (procedure calls, in this case):
: a b p;
: a b (q x y z);
may be abbreviated as follows:
with (a b) do (
p;
q x y z;
);
The with statement is written as follows:
with ( identifier… ) do block
The parentheses may be omitted if there is only one identifier between with and do. The with statement (without the terminating semicolon) can also be used in place of the statement block, or body, of a method declaration. For example:
(proc p
with (a b) do (
...
)
)
The synchronized Statement
The synchronized statement is used to execute a block after acquiring a lock on an object. It is written as follows:
synchronized expression block
The expression yields the object for which the lock must be acquired.
The try Statement
The try statement executes a block while setting up exception handlers. If an exception occurs, the appropriate handler, if any, is executed to handle the exception. A finally clause may also be specified to perform absolutely required processing.
The try statement is written as follows:
try block catch-clauses finally-clause
At least one catch clause or a finally clause must be provided.
The format of the catch clause is as follows:
catch ( exception-declaration ) block
If an exception is thrown within the block executed by the try statement and it can be assigned to the type of exception declared in the catch clause, the block of the catch clause is executed.
The finally clause, if it is provided, is always executed regardless of whether an exception is generated.