Expressions


An assignment statement assigns a value to a symbol. The value is determined by an expression on the right-hand side of the equal sign. Expressions are also used in other DCL commands, such as the IF command. Literals and symbols have already been used as simple expressions. When used in an expression, a literal stands for itself and a symbol stands for its current value. These simple expressions are useful but not powerful enough to compute new values, such as the sum of two integers.

New values are computed using expressions composed of operators and operands. An operator is a character or sequence of characters that stands for some mathematical operation, such as multiplication, or for a string operation, such as concatenation. The operands associated with an operator determine the values that are to participate in the operation. Here is a simple expression:

     a * b 

This denotes that the value of the symbol A is to be multiplied by the value of the symbol B to produce a new value. The gate of the new value is determined by the context in which the expression appears. So far, the only context in which an expression can appear is the assignment statement:

     $ product = a * b 

Here, the product of the values of A and B is assigned to the symbol PRODUCT. When DCL encounters an expression, it applies the operators to their operands in a certain predetermined order, producing a final result that is assigned to a symbol or used for some other purpose. When DCL processes an expression in this manner, we say that DCL evaluates the expression.

In order to completely understand expressions, you must become familiar with the available operators, the operands they expect, and the order in which the operators are applied to their operands. Tables E-2, E-3, and E-4 describe the operators provided by DCL for use with integer, character string and Boolean values, respectively. Table E-5 illustrates the order in which operators are applied.

Table E-2: Integer operators

Operator

Arity

Result type

Result value

+

Unary

Integer

Integer operand unchanged.

-

Unary

Integer

Negative of integer operand.

+

Binary

Integer

Sum of integer operands.

-

Binary

Integer

Difference of integer operands.

*

Binary

Integer

Product of integer operands.

/

Binary

Integer

Quotient of integer operands, truncated toward zero.

.EQ.

Binary

Boolean

True if integer operands are equal, false otherwise.

.NE.

Binary

Boolean

True if integer operands are unequal, false otherwise.

.GT.

Binary

Boolean

True if first integer operand is greater than second, false otherwise.

.GE.

Binary

Boolean

True if first integer operand is greater than or equal to second, false otherwise.

.LT.

Binary

Boolean

True if first integer operand is less than second, false otherwise.

.LE.

Binary

Boolean

True if first integer operand is less than or equal to second, false otherwise.

.NOT.

Unary

Integer

Bitwise Boolean NOT of integer operand. A bit in the result is 1 if the corresponding bit in the operand is zero, and vice versa.

.AND.

Binary

Integer

Bitwise Boolean AND of integer operands. A bit in the result is 1 if both of the corresponding bits in the operands are 1.

.OR.

Binary

Integer

Bitwise Boolean inclusive OR of integer operands. A bit in the result of 1 if either or both of the corresponding bits in the operands are 1.

Table E-3: Character string operators

Operator

Arity

Result type

Result value

*

Binary

String

A copy of the first string operand with a copy of the second one concatenated to it (e.g., "Hello-" + "there." produces "Hello-there.").

-

Binary

String

A copy of the first string operand with the leftmost occurence of the second one removed from it (e.g., "oh-why-oh-why" - "why" produces "oh--oh-why").

.EQS.

Binary

Boolean

True if string operands contain the same character sequence, false otherwise.

.NES.

Binary

Boolean

True if string operands contain different character sequences, false otherwise.

.GTS.

Binary

Boolean

True if first string operand is alphabetically greater than second, false otherwise. The collating sequence is based on the ASCII character set.

.GES.

Binary

Boolean

True if first string operand is greater than or equal to second, false otherwise.

.LTS.

Binary

Boolean

True if first string operand is less than second, false otherswise.

.LES.

Binary

Boolean

True if first string operand is less than or equal to second, false otherwise.

Table E-4: Boolean operators

Operator

Arity

Result type

Result value

.NOT.

Unary

Boolean

True if Boolean operands is false, false if it is true.

.AND.

Binary

Boolean

True if both Boolean operands are true, false otherwise. There is no guarantee about which operand is evaluated first.

.OR.

Binary

Boolean

True if either or both Boolean operands are true, false otherwise. There is no guarantee about which operand is evaluated first.

Table E-5: Operator precedence

Precedence

Operator

8 (highest)

()

7

unary + -

6

*/

5

binary + -

4

.EQ. .NE. .GT. .GE. .LT. .LT. .EQS. .NES. .GTS. .GES. .LTS.

