|
Expressions |
Expressions
An expression specifies the evaluation of a value. It can be used, for example:
Arithmetic expressions
An arithmetic expression is used to evaluate a numeric value.
Example: 2*pi*radius
Table 2 shows the different types of operations possible.
1. The result receives the same type as the operand. If the operand has an alias data type, the result receives the alias "base" type (num or pos).
2. Integer operations, e.g. 14 DIV 4=3, 14 MOD 4=2. (Non-integer operands are illegal.)
3. Preserves integer (exact) representation as long as operands and result are kept within the integer subdomain of the num type.
Logical expressions
A logical expression is used to evaluate a logical value (TRUE/FALSE).
Example: a>5 AND b=3
Table 3 shows the different types of operations possible.
1) Only value data types. Operands must have equal types.
String expressions
A string expression is used to carry out operations on strings.
Example: "IN" + "PUT" gives the result "INPUT"
Table 4 shows the one operation possible.
Using data in expressions
An entire variable, persistent or constant can be a part of an expression.
Example: 2*pi*radius
Arrays
A variable, persistent or constant declared as an array can be referenced to the whole array or a single element. An array element is referenced using the index number of the element. The index is an integer value greater than 0 and may not violate the declared dimension. Index value 1 selects the first element. The number of elements in the index list must fit the declared degree (1, 2 or 3) of the array.
Example: VAR num row{3};
VAR num column{3};
VAR num value;
.value := column{3}; only one element in the array
row := column; all elements in the array
Records
A variable, persistent or constant declared as a record can be referenced to the whole record or a single component. A record component is referenced using the component name.
Example: VAR pos home;
VAR pos pos1;
VAR num yvalue;
.yvalue := home.y; the Y component only
pos1 := home; the whole position
Using aggregates in expressions
An aggregate is used for record or array values.
Example: pos := [x, y, 2*x]; pos record aggregate
posarr := [[0, 0, 100], [0,0,z]]; pos array aggregate
It must be possible to determine the data type of an aggregate the context. The data type of each aggregate member must be equal to the type of the corresponding member of the determined type.
Example VAR pos pl;
p1 :=[1, -100, 12]; aggregate type pos - determined by p1
IF [1, -100, 12] = [a,b,b,] THEN illegal since the data type of neither of the aggregates can be determined by the context.
Using function calls in expressions
A function call initiates the evaluation of a specific function and receives the value returned by the function.
Example: Sin(angle)
The arguments of a function call are used to transfer data to (and possibly from) the called function. The data type of an argument must be equal to the type of the corre-sponding parameter of the function. Optional arguments may be omitted but the order of the (present) arguments must be the same as the order of the formal parameters. In addition, two or more optional arguments may be declared to exclude each other, in which case, only one of them may be present in the argument list. A required (compulsory) argument is separated from the preceding argument by a comma ",". The formal parameter name may be included or omitted.
Example: Polar(3.937, 0.785398) two required arguments
Polar(Dist:=3.937, Angle:=0.785398)... using names
An optional argument must be preceded by a backslash "\" and the formal parameter name. A switch type argument is somewhat special; it may not include any argument expression. Instead, such an argument can only be either "present" or "not present".
Example: Cosine(45) one required argument
Cosine(0.785398\Rad) ... and one switch
Dist(p2) one required argument
Dist(\distance:=pos1, p2) ... and one optional
Conditional arguments are used to support smooth propagation of optional arguments through chains of routine calls. A conditional argument is considered to be "present" if the specified optional parameter (of the calling function) is present, otherwise it is simply considered to be omitted. Note that the specified parameter must be optional.
Example: PROC Read_from_file (iodev File \num Maxtime)
.character:=ReadBin (File \Time?Maxtime);
! Max. time is only used if specified when calling the routine
! Read_from_file
.ENDPROC
The parameter list of a function assigns an access mode to each parameter. The access mode can be either in, inout, var or pers:
- An IN parameter (default) allows the argument to be any expression. The called function views the parameter as a constant.
- An INOUT parameter requires the corresponding argument to be a variable (entire, array element or record component) or an entire persistent. The called function gains full (read/write) access to the argument. - A VAR parameter requires the corresponding argument to be a variable (entire, array element or record component). The called function gains full (read/write) access to the argument.
- A PERS parameter requires the corresponding argument to be an entire persist-ent.The called function gains full (read/update) access to the argument.
Priority between operators
The relative priority of the operators determines the order in which they are evaluated.Parentheses provide a means to override operator priority. The rules below imply the following operator priority:
* / DIV MOD - highest
+ -<
> <> <= >= =
AND
XOR OR NOT - lowest
An operator with high priority is evaluated prior to an operator with low priority. Oper-ators of the same priority are evaluated from left to right.
7.8 Syntax