Skip to content

Language Specification (9) : Predefined Identifiers

Benjamin Kowarsch edited this page May 10, 2023 · 16 revisions

Predefined Identifiers

Predefined identifiers are language defined and built-in. They are pervasive, that is, they are visible in every scope without import. Pervasive identifiers do not have qualified names and are always referenced unqualified. Unlike earlier versions of Modula-2, pervasive identifiers are now reserved and may not be redefined.

The following identifiers are predefined:

NIL, TRUE, FALSE, BOOLEAN, OCTET, CHAR, UNICHAR, CARDINAL, LONGCARD, INTEGER, LONGINT, REAL, LONGREAL, APPEND, INSERT, REMOVE, CHR, UCHR, COLLATION, ORD, ODD, ABS, SGN, MIN, MAX, LOG2, POW2, ENTIER, PRED, SUCC, PTR, CAPACITY, COUNT, LENGTH, FIRST, LAST, PREV, NEXT, TMIN, TMAX, TSIZE, TLIMIT;

Predefined Constants

NIL

Identifier NIL represents an invalid pointer or address value. It is per definition compatible with any pointer type, including all opaque pointer types and all procedure types.

TRUE and FALSE

Identifiers TRUE and FALSE represent the values of type BOOLEAN.

Predefined Types

BOOLEAN

Type BOOLEAN is an enumerated type. It represents logical truth values used in boolean expressions. It is defined as

TYPE BOOLEAN = ( FALSE, TRUE );

The order is significant. ORD(FALSE) is always zero, ORD(TRUE) is always one.

OCTET

Type OCTET is a cardinal type.

It represents the smallest addressable unit which is per definition 8 bits.

Its value range is always [0..255].

Whole number literals are compatible with type OCTET.

CHAR

Type CHAR is a countable type.

It represents the 7-bit character codes of ISO 646 (aka ASCII).

Its value range is always [0u0..0u7F].

Quoted literals of exactly one ISO 646 code point are compatible with type CHAR.

UNICHAR

Type UNICHAR represents the 32-bit character codes of ISO 10646 UCS-4.

Its value range is [0u0..0u10FFFF].

Quoted literals of exactly one ISO 10646 code point are compatible with type UNICHAR.

CARDINAL

Type CARDINAL is a cardinal type. It represents unsigned whole numbers.

Its value range is [0..TMAX(CARDINAL)] where TMAX(CARDINAL) = POW2(8*TSIZE(CARDINAL)) - 1

The value of TSIZE(CARDINAL) is implementation defined, where TSIZE(CARDINAL) <= TSIZE(LONGCARD).

Whole number literals are compatible with type CARDINAL.

LONGCARD

Type LONGCARD is a cardinal type. It represents unsigned whole numbers with extended range.

Its value range is [0..TMAX(LONGCARD)] where TMAX(LONGCARD) = POW2(8*TSIZE(LONGCARD)) - 1

The value of TSIZE(LONGCARD) is implementation defined where TSIZE(LONGCARD) >= TSIZE(ADDRESS)

Whole number literals are compatible with type LONGCARD.

INTEGER

Type INTEGER is an integer type. It represents signed whole numbers.

Its value range is [TMIN(INTEGER)..TMAX(INTEGER)].

The internal representation of type INTEGER is always in two's complement. Thus

  • TMIN(INTEGER) = (-1) * POW2(8*TSIZE(INTEGER) - 1)
  • TMAX(INTEGER) = POW2(8*TSIZE(INTEGER) - 1) - 1

The value of TSIZE(INTEGER) is always equal to TSIZE(CARDINAL).

Whole number literals are compatible with type INTEGER.

LONGINT

Type LONGINT is an integer type. It represents signed whole numbers with extended range.

Its value range is [TMIN(LONGINT)..TMAX(LONGINT)].

The internal representation of type LONGINT is always in two's complement. Thus

  • TMIN(LONGINT) = (-1) * POW2(8*TSIZE(LONGINT) - 1)
  • TMAX(LONGINT) = POW2(8*TSIZE(LONGINT) - 1) - 1

The value of TSIZE(LONGINT) is always equal to TSIZE(LONGCARD).

Whole number literals are compatible with type LONGINT.

REAL

Type REAL is a real number type. It represents real numbers with standard range and precision.

Its value range is [TMIN(REAL)..TMAX(REAL)].

