Chapter 6. Expressions

Table of Contents
6.1. Simple Expressions
6.2. Conditional Expressions

The term expression is reserved for arithmetic expressions. Coral 66 has no designational expressions of Algol 60 type. As there are no Boolean variables and no bracketed Boolean expressions (see Section 6.2.1), the expressions after IF are known as conditions. The syntax for expressions is

Expression ::= 
     Unconditionalexpression
     Conditionalexpression 

Unconditionalexpression ::= 
     Simpleexpression
     String 

Strings are defined in Section 10.4.

6.1. Simple Expressions

Arithmetic is performed with the monadic and dyadic adding operators + and -, and with the dyadic multiplying operators * (multiply) and / (divide). The plus and minus operators work on terms, which are combinations of factors joined by multiplication or division. There is no exponentiation operator. The syntax for simple expression begins as follows

Simpleexpression ::= 
     Term
     Addoperator  Term
     Simpleexpression  Addoperator Term

Term ::= 
     Factor
     Term  Multoperator  Factor 

Addoperator ::= 
    +
    - 

Multoperator ::= 
    *
    / 

6.1.1. Primaries

Primaries are the basic operands in expressions. For example in the analysis of the expression


x + y * (a + b) - 4

we discover three terms, the primary x, the term y * (a + b) and the primary 4. The middle term is the product of two factors, the primary y and the primary (a + b). To complete the analysis, all expressions from within brackets are similarly analyzed until no further reduction is possible an no expression brackets remain. When an expression contains no word-logical operators (see Section 6.1.1.2), a factor must be a primary, which may or may not be of a defined type. Thus,

Factor ::= 
     Primary
     Booleanword 

Primary ::= 
     Untypedprimary
     Typedprimary 

6.1.1.1. Untyped Primaries

Untyped primaries are those operands which cannot be classed as integer, floating-point or fixed-point (of known scale) without reference to their context. For example, the number 3.1416 may be represented, with varying degrees of accuracy, in may different ways within a computer word. The same applies to an expression, whose type is determined by context (see Section 6.1.3).

Untypedprimary ::= 
     Real
    (  Expression ) 

A "real" (see Section 10.2) is an unsigned numerical constant containing a decimal or octal point or a tens exponent.

6.1.1.2. Typed Primaries

Typed primaries are classified as follows

Typedprimary ::= 
     Wordreference
     Partword
     LOCATION (  Wordreference )
     Numbertype (  Expression )
     Procedurecall
     Integer 

6.1.1.2.1. Word References

A simple reference, or a reference to an array element or whole-word table-element, has a type defined in its declaration. Such references may be described as word references because they refer to items of data for which whole computer words are set aside. A further kind of word reference, the anonymous reference, takes the form


[ Index ]

where the index is any expression evaluated to an integer to give the actual location of a computer word. An anonymous reference possesses all the properties of an identified reference, except that it lacks an identifier. Just as a variable i, declared as INTEGER i, may be used in an expression to refer to the contents of the computer word allocated to i, so the use of an anonymous reference in an expression will refer to the contents of the address defined by Index. Such contents are taken to be of numeric type INTEGER, irrespective of any type declaration which may have been associated with that word with some other type. See also Section 6.1.1.2.3. The syntax for a word reference is

Wordreference ::= 
     Id
     Id [  Index ]
     Id [  Index ,  Index]
    [  ] 

Index ::= 
     Expression 

6.1.1.2.2. Part-Words

Any single item of packed data may act as a typed primary. Such an item is either

  1. a reference to a part-word table-element, or

  2. a specified field of any typed primary.

In case (a), the type is defined in the table declaration. In case (b), the desired field is selected by a prefix of the form


BITS[ Totalbits , Bitposition]

in front of the primary to be operated upon. The result of this operation is a positive[1] integer value of width Totalbits and in units of the bit at Bitposition. The value will in general be implementation-dependent, even though the operand must be typed, as no conventions are laid down for the internal representation of floating-point or fixed-point items of data. In all cases, however, the numeric type resulting from the application if BITS is INTEGER. The syntax for a part-word, which should be distinguished from that of a part-word reference (see Section 7.1), is