3

.NOT.

2

.AND.

1

(lowest) .OR.

Not all operators require two operands as multiplication does. Some require only one operand. (In the C language there is an operator that requires three operands.) The number of operands required by an operator is called its arity.

Operators with arity of 2 are called binary operators. Those with an arity of 1 are called unary operators. A few examples:

     $ sum = a + b - c     $ sum = -sum     $ positive = sum .gt. 0     $ negative = not. Positive 

The first example contains an expression composed of two binary operators. The operands for the plus operator are A and B; the operands for the minus operator are the resulting sum and C. The second example uses the unary minus operator to negate its operand. Notice how the hyphen character is used as two different operators with different arities, its meaning determined by context. The third example uses the binary "greater than" operator to compare two numbers. The final example uses the unary "not" operator to invert its Boolean operand. The operator tables specify the arity and meaning of every DCL operator.

The order in which operators are applied to operands is determined by operator precedence. Table E-5 lists the precedence of the DCL operators. An operator with a high precedence is applied before an operator with a lower precedence, regardless of the order of their appearance in the expression. Every operator is assigned a precedence so that the order of application can be determined without ambiguity. Here are a few expressions to illustrate operator precedence:

    $ value = a * b + c    $ value = c + a * b    $ value = a * b - c * d    $ value = -x + y 

Because multiplication has a higher precedence than addition, the first two examples both multiply by A by B before adding C. This is true even though, in the second example, the multiply operator appears after the add operator. The order of evaluation is determined by operator precedence, not merely by order of appearance. The third example calculates the product of A and B, and then the product of C and D, and finally subtracts one product from the other. The fourth example negates X and then adds Y; the precedence of unary minus is higher than that of addition. If there are two or more operators of equal precedence in an expression, such as the multiply operators in the third example above, the operators are evaluated from left to right. In the third example, A * B is evaluated before C * D.

Sometimes the order of evaluation determined by operator precedence is not what you want. Parentheses are used to force operators to be evaluated in a certain order regardless of their precedence. When parentheses surround a portion of an expression, that portion is evaluated before the surrounding expression, regardless of precedence. Here are the preceding examples with parentheses added:

     $ value = a * (b + c)     $ value = (c + a) * b     $ value = a * (b - c) * d     $ value = -(x + y) 

The first example now calculates the sum of B and C and then multiplies it by A. The sum appears in parentheses, so it is evaluated first, even though the precedence of multiplication is higher. The second one adds C and A and then multiplies the sum of B. The third example subtracts C from B, multiplies the difference by A, and then multiplies that result by D. The final example adds X and Y and negates the resulting sum. In each case, the final value is different when parentheses are used.

As DCL evaluates an expression, it must decide whether each operand represents an integer, string, or Boolean value. In some cases, the type of the operands actually affects the meaning of the operator. Such a case is the plus (+) operator, which performs addition when its operands are integers, but performs string concatenation when its operands are character strings. DCL uses the following rules to match operators and operand types:

  • If the operator accepts only integer operands (e.g., * for multiply), then any string operands are first converted to integers.

  • If the operator accepts only string operands (e.g., .EQS. for string compare equal), then any integer operands are first converted to strings.

  • If the operator accepts either integers or strings (e.g., + for add or concatenate) and its operands are of different types, then integers win over strings and the string operand is first converted to an integer. For example, if you attempt to add an integer and a string, the string is first converted to an integer.

  • If the operator is a Boolean operator (e.g., .AND. for logical and), then the operands are interpreted as Boolean values according to the rules given in Table E-1.

A string can be converted to an integer as long as it contains a valid external representation of an integer (e.g., "-372" can be converted to -372). If it does not, it is converted to the integer 0. An integer can always be converted to a string by simply creating a string containing its external representation. Because these operator/operand matching rules are complicated, it is best to avoid using operators with mixed operand types. You can explicitly request that a string be converted to an integer, or vice versa, using the lexical functions F$INTEGER and F$STRING presented in the next section.[1]

[1]The material in Appendix E is drawn from Chapter 3 of Paul C. Anagnostopoulos and Steve Hoffman, Writing Real Programs in DCL, 2nd Edition, 1-55558-191-9, Digital Press, 1999, Butterworth-Heinemann.




Getting Started with OpenVMS System Management
Getting Started with OpenVMS System Management (HP Technologies)
ISBN: 1555582818
EAN: 2147483647
Year: 2004
Pages: 130
Authors: David Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net