The internal representation of type REAL is implementation defined where

  • TMIN(REAL) <= TMIN(LONGINT) and TMAX(REAL) >= TMAX(LONGCARD)

IEEE 754 single or double precision format is recommended for type REAL.

Real number literals are compatible with type REAL.

LONGREAL

Type LONGREAL is a real number type. It represents real numbers with extended range or precision or both.

Its value range is [TMIN(LONGREAL)..TMAX(LONGREAL)].

The internal representation of type LONGREAL is implementation defined where

  • TMIN(LONGREAL) <= TMIN(REAL) and TMAX(LONGREAL) >= TMAX(REAL)
  • the precision of type REAL must not exceed that of type LONGREAL

IEEE 754 double or quadruple precision format is recommended for type LONGREAL.

Real number literals are compatible with type LONGREAL.

Predefined Procedures

APPEND

Procedure APPEND appends one or more values to an array.

PROCEDURE APPEND ( VAR array : ArrayType; values : ARGLIST OF ValueType );

Values to be appended must be of the component type of the array.

INSERT

Procedure INSERT inserts one or more values of a given list into a set.

PROCEDURE INSERT ( VAR set : SetType; values : ARGLIST OF ValueType );

Values passed in the list must be of the value type of the set.

REMOVE

Procedure REMOVE has multiple signatures, depending on the type of its first argument.

If its first argument is a set, it removes the values in a given list from the set.

PROCEDURE REMOVE ( VAR set : SetType; values : ARGLIST OF ValueType );

Values passed in the list must be of the value type of the set.

If its first argument is a dictionary, it removes the keys in a given list along with their values from the dictionary.

PROCEDURE REMOVE ( VAR dict : DictType; values : ARGLIST OF KeyType );

Values passed in the list must be of the key type of the dictionary.

If its first argument is an array, it removes the values addressed by a given index range from the array.

PROCEDURE REMOVE ( VAR array : ArrayType; startIndex, endIndex : LONGCARD );

Any relative (negative) indices passed are translated into absolute (positive) indices by the language processor.

Predefined Functions

CHR

Function CHR returns the character whose character code is its argument.

PROCEDURE CHR ( n : T ) : CHAR;

where 0 ≤ n ≤ 127 ∧ TTcardinal.

UCHR

Function UCHR returns the unicode character whose character code is its argument.

PROCEDURE UCHR ( n : T ) : UNICHAR;

where 0 ≤ n ≤ 0x10FFFF ∧ TTcardinal.

COLLATION

Function COLLATION returns the index within the prevailing collation sequence for a given character.

PROCEDURE COLLATION ( ch : T ) : LONGCARD;

where TTchar.

The return values are determined as follows:

  • for tabulator 0u09 and newline 0u0A, the index for whitespace 0u20 is looked up and returned
  • for all other control characters, TMAX(LONGCARD) is returned — they are ignored by the collation algorithm
  • for characters in range [0u20..0u7E], the return value is looked up in the prevailing collation table
  • for accented letters, the return value equals that of the corresponding unaccented letter
  • for all other characters, the ordinal value of the character is returned

The default collation sequence for printable 7-bit characters is:

␣0123456789@aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ!"#$%&’()*+,-./:;<=>?[\]∧_‘{|}~

The prevailing collation order may be changed by import of a collation library.

ORD

Function ORD returns the ordinal value of its countable argument.

PROCEDURE ORD ( value : T ) : LONGCARD;

where TTcountable.

ODD

Function ODD returns TRUE if its argument is odd, otherwise FALSE.

PROCEDURE ODD ( value : T ) : BOOLEAN;

where TTwholeTmachine.

ABS

Function ABS returns the absolute value of its scalar argument.

PROCEDURE ABS ( x : T1 ) : T2;

where T1TsignedTscalarT2 = T1.

SGN

Function SGN returns the signum value of its argument.

PROCEDURE SGN ( value : T1 ) : T2;

where T1TsignedTscalarT2 = T1.

MIN

Function MIN returns the smallest value in its variadic argument list.

PROCEDURE MIN ( values : ARGLIST OF T1 ) : T2;

where T1TcountableTscalarT2 = T1.

MAX

Function MAX returns the largest value in its variadic argument list.

PROCEDURE MAX ( values : ARGLIST OF T1 ) : T2;

