7. FOR
FOR <output set spec>, <input set spec> <body> END ;
The output set is optional: that is, FOR need not produce output. When the output set is omitted, the syntax is:
FOR <input set spec> <body> END ;
The operations specified in the body are performed once for each member of the input set. References in the body to the input set member are treated as references to the current input set member. When an output set is present, a new member is created and added to the output set for each iteration (i.e., for each input set member). References to the output set member, similarly, are treated as references to the current output set member.
The output set spec must be the name of a LIST member. Each iteration of the FOR will create one such member, and add it to its LIST (hereafter called the output LIST). The body terminates the value that the new member receives. Any STRING in the new member which is not given a value by the body receives he default value of all blanks.
The input set spec must be an expression evaluating to a set of LIST-members (see section 7.1 for details of input set specification). Each iteration for the FOR will input one such member; the FOR will terminate when each member of the set has been processed. The LIST from which the input set members are drawn is called the input LIST.
FOR is effectively a means of accomplishing variants of assignment between a pair of LISTS. FOR is less concise than assignment, but offers more flexibility. Its advantages are:
(a) not all the input LIST-members need be transferred to the
output LIST. A subset can be selected by content.
(b) the user has explicit control over the assignment of values to
output LIST-members.
This is most easily understood by an example:
P PORT LIST F FILE LIST
R STRUCT R STRUCT
B STR A STRUCT
C STR A1 STR
END A2 STR
B STR
C STR
END
(1) P = F ;
(2) FOR P.R, F.R
P.R = F.R ;
END ;
(3) FOR P.R, F.R WITH A1 EQ 'XY' OR A2 GE 'AB'
B = C ;
C = A2 ;
END
Here, (1) and (2) are entirely equivalent requests. However, (3) is quite different and is not expressible as assignment. It selects a subset of F.Rs. The values it gives to the P.Rs could not result from application of the matching rules to F and P.
Because FOR is effectively assignment between a pair of LISTs, the LISTs referenced by a legitimate FOR-operation are largely subject to the same restrictions as LISTs referenced in an assignment. One exception is that the descriptions of the LIST-members need not match.
These restrictions are:
(a) both LISTs must be objects in the context in which the FOR
appears.
(b) both LISTs must be open or contained in open outermost
containers.
(c) if the output LIST is an outermost container, it must be in
WRITE- or APPEND-mode.
(d) If the output LIST is not outermost, the LIST which most
immediately encloses it must be the output LIST of an enclosing
FOR.
The mode of the output LIST of the FOR affects the result much as it would in an assignment: that is, a FOR outputting to a LIST in WRITE-mode overwrites previous contents, while a FOR outputting to a LIST in APPEND-mode adds to previous contents.
CAUTION TO THE READER: For convenience, these specifications use phrases such as 'LISTs referenced by a FOR'. Recall that such a phrase is not literally correct, in the sense that the operands of a FOR are always LIST members, not LISTs.
7.1 Details of input set specification
The input set is specified by a Datalanguage expression that evaluates to a set of LIST-members. Such an expression can be simply the set of all members of a LIST, or it can be a subset of the members of a LIST, specified by content. For example, with the description:
F FILE LIST
R STRUCT
A STR (1)
B STR (2)
END
the expression:
F.R
references all R's on the LIST F. However:
F.R WITH A EQ '5'
references only those R's containing an A having the value '5'. The expressions permitted as input set specifications are of the form:
<list-member-name> WITH <boolexp>
The <boolexp> is constructed of comparison expressions joined by the Boolean operators AND and OR. Any expression can be negated with NOT.
Comparison operators have the highest precedence. Next highest is AND, then OR, then NOT.
The comparison expressions are restricted to the form:
<container name> <comop> <constant>
where:
(a) <constant> is a string constant enclosed in single quotes
(see section 10.2 for a discussion of constants)
(b) <comop> is one of six operators:
EQ equal
NE not equal
LT less than
GT greater than
LE less than or equal to
GE greater than or equal to
(c) <container name> is the name of a STRING that appears once
in each LIST-member.
The constant is truncated or padded with blanks on the right to make it equal in size to the container to which it is being compared. Notice that padding on the right is not always desirable (users will have control over the padding in a future release). In particular, care must be exercised when using numbers in Version 0/9. (A number represented as a STRING should actually be described as a number; eventually it will be possible to do this).
7.2 FOR-body
Two operations are legitimate in a FOR-body: FOR and assignment.
These are subject to the restrictions discussed in Section 6.1 and in the introduction to Section 7. The restrictions are related to three requirements: (1) that the names be recognizable (see Section 9 for details), (2) that a request be consistent regarding direction of data transfer between containers, both within itself and with the MODE of outermost containers, and (3) that transfers occur between objects, not sets of objects. The first two requirements are permanent, but will become weaker in later versions of the language. The last requirement is temporary and will be present only in early versions.
Due to an implementation problem associated with Version 0/9, there is a somewhat bizarre restriction applied to references made in the body of a loop nested in another loop. This restriction is not expected to pose any practical problems for users, and is not part of the language design, but is discussed here for completeness.
The restriction is most easily understood by example:
given the description
F LIST
R STRUCT
A STR (3)
BL LIST (3)
B STR (3)
C STR (3)
END
and the request fragment:
FOR ...,R
FOR ...,B
... = A ;
... = C ;
END
END
observe:
(a) The outer loop processes the set of R's in F.
(b) For each R in F, the inner loop processes the set of B's in
the BL contained in that R.
(c) In the body of the inner loop, there are references to A and
C, which do not occur in B, but do occur in R. That is, the
objects referenced in the inner loop body are defined by the
outer loop, not the inner loop. In general, this is fine;
in the case of C, however, we have a problem.
(d) C occurs beyond the end of BL.
The 0/9 compiler is capable of neither (1) looking ahead enough to locate C before it compiles code for the loop, nor (2) while generating code to loop on the B's in BL, generating a separate body of code that skips to the end of BL to locate C. Thus it can handle A, which has been located before it begins loop generation, but it cannot handle C, which requires a little foresight.
The request fragment shown would not cause problems if the description were changed to:
F LIST
R STRUCT
A STR (3)
C STR (3)
BL LIST (3)
B STR (3)
END
Then both A and C would have been found before code for the inner loop was generated.