If multiples are required in a structure, the structure declaration should only contain the required bounds if it is an actual-declarer. For example, we could declare
STRUCT([]CHAR forename, surname, title) lecturer= ("Nerissa","Leitch","Dr")
where the mode on the left is a formal-declarer (remember that the mode on the left-hand side of an identity declaration is always a formal-declarer). We could equally well declare
STRUCT([]CHAR forename, surname, title) student= ("Tom","MacAllister","Mr")
As you can see, the bounds of the individual multiples differ in the two cases.
When declaring a name, because the mode preceding the name
identifier is an actual-declarer (in an abbreviated declaration), the
bounds of the required multiples must be included. A suitable
declaration for a name which could refer to lecturer
would be
STRUCT([7]CHAR forename, [6]CHAR surname, [3]CHAR title) new lecturer := lecturer
but this would not be able to refer to student
. A better
declaration would use STRING:
STRUCT(STRING forename,surname,title)person
in which case we could now write
person:=lecturer; person:=student
Using field selection, we can write
title OF person
which would have mode REF STRING
. Thus, using field
selection, we can assign to the individual fields of
person
:
surname OF person:="McRae"
When slicing a field which is a multiple, it is necessary to
remember that slicing binds more tightly than
selecting (see chapter 10 for a detailed explanation). Thus the
first character of the surname of student
would be
accessed by writing
(surname OF student)[1]
which would have mode CHAR
. The parentheses ensure
that the selection is elaborated before the slicing. Similarly, the
first five characters of the forename of person
would be
accessed as
(forename OF person)[:5]
with mode REF[]CHAR
.
Consider the following program:
PROGRAM t1 CONTEXT VOID BEGIN MODE AMODE = STRUCT([4]CHAR a,INT b); AMODE a = ("abcde",3); AMODE b:=a; SKIP END FINISH
In the identity declaration for a
, the mode required
is a formal-declarer. In this case, the
a68toc compiler will ignore the bounds in the
declaration of AMODE
giving the mode
STRUCT([]CHAR a,INT b)
which explains why the structure-display on the right is accepted
("abcde"
has bounds [1:5]
). However, although the
program compiles without errors, when it is run, it fails with the
error message
Run time fault (aborting): ASSIGN2: bounds do not match in [] assignment
because the mode used in the declaration of the name b
is an actual-declarer (it contains the bounds given
in the mode declaration) and you cannot assign a
[]CHAR
with bounds [1:5]
to a
REF[]CHAR
with bounds [1:4]
.
With more complicated structures, it is better to use a mode declaration. For example, we could declare
MODE EMPLOYEE = STRUCT(STRING name, [2]STRING address, STRING dept,ni code,tax code, REAL basic,overtime rate, [52]REAL net pay,tax); EMPLOYEE emp
and then read specific values from the keyboard (chapter 9 covers reading data from files):
read((name OF emp,newline, (address OF emp)[1],newline, (address OF emp)[2],newline, ...
The modes of
name OF emp address OF emp net pay OF emp
are
REF STRING REF[]STRING REF[]REAL
respectively. The phrase
(net pay OF emp)[:10]
has the mode REF[]REAL
with bounds [1:10]
and represents the net pay of emp
for the first 10 weeks.
Note that although the monetary values are held as REAL
values, the accuracy with which a REAL
number is stored
is such that no rounding errors will ensue. See
section 12.1.5 which
describes which modes are suitable for storing monetary values.
emp
in the text, what would be
the mode of each of the following: Ansaddress OF emp
basic OF emp
(tax OF emp)[12]
(net pay OF emp)[10:12]
Sian Mountbatten 2012-01-19