Expressions

# Expressions

Alice Pascal supports a large number of operators that can be used in various types of expressions. The operations in an expression are evaluated according to a fixed order of precedence. For example, any multiplications in an expression are normally evaluated before additions (as is the convention in normal arithmetic). This standard order of operation can be altered using parentheses in the usual way.

Some operations share the same precedence (e.g. addition and subtraction). The set of all operations with a given precedence comprises a precedence class. When the time comes for operations of a given class to be performed, they may be evaluated from right to left or left to right, depending on the class.

The sections in the chapter describe the operators in each precedence class. These sections are arranged from highest precedence to lowest. Subsections within each section describe individual operators.

## Factor Operations

The following expressions give results that are known as factors. Factor operations have the highest precedence in Pascal.

variable

unsigned constant

function(argument list)

[ element list ]

( expression )

not expression

### Variables and Unsigned Constants

The term variable refers to several things that can be assigned values: simple variables, elements in an array, or fields in a record. If a variable reference occurs in an expression, Alice will use the value of that variable when evaluating the expression. An unsigned constant may be an unsigned number, a string constant, or an identifier that stands for a constant (either a constant named in the const section, or a name that stands for a value in a scalar type).

### Function Calls

A function call may be used as part of an expression. It takes the form of the name of the function followed by parentheses containing the arguments for the function. If no arguments are being passed to the function, the parentheses are omitted. The value of the function in the expression is the value that is returned as the result of the function.

Note that procedure calls have exactly the same form. However, procedures calls cannot be used in expressions, because procedures do not return values.

### Set Factors

A set factor consists of an element list enclosed in square brackets. The elements of the list are separated by commas. All elements in the list must have the same scalar type. The element list may include subranges as well as single elements. For example,

>     [ 'a'..'z' , 'A'..'Z' , '0'..'9' , ' ' ]

is a set that contains all the characters that may legally appear in a Pascal identifier.

### Parenthesized Expressions

The value of a parenthesized expression is the value of the expression inside the parentheses. Parentheses indicate that the enclosed expression should be evaluated before other (unparenthesized) expressions.

### The not Operator

The not operator is a unary operator that can be applied to `Boolean` or `integer` operands. The form of the operator is

not operand

If the operand is `true`, not makes it `false`. If the operand is `false`, not makes it `true`.

If the operand is an integer, not turns all the 0-bits in the integer into 1-bits, and all the 1-bits into 0-bits. This is only of interest to those familiar with bit operations on your computer.

## Multiplying Operators

Multiplying operators follow the factor operations in precedence. Multiplying operators are executed from left to right, in the order in which they appear on the line. Below we list the possible multiplying operations.

expression * factor

expression / factor

expression div factor

expression mod factor

expression and factor

expression shl factor

expression shr factor

### The * Operator

The `*` operator may be applied either to numbers or to sets. When `*` is applied to numeric values, it stands for normal multiplication. The operands can be either `real` or `integer`. If both operands are `integer`, the result will be `integer`. If either operand is `real`, the result will be `real`. Pascal allows multiplication of real values by integers.

When `*` is applied to set values, it stands for intersection. If `A` and `B` are sets, `A*B` is the set that contains all the elements that are in both `A` and `B`. For example,

>     [1..10,20,30,40] * [20..34]

yields the set value

>     [20,30]

### The / Operator

The `/` operator stands for real division. `A/B` divides `A` by `B`. The operands may be `real` or `integer`. Integer operands will be converted to `real` before the division takes place. The result of real division is always `real`.

### The div Operator

The `div` operator stands for integer division. The operands must be `integer` and its result is `integer`. The result of

>     A div B

is the integer you get when you divide `A` by `B` and throw away any remainder. For example,

>     7 div 2

is the integer `3`.

>     -7 div 2

is the integer `-3`.

### The mod Operator

The mod operator stands for the mathematical ``modulo'' operation. Its operands must both be `integer`. The general form of the operation is

