It shouldn't always be SEA

When the subjects are 'Unix' and 'programming' most people can only think of one language: C and its derivatives or the scripting tools that slowly evolved.
But there is more. For one, there is a dense club of assembly language programmers (cf www.janw.easynet.be) and also the classical languages Cobol and Fortran are still in use.

Most of these languages work with a dedicated compiler. But there are also translating compiler avaialbale like P2C which translates Pascal source code into C source. The latter is hen compiled with the gcc compiler. It's a detour, but it's a detour that does the job.
My favorite language is Modula-2, the successor of Pascal. Consult www.modula2.org for more details. Modula-2 is a strongly typed language. In every day use this means that you have to adapt to the rules of the compiler.
But you get a lot in return: securities, lots of it. The prime goal of Modula-2 was writing operating systems in. The Lilith computer had an OS that was entirely written in Modula-2. That's why Modula-2 has both low AND high level functions. It can control the mouse and refresh the screen. More about that in parts to follow.

There are several Modula-2 compilers for Unix. Consult www.modula2.org for more details. I use the Mocka compiler: MOdula-2 Compiler from KArlsruhe university. It's a fast and reliable compiler that comes with it's own IDE (Integrated Development Environment), be it a simple one. And the compiler is FREE as defined in the GNU GPL, something which cannot be said by most other compilers. XDS has more features, but it's a commercial compiler that may be used for free by single users.

Download the Mocka compiler from www.info.uni-karlsruhe.de/~modula/ and install it on your system. If there are hickups, visit fruttenboel and see if I got the same and how I solved it.
The Mocka compiler comes with full documentation with respect to its operation. If you need a Modula-2 tutorial, check out fruttenboel as well and go for the Coronado course.

After the installation.

On my system I use jed for standard and my Mocka invocation script is called 'MC' (watch the capitals). I strat the compiler with 'MC' after which I get the '>>' prompt. The two most important commands now are 'i' and 'p'. The 'i' command is for invoking the editor and the p command is for compiling that source to an executable.
There are some more commands, but for the time being, this is sufficient to know.

When you got the '>>' prompt, you can issue all IDE commands, plus all standard Unix commands like 'ls', 'strip' and 'mount'.

About the language.

Modula-2 is a super structured language. The compiler helps you during the compiling, but also in runtime, to protect you against the evil world. The price you have to pay for this, is called 'structure'. You simply HAVE to code modular and structured. This is the cornerstone of Modula-2.
In certain other languages (whose identities I will not disclose since the name of the language is just a single letter) you can (for example) combine floating point numbers and integers in one and the same formula. This will not work in Modula-2. And it is not done to limit your capabillities, but to prevent you from accidentally ending up with the result in a wrong datatype.

Furthermore, the compiled executable performs runtime checks which looks at the boundaries of arrays, the state of the machine stack and on certain kinds of overflow.
In short: many of the errors for which C is notorious, will not occur as such in Modula-2 executables. The program might abort with an error, but it will not run on and corrupt everything else.

And one more note for the chaotics among us: there just isn't any GOTO statement in Modula-2. To compensate for that, a plethora of loop constructs is available so that the GOTO is never needed or even missed.

The language knows only few 'reserved words':

  40 reserved words
  12 standard functions
   7 standard procedures
   9 standard TYPEs (depends a bit of the compiler)
   
This low amount of 'own' words means that you don't have to learn much to master the language.

Modula-2 is 'case sensitive'. So there is a big difference between the words 'BEGIN' and 'Begin'. The former is a reserved word, whereas the latter can be anything.

An example program.

Enough chatting. Time for some code. First we're gonna make an empty program. It has been proven that empty programs contain considerably less errors than bigger ones. And they don't come more empty than this one:

   MODULE empty;

   BEGIN
   END empty.
   
Emptier than this is not easy. But it tells the construction of a typical Modula-2 program. Every Modula-2 programsource starts with its own name, prepended by the word 'MODULE'. You use a semicolon (;) to terminate a line or statement. Not all statements need a semicolon, but we will come to that later.

The main routine of a program starts with the word 'BEGIN'. A 'BEGIN' is the start of something so it is not a real statement by itself, so it doesn't need a semicolon for termination.
The end of the main routine is signaled by the word 'END'. And since this 'END' also flags the end of the sourcecode, we add the name of the program. And since this is the final statement in the source, we terminate it with a period. Just like we do in a sentence.

Start your compiler and enter this source. That will go like below:

  jan@hydrogen:~/modula$ mocka
  Mocka 0608m
  >> i empty
  >> p empty
  >> strip empty
  >> ls -l empty*
  -rwxr-xr-x    1 jan      users        3584 May  6 11:51 empty
  -rw-r--r--    1 jan      users          30 May  6 11:50 empty.mi
  -rw-r--r--    1 jan      users        1364 May  6 11:50 empty.o
  -rw-r--r--    1 jan      users           9 May  6 11:50 empty.r
  >>
   