where T1TcountableTscalarT2 = T1.

LOG2

Function LOG2 returns the truncated binary logarithm of its cardinal argument.

PROCEDURE LOG2 ( n : T1 ) : T2;

where n ≠ 0 ∧ T1TcardinalT2 = T1.

POW2

Function POW2 returns the value of its argument raised to the power of 2.

PROCEDURE POW2 ( n : T1 ) : T2;

where T1TcardinalT2 = T1.

ENTIER

Function ENTIER returns the largest integer or entier of its real number argument.

PROCEDURE ENTIER ( r : T1 ) : T2;

where T1TrealT2 = T1.

PRED

Function PRED returns the predecessor of its countable argument.

PROCEDURE PRED ( value : T1 ) : T2;

where T1TcountableT2 = T1.

SUCC

Function SUCC returns the successor of its countable argument.

PROCEDURE SUCC ( value : T1 ) : T2;

where T1TcountableT2 = T1.

PTR

Function PTR returns a pointer to its argument.

PROCEDURE PTR ( value : T1 ) : POINTER TO T2;

where T1TanyT2 = T1

CAPACITY

Function CAPACITY returns the capacity of its argument.

PROCEDURE CAPACITY ( entity : T ) : LONGCARD;

where TTcollection

COUNT

Function COUNT returns the cardinality of its argument.

PROCEDURE COUNT ( entity : T ) : LONGCARD;

where TTcollectionTstringToctetseq

The argument must be either (1) an instance of a collection type that is not a string type, or (2) the identifier of a formal open array parameter whose value type is not a character type, or (3) the identifier of a variadic argument list.

LENGTH

Function LENGTH returns the length of the character string or octet sequence denoted by its argument.

PROCEDURE LENGTH ( s : T ) : LONGCARD;

where TTcollectionToctetseq

The argument must be either (1) a value of a string type, or (2) the identifier of an pen array parameter whose value type is a character type, or (3) the identifier of a record field or formal parameter of type OCTETSEQ.

FIRST

Function FIRST returns the first key of the dictionary denoted by its argument.

PROCEDURE FIRST ( dict : T1 ) : T2;

where TTdictionaryT2 is-keytype-of T1

The dictionary type must be a library ADT that implements key/value storage and provides a binding to FIRST otherwise a compile time error will occur. The function returns NIL if its argument is an empty dictionary.

LAST

Function LAST returns the last key of the dictionary denoted by its argument.

PROCEDURE LAST ( dict : T1 ) : T2;

where TTdictionaryT2 is-keytype-of T1

The dictionary type must be a library ADT that implements key/value storage and provides a binding to LAST otherwise a compile time error will occur. The function returns NIL if its argument is an empty dictionary.

PREV

Function PREV returns the key that precedes the given key of the given dictionary.

PROCEDURE PREV ( dict : T1; key : T2 ) : T2;

where TTdictionaryT2 is-keytype-of T1

The dictionary type must be a library ADT that implements key/value storage and provides a binding to PREV otherwise a compile time error will occur. The function returns NIL if the second argument is the first key.

NEXT

Function NEXT returns the key that succeeds the given key of the given dictionary.

PROCEDURE NEXT ( dict : T1; key : T2 ) : T2;

where TTdictionaryT2 is-keytype-of T1

The dictionary type must be a library ADT that implements key/value storage and provides a binding to NEXT otherwise a compile time error will occur. The function returns NIL if the second argument is the last key.

Built-in Macros

TMIN

An invocation of macro TMIN is replaced by the smallest value of the type denoted by its argument.

(*MACRO*) PROCEDURE TMIN ( TypeIdent ) : T;

where TypeIdent = id TTTcountableTscalar

TMAX

An invocation of macro TMAX is replaced by the largest value of the type denoted by its argument.

(*MACRO*) PROCEDURE TMAX ( TypeIdent ) : T;

where TypeIdent = id TTTcountableTscalar

TSIZE

An invocation of macro TSIZE is replaced by the allocation size of the type denoted by its argument.

If the (primary) argument does not denote an indeterminate type, no second argument may be passed.

(*MACRO*) PROCEDURE TSIZE ( TypeIdent ) : LONGCARD;

If the primary argument denotes a pointer type with indeterminate target, a second argument must be passed,

(*MACRO*) PROCEDURE TSIZE ( TypeIdent; capacity : LONGCARD ) : LONGCARD;

