The Modules of the Compiler and their Interfaces

The compiler consists of the following modules:
    Cmp       - the main module
    CmpScn    - the scanner
    CmpPrs    - the parser
    CmpGen    - the code generator and assembler
    CmpDic    - the dictionary module

The main module does not have the interface part and contains the definition of the goal function Main. All other modules consist of two parts: the interface and the implementation.

The module CmpScn has the following interface:
//
// File CmpScn.rfi
//

$func  InitScanner  s.Channel = ;
$func  ReadToken    = s.TokenClass s.TokenInfo;
$func  TermScanner  = ;

The module exports three functions.

The function InitScanner initializes the scanner. The parameter s.Channel is a reference to the channel that provides characters read by the scanner. This channel must have been opened for reading before calling InitScanner.

The function TermScanner must be called after the reading of the source program has been finished. This enables the scanner to terminate its activities and to get ready for reading another source program.

The function ReadToken returns the source programs's current token represented by two symbols: the first symbol indicates the class the token belongs to, while the second symbol provides additional information about the token.

The module CmpPrs has the following interface:
//
// File: Cmp.rfi
//

$func Parse  s.Channel = t.Program;

The interface exports the function Parse, which reads the source program from the channel s.Channel (via the scanner) and produces the abstract program t.Program. The channel s.Channel must have been opened for reading before calling Parse.

If the source program contains syntax errors, the function Parse returns $error(Ge), where Ge is an error message describing the first error encountered by Parse.

The module CmpGen has the following interface:
//
// File: CmpGen.rfi
//

$func GenCode    t.Program = t.Code;
$func WriteCode  t.Code = ;

The interface exports two functions.

The function GenCode takes as argument t.Program, an abstract program, and returns t.Code, the result of compiling t.Program into the machine code. The program t.Code is represented by an abstract syntax tree.

The function WriteCode takes as argument a machine code program represented by an abstract syntax tree, and, upon converting it into the character stream representation, writes it to the standard output device.

The module CmpDic has the following interface:
//
// File: CmpDic.rfi
//

$func MakeDic      = s.Dic;
$func LookupDic    s.Key s.Dic = s.Ref;
$func AllocateDic  s.Dic s.StartAddr = s.FreeAddr;
$func WriteDic s = ;

The interface exports four functions.

The function MakeDic returns a reference to a new empty dictionary.

The function LookupDic returns the label associated with the key s.Key in the dictionary referred to by s.Dic. If the key s.Key has not been registered in the dictionary, a new unique label is created, associated with the key s.Key, and returned as the function's result.

The function AllocateDic looks through the dictionary referred to by s.Dic and binds all labels registered in the dictionary to different addresses. If the dictionary contains N keys, the labels get bound to consecutive addresses starting with s.StartAddr. The result returned by the function is the first free address.