We started the editor with the 'i' command, followed by the name of the program, not the name of the sourcecode file. With the 'p' command we started the compiler. The other commands are standard Unix commands.

Just execute the 'empty' program. It will not misbehave, I assure you.

A second executable: hallo.

We're about to compile a second source. Start the compiler and issue the command:

  >>i hallo
   
and enter the following lines:
   MODULE hallo;
   
   BEGIN
      WriteString ('Hallo Linux magazine lezers.');
      WriteLn;
      Write (LF)
   END hallo.
   
before we start a compilerrun with
  >>p hallo
   
What's THAT? Errors? In MY sources? We're not used to that. :o) Now, one of the many features of the Mocka system pops up: the editor is automatically invoked and the errors are indicated in the right places in the source. We'll walk through them one by one:
   @ LISTING
   MODULE hallo;

   BEGIN
      WriteString ('Hallo Linux magazine lezers.');
   @  ^
   @ Col 4: 'WriteString' not declared
      WriteLn;
   @  ^
   @ Col 4: 'WriteLn' not declared
      Write (LF)
   @  ^
   @ Col 4: 'Write' not declared
   END hallo.
   
The compiler doesn't know the indicated words. Strange. They sound so logical. For the compiler, however, they are complete gobbledygook. It wants to know if and how these words should perform. That's why we enter one more line in the source:
   @ LISTING
   MODULE hallo;

-> FROM   InOut   IMPORT  WriteString, WriteLn, Write;

   BEGIN
      WriteString ('Hallo Linux magazine lezers.');
   @  ^
   @ Col 4: 'WriteString' not declared
      WriteLn;
   @  ^
   @ Col 4: 'WriteLn' not declared
      Write (LF)
   @  ^
   @ Col 4: 'Write' not declared
   END hallo.
   
With this line, we tell the compiler that we want to borrow the procedures 'WriteString', 'WriteLn' and 'Write' from the library 'InOut'.

For the rest we do nothing. We do not remove errormessages or indicators. that will all be done by the Mocka system. We save the file to disk and start a second compiler run. That's easy now: if we issued the 'p' command before, the IDE will remember the source and from that moment on, it suffices to just press 'Enter". The result:

   @ LISTING
   MODULE hallo;

   FROM  InOut  IMPORT WriteString, WriteLn, Write;

   BEGIN
      WriteString ('Hallo Linux magazine lezers.');
      WriteLn;
      Write (LF)
   @         ^
   @ Col 11: 'LF' not declared
   END hallo.
   
That's already a lot better. Most errors are gone. We only seem to have a constant which was not defined. We can do this in many ways. The easiest way is:
   CONST  LF    = 12C;
   
to be inserted before the 'BEGIN' word. With this, we define a constant with the name 'LF' and the value '12' octal which is '10' decimal. The trailing 'C' after the '12' tells the compiler that the numer '12' is to be treated as an octal number. Character constants always must be expressed as octal numbers.

Another way of defining the 'LF' constant would have been to borrow it from an existing library:

   FROM   ASCII   IMPORT   LF;
   
Put one of these two constructs between the line with the first 'FROM' and the 'BEGIN' word. Recompile and run your program.
   >> <Enter>
   .. Compiling Program Module hallo I/0001 II/0001
   .. Linking hallo
   >> hallo
   Hallo Linux magazine lezers.
   >>
   
But there's a still better method for including the 'LF' constant. This method is called 'qualified imports'. Look at this source:
   MODULE hallo;

   FROM   InOut  IMPORT WriteString, WriteLn, Write;
   IMPORT ASCII;

   BEGIN
      WriteString ('Hallo Linux magazine lezers.');
      WriteLn;
      Write (ASCII.LF)
   END hallo.
   
We import the full ASCII library and later we retrieve the 'LF' from it by means of the dot operator: ASCII.LF Other ASCII constants can be accessed in similar ways: ASCII.ESC, ASCII.CR etcetera.

This is a generic method in Modula-2 to define functions from libraries. If we would hagve written 'IMPORT InOut' then we should have written 'InOut.WriteLn' for each 'WriteLn' in the current source. This method is particularly handy, when you need to use several functions with identical names but from different libraries.
Also, for bigger sources, with many imported funtions, it is easier to remember which function comes from which library. The dot-operator looks a bit similar to the way in which C++ handles classes and objects. Only: Modula-2 is much older than C++ so this might be an explanation how Stroustrup came to use the operator....

Epilog

The small fragments of code that we saw until now, show how charming the Modula-2 language is. It's readable for all. I like to call this language 'Self documenting'. Even without comments, the source is perfectly good to read and understand. Presumed that the source was well written.

In coming articles, you will find out that the 'curly braces languages' are often unnecessary complex. If you want to know more about programming in this magnificent language, visit my website called 'fruttenboel' and go to the 'Modula-2' or to the 'Mocka' section. Also the 'cgi' section will be interesting since I am writing some CGI executables instead of the slow Perl scripts.

Next time, we're going to write our own functions. And we will immediately see the differences with the curly braces guys.

Page created in 2003,