www.modula7.org - Official Website of the Modula-7 Programming Language
Modula-7 Language Reference
   

By Andrew J. Wozniewicz
Work-In-Progress

In what follows, the ellipsis "..." indicates a block of zero or more executable statements.

Blocks

Modula-7 is a recursively block-structured programming language in which the source code is organized in blocks contained within pairs of matching keywords. A block in Modula-7 starts with one of the block-introducing keywords, such as module, class, procedure, while, if, begin, etc. A block in Modula-7 could be a namespace, a declaration block (e.g. types, variables, constants), or a statement block (e.g. module, class, record, procedure, function).

The universal block terminator in Modula-7 is the keyword end, optionally followed by a semicolon. All blocks always require the termination keyword end.

//Nesting within statement blocks
procedure Proc1;
  procedure Nested1;
     procedure Nested2;
       ...
       //A nested declaration block
       variables 
         Var1,
         Var2: Float = 0.0;
         AStr: String;           
       end;  
       ...
       procedure Nested3;
         ...
       end;
       ...
       procedure Nested4;
       ...
       end;
     ...
     end;
   ...
   end;
...
end;

There are three types of blocks in Modula-7: statement blocks, declaration blocks, and namespace blocks.

Modula-7 blocks are recursive, in that statement blocks may have other blocks nested within them to arbitrary depth. Declaration blocks do not allow nesting of other blocks.

Executable statements may appear anywhere within the scope of a statement block. Declaration blocks may not contain executable statements.

Declaration Blocks

The following keywords introduce declaration-blocks that do not enclose other blocks, but must be enclosed within a statement block:

    dependencies
    imports
    types
    variables
    constants

Namespace Block

The following keyword introduces a namespace declaration-block that can only enclose statement blocks, and cannot itself be enclosed in any block:

    namespace

Statement Blocks

The following keywords introduce statement blocks that can enclose statements and other (nested) blocks, to arbitrary depth:

    module
    class
    record
    method
    procedure
    function
    constructor
    destructor

Classes

A class is a "module" that can have multiple instances. Classes can be nested. All sections in a class are optional. A class is automatically instantiated within its containing module, but instances of a class must be created explicitly.

class Class1;
  ...
  static var 
    ClassVar1,
    ClassVar2: Float = 0.0;
  ...
  var Field1: Integer;
  var Field2: String;  
  ...
  constructor Create;
    ...
  end;
  ...
  destructor Destroy;
    ...
  end;
  ...
  procedure Proc1;
    ...
  end;
  ...
  function Func2: Integer;
    ...
  end;
  ...
  var Field3: String;
  ...
end;

Modules

A module is a restricted class that can only have a single instance (singleton) and therefore lacks constructors and destructors. All module variables are static, i.e. they are automatically treated as class-variables and do not require the "static" keyword in their declarations. A Module is automatically instantiated by the environment.

module Module1;
  ...
  var ModuleVar1: Integer;
  var ModuleVar2: String;
  ...
  procedure Proc1;
    ...
  end;
  ...
  function Func2;
    ...
  end;
  ...  
  var ModuleVar3: String;
  ...  
end;

Subroutines

There are two kinds of subroutines - functions, and procedures. All subroutines are methods of a class/module. Corresponding to the main-end block of a class/module, a subroutine has a begin-end block containing the statements to be executed when the subroutine is called.

A subroutine is a special class/module that is instantiated only during its invocation. It lacks constructors and destructors, because it is instantiated implicitly, by being invoked.

Functions

A function returns a value and implicitly defines a Result variable. Functions can be invoked by being included in expressions, as well as being called directly, in a (call) statement.

function Fun1: Integer;
  var LocalVar1: Integer;
  var LocalVar2: String;
  ...
  procedure Proc1;
    ...
  end;
  ...
  function Func2;
    ...
  end;
  ...
  var LocalVar3: String; 
  ...
end;

Procedures

A procedure is a special kind of function - one that does not return a value. A procedure does not define the Result variable. Procedures are invoked by being called in a (call) statement. Procedures may not appear in expressions.

procedure Proc1;
var 
  LocalVar1: Integer;
  LocalVar2;
  ...     
  procedure Proc1;
    ...
  end;
  ...  
  function Func2;
    ...
  end;
  ...    
var 
  LocalVar3: String;
  ...  
end;
"High thoughts must have a high language." [Aristophanes]