# 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.

      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

- Basic types
- Array types
- Record types
- Pointer types
- Procedure types

## Basic types

The basic types are denoted by predeclared identifiers. The associated operators are defined in Operators and the predeclared function procedures in Predeclared procedures 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

## 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 Pointer types), element types of open array types, and formal parameter types (see Formal parameters). Examples:

       ARRAY 10, N OF INTEGER
       ARRAY OF CHAR

## 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 Operands). 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 section Definition of terms). An extended type T1 consists of the fields of its base type and of the fields which are declared in T1 (see section Type declarations). 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

## 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 TO` T, a call of the predeclared procedure `NEW(p)` (see Predeclared procedures) 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.

## 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 section Formal parameters) of P and T must match (see Definition of terms). P must not be a predeclared or type-bound procedure nor may it be local to another procedure.

      ProcedureType  = PROCEDURE [FormalParameters].