Skip to content

expressions

Mike Bergsma edited this page Aug 1, 2014 · 3 revisions

Expression syntax is independent of operand type. Basic rules for operator precedence and evaluation order are presented in the section on Operators. This section describes the construction and meaning of expressions using the defined operators.

Primary Expressions

Primary expressions produce values. Operands of primary expressions are previously declared identifiers, literals, array references, and function calls.

Parenthesis can be used to change the evaluation order within a primary expression. For example:

( ( a + b ) / c ) != ( a + b / c )

A function call is a primary expression followed by a pair of parentheses. The parenthesis may contain a list of arguments (separated by commas) or may be empty. All function calls return a value. For example:

str ss = strext ( "abcdef", 3, 2 ); /* ss contains "de" */

Subscript operators are used to refer to elements of lists and vectors. Given a list, x, you refer to the _i_th element as x[i-1].

If a subscript expression is unspecified, evaluates to a non-integer, or operates on a non-variable, then the STATUS variable is set to the error code %EXPRESSION. If the subscript value is out-of-bounds, then the STATUS variable is set to the error code %BOUNDS. A handler declared by the on_error statement can trap these errors.

Unary Expressions

Unary expressions are formed by combining a unary operator with a single operand. All unary operators evaluate from right to left. They perform the following functions:

  • Member selection (. and ->).
  • Increment (++) and decrement (--).
  • Negate a variable arithmetically (-) or logical (!).
  • Find reference (&) and dereference (*) variables.

Binary Expressions

The binary operators fall into the following categories:

  • Additive operators: addition (+) and subtraction (-)
  • Multiplicative operators: multiplication (*), mod (%), and division (/).
  • Equality operators: equality (==) and inequality (!=).
  • Relational operators: less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=).
  • Bitwise operators: AND (&), OR (|), and XOR (^).
  • Logical operators: AND (&&) and OR ( | | ).

For divide-by-zero errors, the STATUS variable is set to the error code %BOUNDS. If the binary expression operators on two list structures, the STATUS variable is also set to the error code %BOUNDS. A handler declared by the on_error statement can trap these errors.

Conditional Expression

The conditional operator (?:) takes three operands. It tests the result of the first operand and then evaluates one of the other two operands based on the result of the first. For example:

E1 ? E2 : E3

If E1 is non-zero, then E2 is evaluated, otherwise E3 is evaluated. Conditional expressions evaluate from left to right.

Assignment Expression

HS variables are both defined and assigned in the same statement. For Example:

i = 1;

defines the variable _i _ and assigns it the value 1.

All variables are inherently arrays, or lists. Thus the previous expression is equivalent to:

i = { 1 };

To assign a list of values to a variable, the values are separated by commas. For Example:

lotids = { "F12345.F1X", "F12346.F1Y" };

Multiple values are often used when specifying strings:

TEXT = { "The name of the first lot is ", lotids[0] };

An empty (or NULL) value is specified like this:

x = {};

Lists can be embedded in other lists, this is useful when inserting or appending data from one list into another list.

x = { 3, 4 };
y = { 1, 2, x, {5, 6} };        /* y = { 1, 2, 3, 4, 5, 6 } */

The assignment expression can also be used as an operand in an expression.

if ( !( s = event( sender, "MESSAGE", TEXT ) ) goto BAD_EVENT;

For assignments to non-variables, the STATUS variable is set to the error code %EXPRESSION. A handler declared by the on_error statement can trap this error.

Comma Expression

When two expressions are separated by the comma operator, they are evaluated from left to right, and the result of the left expression is discarded. For example:

R = ( T = 1, T = T + 2 );

assigns 3 to both R and T.

Note that comma expressions must be parenthesized if they appear where commas have some other meaning, as in argument and list initializations. For example:

x = { 1, 2, ( c = 3, d = 5 ), 4 };        /* x contains { 1, 2, 'd', 4 } */

assigns c the value 3 (c is later discarded), d the value 5 (d becomes a member of x), and x the values { 1, 2, 'd', 4 }.

Clone this wiki locally