295 lines
84 KiB
Plaintext
295 lines
84 KiB
Plaintext
Copyright (c) 1986 Xerox Corporation. All rights reserved.
|
||
|
||
9.8.7 Defining New Iterative Statement Operators
|
||
1
|
||
|
||
The following function is available for defining new or redefining existing iterative statement operators:
|
||
(I.S.OPR NAME FORM OTHERS EVALFLG) [Function]
|
||
1
|
||
|
||
NAME is the name of the new i.s.opr. If FORM is a list, NAME will be a new i.s.type (see ("i.s.type" . TAG)), and FORM its body.
|
||
OTHERS is an (optional) list of additional i.s. operators and operands which will be added to the i.s. at the place where NAME appears. If FORM is NIL, NAME is a new i.s.opr defined entirely by OTHERS.
|
||
In both FORM and OTHERS, the atom $$VAL can be used to reference the value to be returned by the i.s., I.V. to reference the current i.v., and BODY to reference NAME's operand. In other words, the current i.v. will be substituted for all instances of I.V. and NAME's operand will be substituted for all instances of BODY throughout FORM and OTHERS.
|
||
If EVALFLG is T, FORM and OTHERS are evaluated at translation time, and their values used as described above. A dummy variable for use in translation that does not clash with a dummy variable already used by some other i.s. operators can be obtained by calling (GETDUMMYVAR). (GETDUMMYVAR T) will return a dummy variable and also insure that it is bound as a PROG variable in the translation.
|
||
If NAME was previously an i.s.opr and is being redefined, the message (NAME REDEFINED) will be printed (unless DFNFLG=T), and all expressions using the i.s.opr NAME that have been translated will have their translations discarded.
|
||
1
|
||
|
||
The following are some examples of how I.S.OPR could be called to define some existing i.s.oprs, and create some new ones:
|
||
COLLECT
|
||
(I.S.OPR 'COLLECT
|
||
'(SETQ $$VAL (NCONC1 $$VAL BODY)))
|
||
SUM
|
||
(I.S.OPR 'SUM
|
||
'($$VAL_$$VAL+BODY)
|
||
'(FIRST $$VAL_0))
|
||
Note: $$VAL+BODY is used instead of (IPLUS $$VAL BODY) so that the choice of function used in the translation, i.e., IPLUS, FPLUS, or PLUS, will be determined by the declarations then in effect.
|
||
NEVER
|
||
(I.S.OPR 'NEVER
|
||
'(if BODY then $$VAL_NIL (GO $$OUT)))
|
||
Note: (if BODY then (RETURN NIL)) would exit from the i.s. immediately and therefore not execute the operations specified via a FINALLY (if any).
|
||
THEREIS
|
||
(I.S.OPR 'THEREIS
|
||
'(if BODY then $$VAL_I.V. (GO $$OUT)))
|
||
RCOLLECT To define RCOLLECT, a version of COLLECT which uses CONS instead of NCONC1 and then reverses the list of values:
|
||
(I.S.OPR 'RCOLLECT
|
||
'($$VAL_(CONS BODY $$VAL))
|
||
'(FINALLY (RETURN (DREVERSE $$VAL)))]
|
||
TCOLLECT To define TCOLLECT, a version of COLLECT which uses TCONC:
|
||
(I.S.OPR 'TCOLLECT
|
||
'(TCONC $$VAL BODY)
|
||
'(FIRST $$VAL_(CONS) FINALLY (RETURN (CAR $$VAL)))]
|
||
PRODUCT
|
||
(I.S.OPR 'PRODUCT
|
||
'($$VAL_$$VAL*BODY)
|
||
'(FIRST $$VAL_1)]
|
||
UPTO To define UPTO, a version of TO whose operand is evaluated only once:
|
||
(I.S.OPR 'UPTO
|
||
NIL
|
||
'(BIND $$FOO_BODY TO $$FOO)]
|
||
TO To redefine TO so that instead of recomputing FORM each iteration, a variable is bound to the value of FORM, and then that variable is used:
|
||
(I.S.OPR 'TO
|
||
NIL
|
||
'(BIND $$END FIRST $$END_BODY ORIGINAL TO $$END)]
|
||
Note the use of ORIGINAL to redefine TO in terms of its original definition. ORIGINAL is intended for use in redefining built-in operators, since their definitions are not accessible, and hence not directly modifiable. Thus if the operator had been defined by the user via I.S.OPR, ORIGINAL would not obtain its original definition. In this case, one presumably would simply modify the i.s.opr definition.
|
||
I.S.OPR can also be used to define synonyms for already defined i.s. operators by calling I.S.OPR with FORM an atom, e.g., (I.S.OPR 'WHERE 'WHEN) makes WHERE be the same as WHEN. Similarly, following (I.S.OPR 'ISTHERE 'THEREIS), one can write (ISTHERE ATOM IN Y), and following (I.S.OPR 'FIND 'FOR) and (I.S.OPR 'SUCHTHAT 'THEREIS), one can write (find X in Y suchthat X member Z). In the current system, WHERE is synonymous with WHEN, SUCHTHAT and ISTHERE with THEREIS, FIND with FOR, and THRU with TO.
|
||
If FORM is the atom MODIFIER, then NAME is defined as an i.s.opr which can immediately follow another i.s. operator (i.e., an error will not be generated, as described previously). NAME will not terminate the scope of the previous operator, and will be stripped off when DWIMIFY is called on its operand. OLD is an example of a MODIFIER type of operator. The MODIFIER feature allows the user to define i.s. operators similar to OLD, for use in conjunction with some other user defined i.s.opr which will produce the appropriate translation.
|
||
The file package command I.S.OPRS (("I.S.OPRS" . FilePackageCommand)) will dump the definition of i.s.oprs. (I.S.OPRS PRODUCT UPTO) as a file package command will print suitable expressions so that these iterative statement operators will be (re)defined when the file is loaded.
|
||
|