6. Type declarations
A data type determines the set of values which variables of that type
may assume, and the operators that are applicable. A type declaration associates
an identifier with a type. In the case of structured types (arrays and records)
it also defines the structure of variables of this type. A structured type
cannot contain itself.
| TypeDeclaration |
= |
IdentDef "=" Type. |
| Type |
= |
Qualident | ArrayType | RecordType | PointerType | ProcedureType.
|
Examples:
Table = ARRAY N OF REAL
Tree = POINTER TO Node
Node = RECORD
key: INTEGER;
left, right: Tree
END
CenterTree = POINTER TO CenterNode
CenterNode = RECORD (Node)
width: INTEGER;
subnode: Tree
END
Function = PROCEDURE(x: INTEGER): INTEGER
6.1 Basic types
The basic types are denoted by predeclared identifiers. The associated
operators are defined in 8.2 and the predeclared
function procedures in 10.3. The values of the given basic types are the
following:
| 1. |
BOOLEAN |
the truth values TRUE and FALSE |
| 2. |
CHAR |
the characters of the extended ASCII set (0X .. 0FFX) |
| 3. |
SHORTINT |
the integers between MIN(SHORTINT) and MAX(SHORTINT) |
| 4. |
INTEGER |
the integers between MIN(INTEGER) and MAX(INTEGER) |
| 5. |
LONGINT |
the integers between MIN(LONGINT) and MAX(LONGINT) |
| 6. |
REAL |
the real numbers between MIN(REAL) and MAX(REAL) |
| 7. |
LONGREAL |
the real numbers between MIN(LONGREAL) and MAX(LONGREAL) |
| 8. |
SET |
the sets of integers between 0 and MAX(SET) |
Types 3 to 5 are integer types, types 6 and 7 are real types,
and together they are called numeric types. They form a hierarchy;
the larger type includes (the values of) the smaller type:
LONGREAL >= REAL >= LONGINT >= INTEGER >= SHORTINT
6.2 Array types
An array is a structure consisting of a number of elements which are all
of the same type, called the element type. The number of elements of
an array is called its length. The elements of the array are designated
by indices, which are integers between 0 and the length minus 1.
| ArrayType |
= |
ARRAY [Length {"," Length}] OF Type. |
| Length |
= |
ConstExpression. |
A type of the form
ARRAY L0, L1, ..., Ln OF T
is understood as an abbreviation of
ARRAY L0 OF
ARRAY L1 OF
...
ARRAY Ln OF T
Arrays declared without length are called open arrays. They are
restricted to pointer base types (see 6.4), element
types of open array types, and formal parameter types (see 10.1). Examples:
ARRAY 10, N OF INTEGER
ARRAY OF CHAR
6.3 Record types
A record type is a structure consisting of a fixed number of elements,
called fields, with possibly different types. The record type declaration
specifies the name and type of each field. The scope of the field identifiers
extends from the point of their declaration to the end of the record type,
but they are also visible within designators referring to elements of record
variables (see 8.1). If a record type
is exported, field identifiers that are to be visible outside the declaring
module must be marked. They are called public fields; unmarked elements
are called private fields.
| RecordType |
= |
RECORD ["("BaseType")"] FieldList {";" FieldList} END. |
| BaseType |
= |
Qualident. |
| FieldList |
= |
[IdentList ":" Type ]. |
Record types are extensible, i.e. a record type can be declared as an
extension of another record type. In the example
T0 = RECORD x: INTEGER END
T1 = RECORD (T0) y: REAL END
T1 is a (direct) extension of T0 and T0 is
the (direct) base type of T1 (see App.
A). An extended type T1 consists of the fields of its base type
and of the fields which are declared in T1. All identifiers declared
in the extended record must be different from the identifiers declared in
its base type record(s).
Examples of record type declarations:
RECORD
day, month, year: INTEGER
END
RECORD
name, firstname: ARRAY 32 OF CHAR;
age: INTEGER;
salary: REAL
END
6.4 Pointer types
Variables of a pointer type P assume as values pointers to
variables of some type T. T is called the pointer base type
of P and must be a record or array type. Pointer types adopt the extension
relation of their pointer base types: if a type T1 is an extension
of T, and P1 is of type POINTER TO T1, then P1
is also an extension of P.
| PointerType |
= |
POINTER TO Type. |
If p is a variable of type P = POINTER TOT, a call
of the predeclared procedure NEW(p) (see 10.3) allocates a variable
of type T in free storage. If T is a record type or an array
type with fixed length, the allocation has to be done with NEW(p);
if T is an n-dimensional open array type the allocation has to be done with
NEW(p, e0, ..., en-1) where T is allocated with lengths given
by the expressions e0, ..., en-1. In either case a pointer to the
allocated variable is assigned to p. p is of type P.
The referenced variable p^ (pronounced as p-referenced)
is of type T. Any pointer variable may assume the value NIL, which
points to no variable at all.
6.5 Procedure types
Variables of a procedure type T have a procedure (or NIL) as value.
If a procedure P is assigned to a variable of type T, the formal
parameter lists (see Ch. 10.1) of P and T must match
(see App. A). P must not be a predeclared
or type-bound procedure nor may it be local to another procedure.
| ProcedureType |
= |
PROCEDURE [FormalParameters]. |
Previous Section, Next Section, Contents
Adapted to HTML by Jürgen Geßwein; 8. Juni 1995