Previous chapters dealt with values that have always been known
when the program was written. If a program is to be able to react to
its environment, it must be able to convert external values into
internal values and then manipulate them. Analogous to
print
, the conversion can be done by
read which constructs internal values from
external character sequences. In order to manipulate such converted
values, we need some way of referring to them. Algol 68 can
generate values which can refer to other values. This kind of value
is called a name. Although a name has a
value, it is quite different from the value referred to. The
difference is rather like your name: your name refers to you, but is
quite distinct from you.
For example, suppose read
is presented with the
character sequence “123G
” and is expecting an
integer. read
will convert the digits into the number
“one hundred and twenty-three”, held in a special internal
form called “2's-complement binary”.
To manipulate that value, a name must be generated to refer to it.
The mode of a name is called a “reference mode”.
A name which can refer to a value of mode INT
is said
to have the mode REF INT
. Likewise, we can create names
with modes
REF BOOL REF[]CHAR REF[,]REAL
As you can see, REF
can precede any mode. It can also include
a mode already containing REF
. Thus it is possible to
construct modes such as
REF REF INT REF[]REF REAL REF[]REF[]CHAR REF REF REF BOOL
but we shall defer discussion of these latter modes to chapter 11.
Names are created using generators. There are two kinds of generator: local and global. The extent to which a name is valid is called its scope. The scope of a local name is restricted to the smallest enclosing clause which contains declarations. The scope of a global name extends to the whole program. In general, values have scope, identifiers have range. We shall meet global generators in chapters 6 and 11.
The phrase LOC INT
generates a name of mode REF
INT
which can refer to a value of mode
INT
.6.1 The
LOC stands for local. It is quite
reasonable to write the phrase
read(LOC INT)
Unfortunately, the created name is an anonymous name in the sense
that it has no identifier so that once the read
has
completed, the name disappears. We need some way of linking an
identifier with the generated name so that we can access the name
after read
has finished. This is done with an identity
declaration. Here is an identity declaration with a
local generator:
REF INT a = LOC INT
The value identified by a
has the mode REF
INT
because the phrase LOC INT
generates a name of
mode REF INT
. Thus it is a name, and it can refer to a
value (as yet undefined) of mode INT
(the value referred
to always has a mode of one less REF
). So now, we can
write
read(a)
After that phrase has been elaborated, a
identifies a
name which now refers to an integer.
Names can also be declared using a predeclared name on the
right-hand side of the identity declaration. Here is another identity
declaration using a
:
REF INT b = a
In this declaration, b
has the mode REF
INT
so it identifies a name. a
also has the mode
REF INT
and therefore also identifies a name. The
identity declaration makes b
identify the same name as
a
. This means that if the name identified by
a
refers to a value, then the name identified by
b
(the same name) will always refer to the same
value.