Partword ::= 
     Id [  Index ]
     BITS [  Totalbits ,  BitpositionTypedprimary 

6.1.1.2.3. Locations

The computer location of any word reference is obtainable by the location operator which is written in the form


LOCATION( Wordreference )

and has the value of type INTEGER. It may be notes that if i and j refer to integers, [LOCATION(i)] is equivalent to i, and LOCATION([j]) is equivalent to j. The reasoning is as follows. LOCATION(i) is the address of the computer word allocated to i. Enclosure in square brackets forms an entity equivalent to an identifier standing for this address, which by hypothesis is i. Similarly, [23] is equivalent to an identifier for the address 23, and LOCATION([23]) is the address for which this fictitious identifier stands, which is 23 by hypothesis.

6.1.1.2.4. Explicit Type-Changing

A typed primary may have its type changed, and an untyped primary may be typed, by enclosure within round brackets preceded by a specific Numbertype as described in Section 6.1.3.

6.1.1.2.5. Functions

The call of a typed procedure (see Chapter 8) may be treated as a function and used as a primary in any expression. For the syntax of a procedure call, See Section 7.3.

6.1.1.2.6. Integers

An integer used in any expression (see Section 10.2) can be assumed to have the numeric type INTEGER before any necessary type-changes are enforced by context.

6.1.2. Word-Logic

Three dyadic logical operators are defined for use between typed primaries. The effect of these operators is implementation-dependent to the extent that the word-representation of data is not defined by the language. The ith bit of the result is a given logical function of the ith bits of the two operands, and the result as a whole has the numeric type INTEGER. To avoid confusion with Boolean operators in conditions (see Section 6.2.1), a different terminology is used. The operators are


DIFFER        UNION         MASK
  0 1          0 1          0 1
 -----        -----        -----
0|0 1        0|0 1        0|0 0
1|1 0        1|1 1        1|0 1

DIFFER is recognizable as "not equivalent", UNION as "inclusive or" and MASK as "and". The operators are shown in order of increasing tightness of binding. As bracketed expressions are untyped, the use of brackets to overcome binding priorities entails explicit integer scaling. For example


a MASK INTEGER(b UNION c)

The formal syntax continued from Section 6.1.1, is

Booleanword ::= 
     Booleanword2 
     Booleanword4  DIFFER  Booleanword5 

Booleanword2 ::= 
     Booleanword3 
     Booleanword5  UNION  Booleanword6 

Booleanword3 ::= 
     Booleanword6  MASK  Typedprimary 

Booleanword4 ::= 
     Booleanword 
     Typedprimary 

Booleanword5 ::= 
     Booleanword2 
     Typedprimary 

Booleanword6 ::= 
     Booleanword3 
     Typedprimary 

6.1.3. Evaluation of Expressions

Expressions are used in assignment statements, as value parameters of procedures and as integer indexes, all of which contexts determine the numeric type finally required. Coral 66 expressions are automatically evaluated to this type, but in the process of calculation, data may be subjected by the compiler to various intermediate transformations. Although an algorithm for evaluating expressions does not form part of the official definition of the language, all syntactically outermost terms in an expression will be evaluated to the required numeric type before the adding operators are applied. In the simplest cases, this rule ensures predictable results, though it should be particularly noted that rounding-off errors will not be minimal, and overflow may occur. If an expression is enclosed in round brackets, its terms are not "outermost", the rule no longer applies, and the algorithm for the particular compiler determines the sequence of events. The programmer can impose any desired system of evaluation by the use of Numbertype(Expression), which is a typed primary (see Section 6.1.1.2), any occurrence of which behaves like a variable, ref (say), declared as


Numbertype ref;

and assigned a value by


ref := Expression

before it is used. For example, if i and j are integer references and x is a floating-point reference, the assignment statement


x := i - j

causes i and j to be converted to floating-point before the subtraction, whilst


x := INTEGER(i - j)

causes subtraction of integers before conversion to floating point. Although the order of evaluation of expression is undefined, the following rule concerning functions will apply. Value parameters of a function are necessarily evaluated before the function itself is computed, so that the order of evaluation of sin(cos(expn)) will be expn, cos, sin. Apart from this type of reversal, functions occurring in a simple expression will be evaluated in the order in which they appear when the expression is read from left to right, regardless of brackets.

Notes

[1]

It is assumed that Totalbits will not be set equal to the full word length