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:

There are some rules to take into account:
  1. The DEFINITION MODULE should be changed as little as possible!
  2. The DEFINITION MODULE should be changed as little as possible!
  3. The DEFINITION MODULE should be changed as little as possible!
  4. The IMPLEMENTATION MODULE may require major surgery
Rules 1, 2 and 3 are no copy/paste mistake! The dfinition module is the call interface to the rest of the world. Many existing programs already relie on it. So this file should be kept as it is as much as possible. Only for some superficial things, changes may be applied.


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.

For the rest, the changes to TextIO.def are minimal and mainly related to the comment style. We want this library to be usable.

The main work is done in the implementation module. Here, major changes are made. here are the first few lines of TextIO.mod:
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.
As a rule of thumb: leave in as much of the old module as possible. Then, afterwards, start methodically removing and replacing things. Program development through stepwise refinements, as the Grand Master calls this in his works. In another file, the new TextIO modules are explained.

Page created on 21 July 2010 and

Page equipped with FroogleBuster technology