[net.lang.mod2] Compiling Modula-2 in one pass?

powell@decwrl.UUCP (Mike Powell) (08/25/84)

In the Report on The Programming Language Modula-2 (p.143 of Wirth's
Programming in Modula-2, Springer-Verlag, 1982), Section 4, paragraph 2,
it says:
    In general, the scope [of an identifier] extends over the entire block
    (procedure or module declaration) to which the declaration belongs and
    to which the object is local.  The scope rule is augmented by the following
    cases:

    1. If an identifier x defined by a declaration D1 is used in another
    declaration (not statement[!]) D2, then D1 must textually precede D2.
    ...

From the general rule, and the "not statement" comment, it seems that a
statement may make a forward reference to a declaration.  Case 1 does prevent
declarations from making forward references to other declarations.  The absence
of a forward procedure declaration, and the usefulness of allowing two local
modules to import each other's exports both argue that this was an intentional
decision to require a multi-pass compiler.

Note that I, too, had a one-pass compiler until I compiled some of the ETH
software and found it had forward references from statements to declarations.
I have heard that the Volition compiler permits forward declarations of at
least procedures and perhaps modules and other things, but I don't have direct
knowledge of it.
					Michael L. Powell
					Digital Equipment Corporation
					Western Research Laboratory
					Los Altos, CA  94022
					{decvax,ucbvax}!decwrl!powell
					powell@decwrl

ian@loral.UUCP (09/06/84)

   
   A previous article by Jacob Gore suggested that forward references
   in Modula are confined to references to procedures.  Mr. Gore suggests
   that since this is the case, a one pass compiler could be constructed,
   since only simple "back patching" of the symbol table would be
   necessary.  As discussed in a previous net article by Mike Powell,
   forward references are NOT confined to procedures.  The following
   program is allowed in Modula:

       MODULE ForwardReference;

	 PROCEDURE LookForward;
	 BEGIN
	   SomeHairyRec.Num := 0;
         END LookForward;

       VAR  (* note that this is global scope *)
	 SomeHairyRec : RECORD
			  Num : INTEGER;
			  AString : ARRAY[ 0..79 ] OF CHAR;
			  TheRealThing : REAL;
                        END;
       
       BEGIN
	 LookForward; 
       END ForwardReference.

   Please forgive any syntax errors, I don't have a Modula compiler on the
   VAX I use.

   Note that the statement which sets the record field to zero can not be
   checked until the record is entered in the symbol table.  No reasonable
   amount of "back patching" can take care of this problem.

			    Ian Kaplan
			    ucbvax!sdcc3!loral
			    Loral Data Flow Group
			    Loral Instrumentation
			    8401 Aero Dr.
			    San Diego, CA
			    92123
			    (619) 560-5888 x4812

                      PS: note name change of UC San Diego UNIX
			  Systems (i.e., sdccsu3 => sdcc3).