>     A mod B

where `A` and `B` are expressions. The value of `B` must be positive.

The result of the mod operation is always non-negative, and is equal to `A` modulo `B`. If `A` is non-negative, this value will be equal to

>     A - ( (A div B) * B )

In other words, it is the non-negative remainder that is left after `A` is divided by `B`.

### The and Operator

The and operator has the same precedence as the other multiplying operators, but is not really related to them. Both operands must have the same type and must be `Boolean` or `integer`. The result is the same type as the operands. If the operands are `Boolean`,

>     A and B

yields a `Boolean` result that is `true` when both `A` and `B` are `true`, and is `false` otherwise.

If the operands are `integer`, the result is an integer with 1-bits wherever both operands have 1-bits and 0-bits everywhere else. This is only of interest to those who are familiar with bit operations on the computer.

It is important to point out that and has a higher precedence than relational comparisons like `>`, `=`, and so on. This means that comparisons joined with and must be parenthesized, as in

>     if (a > b) and (b > c) then ...

Omitting the parentheses will result in a syntax error.

### The shl Operator

The shl operator shifts the bits of an integer to the left. Vacated bits are filled with zeroes. The form of the operation is

>     A shl B

`A` is the integer that is shifted. `B` is an integer giving the number of bits to shift. `B` must be in the range `0..15`.

### The shr Operator

The shr operator shifts the bits of an integer to the right. Vacated bits are filled with the sign bit of the integer. The form of the operation is

>     A shr B

`A` is the integer that is shifted. `B` is an integer giving the number of bits to shift. `B` must be in the range `0..15`.

## Adding Operators

Adding operators follow the multiplying operations in precedence. Adding operators are executed from left to right, in the order in which they appear on the line. Below we list the possible adding operations.

+ expression

- expression

expression + expression

expression - expression

expression or expression

expression xor expression

### The Unary + Operator

The operation

+ expression

is known as the unary `+` operation. The value of this operation is just the value of the expression that follows the `+`. The operand must be `integer` or `real`.

### The Unary - Operator

The operation

- expression

is known as the unary `-` operation. The operand must be `integer` or `real`. The value of the operation is the negative of the expression that follows the `-`.

### The Binary + Operation

The binary `+` may be applied to numbers, sets, or strings. It has the general form

>     A + B

If `A` and `B` are numbers (either `integer` or `real`), `A+B` is the sum of `A` and `B`. If both operands are `integer`, the result will be `integer`. Otherwise, the result will be `real`. Pascal permits the addition of a `real` and an `integer`.

If `A` and `B` are sets, `A+B` is the union of the sets. In other words, it is the set containing all the elements that are in `A` or `B` or both. For example,

>     [1..10,20,30,40] + [20..34]

is the set

>     [1..10,20..34,40]

If `A` and `B` are string types, `A+B` is the string you get when you concatenate `B` to the end of `A`. For example,

>     "abc" + "def"

is the string

>     "abcdef"

### The Binary - Operation

The binary `-` may be applied to numbers or sets. It has the general form

>     A - B

If `A` and `B` are numbers (either `integer` or `real`), `A-B` gives the result of `A` minus `B`. If both operands are `integer`, the result will be `integer`. Otherwise, the result will be `real`. Pascal permits the subtraction of a `real` and an `integer`.

If `A` and `B` are sets, `A-B` is the difference of the sets. In other words, it is the set containing all the elements that are in `A` but are not in `B`. For example,

>     [1..10,20,30,40] - [20..34]

is the set

>     [1..10,40]

### The or Operator

The or operator has the same precedence as the other adding operators, but is not really related to them. The operands of or must have the same type, either `Boolean` or `integer`. The result is the same type as the operands. If the operands are `Boolean`,

>     A or B

yields a `Boolean` result that is `false` when both `A` and `B` are `false`, and is `true` otherwise.

If the operands are `integer`, the result is an integer with 0-bits where both `A` and `B` have 0-bits, and 1-bits elsewhere.

