modula-2 home

  Home  
  Tutorial  
  Win32 API  
  Reference  
  Projects  
 

 

Declarations and scope rules

Every identifier occurring in a program must be introduced by a declaration, unless it is a standard identifier (see below). Declarations also serve to specify certain permanent properties of an object, such as whether it is a constant, a type, a variable, a procedure, or a module.

The identifier is then used to refer to the associated object. This is possible in those parts of a program only which are within the so called scope of the declaration. In general, the scope extends over the entire block (procedure or module declaration) to which the declaration belongs and to which the object is local. The scope rule is augmented by the following cases:

  1. If an identifier x defined by a declaration D1 is used in another declaration (not statement) D2, then D1 must textually precede D2.
  2. A type T1 can be used in a declaration of a pointer type T which textually precedes the declaration of T1, if both T and T1 are declared in the same block. This is a relaxation of rule 1.
  3. If an identifier defined in a module M1 is exported, the scope expands over the block which contains M1. If M1 is a compilation unit, it extends to all those units which import M1.
  4. Field identifiers of a record declaration are valid only in field designators and in with statements referring to a variable of that record type.

An identifier may be qualified. In this case it is prefixed by another identifier which designates the module in which the qualified identifier is defined. The prefix and the identifier are separated by a period.

qualident = ident {"." Ident}.

Standard identifiers

Standard identifiers are considered to be predeclared, and they are valid in all parts of a program. For this reason they are called pervasive.

   ABS              BITSET       BOOLEAN    CARDINAL
   CAP              CHR          CHAR       COMPLEX
   CMPLX            DEC          DISPOSE    EXCL
   FALSE            FLOAT        HALT       HIGH
   IM               INC          INCL       INT
   INTERRUPTIBLE    INTEGER      LENGTH     LFLOAT
   LONGCOMPLEX      LONGREAL     MAX        MIN
   NEW              NIL          ODD        ORD
   PROC             PROTECTION   RE         REAL
   SIZE             TRUE         TRUNC      UNINTERRUPTIBLE
   VAL

The pervasive identifiers COMPLEX, CMPLX, IM, INT, INTERRUPTIBLE, LENGTH, LFLOAT, LONGCOMPLEX, LONGREAL, PROTECTION, RE and UNINTERRUPTIBLE were added for ISO Modula-2.


Source:

  • Wirth N: Programming in Modula-2, 3rd ed. Springer Verlag, Berlin, 1985.
  • Stony Brook Modula-2 documentation. Used with permission. Note: Stony-Brook M2 offers an extended syntax with features not described here. Stony Brook M2 users are encouraged to visit the Stony Brook website and to consult the Stony Brook help system.
  • Modula-2: Abstractions for Data and Programming Structures. Shareware text by Richard J. Sutcliffe