Modula-2 for Pascal programmers

Content:

  1. Introduction
  2. History
  3. Compilers
  4. Like Pascal
  5. IO
  6. Modules
  7. Various

Introduction:

This article will explain Modula-2 for Pascal programmers.

C/C++/Java/C# programmers should go here.

History:

Compilers:

Like Pascal:

Getting started with Modula-2 is really easy for Pascal programmers.

You just write Pascal code with the following changes:

Let us see a few examples.

Array:

Record:

Procedure:

Pointer and dynamic allocation:

Set:

All very similar to Pascal.

Modula-2 string procedures are similar to Delphi string procedures (standard Pascal does not have such and VMS Pascal string procedures are different).

Example:

A few areas are very different from Pascal:

IO:

Modules:

Let us take a simple Delphi/FPC example.

program Test(input,output);

{$MODE OBJFPC}
 
uses
    Classes, SysUtils;

type
    Acc = class(TObject)
              private
                  v : integer;
              public
                   constructor Create;
                   procedure Inc;
                   function Get : integer;
                   procedure Free;
              end;

constructor Acc.Create;

begin
    v := 0;
end;

procedure Acc.Inc;

begin
    v := v + 1;
end;

function Acc.Get : integer;

begin
    Get := v;
end;

procedure Acc.Free;

begin
end;

var
    a : Acc;
    i : integer;

begin
    a := Acc.Create;
    for i := 1 to 3 do begin
        a.Inc;
    end;
    writeln(a.Get():1);
    a.Free;
end.

Various:

Article history:

Version Date Description
1.0 August 27th 2021 Initial version
1.1 August 28th 2021 Add system/coroutines/c interface section
1.2 September 2nd 2021 Update C interface section and add assembler example

Other articles:

See list of all articles here

Comments:

Please send comments to Arne Vajhøj