As with the and operator, or has a higher precedence than the relational comparisons. This means that comparisons joined with or must be parenthesized, as in

>     if (a > b) or (a < c) then ...

### The xor Operator

The xor operator performs the ``exclusive OR'' operation. Its arguments must both have the same type and must be either `Boolean` or `integer`. The result has the same type as the operands. If the operands are `Boolean`,

>     A xor B

is `false` if `A` and `B` are both false or both true. It is `true` if one operand is true but not the other.

If the operands are `integer`, the result is an integer that has 1-bits where one operand has a 1-bit and the other has a 0-bit; the result has 0-bits where both operands have either 1-bits or 0-bits.

## The Relational Operators

The relational operators have the lowest precedence of any Pascal operators. All the relational operators yield `Boolean` results. Below we list the relational operators and what data types they may be applied to. (Some also apply to strings but this will be described later.)

expression = expression (`scalars,reals,sets,pointers`)

expression <> expression (`scalars,reals,sets,pointers`)

expression > expression (`scalars,reals`)

expression >= expression (`scalars,reals,sets`)

expression < expression (`scalars,reals`)

expression <= expression (`scalars,reals,sets`)

### The Equal Comparison (=)

The `=` operator can be used to compare any two operands of the same scalar type or the `real` type.

>     A = B

is `true` if the operands have the same value, and `false` otherwise. `=` can also be used to compare sets and pointers for equality, but it cannot be used to compare objects of other data types (e.g. records).

### The Not Equal Comparison (<>)

The ``not equal'' comparison can be used to compare any two operands of the same scalar type or the `real` type.

>     A <> B

is `false` if the operands have the same value, and `true` otherwise. `<>` can also be used to compare sets and pointers for inequality, but it cannot be used to compare objects of other data types (e.g. records).

### The Greater Than Comparison (>)

The `>` operator can be used to compare any two operands of the same scalar type or the `real` type. For integers and real values,

>     A > B

is `true` if `A` is greater than `B`, and `false` otherwise. For scalar types that were created by listing the elements of the type, `A` is greater than `B` if `A` came later in the list than `B`. For `Boolean` values, `true` is greater than `false`.

### The Less Than Comparison (<)

The `<` operator can be used to compare any two operands of the same scalar type or the `real` type. For integers and real values,

>     A < B

is `true` if `A` is less than `B`, and `false` otherwise. For scalar types that were created by listing the elements of the type, `A` is less than `B` if `A` came earlier in the list than `B`. For `Boolean` values, `false` is less than `true`.

### The Greater Than or Equal Comparison (>=)

The `>=` operator can be used to compare any two operands of the same scalar type or the `real` type. For all these types,

>     A >= B

is `true` if either `A>B` or `A=B`; otherwise, the comparison is `false`.

The `>=` operator can also be used to compare two set operands. In this case, `A>=B` is `true` if the set `A` contains `B` as a subset; otherwise, it is `false`.

### The Less Than or Equal Comparison (<=)

The `<=` operator can be used to compare any two operands of the same scalar type or the `real` type. For all these types,

>     A <= B

is `true` if either `A<B` or `A=B`; otherwise, the comparison is `false`.

The `<=` operator can also be used to compare two set operands. In this case, `A<=B` is `true` if the set `A` is a subset of `B`; otherwise, it is `false`.

### The in Operator

The in operator is used to determine if a particular value happens to be an element of a set. The operation has the form

>     A in B

where `A` is a value of a scalar type and `B` is a set of that type. The result of the comparison will be `true` if the value `A` is in `B`, and `false` otherwise.

### String Comparisons

The operators

>     =    <>    >    >=    <     <=

can be used to compare packed array of char strings. One string is greater than another if it is lexicographically greater (i.e., if it comes later when the strings are sorted according to the ASCII collating sequence).

String comparisons are made character by character. The comparison will stop at the end of the shorter string, or when the special `StrEnd` character is found in either string.