VMS Help PASCAL, Expressions *Conan The Librarian (sorry for the slow response - running on an old VAX) |
Compaq Pascal expressions consist of one or more operands that result in a single value. If the expression contains more than one operand, the operands are separated by operators. Operands include numbers, strings, constants, variables, and function designators. Operators include arithmetic, relational, logical, string, set, and typecase operators. Compaq Pascal recognizes two forms of expressions: constant expressions and run-time expressions. Constant expressions result in a value at the time you compile your program. These expressions can include constants, constant identifiers, operators, and some predeclared functions. Run-time expressions can only result in a value at the time you execute your program. These expressions can include variables, predeclared functions, user-declared functions, and everything that a constant expression cannot contain. See the "Compaq Pascal Language Reference Manual" for restrictions on constant expressions and information on evaluation of expressions in statements. Syntax: simple-expression [[ {<> | < | <= | = | > | >= | IN} simple-expression ]]
1 - simple_expression |
The syntax for a simple expression is: {+ | -} term [[ {{+ | - | OR | OR_ELSE} term}...]]
2 - term |
The syntax for a term is: primary [[ {{* | / | DIV | REM | MOD | AND | AND_THEN} primary}... ]]
3 - primary |
The syntax for a primary is: factor [[ {** factor}... ]]
4 - factor |
A factor in a run-time expression can be any of the following: array-type-identifier array-constructor constant-identifier constructor of schema type or of types containing schema components (expression) [[ :: type-identifier ]] function-identifier [[ actual-parameter-list ]] NOT factor numeric-constant real-constant record-type-identifier record-constructor schema discriminant [[ set-type-identifier ]] set-constructor string-constant variable A factor in a compile-time expression cannot include the following: call to user-defined functions call to EOF and EOLN predeclared functions constructor of schema type or of types containing schema components schema discriminant variable
5 - Examples |
1. VARIABLES A variable can be in an expression: foo -'foo' is a predefined variable of some type new[1] -the first position of array 'new' rec.field -a field of record 'rec' pointer^ -the pointer variable of the pointer type 'pointer' cast::INTEGER -the variable 'cast' type cast as an integer 2. STRING CONSTANTS A string constant can have the following forms: name-string or {name-string ({constant-expression},...)}... [[name-string]] The 'name-string' is a quoted sequence of spaces, tabs, and any other printing characters. An apostrophe is expressed as ''. For example, 'hello there' is a name-string. The '{name-string ({constant-expressions},...)}... [[name-string]]' is a sequence that makes up a name-string. For example, when the list ('bell ' (7) 'character') is output to the terminal, you will see the string 'bell character' and the bell will ring (as indicated by the constant expression '(7)'). Additionally, Compaq Pascal allows string constants to be formed with double quotes. Inside of these double-quoted string constants, Compaq Pascal is able to recognize special characters that are specified with a backslash, as follows: o "\a" (Alert (bell) character) o "\b" (Backspace character) o "\f" (Forfeed character) o "\n" (New line or line feed character) o "\r" (Carriage return character) o "\t" (Horizontal tab character) o "\v" (Vertical tab character) o "\\" (Backslash character) o "\"" (Double quotation mark character) o "\'" (Single quotation mark character) o "\nnn (Character whose value is nnn, where nn is an octal number from 00 to 377.) o "\xnn" (Character whose value if nn, where nn is a hexadecimal number from 00 to FF.) 3. CONSTANT IDENTIFIER A constant identifier is an identifier of a type that can be determined at compile time. The following are examples of constant identifiers: CONST Foo = 3; Exp = 8 * 9; Func = MAX( 3, 2, 4 ); 4. EXPRESSION IDENTIFIER An expression identifier is an expression in parentheses optionally followed by a type cast structure. Examples are: ( a + b ) - 'a' and 'b' are predeclared variable identifiers ( Foo ) :: INTEGER - expression 'Foo' type cast as an integer 5. FUNCTION IDENTIFIER A function identifier is the name of a predeclared function. If the function has formal parameters, the function identifier must be followed by one actual parameter for each formal parameter listed. For example: FUNCTION Foo ( VAR n : INTEGER; start : Boolean ) : REAL; A call to function 'Foo' could look like the following: Foo( bar, TRUE ) Function 'Foo' returns a REAL value.
6 - Operators |
Pascal provides several classes of operators. You can form complex expressions by using operators to combine constants, constant identifiers, variables, and function designators.
6.1 - Arithmetic Operators
An arithmetic operator usually provides a formula for calculating a value. To perform arithmetic operations, numeric operands are combined with one or more of the arithmetic operators. Arithmetic Operators: operator | example | result -------------------------------------------- + A + B Sum of A and B - A - B B subtracted from A * A * B Product of A and B ** A ** B A raised to the power of B / A / B A divided by B DIV A DIV B Result of A divided by B, truncated toward zero REM A REM B Remainder of A divided by B MOD A MOD B Modulus of A with respect to B
6.2 - Relational Operators
A relational operator tests the relationship between two ordinal, real, DOUBLE, or QUADRUPLE expressions and returns a Boolean value. If the relationship holds, the result is TRUE; otherwise the result is FALSE. You can also apply some of the relational operators to string operands and to set operators. Relational Operators: operator | example | result -------------------------------------------- <> A <> B TRUE if A is not equal to B < A < B TRUE if A is less than B <= A <= B TRUE if A is less than or equal to B > A > B TRUE if A is greater than B >= A >= B TRUE if A is greater than or equal to B
6.3 - Logical Operators
A logical operator evaluates one or more Boolean expressions and returns a Boolean value. Logical Operators: operator | example | result ------------------------------------------------- AND A AND B TRUE if both A and B are TRUE OR A OR B TRUE if either A or B is TRUE, or if both are TRUE NOT NOT A TRUE if A is FALSE, and FALSE if A is TRUE AND_THEN A AND_THEN B TRUE if both A and B are TRUE (forces left-to-right evaluation order with short-circuiting) OR_ELSE A OR_ELSE B TRUE if either A or B is TRUE, or if both are TRUE (forces left-to-right evaluation order with short-circuiting)
6.4 - String Operators
A string operator concatenates or compares character-string expressions. The result is either a string or a Boolean value. String Operators: operator | example | result -------------------------------------------- + A + B String that is the concatenation of strings <> A <> B TRUE if strings A and B have unequal ASCII values < A < B TRUE if the ASCII value of string A is less than that of string B <= A <= B TRUE if the ASCII value of string A is less than or equal to that of string B > A > B TRUE if the ASCII value of string A is greater than that of string B >= A >= B TRUE if the ASCII value of string A is greater than or equal to that of string B
6.5 - Set Operators
A set operator forms the union, intersection, difference, or exclusive-OR of two sets, compares two sets, or tests an ordinal value for inclusion in a set. Its result is either a set or a Boolean value. Set Operators: operator | example | result -------------------------------------------- + A + B Set that is the union of sets A and B * A * B Set that is the intersection of sets A and B - A - B Set of those elements in set A that are not also in set B <> A <> B TRUE if set A is not equal to set B <= A <= B TRUE if set A is a subset of set B >= A >= B TRUE if set B is a subset of set A IN C IN B TRUE if C is an element of set B
6.6 - Type Cast Operator
The type cast operator changes the context in which a variable or an expression of a certain data type can be used. The actual representation of the object being cast is not altered by the type cast operator. Compaq Pascal overrides the type only for the duration of one operation. It has one of the following forms: variable-identifier :: type-identifier (expression) :: type-identifier The type cast operator (::) separates the name of the variable or an expression in parentheses from its target type, the type to which it is being cast. Example: TYPE F_Float = PACKED RECORD Frac1 : 0..127; Expo : 0..255; Sign : Boolean; Frac2 : 0..65535; END; VAR A : REAL; {In the executable section:} A :: F_Float.Expo := A :: F_Float.Expo + 1; The record type 'F_Float' illustrates the layout of an F_floating real number. The real variable 'A' is cast as a record of this type, allowing access to the fields containing the mantissa, exponent, sign, and fraction of 'A'. Adding 1 to the field containing the exponent would give the same result as multiplying 'A' by 2.0.
|