Porting a MODULE
Mocka is a fine Modula-2 compiler. It is free and it has lots of good modules from which programs can be
borrowed. Still, Mocka is not 100% PIM (the PIM is the unit of compatibility for a Modula-2 compiler; PIM is
short for Programming In Modula-2, Wirths Magnum Epos about this language). MHC is more PIM since it uses the
same Modules as Wirth proposes.
Still, I can read and write with Mocka so it might be a good idea to port (i.e. adapt) some of the Mocka
MODULES to MHC. The first one will be TextIO, the library that allows the handling files and file operations.
Sometimes people ask: how do I port a MODULE from one compiler to another? A good question. And the answer is
dead simple: just make sure it compiles! In order to get this done I work as follows:
An example: TextIO
Here are the first few lines of TextIO.def:
DEFINITION MODULE TextIO; IMPORT BasicIO; TYPE File = BasicIO.File; (*=== Open/Close ===*) PROCEDURE OpenInput (VAR file : File; VAR name : ARRAY OF CHAR); (* Open file 'file' for input. Use External name 'name' *)Line 3 is important. It defines how and where in Mocka a File is defined. In MHC this is in another place. So I change this section into:
DEFINITION MODULE TextIO; IMPORT FileSystem; TYPE File = FileSystem.File; (* === Open/Close === *) PROCEDURE OpenInput (VAR file : File; name : ARRAY OF CHAR); (* Open file 'file' for input. Use External name 'name' *)That's it. This is the major change to TextIO.def. If you look at the OpenInput definition you see I made some other changes as well: in Mocka, OpenInput takes two VAR arguments, but only one is essential: the file handle. So for MHC I remove the second VAR attribute. It will be without consequences for the Module since MHC inserts pointers to strings by default.
IMPLEMENTATION MODULE TextIO; IMPORT InOut, RealConversions; CONST MAXFILE = 50; BUFFSIZE = 1024; VAR MinReal, (* = MIN (REAL) *) MaxReal : LONGREAL; (* = MAX (REAL) *) done : BOOLEAN; (* These "quasi-constants" are necessary because mocka chokes on VAL (LONGREAL, MIN(REAL)) despite this is perfectly legal modula-2 ... *) (*=== Open/Close ===*) PROCEDURE OpenInput (VAR file : File; name : ARRAY OF CHAR); BEGIN FileSystem.OpenReader (file, name); IF file.res = 0 THEN done := TRUE ELSE done := FALSE END END OpenInput;As you can see, I did not IMPORT FileSystem in the IMPLEMENTATION MODULE. I already did this in the DEFINITION MODULE. If the IMPORT is in two files, it is considered a double definition. The same applies to variables. Public vars are defined in the DEFINITION MODULE, all hidden variables are defined in the IMPLEMENTATION MODULE.
Page created on 21 July 2010 and
Page equipped with FroogleBuster technology