loop.hlp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. ;Loop macro blathering.
  2. ;
  3. ; This doc is totally wrong. Complete documentation (nice looking
  4. ; hardcopy) is available from GSB, or from ML:LSBDOC;LPDOC (which
  5. ; needs to be run through BOLIO).
  6. ;
  7. ;This is intended to be a cleaned-up version of PSZ's FOR package
  8. ;which is a cleaned-up version of the Interlisp CLisp FOR package.
  9. ;Note that unlike those crocks, the order of evaluation is the
  10. ;same as the textual order of the code, always.
  11. ;
  12. ;The form is introduced by the word LOOP followed by a series of clauses,
  13. ;each of which is introduced by a keyword which however need not be
  14. ;in any particular package. Certain keywords may be made "major"
  15. ;which means they are global and macros themselves, so you could put
  16. ;them at the front of the form and omit the initial "LOOP".
  17. ;
  18. ;Each clause can generate:
  19. ;
  20. ; Variables local to the loop.
  21. ;
  22. ; Prologue Code.
  23. ;
  24. ; Main Code.
  25. ;
  26. ; Epilogue Code.
  27. ;
  28. ;Within each of the three code sections, code is always executed strictly
  29. ;in the order that the clauses were written by the user. For parallel assignments
  30. ;and such there are special syntaxes within a clause. The prologue is executed
  31. ;once to set up. The main code is executed several times as the loop. The epilogue
  32. ;is executed once after the loop terminates.
  33. ;
  34. ;The term expression means any Lisp form. The term expression(s) means any number
  35. ;of Lisp forms, where only the first may be atomic. It stops at the first atom
  36. ;after the first form.
  37. ;
  38. ;The following clauses exist:
  39. ;
  40. ;Prologue:
  41. ; INITIALLY expression(s)
  42. ; This explicitly inserts code into the prologue. More commonly
  43. ; code comes from variable initializations.
  44. ;
  45. ;Epilogue:
  46. ; FINALLY expression(s)
  47. ; This is the only way to explicitly insert code into the epilogue.
  48. ;
  49. ;Side effects:
  50. ; DO expression(s)
  51. ; The expressions are evaluated. This is how you make a "body".
  52. ; DOING is synonymous with DO.
  53. ;
  54. ;Return values:
  55. ; RETURN expression(s)
  56. ; The last expression is returned immediately as the value of the form.
  57. ; This is equivalent to DO (RETURN expression) which you will
  58. ; need to use if you want to return multiple values.
  59. ; COLLECT expression(s)
  60. ; The return value of the form will be a list (unless over-ridden
  61. ; with a RETURN). The list is formed out of the values of the
  62. ; last expression.
  63. ; COLLECTING is synonymous with COLLECT.
  64. ; APPEND (or APPENDING) and NCONC (or NCONCING) can be used
  65. ; in place of COLLECT, forming the list in the appropriate ways.
  66. ; COUNT expression(s)
  67. ; The return value of the form will be the number of times the
  68. ; value of the last expression was non-NIL.
  69. ; SUM expression(s)
  70. ; The return value of the form will be the arithmetic sum of
  71. ; the values of the last expression.
  72. ; The following are a bit wierd syntactically, but Interlisp has them
  73. ; so they must be good.
  74. ; ALWAYS expression(s)
  75. ; The return value will be T if the last expression is true on
  76. ; every iteration, NIL otherwise.
  77. ; NEVER expressions(s)
  78. ; The return value will be T if the last expression is false on
  79. ; every iteration, NIL otherwise.
  80. ; THEREIS expression(s)
  81. ; This is wierd, I'm not sure what it really does.
  82. ; You probably want WHEN (NUMBERP X) RETURN X
  83. ; or maybe WHEN expression RETURN IT
  84. ;
  85. ;Conditionals: (these all affect only the main code)
  86. ;
  87. ; WHILE expression
  88. ; The loop terminates at this point if expression is false.
  89. ; UNTIL expression
  90. ; The loop terminates at this point if expression is true.
  91. ; WHEN expression clause
  92. ; Clause is performed only if expression is true.
  93. ; This affects only the main-code portion of a clause
  94. ; such as COLLECT. Use with FOR is a little unclear.
  95. ; IF is synonymous with WHEN.
  96. ; WHEN expression RETURN IT (also COLLECT IT, COUNT IT, SUM IT)
  97. ; This is a special case, the value of expression is returned if non-NIL.
  98. ; This works by generating a temporary variable to hold
  99. ; the value of the expression.
  100. ; UNLESS expression clause
  101. ; Clause is performed only if expression is false.
  102. ;
  103. ;Variables and iterations: (this is the hairy part)
  104. ;
  105. ; WITH variable = expression {AND variable = expression}...
  106. ; The variable is set to the expression in the prologue.
  107. ; If several variables are chained together with AND
  108. ; the setq's happen in parallel. Note that all variables
  109. ; are bound before any expressions are evaluated (unlike DO).
  110. ;
  111. ; FOR variable = expression {AND variable = expression}...
  112. ; At this point in the main code the variable is set to the expression.
  113. ; Equivalent to DO (PSETQ variable expression variable expression...)
  114. ; except that the variables are bound local to the loop.
  115. ;
  116. ; FOR variable FROM expression TO expression {BY expression}
  117. ; Numeric iteration. BY defaults to 1.
  118. ; BY and TO may be in either order.
  119. ; If you say DOWNTO instead of TO, BY defaults to -1 and
  120. ; the end-test is reversed.
  121. ; If you say BELOW instead of TO or ABOVE instead of DOWNTO
  122. ; the iteration stops before the end-value instead of after.
  123. ; The expressions are evaluated in the prologue then the
  124. ; variable takes on its next value at this point in the loop;
  125. ; hair is required to win the first time around if this FOR is
  126. ; not the first thing in the main code.
  127. ; FOR variable IN expression
  128. ; Iteration down members of a list.
  129. ; FOR variable ON expression
  130. ; Iteration down tails of a list.
  131. ; FOR variable IN/ON expression BY expression
  132. ; This is an Interlisp crock which looks useful.
  133. ; FOR var ON list BY expression[var]
  134. ; is the same as FOR var = list THEN expression[var]
  135. ; FOR var IN list BY expression[var]
  136. ; is similar except that var gets tails of the list
  137. ; and, kludgiferously, the internal tail-variable
  138. ; is substituted for var in expression.
  139. ; FOR variable = expression THEN expression
  140. ; General DO-type iteration.
  141. ; Note that all the different types of FOR clauses can be tied together
  142. ; with AND to achieve parallel assignment. Is this worthwhile?
  143. ; [It's only implemented for = mode.]
  144. ; AS is synonymous with FOR.
  145. ;
  146. ; FOR variable BEING expression(s) AND ITS pathname
  147. ; FOR variable BEING expression(s) AND ITS a-r
  148. ; FOR variable BEING {EACH} pathname {OF expression(s)}
  149. ; FOR variable BEING {EACH} a-r {OF expression(s)}
  150. ; Programmable iteration facility. Each pathname has a
  151. ; function associated with it, on LOOP-PATH-KEYWORD-ALIST; the
  152. ; alist has entries of the form (pathname function prep-list).
  153. ; prep-list is a list of allowed prepositions; after either of
  154. ; the above formats is parsed, then pairs of (preposition expression)
  155. ; are collected, while preposition is in prep-list. The expression
  156. ; may be a progn if there are multiple prepositions before the next
  157. ; keyword. The function is then called with arguments of:
  158. ; pathnname variable prep-phrases inclusive? prep-list
  159. ; Prep-phrases is the list of pairs collected, in order. Inclusive?
  160. ; is T for the first format, NIL otherwise; it says that the init
  161. ; value of the form takes on expression. For the first format, the
  162. ; list (OF expression) is pushed onto the fromt of the prep-phrases.
  163. ; In the above examples, a-r is a form to be evaluated to get an
  164. ; attachment-relationship. In this case, the pathname is taken as
  165. ; being ATTACHMENTS, and a-r is passed in by being treated as if it
  166. ; had been used with the preposition IN. The function should return
  167. ; a list of the form (bindings init-form step-form end-test); bindings
  168. ; are stuffed onto loop-variables, init-form is initialization code,
  169. ; step-form is step-code, and end-test tells whether or not to exit.
  170. ;
  171. ;Declarations? Not needed by Lisp machine. For Maclisp these will be done
  172. ;by a reserved word in front of the variable name as in PSZ's macro.
  173. ;
  174. ;The implementation is as a PROG. No initial values are given for the
  175. ;PROG-variables. PROG1 is used for parallel assignment.
  176. ;
  177. ;The iterating forms of FOR present a special problem. The problem is that
  178. ;you must do everything in the order that it was written by the user, but the
  179. ;FOR-variable gets its value in a different way in the first iteration than
  180. ;in the subsequent iterations. Note that the end-tests created by FOR have
  181. ;to be done in the appropriate order, since otherwise the next clause might get
  182. ;an error.
  183. ;
  184. ;The most general way is to introduce a flag, !FIRST-TIME, and compile the
  185. ;clause "FOR var = first TO last" as "INITIALLY (SETQ var first)
  186. ;WHEN (NOT !FIRST-TIME) DO (SETQ var (1+ var)) WHILE (<= var last)".
  187. ;However we try to optimize this by recognizing a special case:
  188. ;The special case is recognized where all FOR clauses are at the front of
  189. ;the main code; in this case if there is only one its stepping and
  190. ;endtest are moved to the end, and a jump to the endtest put at the
  191. ;front. If there are more than one their stepping and endtests are moved
  192. ;to the end, with duplicate endtests at the front except for the last
  193. ;which doesn't need a duplicate endtest. If FORs are embedded in the
  194. ;main code it can only be implemented by either a first-time flag or
  195. ;starting the iteration variable at a special value (initial minus step
  196. ;in the numeric iteration case). This could probably just be regarded as
  197. ;an error. The important thing is that it never does anything out of
  198. ;order.