Linked lists

This stunning little compiler has a few omissions. One of them is 'DISPOSE' to get rid of old memory dynamic memory structures. Since the compiler produces Java bytecode, the documentation says, there is no need for DISPOSE. The Java garbage man will collect and remove the memory garbage automatically. With Mocka I was a very fond user of the Memory Pool mechanism. Noe, I'm going to throw myself in the deep end of the (swimming) pool and see what it's going to be. I will start out with the lili.mod file that I intriduced in the Mocka section: http://localhost/fruttenboel/mocka/linklist.html.

Herre is the new source, adopted for MHC:

MODULE lili;

(*     Linked Lists with MHC		*)

IMPORT InOut, Strings, SYSTEM;

TYPE   Identifier        = ARRAY [0..57] OF CHAR;
       NamePtr	    	 = POINTER TO NameNode;
       NameNode		 = RECORD
       			      Name	: Identifier;
			      next	: NamePtr
       			   END;

VAR    string		 : Identifier;
       FirstName	 : NamePtr;


PROCEDURE StoreName (str   : Identifier) : BOOLEAN;

VAR	  deze, prev, neww	   : NamePtr;
	  result   	   	   : INTEGER;

BEGIN
   deze := FirstName;
   prev := NIL;
   LOOP
      result := Strings.Compare (deze^.Name, str);
      IF  result = 0  THEN  RETURN FALSE  END;
      IF  result = 1  THEN  EXIT  END;
      prev := deze;
      deze := deze^.next
   END;
   NEW (neww);
   
At this point, the pointer is assigned and some memory is claimed. In the next two lines, the actual record fields are claimed and filled. This is very neat and elegant.
   neww^.Name := str;
   neww^.next := deze;
   IF  prev = NIL  THEN
      FirstName := neww
   ELSE
      prev^.next := neww
   END;
   RETURN TRUE
END StoreName;


PROCEDURE PrintNames;

VAR	  ThisOne    		: NamePtr;

BEGIN
   ThisOne := FirstName;
   IF  Strings.Compare (ThisOne^.Name, "|") = 0  THEN  ThisOne := ThisOne^.next  END;
   LOOP
      InOut.WriteString (ThisOne^.Name);		InOut.WriteLn;
      IF  ThisOne^.next = NIL  THEN
         EXIT
      ELSE
         ThisOne := ThisOne^.next
      END
   END
END PrintNames;


PROCEDURE Init;

BEGIN
   NEW (FirstName);
   WITH  FirstName^  DO
      Name := "|";
      next := NIL
   END
END Init;


BEGIN
   Init;
   LOOP
      InOut.ReadString (string);
      IF  Strings.Compare (string, "Exit") = 0  THEN  EXIT  END;
      IF  StoreName (string) = FALSE  THEN
         InOut.WriteString (">>> Duplicate name : ");
	 InOut.WriteString (string);
	 InOut.WriteLn
      END
   END;
   PrintNames
END lili.
   
The things in maroon were changed. The rest was identical to the Mocka program. Apart from two symbols ('this' and 'new') that are reserved words in Java and had to be changed.
In normal Modula-2, you allocate space (SIZE (NameNode)) and the OS returns a pointer to that piece of memory. Not with MHC. Things are so very simple now:
  1. Set up the TYPEs and RECORDs
  2. Assign some variables
  3. Do a call to NEW to get a new pointer
  4. When the record is filled in, memory is allocated on the heap
So these are no longer dynamic variables, these are hyperdynamic variables! It looks like the members of the record are just pointers themselves and the record never takes up more space than the number of fields multiplied by the size of a pointer....

See it compile an will it run?

jan@Beryllium:~/modula/test/tmp$ jed lili.mod
jan@Beryllium:~/modula/test/tmp$ mhc lili

Modula-2 2.5.67 (c) 1998-2006 J.Neuhoff (www.mhccorp.com)
Licenced to Jan Verhoeven
Compiling: /home/com.mhccorp/mhc/runtime/mod/FileSystem.def
Compiling: /home/com.mhccorp/mhc/runtime/mod/Strings.def
Compiling: /home/com.mhccorp/mhc/runtime/mod/InOut.def
Compiling: /home/com.mhccorp/mhc/runtime/mod/Conversions.def
Compiling: lili.mod
Compilation done: lili.mod

jan@Beryllium:~/modula/test/tmp$ javac *java

SYSTEM.java:1095: warning: non-varargs call of varargs method with inexact argument type for last parameter;
cast to java.lang.Object for a varargs call
cast to java.lang.Object[] for a non-varargs call and to suppress this warning
            arrayobj[i] = clone_method.invoke( arrayobj[i], null );
                                                            ^
Note: SYSTEM.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 warning

jan@Beryllium:~/modula/test/tmp$
   
Hmm. It compiles. That's a good sign. To save on typing, I prepared a text file with a nursetry rhime for testing (the Exit will stop the program):
Klein klein kleutertje wat doe je in mijn hof
Je plukt er alle bloempjes af en maakt het veel te grof
Exit
   
And this is what it does:
jan@Beryllium:~/modula/test/tmp$ java lili < file
Je
Klein
af
alle
bloempjes
doe
en
er
grof
het
hof
in
je
klein
kleutertje
maakt
mijn
plukt
te
veel
wat
|
jan@Beryllium:~/modula/test/tmp$ 
   
lili reads a word from standard input and inserts it in a sorted linked list. After 'Exit' was entered, the program prints the list and returns to the operating system.

MHC Modula-2 is an amazing compiler!

Page created on 4 July 2010 and

Page equipped with FroogleBuster technology