Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site decwrl.UUCP Path: utzoo!watmath!clyde!burl!mgnetp!ihnp4!mhuxl!ulysses!allegra!mit-eddie!genrad!decvax!decwrl!powell From: powell@decwrl.UUCP (Mike Powell) Newsgroups: net.lang.mod2 Subject: One pass compilation/forward references Message-ID: <3527@decwrl.UUCP> Date: Thu, 6-Sep-84 01:36:49 EDT Article-I.D.: decwrl.3527 Posted: Thu Sep 6 01:36:49 1984 Date-Received: Wed, 12-Sep-84 01:08:40 EDT Sender: powell@decwrl.UUCP Organization: DEC Western Research Lab, Los Altos, CA Lines: 58 Gore's comments on forward references overlook an important point. Although Pascal requires all variable declarations in a block to precede any nested procedure definitions, Modula-2 does not. Therefore, a statement can refer to a variable (constant, type, or procedure, as well) that is defined in an enclosing procedure after the point of reference. E.g., module ForwardLooking; procedure Reference; begin v := c; (* this is OK in real Modula-2 *) end Reference; var v : cardinal; const c = 10; begin v := c; (* of course, this is OK *) end ForwardLooking. It is true that the compiler could go back and fix up the references when it encounters the definitions (We used to call this making 1 + epsilon passes). However, doing so can be awkward in general, since the compiler knows so little about v and c when it first sees the reference. Although by context, v must be a variable, c could be a constant or a variable (Thanks to the parentheses required on functions, it cannot be a function call, although c might be the name of a procedure if v were a procedure variable). To see that forward referencing can be useful, consider the use of internal modules in the following example. module Dilemma; module chicken; module egg; import numEggs; import numChickens; export numChickens; export numEggs; var numChickens : cardinal; var numEggs : cardinal; procedure Lay(); procedure Hatch(); begin begin inc(numEggs); dec(numEggs); end Lay; inc(numChickens); end chicken; end Hatch; end egg; end Dilemma. Although we might want Modula-2 to accept them as they are written above, in real life, one or the other must appear first. Although I am not a big fan of internal modules, I have seen non-trivial examples of them for which it would be difficult to sort the definitions. (I sometimes wonder why Wirth didn't allow forward references in declarations, as well; then I recall that it would require yet another pass.) Michael L. Powell Digital Equipment Corporation Western Research Laboratory Los Altos, CA 94022 {decvax,ucbvax}!decwrl!powell powell@decwrl