and the result is the allocation size for the target with the capacity given by the second argument.

In any event, the (primary) argument must be a type identifier.

TLIMIT

An invocation of macro TLIMIT is replaced by the capacity limit of the type denoted by its argument.

(*MACRO*) PROCEDURE TLIMIT ( TypeIdent ) : LONGCARD;

The argument must be the identifier of a collection type.

Primitives

Primitives are internal-use procedures or macros that represent certain built-in syntax forms. They are not callable by user code but may be targeted by syntax bindings in ADT library modules. When a syntax form that is implemented by a primitive is found, it is internally replaced with an invocation of the associated primitive. The identifiers of primitives are reserved.

The following primitives are defined:

VALUE, ATVALUE, STORE, ATSTORE, ATINSERT, ATREMOVE, ALLOC, DEALLOC, STDIN, STDOUT;

Primitive VALUE

Primitive VALUE implements R-value subscript designators for dictionary types. It returns the value stored for a given key in a key/value collection.

PROCEDURE VALUE ( CONST dict : DictType; key : KeyType ) : KeyType;

Any R-value subscript designator dict[key] is replaced by a function call VALUE(dict, key).

Primitive ATVALUE

Primitive ATVALUE implements R-value subscript designators for array types.

It returns the value stored at a given index in an indexed collection.

PROCEDURE ATVALUE ( CONST c : Collection; atIndex : LONGCARD ) : ValueType;

Any R-value subscript designator a[i] is replaced by a function call ATVALUE(a, i).

Primitive STORE

Primitive STORE implements L-value subscript designators for dictionary types. It stores a given value for a given key in a key/value collection.

PROCEDURE STORE ( VAR dict : DictType; key : KeyType; value : ValueType );

A statement of the form dict[key] := value is replaced by a procedure call STORE(dict, key, value).

Primitive ATSTORE

Primitive ATSTORE implements L-value subscript designators for array types. It stores a given value at a given index in an indexed collection.

PROCEDURE ATSTORE ( VAR c : Collection; atIndex : LONGCARD; value : ValueType );

A statement of the form a[i] := v is replaced by a procedure call ATSTORE(a, i, v).

Primitive ATINSERT

Primitive ATINSERT implements L-value subscript insertion designators. It inserts one or more values into an indexed collection starting at a given index.

PROCEDURE ATINSERT ( VAR c : Collection; atIndex : LONGCARD; values : ARGLIST OF ValueType );

A statement of the form a[i..] := v is replaced by a procedure call ATINSERT(a, i, v).

Primitive ATREMOVE

Primitive ATREMOVE implements syntax for indexed value removal. It removes a range of values from an indexed collection within a given index range.

PROCEDURE ATREMOVE ( VAR c : Collection; startIndex, endIndex : LONGCARD );

For values a of an array type, s of a string type, and c of any indexed collection type

  • a statement of the form a := {} is replaced by a procedure call ATREMOVE(a, 0, COUNT(a)-1)
  • a statement of the form s := "" is replaced by a procedure call ATREMOVE(s, 0, LENGTH(s)-1)
  • a statement of the form REMOVE(c, i, j) is replaced by a procedure call ATREMOVE(c, i, j)

Primitive ALLOC

Primitive ALLOC implements type independent memory allocation for NEW statements.

It allocates a memory block of a given size in octets and passes a pointer to it in its first argument.

PROCEDURE ALLOC ( VAR p : CAST ADDRESS; size : LONGCARD );

Primitive DEALLOC

Primitive DEALLOC implements type independent memory deallocation for RELEASE statements.

It deallocates a memory block pointed to by its argument and passes NIL back therein.

PROCEDURE DEALLOC ( VAR p : CAST ADDRESS );

Primitive STDIN

Primitive STDIN inserts the default value for an omitted input channel in READ statements.

PROCEDURE STDIN : LibraryDefinedChannelType;

Primitive STDOUT

Primitive STDOUT inserts the default value for an omitted output channel in WRITE statements.

PROCEDURE STDOUT : LibraryDefinedChannelType;

Restrictions

Predefined constants may be passed to function UNSAFE.CAST() but may not be passed to functions PTR() and UNSAFE.ADR(). Predefined procedures, functions and macros may not be assigned and may not be passed as arguments to any procedure, function or macro.

Clone this wiki locally