Modula-2 Extensions For Experts
Syntax Reference For
Advanced
Programmers
Procedure types & variables
TYPE
suffix Pr
DoSomethingPr = PROCEDURE (CARDINAL, VAR
INTEGER): BOOLEAN;
Use: procedure variables
- VAR
proc:DoSomethingPr; declaration
- proc :=
MyProc;
where MyProc is a defined procedure
- boolean := proc(4, integer); procedure
call
- NIL value: no procedure
Pointers
- variable declaration: VAR ptr: POINTER TO CARDINAL;
- assignment
- ptr := ptr2;
- NIL value: pointer points to nothing
- pointer value: ptr^
- dynamic storage allocation:
- NEW(ptr);
- DISPOSE(ptr); (ptr becomes NIL)
- pointer type: TYPE NodePt = POINTER TO NODE; (*
suffix
Pt
*)
- for pointers to structured types:
- TYPE ListArPt = POINTER TO ARRAY... (* suffix ArPt
*)
- PersonRcTp = POINTER TO RECORD
... (*
suffix RcPt *)
- dynamics arrays (compile options M2EXTENSIONS and STORAGE
must be set!)
- declare as an open array:
- TYPE ArrTp = ARRAY OF CARDINAL;
- VAR arr: POINTER TO ArrTp;
- multidimensional arrays: TYPE ArrTp: ARRAY OF ARRAY
OF
CARDINAL;
- allocate
- NEW(arr, 10);
- multidimensional array: NEW(arr, 10, 5);
- use
- arr^[0] := 12; (first element has
index 0)
- arr^[9][4] := 12;
- deallocate (free the memory)
Libraries
= export constants, types, procedures and variables to other modules.
The
Definition Module
DEFINITION MODULE DemoLibrary;
CONST
STRING_MAX = 81;
TYPE
StringTp = ARRAY[0..STRING_MAX] OF CHAR;
PROCEDURE StringLength(str: StringTp): CARDINAL;
procedure heading only
only open arrays or array types
VAR
errorMessage: StringTp;
END DemoLibrary.
The Implementation Module
IMPLEMENTATION MODULE DemoLibrary;
PROCEDURE StringLength(str: StringTp): CARDINAL;
...
BEGIN
...
procedure implementation
END StringLength;
BEGIN
errorMessage := "no error until now"; initialisation
of variables
END DemoLibrary.
Use
MODULE TestLibrary;
FROM IO IMPORT WrStr, WrLn; import
procedures
IMPORT
DemoLibrary;
or import the whole module
VAR
str1: DemoLibrary.StringTp; use
BEGIN
WrStr(DemoLibrary.errorMessage);
END;
Use
within XDS
- create DemoLibrary.def for definition module, DemoLibrary.mod for
implementation
module
- compile first the definition module, then the implementation
module.
- put the 2 files in the same directory as the files where you want
to
use
it
or
- create a lib file with the
following command: XLIB /NEW DemoLibrary.lib DemoLibrary.obj (you find xlib under the bin folder)
- make it available within XDS:
Compile options
- use: add <* OPTION + *> in the code
directly
after the
first line MODULE ...
- see XDS help for a complete list (Compiler options and
equations),
the most important options are:
- <* WOFF + *> doesn't show compiler warnings
- <* NOOPTIMIZE + *> to prevent the compiler
for
optimising your
code, because then you won't see all your code in the debugger
- <* M2EXTENSIONS + *> and <*
M2ADDTYPES + *>
for Modula-2 extensions as pointers, LONGCARD etc
- increase STACKLIMIT, HEAPLIMIT and GCTRESHOLD
(eg. in the configuration file xc.cfg in XDS-directory/BIN) to allow
your
program to use more memory
Further (see Modula-2 book)
- other libraries
- variant records
- opaque types (p 404): like void pointers in C
- WORD, ADDRESS
- SIZE() procedure