[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Some modules from Modula-2



>  From: Mike Griebling <grieblm@trt.allied.com>
>  
>  I've been attempting to convert the ConvTypes definition but have problems
>  with the ScanState PROCEDURE type declaration which appears to be recursive.
>  
>  Anyone have any ideas on how this should be handled?
>  
I always wondered why Modula-2 allows to recursively reference a
procedure type in its own declaration.  Now I know :).

In O2 we have only one recursive type: pointers.  So the procedure
variable has to be encapsulated into a record (or array).  It could
look like this:

MODULE ConvTypes;

TYPE
  (* ... *)
  ScanClass* = SHORTINT;
  
  (* The type of lexical scanning control procedures *)
  ScanState* = POINTER TO ScanStateDesc;
  ScanStateDesc* = RECORD
    proc*: PROCEDURE (inputCh: CHAR; VAR chClass: ScanClass;
                      VAR nextState: ScanState)
  END;
 
END ConvTypes.

--------

MODULE WholeStr;

IMPORT
  ConvTypes;
  
VAR
  ScanInt-: ConvTypes.ScanState;
  
PROCEDURE ScanIntProc (inputCh: CHAR; VAR chClass: ConvTypes.ScanClass;
                       VAR nextState: ConvTypes.ScanState);
  BEGIN
    (* ... *)
  END ScanIntProc;

(* ... *)

BEGIN
  NEW (ScanInt);
  ScanInt. proc := ScanIntProc
END WholeStr.

--------

Not very nice, but it should suffice.

--mva