Modula-2
ISO/IEC 10514
programming language tutorial
Modules
Julian Miglio 2019
IMPLEMENTATION
The structure of a program
A complete Modula-2 program consists of a set of top-down interconnected modules in a dependency graph.
Modules are a powerful tool for creating large programs. They help make programs easier to modify by supporting information
hiding, help make programs more portable, make separate compilation possible,
and enable the development of libraries of reusable code.
Julian Miglio 2019
MODULE Main
DEFINITION
Main.mod
A.def
A.mod
IMPLEMENTATION
DEFINITION
C.def
C.mod
IMPLEMENTATION
DEFINITION
B.def
B.mod
The structure of a program
Modules are composed by two parts: the definition part and the implementation part that are contained in two files:
modulename.def (definition) , modulename.mod (implementation).
Modules act as walls. Entities declared inside a module are not visible outside the module unless they have been exported
by the module. A module may import from multiple other modules.
Julian Miglio 2019
MODULE MyProgram;
END MyProgram.
DEFINITION MODULE MyModule;
END MyModule.
IMPLEMENTATION MODULE MyModule;
END MyModule.
MyProgram.mod
MyModule.def
MyModule.mod
MyProgram uses entities exported by MyModule
IMPLEMENTATION MODULE MyModule;
BEGIN
END MyModule.
The structure of a module
DEFINITION MODULE MyModule;
END MyModule.
The definition part specifies the entities that the module exports, constitutes the externally visible interface of the module.
The implementation part contains the full implementation of all the entities.
The implementation part can have optionally a statemets part that is executed only once at the startup.
module name
MyModule.mod
MyModule.def
Julian Miglio 2019
end of module
statements for the startup part
An example of a module (definition)
DEFINITION MODULE Queue;
TYPE InroEntry = RECORD (* some fields *) END;
PROCEDURE Enter(info : InfoEntry; VAR ok: BOOLEAN);
PROCEDURE Remove(VAR info: InfoEntry; VAR ok: BOOLEAN);
END Queue.
In this example, the module Queue exports three entities: InfoEntry, Enter, Remove.
InfoEntry is a record type, in this example the fields are not defined for simplicity.
Enter and Remove are procedures.
Queue.def
Julian Miglio 2019
exported entities
An example of a module (implementation)
IMPLEMETATION MODULE Queue;
CONST buffleng = 128;
TYPE Bufferindex = [0 .. buffleng- 1];
VAR buffer : ARRAY Bufferindex OF InfoEntry;
enter, remove : Bufferindex;
PROCEDURE Enter(info : InfoEntry; VAR ok: BOOLEAN);
(* procedure implementation *)
END Enter.
PROCEDURE Remove(VAR info: InfoEntry; VAR ok: BOOLEAN);
(* procedure implementation *)
END Enter.
END Queue.
The exported procedures Enter and Remove are implemented in the implementation part of the module as well as
the other entities bufferlen, Bufferindex , enter and remove that are visible only inside on the module Queue.
In this example the procedures Enter and Remove are empty for simplicity.
Note that all the entities defined in the definition part are visible in the implementation part (e.g. InfoEntry).
Note that Modula-2 is case sensitive, Enter (procedure) is different than enter (variable).
Julian Miglio 2019
Queue.mod
Reference
Julian Miglio 2019
• Modula-2: Abstractions for Data and Programming Structures:
http://www.arjay.bc.ca/Modula-2/Text/index.html
• ISO/IEC 10514 Modula-2, Base Language: https://www.iso.org/standard/18583.html
• Modula2.org: https://www.modula2.org/

Modula-2 tutorial - 002 - modules

  • 1.
    Modula-2 ISO/IEC 10514 programming languagetutorial Modules Julian Miglio 2019
  • 2.
    IMPLEMENTATION The structure ofa program A complete Modula-2 program consists of a set of top-down interconnected modules in a dependency graph. Modules are a powerful tool for creating large programs. They help make programs easier to modify by supporting information hiding, help make programs more portable, make separate compilation possible, and enable the development of libraries of reusable code. Julian Miglio 2019 MODULE Main DEFINITION Main.mod A.def A.mod IMPLEMENTATION DEFINITION C.def C.mod IMPLEMENTATION DEFINITION B.def B.mod
  • 3.
    The structure ofa program Modules are composed by two parts: the definition part and the implementation part that are contained in two files: modulename.def (definition) , modulename.mod (implementation). Modules act as walls. Entities declared inside a module are not visible outside the module unless they have been exported by the module. A module may import from multiple other modules. Julian Miglio 2019 MODULE MyProgram; END MyProgram. DEFINITION MODULE MyModule; END MyModule. IMPLEMENTATION MODULE MyModule; END MyModule. MyProgram.mod MyModule.def MyModule.mod MyProgram uses entities exported by MyModule
  • 4.
    IMPLEMENTATION MODULE MyModule; BEGIN ENDMyModule. The structure of a module DEFINITION MODULE MyModule; END MyModule. The definition part specifies the entities that the module exports, constitutes the externally visible interface of the module. The implementation part contains the full implementation of all the entities. The implementation part can have optionally a statemets part that is executed only once at the startup. module name MyModule.mod MyModule.def Julian Miglio 2019 end of module statements for the startup part
  • 5.
    An example ofa module (definition) DEFINITION MODULE Queue; TYPE InroEntry = RECORD (* some fields *) END; PROCEDURE Enter(info : InfoEntry; VAR ok: BOOLEAN); PROCEDURE Remove(VAR info: InfoEntry; VAR ok: BOOLEAN); END Queue. In this example, the module Queue exports three entities: InfoEntry, Enter, Remove. InfoEntry is a record type, in this example the fields are not defined for simplicity. Enter and Remove are procedures. Queue.def Julian Miglio 2019 exported entities
  • 6.
    An example ofa module (implementation) IMPLEMETATION MODULE Queue; CONST buffleng = 128; TYPE Bufferindex = [0 .. buffleng- 1]; VAR buffer : ARRAY Bufferindex OF InfoEntry; enter, remove : Bufferindex; PROCEDURE Enter(info : InfoEntry; VAR ok: BOOLEAN); (* procedure implementation *) END Enter. PROCEDURE Remove(VAR info: InfoEntry; VAR ok: BOOLEAN); (* procedure implementation *) END Enter. END Queue. The exported procedures Enter and Remove are implemented in the implementation part of the module as well as the other entities bufferlen, Bufferindex , enter and remove that are visible only inside on the module Queue. In this example the procedures Enter and Remove are empty for simplicity. Note that all the entities defined in the definition part are visible in the implementation part (e.g. InfoEntry). Note that Modula-2 is case sensitive, Enter (procedure) is different than enter (variable). Julian Miglio 2019 Queue.mod
  • 7.
    Reference Julian Miglio 2019 •Modula-2: Abstractions for Data and Programming Structures: http://www.arjay.bc.ca/Modula-2/Text/index.html • ISO/IEC 10514 Modula-2, Base Language: https://www.iso.org/standard/18583.html • Modula2.org: https://www.modula2.org/