| FORTRAN | Algol | |
|---|---|---|
| Two levels of scope: local and global. | Nested blocks which define nested scopes. | |
| A variable name is statically bound to a memory location at load time (LT). | Bindings change at run time (RT). |
Scopes in Algol are defined by:
Blocks -- A block, like a compound statement, is delimited with a
begin-end pair, but also contains declarations, and thus defines a scope
extending from the begin to the end.
E.g.,
Procedure declarations -- Formal parameters are local to the procedure.
Scoping lines show the extent of the program in which the declaration of a variable is effective. (See Figure 3.3 in the textbook.)
Contour diagrams are like scoping lines extended into boxes, but also include boxes for identifiers and arrows from function name identifiers to the nested contour diagrams that define their scope. (See Figure 3.4 in the textbook.)
The goal of name structures is to limit the context which a programmer must remember at any time, i.e., the number of names visible.
Block structure restricts the visibility of a name to the block in which it is declared.
An inner block implicitly inherits access to all variables accessible in its immediately surrounding block (unless blocked by declaration of another variable with the same name).
E.g. In Figures 3.3 and 3.4, what binding of the variable x is used for the reference to x which is inside function cosh?
Answer: Whichever binding is seen first by the reference as it looks first inside then out of its box of the contour diagram; in this case, the parameter x is the innermost binding, and it blocks access to the global x.
Algol uses static scoping.
Review of static vs. dynamic scoping:
The meaning of a statement or expression is determined by the context in which it is interpreted.
In dynamic scoping, the scopes of names are determined by the dynamic structure of computations (i.e., can vary at run-time [RT]).
In static scoping, scopes are determined by the static structure of the program (i.e., meanings are fixed at compile time [CT]).
Static scoping is preferable according to the Structure Principle.
FORTRAN used EQUIVALENCE to share memory space between variables.
| + | Improves efficiency. |
| - | Results in aliasing problems. |
Algol uses activation records (AR's) to save memory.
When a block is not active, its AR does not exist.
Memory for variables is kept in the AR of the block where they are declared.
Memory is free of all variables whose block is not active.
Exception: An own variable (like a static local variable in C) is a local variable whose memory is not deallocated when its block is deactivated.
Superior to FORTRAN's EQUIVALENCE as aliasing is not possible.
E.g.,
a: begin
integer m, n
b: begin
real array x[1:100];
real y
. . .
end
. . .
c: begin
integer k
integer array m[0:50];
. . .
end
. . .
end
Blocks b and c are disjoint (i.e., cannot be active at the same time), so may share memory.
Primitive scalars: integer, real, boolean
Constructor: array
Array bounds and dimensions are unconstrained.
(See the Zero-One-Infinity Principle.)
E.g.,
integer array aMatrix[-10:10, 0:20];
Arrays are dynamic: space is allocated at block entry time and fixed until block exit.
E.g.,
begin
integer array aList[lowest:highest];
. . .
end
E.g.,
begin
integer i;
procedure f(x);
real x; x := 0;
integer array days[ 0: i ];
begin
. . .
end
begin
i := 7;
f(x);
. . .
i := 30;
f(x);
. . .
end
end
Strong typing -- Each variable has an associated type and the compiler checks that the arguments of each operation, function, or procedure call are of the correct type.