powell@decwrl.UUCP (Mike Powell) (09/06/84)
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