What does the MHC compiler produce?

The MHC compiler produces java source code. So a short program can shed some light on what exactly it produces. Let's use the small source file dum1.mod which I used while exploring the Read from File issue. Here it is in a side by side comparison:

Modula-2Java
MODULE dum1;

IMPORT	FileSystem, InOut, SYSTEM;

VAR	inF	: FileSystem.File;
	ch	: CHAR;

BEGIN
(*  FileSystem.Lookup (inF, "dum1.mod", FALSE);	*)







  SYSTEM.INLINE 
  ( 
    try
    {
      inF.id = new java.io.FileReader ("dum1.mod");
    }
    catch (java.io.FileNotFoundException e)
    {
      inF.id = 0;
    }
  );
  IF  inF.res = FileSystem.done   THEN
    InOut.WriteString ("File 'dum1.mod' opened");
    InOut.WriteLn;
    FileSystem.SetRead (inF);
    FileSystem.ReadChar (inF, ch);



    WHILE  inF.eof = FALSE  DO
      InOut.Write (ch);
      FileSystem.ReadChar (inF, ch)
    END;
    FileSystem.Close (inF)
  ELSE
    InOut.WriteString ("File not found");
    InOut.WriteLn
  END;
  InOut.WriteLn
END dum1.
      
public class dum1
{
  static boolean dum1_loaded = false;
  static FileSystem_File inF = new FileSystem_File();
  static char ch;

  public static void main( java.lang.String[] _args )
  {
    try
    {
      if (dum1_loaded)
      {
        return;
      }
      dum1_loaded = true;
      SYSTEM.main( _args );
      FileSystem.main( _args );
      InOut.main( _args );
      try
      {
        inF.id = new java.io.FileReader ("dum1.mod");
      }
      catch (java.io.FileNotFoundException e)
      {
        inF.id = 0;
      }
  
      if (SYSTEM.CMP_4( inF.res, (int)FileSystem.done ) == 0)
      {
        InOut.WriteString( "File \'dum1.mod\' opened".toCharArray() );
        InOut.WriteLn();
        FileSystem.SetRead( inF );
        SYSTEM_PCHAR _0 = new SYSTEM_PCHAR( ch );
        FileSystem.ReadChar( inF, _0 );
        ch = _0.val;
        while ( inF.eof == false )
        {
          InOut.Write( ch );
          SYSTEM_PCHAR _1 = new SYSTEM_PCHAR( ch );
          FileSystem.ReadChar( inF, _1 );
          ch = _1.val;
        }
        FileSystem.Close( inF );
      }
      else
      {
        InOut.WriteString( "File not found".toCharArray() );
        InOut.WriteLn();
      }
      InOut.WriteLn();
      if (_args != null)
      {
        java.lang.System.runFinalization();
        java.lang.System.exit(0);
      }
    }
    finally { }
  }
}
      
not bad, isn't it? And the nice part is: IT REALLY WORKS!

Page created on 19 July 2010 and

Page equipped with FroogleBuster technology