/[cmucl]/src/code/loop.lisp
ViewVC logotype

Contents of /src/code/loop.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.34 - (show annotations)
Tue Apr 20 17:57:44 2010 UTC (3 years, 11 months ago) by rtoy
Branch: MAIN
CVS Tags: sparc-tramp-assem-base, release-20b-pre1, release-20b-pre2, sparc-tramp-assem-2010-07-19, GIT-CONVERSION, cross-sol-x86-merged, RELEASE_20b, cross-sol-x86-base, snapshot-2010-12, snapshot-2010-11, snapshot-2011-09, snapshot-2011-06, snapshot-2011-07, snapshot-2011-04, snapshot-2011-02, snapshot-2011-03, snapshot-2011-01, snapshot-2010-05, snapshot-2010-07, snapshot-2010-06, snapshot-2010-08, cross-sol-x86-2010-12-20, cross-sparc-branch-base, HEAD
Branch point for: cross-sparc-branch, RELEASE-20B-BRANCH, sparc-tramp-assem-branch, cross-sol-x86-branch
Changes since 1.33: +5 -5 lines
Change uses of _"foo" to (intl:gettext "foo").  This is because slime
may get confused with source locations if the reader macros are
installed.
1 ;;; -*- Mode: LISP; Package: ANSI-LOOP; Syntax: Common-lisp; Base: 10; Lowercase:T -*-
2 ;;;>
3 ;;;> Portions of LOOP are Copyright (c) 1986 by the Massachusetts Institute of Technology.
4 ;;;> All Rights Reserved.
5 ;;;>
6 ;;;> Permission to use, copy, modify and distribute this software and its
7 ;;;> documentation for any purpose and without fee is hereby granted,
8 ;;;> provided that the M.I.T. copyright notice appear in all copies and that
9 ;;;> both that copyright notice and this permission notice appear in
10 ;;;> supporting documentation. The names "M.I.T." and "Massachusetts
11 ;;;> Institute of Technology" may not be used in advertising or publicity
12 ;;;> pertaining to distribution of the software without specific, written
13 ;;;> prior permission. Notice must be given in supporting documentation that
14 ;;;> copying distribution is by permission of M.I.T. M.I.T. makes no
15 ;;;> representations about the suitability of this software for any purpose.
16 ;;;> It is provided "as is" without express or implied warranty.
17 ;;;>
18 ;;;> Massachusetts Institute of Technology
19 ;;;> 77 Massachusetts Avenue
20 ;;;> Cambridge, Massachusetts 02139
21 ;;;> United States of America
22 ;;;> +1-617-253-1000
23 ;;;>
24 ;;;> Portions of LOOP are Copyright (c) 1989, 1990, 1991, 1992 by Symbolics, Inc.
25 ;;;> All Rights Reserved.
26 ;;;>
27 ;;;> Permission to use, copy, modify and distribute this software and its
28 ;;;> documentation for any purpose and without fee is hereby granted,
29 ;;;> provided that the Symbolics copyright notice appear in all copies and
30 ;;;> that both that copyright notice and this permission notice appear in
31 ;;;> supporting documentation. The name "Symbolics" may not be used in
32 ;;;> advertising or publicity pertaining to distribution of the software
33 ;;;> without specific, written prior permission. Notice must be given in
34 ;;;> supporting documentation that copying distribution is by permission of
35 ;;;> Symbolics. Symbolics makes no representations about the suitability of
36 ;;;> this software for any purpose. It is provided "as is" without express
37 ;;;> or implied warranty.
38 ;;;>
39 ;;;> Symbolics, CLOE Runtime, and Minima are trademarks, and CLOE, Genera,
40 ;;;> and Zetalisp are registered trademarks of Symbolics, Inc.
41 ;;;>
42 ;;;> Symbolics, Inc.
43 ;;;> 8 New England Executive Park, East
44 ;;;> Burlington, Massachusetts 01803
45 ;;;> United States of America
46 ;;;> +1-617-221-1000
47
48 ;; $aclHeader: loop.cl,v 1.5 91/12/04 01:13:48 cox acl4_1 $
49
50 #+cmu
51 (ext:file-comment
52 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/loop.lisp,v 1.34 2010/04/20 17:57:44 rtoy Rel $")
53
54 ;;;; LOOP Iteration Macro
55
56 (in-package :ansi-loop)
57 (intl:textdomain "cmucl")
58
59 (provide :loop)
60
61 ;;; Technology.
62 ;;;
63 ;;; The LOOP iteration macro is one of a number of pieces of code
64 ;;; originally developed at MIT and licensed as set out above. This
65 ;;; version of LOOP, which is almost entirely rewritten both as a
66 ;;; clean-up and to conform with the ANSI Lisp LOOP standard, started
67 ;;; life as MIT LOOP version 829 (which was a part of NIL, possibly
68 ;;; never released).
69 ;;;
70 ;;; A "light revision" was performed by Glenn Burke while at Palladian
71 ;;; Software in April 1986, to make the code run in Common Lisp. This
72 ;;; revision was informally distributed to a number of people, and was
73 ;;; sort of the "MIT" version of LOOP for running in Common Lisp.
74 ;;;
75 ;;; A later more drastic revision was performed at Palladian perhaps a
76 ;;; year later. This version was more thoroughly Common Lisp in
77 ;;; style, with a few miscellaneous internal improvements and
78 ;;; extensions. Glenn Burke lost track of this source, apparently
79 ;;; never having moved it to the MIT distribution point; and does not
80 ;;; remember if it was ever distributed.
81 ;;;
82 ;;; This revision for the ANSI standard is based on the code of Glenn
83 ;;; Burke's April 1986 version, with almost everything redesigned
84 ;;; and/or rewritten.
85
86
87 ;;; The design of this LOOP is intended to permit, using mostly the same
88 ;;; kernel of code, up to three different "loop" macros:
89 ;;;
90 ;;; (1) The unextended, unextensible ANSI standard LOOP;
91 ;;;
92 ;;; (2) A clean "superset" extension of the ANSI LOOP which provides
93 ;;; functionality similar to that of the old LOOP, but "in the style of"
94 ;;; the ANSI LOOP. For instance, user-definable iteration paths, with a
95 ;;; somewhat cleaned-up interface.
96 ;;;
97 ;;; (3) Extensions provided in another file which can make this LOOP
98 ;;; kernel behave largely compatibly with the Genera-vintage LOOP macro,
99 ;;; with only a small addition of code (instead of two whole, separate,
100 ;;; LOOP macros).
101 ;;;
102 ;;; Each of the above three LOOP variations can coexist in the same LISP
103 ;;; environment.
104 ;;;
105
106
107 ;;;; Miscellaneous Environment Things
108
109
110
111 ;;;@@@@The LOOP-Prefer-POP feature makes LOOP generate code which "prefers" to use POP or
112 ;;; its obvious expansion (prog1 (car x) (setq x (cdr x))). Usually this involves
113 ;;; shifting fenceposts in an iteration or series of carcdr operations. This is
114 ;;; primarily recognized in the list iterators (FOR .. {IN,ON}), and LOOP's
115 ;;; destructuring setq code.
116 (eval-when (compile load eval)
117 #+(or Genera Minima) (pushnew :LOOP-Prefer-POP *features*)
118 )
119
120
121 ;;; The uses of this macro are retained in the CL version of loop, in
122 ;;; case they are needed in a particular implementation. Originally
123 ;;; dating from the use of the Zetalisp COPYLIST* function, this is used
124 ;;; in situations where, were cdr-coding in use, having cdr-NIL at the
125 ;;; end of the list might be suboptimal because the end of the list will
126 ;;; probably be RPLACDed and so cdr-normal should be used instead.
127 (defmacro loop-copylist* (l)
128 #+Genera `(lisp:copy-list ,l nil t) ; arglist = (list &optional area force-dotted)
129 ;;@@@@Explorer??
130 #-Genera `(copy-list ,l)
131 )
132
133
134 (defvar *loop-gentemp*
135 nil)
136
137 (defun loop-gentemp (&optional (pref 'loopvar-))
138 (if *loop-gentemp*
139 (gensym (string pref))
140 (gensym)))
141
142
143
144 (defvar *loop-real-data-type* 'real)
145
146
147 (defun loop-optimization-quantities (env)
148 ;;@@@@ The ANSI conditionalization here is for those lisps that implement
149 ;; DECLARATION-INFORMATION (from cleanup SYNTACTIC-ENVIRONMENT-ACCESS).
150 ;; It is really commentary on how this code could be written. I don't
151 ;; actually expect there to be an ANSI #+-conditional -- it should be
152 ;; replaced with the appropriate conditional name for your
153 ;; implementation/dialect.
154 (declare #-ANSI (ignore env)
155 #+Genera (values speed space safety compilation-speed debug))
156 #+ANSI (let ((stuff (declaration-information 'optimize env)))
157 (values (or (cdr (assoc 'speed stuff)) 1)
158 (or (cdr (assoc 'space stuff)) 1)
159 (or (cdr (assoc 'safety stuff)) 1)
160 (or (cdr (assoc 'compilation-speed stuff)) 1)
161 (or (cdr (assoc 'debug stuff)) 1)))
162 #+CLOE-Runtime (values compiler::time compiler::space
163 compiler::safety compiler::compilation-speed 1)
164 #-(or ANSI CLOE-Runtime) (values 1 1 1 1 1))
165
166
167 ;;;@@@@ The following form takes a list of variables and a form which presumably
168 ;;; references those variables, and wraps it somehow so that the compiler does not
169 ;;; consider those variables have been referenced. The intent of this is that
170 ;;; iteration variables can be flagged as unused by the compiler, e.g. I in
171 ;;; (loop for i from 1 to 10 do (print t)), since we will tell it when a usage
172 ;;; of it is "invisible" or "not to be considered".
173 ;;;We implicitly assume that a setq does not count as a reference. That is, the
174 ;;; kind of form generated for the above loop construct to step I, simplified, is
175 ;;; `(SETQ I ,(HIDE-VARIABLE-REFERENCES '(I) '(1+ I))).
176 (defun hide-variable-references (variable-list form)
177 (declare #-Genera (ignore variable-list))
178 #+Genera (if variable-list `(compiler:invisible-references ,variable-list ,form) form)
179 #-Genera form)
180
181
182 ;;;@@@@ The following function takes a flag, a variable, and a form which presumably
183 ;;; references that variable, and wraps it somehow so that the compiler does not
184 ;;; consider that variable to have been referenced. The intent of this is that
185 ;;; iteration variables can be flagged as unused by the compiler, e.g. I in
186 ;;; (loop for i from 1 to 10 do (print t)), since we will tell it when a usage
187 ;;; of it is "invisible" or "not to be considered".
188 ;;;We implicitly assume that a setq does not count as a reference. That is, the
189 ;;; kind of form generated for the above loop construct to step I, simplified, is
190 ;;; `(SETQ I ,(HIDE-VARIABLE-REFERENCES T 'I '(1+ I))).
191 ;;;Certain cases require that the "invisibility" of the reference be conditional upon
192 ;;; something. This occurs in cases of "named" variables (the USING clause). For instance,
193 ;;; we want IDX in (LOOP FOR E BEING THE VECTOR-ELEMENTS OF V USING (INDEX IDX) ...)
194 ;;; to be "invisible" when it is stepped, so that the user gets informed if IDX is
195 ;;; not referenced. However, if no USING clause is present, we definitely do not
196 ;;; want to be informed that some random gensym is not used.
197 ;;;It is easier for the caller to do this conditionally by passing a flag (which
198 ;;; happens to be the second value of NAMED-VARIABLE, q.v.) to this function than
199 ;;; for all callers to contain the conditional invisibility construction.
200 (defun hide-variable-reference (really-hide variable form)
201 (declare #-Genera (ignore really-hide variable))
202 #+Genera (if (and really-hide variable (atom variable)) ;Punt on destructuring patterns
203 `(compiler:invisible-references (,variable) ,form)
204 form)
205 #-Genera form)
206
207
208 ;;;; List Collection Macrology
209
210
211 (defmacro with-loop-list-collection-head ((head-var tail-var &optional user-head-var)
212 &body body)
213 ;;@@@@ TI? Exploder?
214 #+LISPM (let ((head-place (or user-head-var head-var)))
215 `(let* ((,head-place nil)
216 (,tail-var
217 ,(hide-variable-reference
218 user-head-var user-head-var
219 `(progn #+Genera (scl:locf ,head-place)
220 #-Genera (system:variable-location ,head-place)))))
221 ,@body))
222 #-LISPM (let ((l (and user-head-var (list (list user-head-var nil)))))
223 #+CLOE `(sys::with-stack-list* (,head-var nil nil)
224 (let ((,tail-var ,head-var) ,@l)
225 ,@body))
226 #-CLOE `(let* ((,head-var (list nil)) (,tail-var ,head-var) ,@l)
227 ,@body)))
228
229
230 (defmacro loop-collect-rplacd (&environment env
231 (head-var tail-var &optional user-head-var) form)
232 (declare
233 #+LISPM (ignore head-var user-head-var) ;use locatives, unconditionally update through the tail.
234 )
235 (setq form (macroexpand form env))
236 (flet ((cdr-wrap (form n)
237 (declare (fixnum n))
238 (do () ((<= n 4) (setq form `(,(case n
239 (1 'cdr)
240 (2 'cddr)
241 (3 'cdddr)
242 (4 'cddddr))
243 ,form)))
244 (setq form `(cddddr ,form) n (- n 4)))))
245 (let ((tail-form form) (ncdrs nil))
246 ;;Determine if the form being constructed is a list of known length.
247 (when (consp form)
248 (cond ((eq (car form) 'list)
249 (setq ncdrs (1- (length (cdr form))))
250 ;;@@@@ Because the last element is going to be RPLACDed,
251 ;; we don't want the cdr-coded implementations to use
252 ;; cdr-nil at the end (which would just force copying
253 ;; the whole list again).
254 #+LISPM (setq tail-form `(list* ,@(cdr form) nil)))
255 ((member (car form) '(list* cons))
256 (when (and (cddr form) (member (car (last form)) '(nil 'nil)))
257 (setq ncdrs (- (length (cdr form)) 2))))))
258 (let ((answer
259 (cond ((null ncdrs)
260 `(when (setf (cdr ,tail-var) ,tail-form)
261 (setq ,tail-var (last (cdr ,tail-var)))))
262 ((< ncdrs 0) (return-from loop-collect-rplacd nil))
263 ((= ncdrs 0)
264 ;;@@@@ Here we have a choice of two idioms:
265 ;; (rplacd tail (setq tail tail-form))
266 ;; (setq tail (setf (cdr tail) tail-form)).
267 ;;Genera and most others I have seen do better with the former.
268 `(rplacd ,tail-var (setq ,tail-var ,tail-form)))
269 (t `(setq ,tail-var ,(cdr-wrap `(setf (cdr ,tail-var) ,tail-form)
270 ncdrs))))))
271 ;;If not using locatives or something similar to update the user's
272 ;; head variable, we've got to set it... It's harmless to repeatedly set it
273 ;; unconditionally, and probably faster than checking.
274 #-LISPM (when user-head-var
275 (setq answer `(progn ,answer (setq ,user-head-var (cdr ,head-var)))))
276 answer))))
277
278
279 (defmacro loop-collect-answer (head-var &optional user-head-var)
280 (or user-head-var
281 (progn
282 ;;If we use locatives to get tail-updating to update the head var,
283 ;; then the head var itself contains the answer. Otherwise we
284 ;; have to cdr it.
285 #+LISPM head-var
286 #-LISPM `(cdr ,head-var))))
287
288
289 ;;;; Maximization Technology
290
291
292 #|
293 The basic idea of all this minimax randomness here is that we have to
294 have constructed all uses of maximize and minimize to a particular
295 "destination" before we can decide how to code them. The goal is to not
296 have to have any kinds of flags, by knowing both that (1) the type is
297 something which we can provide an initial minimum or maximum value for
298 and (2) know that a MAXIMIZE and MINIMIZE are not being combined.
299
300 SO, we have a datastructure which we annotate with all sorts of things,
301 incrementally updating it as we generate loop body code, and then use
302 a wrapper and internal macros to do the coding when the loop has been
303 constructed.
304 |#
305
306
307 (defstruct (loop-minimax
308 (:constructor make-loop-minimax-internal)
309 (:copier nil)
310 (:predicate nil))
311 answer-variable
312 type
313 temp-variable
314 flag-variable
315 operations
316 infinity-data)
317
318
319 (defvar *loop-minimax-type-infinities-alist*
320 ;;@@@@ This is the sort of value this should take on for a Lisp that has
321 ;; "eminently usable" infinities. n.b. there are neither constants nor
322 ;; printed representations for infinities defined by CL.
323 ;;@@@@ This grotesque read-from-string below is to help implementations
324 ;; which croak on the infinity character when it appears in a token, even
325 ;; conditionalized out.
326 #+Genera
327 '#.(read-from-string
328 "((fixnum most-positive-fixnum most-negative-fixnum)
329 (short-float +1s -1s)
330 (single-float +1f -1f)
331 (double-float +1d -1d)
332 (long-float +1l -1l))")
333 ;;This is how the alist should look for a lisp that has no infinities. In
334 ;; that case, MOST-POSITIVE-x-FLOAT really IS the most positive.
335 #+(or CLOE-Runtime Minima)
336 '((fixnum most-positive-fixnum most-negative-fixnum)
337 (short-float most-positive-short-float most-negative-short-float)
338 (single-float most-positive-single-float most-negative-single-float)
339 (double-float most-positive-double-float most-negative-double-float)
340 (long-float most-positive-long-float most-negative-long-float))
341 ;; CMUCL has infinities so let's use them.
342 #+CMU
343 '((fixnum most-positive-fixnum most-negative-fixnum)
344 (short-float ext:single-float-positive-infinity ext:single-float-negative-infinity)
345 (single-float ext:single-float-positive-infinity ext:single-float-negative-infinity)
346 (double-float ext:double-float-positive-infinity ext:double-float-negative-infinity)
347 (long-float ext:long-float-positive-infinity ext:long-float-negative-infinity))
348 ;; If we don't know, then we cannot provide "infinite" initial values for any of the
349 ;; types but FIXNUM:
350 #-(or Genera CLOE-Runtime Minima CMU)
351 '((fixnum most-positive-fixnum most-negative-fixnum))
352 )
353
354
355 (defun make-loop-minimax (answer-variable type)
356 (let ((infinity-data (cdr (assoc type *loop-minimax-type-infinities-alist* :test #'subtypep))))
357 (make-loop-minimax-internal
358 :answer-variable answer-variable
359 :type type
360 :temp-variable (loop-gentemp 'loop-maxmin-temp-)
361 :flag-variable (and (not infinity-data) (loop-gentemp 'loop-maxmin-flag-))
362 :operations nil
363 :infinity-data infinity-data)))
364
365
366 (defun loop-note-minimax-operation (operation minimax)
367 (pushnew (the symbol operation) (loop-minimax-operations minimax))
368 (when (and (cdr (loop-minimax-operations minimax))
369 (not (loop-minimax-flag-variable minimax)))
370 (setf (loop-minimax-flag-variable minimax) (loop-gentemp 'loop-maxmin-flag-)))
371 operation)
372
373
374 (defmacro with-minimax-value (lm &body body)
375 (let ((init (loop-typed-init (loop-minimax-type lm)))
376 (which (car (loop-minimax-operations lm)))
377 (infinity-data (loop-minimax-infinity-data lm))
378 (answer-var (loop-minimax-answer-variable lm))
379 (temp-var (loop-minimax-temp-variable lm))
380 (flag-var (loop-minimax-flag-variable lm))
381 (type (loop-minimax-type lm)))
382 (if flag-var
383 `(let ((,answer-var ,init) (,temp-var ,init) (,flag-var nil))
384 (declare (type ,type ,answer-var ,temp-var))
385 ,@body)
386 `(let ((,answer-var ,(if (eq which 'min) (first infinity-data) (second infinity-data)))
387 (,temp-var ,init))
388 (declare (type ,type ,answer-var ,temp-var))
389 ,@body))))
390
391
392 (defmacro loop-accumulate-minimax-value (lm operation form)
393 (let* ((answer-var (loop-minimax-answer-variable lm))
394 (temp-var (loop-minimax-temp-variable lm))
395 (flag-var (loop-minimax-flag-variable lm))
396 (test
397 (hide-variable-reference
398 t (loop-minimax-answer-variable lm)
399 `(,(ecase operation
400 (min '<)
401 (max '>))
402 ,temp-var ,answer-var))))
403 `(progn
404 (setq ,temp-var ,form)
405 (when ,(if flag-var `(or (not ,flag-var) ,test) test)
406 (setq ,@(and flag-var `(,flag-var t))
407 ,answer-var ,temp-var)))))
408
409
410
411 ;;;; Loop Keyword Tables
412
413
414 #|
415 LOOP keyword tables are hash tables string keys and a test of EQUAL.
416
417 The actual descriptive/dispatch structure used by LOOP is called a "loop
418 universe" contains a few tables and parameterizations. The basic idea is
419 that we can provide a non-extensible ANSI-compatible loop environment,
420 an extensible ANSI-superset loop environment, and (for such environments
421 as CLOE) one which is "sufficiently close" to the old Genera-vintage
422 LOOP for use by old user programs without requiring all of the old LOOP
423 code to be loaded.
424 |#
425
426
427 ;;;; Token Hackery
428
429
430 ;;;Compare two "tokens". The first is the frob out of *LOOP-SOURCE-CODE*,
431 ;;; the second a symbol to check against.
432 (defun loop-tequal (x1 x2)
433 (and (symbolp x1) (string= x1 x2)))
434
435
436 (defun loop-tassoc (kwd alist)
437 (and (symbolp kwd) (assoc kwd alist :test #'string=)))
438
439
440 (defun loop-tmember (kwd list)
441 (and (symbolp kwd) (member kwd list :test #'string=)))
442
443
444 (defun loop-lookup-keyword (loop-token table)
445 (and (symbolp loop-token)
446 (values (gethash (symbol-name loop-token) table))))
447
448
449 (defmacro loop-store-table-data (symbol table datum)
450 `(setf (gethash (symbol-name ,symbol) ,table) ,datum))
451
452
453 (defstruct (loop-universe
454 (:print-function print-loop-universe)
455 (:copier nil)
456 (:predicate nil))
457 keywords ;hash table, value = (fn-name . extra-data).
458 iteration-keywords ;hash table, value = (fn-name . extra-data).
459 for-keywords ;hash table, value = (fn-name . extra-data).
460 path-keywords ;hash table, value = (fn-name . extra-data).
461 type-symbols ;hash table of type SYMBOLS, test EQ, value = CL type specifier.
462 type-keywords ;hash table of type STRINGS, test EQUAL, value = CL type spec.
463 ansi ;NIL, T, or :EXTENDED.
464 implicit-for-required ;see loop-hack-iteration
465 )
466
467
468 (defun print-loop-universe (u stream level)
469 (declare (ignore level))
470 (let ((str (case (loop-universe-ansi u)
471 ((nil) "Non-ANSI")
472 ((t) "ANSI")
473 (:extended "Extended-ANSI")
474 (t (loop-universe-ansi u)))))
475 ;;Cloe could be done with the above except for bootstrap lossage...
476 #+CLOE
477 (format stream "#<~S ~A ~X>" (type-of u) str (sys::address-of u))
478 #+(or Genera cmu) ;@@@@ This is reallly the ANSI definition.
479 (print-unreadable-object (u stream :type t :identity t)
480 (princ str stream))
481 #-(or Genera CLOE cmu)
482 (format stream "#<~S ~A>" (type-of u) str)
483 ))
484
485
486 ;;;This is the "current" loop context in use when we are expanding a
487 ;;;loop. It gets bound on each invocation of LOOP.
488 (defvar *loop-universe*)
489
490
491 (defun make-standard-loop-universe (&key keywords for-keywords iteration-keywords path-keywords
492 type-keywords type-symbols ansi)
493 #-(and CLOE Source-Bootstrap) (check-type ansi (member nil t :extended))
494 (flet ((maketable (entries)
495 (let* ((size (length entries))
496 (ht (make-hash-table :size (if (< size 10) 10 size) :test #'equal)))
497 (dolist (x entries) (setf (gethash (symbol-name (car x)) ht) (cadr x)))
498 ht)))
499 (make-loop-universe
500 :keywords (maketable keywords)
501 :for-keywords (maketable for-keywords)
502 :iteration-keywords (maketable iteration-keywords)
503 :path-keywords (maketable path-keywords)
504 :ansi ansi
505 :implicit-for-required (not (null ansi))
506 :type-keywords (maketable type-keywords)
507 :type-symbols (let* ((size (length type-symbols))
508 (ht (make-hash-table :size (if (< size 10) 10 size) :test #'eq)))
509 (dolist (x type-symbols)
510 (if (atom x) (setf (gethash x ht) x) (setf (gethash (car x) ht) (cadr x))))
511 ht))))
512
513
514 ;;;; Setq Hackery
515
516
517 (defvar *loop-destructuring-hooks*
518 nil
519 "If not NIL, this must be a list of two things:
520 a LET-like macro, and a SETQ-like macro, which perform LOOP-style destructuring.")
521
522
523 (defun loop-make-psetq (frobs)
524 (and frobs
525 (loop-make-desetq
526 (list (car frobs)
527 (if (null (cddr frobs)) (cadr frobs)
528 `(prog1 ,(cadr frobs)
529 ,(loop-make-psetq (cddr frobs))))))))
530
531
532 (defun loop-make-desetq (var-val-pairs)
533 (if (null var-val-pairs)
534 nil
535 (cons (if *loop-destructuring-hooks*
536 (cadr *loop-destructuring-hooks*)
537 'loop-really-desetq)
538 var-val-pairs)))
539
540
541 (defvar *loop-desetq-temporary*
542 (make-symbol "LOOP-DESETQ-TEMP"))
543
544
545 (defmacro loop-really-desetq (&environment env &rest var-val-pairs)
546 (labels ((find-non-null (var)
547 ;; see if there's any non-null thing here
548 ;; recurse if the list element is itself a list
549 (do ((tail var)) ((not (consp tail)) tail)
550 (when (find-non-null (pop tail)) (return t))))
551 (loop-desetq-internal (var val &optional temp)
552 ;; returns a list of actions to be performed
553 (typecase var
554 (null
555 (when (consp val)
556 ;; don't lose possible side-effects
557 (if (eq (car val) 'prog1)
558 ;; these can come from psetq or desetq below.
559 ;; throw away the value, keep the side-effects.
560 ;;Special case is for handling an expanded POP.
561 (mapcan #'(lambda (x)
562 (and (consp x)
563 (or (not (eq (car x) 'car))
564 (not (symbolp (cadr x)))
565 (not (symbolp (setq x (macroexpand x env)))))
566 (cons x nil)))
567 (cdr val))
568 `(,val))))
569 (cons
570 (let* ((car (car var))
571 (cdr (cdr var))
572 (car-non-null (find-non-null car))
573 (cdr-non-null (find-non-null cdr)))
574 (when (or car-non-null cdr-non-null)
575 (if cdr-non-null
576 (let* ((temp-p temp)
577 (temp (or temp *loop-desetq-temporary*))
578 (body #+LOOP-Prefer-POP `(,@(loop-desetq-internal
579 car
580 `(prog1 (car ,temp)
581 (setq ,temp (cdr ,temp))))
582 ,@(loop-desetq-internal cdr temp temp))
583 #-LOOP-Prefer-POP `(,@(loop-desetq-internal car `(car ,temp))
584 (setq ,temp (cdr ,temp))
585 ,@(loop-desetq-internal cdr temp temp))))
586 (if temp-p
587 `(,@(unless (eq temp val)
588 `((setq ,temp ,val)))
589 ,@body)
590 `((let ((,temp ,val))
591 ,@body))))
592 ;; no cdring to do
593 (loop-desetq-internal car `(car ,val) temp)))))
594 (otherwise
595 (unless (eq var val)
596 `((setq ,var ,val)))))))
597 (do ((actions))
598 ((null var-val-pairs)
599 (if (null (cdr actions)) (car actions) `(progn ,@(nreverse actions))))
600 (setq actions (revappend
601 (loop-desetq-internal (pop var-val-pairs) (pop var-val-pairs))
602 actions)))))
603
604
605 ;;;; LOOP-local variables
606
607 ;;;This is the "current" pointer into the LOOP source code.
608 (defvar *loop-source-code*)
609
610
611 ;;;This is the pointer to the original, for things like NAMED that
612 ;;;insist on being in a particular position
613 (defvar *loop-original-source-code*)
614
615
616 ;;;This is *loop-source-code* as of the "last" clause. It is used
617 ;;;primarily for generating error messages (see loop-error, loop-warn).
618 (defvar *loop-source-context*)
619
620
621 ;;;List of names for the LOOP, supplied by the NAMED clause.
622 (defvar *loop-names*)
623
624 ;;;The macroexpansion environment given to the macro.
625 (defvar *loop-macro-environment*)
626
627 ;;;This holds variable names specified with the USING clause.
628 ;;; See LOOP-NAMED-VARIABLE.
629 (defvar *loop-named-variables*)
630
631 ;;; LETlist-like list being accumulated for one group of parallel bindings.
632 (defvar *loop-variables*)
633
634 ;;;List of declarations being accumulated in parallel with
635 ;;;*loop-variables*.
636 (defvar *loop-declarations*)
637
638 ;;;Used by LOOP for destructuring binding, if it is doing that itself.
639 ;;; See loop-make-variable.
640 (defvar *loop-desetq-crocks*)
641
642 ;;; List of wrapping forms, innermost first, which go immediately inside
643 ;;; the current set of parallel bindings being accumulated in
644 ;;; *loop-variables*. The wrappers are appended onto a body. E.g.,
645 ;;; this list could conceivably has as its value ((with-open-file (g0001
646 ;;; g0002 ...))), with g0002 being one of the bindings in
647 ;;; *loop-variables* (this is why the wrappers go inside of the variable
648 ;;; bindings).
649 (defvar *loop-wrappers*)
650
651 ;;;This accumulates lists of previous values of *loop-variables* and the
652 ;;;other lists above, for each new nesting of bindings. See
653 ;;;loop-bind-block.
654 (defvar *loop-bind-stack*)
655
656 ;;;This is a LOOP-global variable for the (obsolete) NODECLARE clause
657 ;;;which inhibits LOOP from actually outputting a type declaration for
658 ;;;an iteration (or any) variable.
659 (defvar *loop-nodeclare*)
660
661 ;;;This is simply a list of LOOP iteration variables, used for checking
662 ;;;for duplications.
663 (defvar *loop-iteration-variables*)
664
665
666 ;;;List of prologue forms of the loop, accumulated in reverse order.
667 (defvar *loop-prologue*)
668
669 (defvar *loop-before-loop*)
670 (defvar *loop-body*)
671 (defvar *loop-after-body*)
672
673 ;;;This is T if we have emitted any body code, so that iteration driving
674 ;;;clauses can be disallowed. This is not strictly the same as
675 ;;;checking *loop-body*, because we permit some clauses such as RETURN
676 ;;;to not be considered "real" body (so as to permit the user to "code"
677 ;;;an abnormal return value "in loop").
678 (defvar *loop-emitted-body*)
679
680
681 ;;;List of epilogue forms (supplied by FINALLY generally), accumulated
682 ;;; in reverse order.
683 (defvar *loop-epilogue*)
684
685 ;;;List of epilogue forms which are supplied after the above "user"
686 ;;;epilogue. "normal" termination return values are provide by putting
687 ;;;the return form in here. Normally this is done using
688 ;;;loop-emit-final-value, q.v.
689 (defvar *loop-after-epilogue*)
690
691 ;;;The "culprit" responsible for supplying a final value from the loop.
692 ;;;This is so loop-emit-final-value can moan about multiple return
693 ;;;values being supplied.
694 (defvar *loop-final-value-culprit*)
695
696 ;;;If not NIL, we are in some branch of a conditional. Some clauses may
697 ;;;be disallowed.
698 (defvar *loop-inside-conditional*)
699
700 ;;;If not NIL, this is a temporary bound around the loop for holding the
701 ;;;temporary value for "it" in things like "when (f) collect it". It
702 ;;;may be used as a supertemporary by some other things.
703 (defvar *loop-when-it-variable*)
704
705 ;;;Sometimes we decide we need to fold together parts of the loop, but
706 ;;;some part of the generated iteration code is different for the first
707 ;;;and remaining iterations. This variable will be the temporary which
708 ;;;is the flag used in the loop to tell whether we are in the first or
709 ;;;remaining iterations.
710 (defvar *loop-never-stepped-variable*)
711
712 ;;;List of all the value-accumulation descriptor structures in the loop.
713 ;;; See loop-get-collection-info.
714 (defvar *loop-collection-cruft*) ; for multiple COLLECTs (etc)
715
716
717 ;;;; Code Analysis Stuff
718
719
720 (defun loop-constant-fold-if-possible (form &optional expected-type)
721 #+Genera (declare (values new-form constantp constant-value))
722 (let ((new-form form) (constantp nil) (constant-value nil))
723 #+Genera (setq new-form (compiler:optimize-form form *loop-macro-environment*
724 :repeat t
725 :do-macro-expansion t
726 :do-named-constants t
727 :do-inline-forms t
728 :do-optimizers t
729 :do-constant-folding t
730 :do-function-args t)
731 constantp (constantp new-form *loop-macro-environment*)
732 constant-value (and constantp (lt:evaluate-constant new-form *loop-macro-environment*)))
733 #-Genera (when (setq constantp (constantp new-form))
734 (setq constant-value (eval new-form)))
735 (when (and constantp expected-type)
736 (unless (typep constant-value expected-type)
737 (loop-warn "The form ~S evaluated to ~S, which was not of the anticipated type ~S."
738 form constant-value expected-type)
739 (setq constantp nil constant-value nil)))
740 (values new-form constantp constant-value)))
741
742
743 (defun loop-constantp (form)
744 #+Genera (constantp form *loop-macro-environment*)
745 #-Genera (constantp form))
746
747
748 ;;;; LOOP Iteration Optimization
749
750 (defvar *loop-duplicate-code*
751 nil)
752
753
754 (defvar *loop-iteration-flag-variable*
755 (make-symbol "LOOP-NOT-FIRST-TIME"))
756
757
758 (defun loop-code-duplication-threshold (env)
759 (multiple-value-bind (speed space) (loop-optimization-quantities env)
760 (+ 40 (* (- speed space) 10))))
761
762
763 (defmacro loop-body (&environment env
764 prologue
765 before-loop
766 main-body
767 after-loop
768 epilogue
769 &aux rbefore rafter flagvar)
770 (unless (= (length before-loop) (length after-loop))
771 (error (intl:gettext "LOOP-BODY called with non-synched before- and after-loop lists.")))
772 ;;All our work is done from these copies, working backwards from the end:
773 (setq rbefore (reverse before-loop) rafter (reverse after-loop))
774 (labels ((psimp (l)
775 (let ((ans nil))
776 (dolist (x l)
777 (when x
778 (push x ans)
779 (when (and (consp x) (member (car x) '(go return return-from)))
780 (return nil))))
781 (nreverse ans)))
782 (pify (l) (if (null (cdr l)) (car l) `(progn ,@l)))
783 (makebody ()
784 (let ((form `(tagbody
785 ;; ANSI CL 6.1.7.2 says that initially clauses are
786 ;; evaluated in the loop prologue, which precedes
787 ;; all loop code except for the initial settings
788 ;; provided by with, for, or as.
789 ,@(psimp (append (nreverse rbefore) prologue))
790 next-loop
791 ,@(psimp (append main-body (nreconc rafter `((go next-loop)))))
792 end-loop
793 ,@(psimp epilogue))))
794 (if flagvar `(let ((,flagvar nil)) ,form) form))))
795 (when (or *loop-duplicate-code* (not rbefore))
796 (return-from loop-body (makebody)))
797 ;; This outer loop iterates once for each not-first-time flag test generated
798 ;; plus once more for the forms that don't need a flag test
799 (do ((threshold (loop-code-duplication-threshold env))) (nil)
800 (declare (fixnum threshold))
801 ;; Go backwards from the ends of before-loop and after-loop merging all the equivalent
802 ;; forms into the body.
803 (do () ((or (null rbefore) (not (equal (car rbefore) (car rafter)))))
804 (push (pop rbefore) main-body)
805 (pop rafter))
806 (unless rbefore (return (makebody)))
807 ;; The first forms in rbefore & rafter (which are the chronologically
808 ;; last forms in the list) differ, therefore they cannot be moved
809 ;; into the main body. If everything that chronologically precedes
810 ;; them either differs or is equal but is okay to duplicate, we can
811 ;; just put all of rbefore in the prologue and all of rafter after
812 ;; the body. Otherwise, there is something that is not okay to
813 ;; duplicate, so it and everything chronologically after it in
814 ;; rbefore and rafter must go into the body, with a flag test to
815 ;; distinguish the first time around the loop from later times.
816 ;; What chronologically precedes the non-duplicatable form will
817 ;; be handled the next time around the outer loop.
818 (do ((bb rbefore (cdr bb)) (aa rafter (cdr aa)) (lastdiff nil) (count 0) (inc nil))
819 ((null bb) (return-from loop-body (makebody))) ;Did it.
820 (cond ((not (equal (car bb) (car aa))) (setq lastdiff bb count 0))
821 ((or (not (setq inc (estimate-code-size (car bb) env)))
822 (> (incf count inc) threshold))
823 ;; Ok, we have found a non-duplicatable piece of code. Everything
824 ;; chronologically after it must be in the central body.
825 ;; Everything chronologically at and after lastdiff goes into the
826 ;; central body under a flag test.
827 (let ((then nil) (else nil))
828 (do () (nil)
829 (push (pop rbefore) else)
830 (push (pop rafter) then)
831 (when (eq rbefore (cdr lastdiff)) (return)))
832 (unless flagvar
833 (push `(setq ,(setq flagvar *loop-iteration-flag-variable*) t) else))
834 (push `(if ,flagvar ,(pify (psimp then)) ,(pify (psimp else)))
835 main-body))
836 ;; Everything chronologically before lastdiff until the non-duplicatable form (car bb)
837 ;; is the same in rbefore and rafter so just copy it into the body
838 (do () (nil)
839 (pop rafter)
840 (push (pop rbefore) main-body)
841 (when (eq rbefore (cdr bb)) (return)))
842 (return)))))))
843
844
845
846 (defun duplicatable-code-p (expr env)
847 (if (null expr) 0
848 (let ((ans (estimate-code-size expr env)))
849 (declare (fixnum ans))
850 ;;@@@@ Use (DECLARATION-INFORMATION 'OPTIMIZE ENV) here to get an alist of
851 ;; optimize quantities back to help quantify how much code we are willing to
852 ;; duplicate.
853 ans)))
854
855
856 (defvar *special-code-sizes*
857 '((return 0) (progn 0)
858 (null 1) (not 1) (eq 1) (car 1) (cdr 1)
859 (when 1) (unless 1) (if 1)
860 (caar 2) (cadr 2) (cdar 2) (cddr 2)
861 (caaar 3) (caadr 3) (cadar 3) (caddr 3) (cdaar 3) (cdadr 3) (cddar 3) (cdddr 3)
862 (caaaar 4) (caaadr 4) (caadar 4) (caaddr 4)
863 (cadaar 4) (cadadr 4) (caddar 4) (cadddr 4)
864 (cdaaar 4) (cdaadr 4) (cdadar 4) (cdaddr 4)
865 (cddaar 4) (cddadr 4) (cdddar 4) (cddddr 4)))
866
867
868 (defvar *estimate-code-size-punt*
869 '(block
870 do do* dolist
871 flet
872 labels lambda let let* locally
873 macrolet multiple-value-bind
874 prog prog*
875 symbol-macrolet
876 tagbody
877 unwind-protect
878 with-open-file))
879
880
881 (defun destructuring-size (x)
882 (do ((x x (cdr x)) (n 0 (+ (destructuring-size (car x)) n)))
883 ((atom x) (+ n (if (null x) 0 1)))))
884
885
886 (defun estimate-code-size (x env)
887 (catch 'estimate-code-size
888 (estimate-code-size-1 x env)))
889
890
891 (defun estimate-code-size-1 (x env)
892 (flet ((list-size (l)
893 (let ((n 0))
894 (declare (fixnum n))
895 (dolist (x l n) (incf n (estimate-code-size-1 x env))))))
896 ;;@@@@ ???? (declare (function list-size (list) fixnum))
897 (cond ((constantp x #+Genera env) 1)
898 ((symbolp x) (multiple-value-bind (new-form expanded-p) (macroexpand-1 x env)
899 (if expanded-p (estimate-code-size-1 new-form env) 1)))
900 ((atom x) 1) ;??? self-evaluating???
901 ((symbolp (car x))
902 (let ((fn (car x)) (tem nil) (n 0))
903 (declare (symbol fn) (fixnum n))
904 (macrolet ((f (overhead &optional (args nil args-p))
905 `(the fixnum (+ (the fixnum ,overhead)
906 (the fixnum (list-size ,(if args-p args '(cdr x))))))))
907 (cond ((setq tem (get fn 'estimate-code-size))
908 (typecase tem
909 (fixnum (f tem))
910 (t (funcall tem x env))))
911 ((setq tem (assoc fn *special-code-sizes*)) (f (second tem)))
912 #+Genera
913 ((eq fn 'compiler:invisible-references) (list-size (cddr x)))
914 ((eq fn 'cond)
915 (dolist (clause (cdr x) n) (incf n (list-size clause)) (incf n)))
916 ((eq fn 'desetq)
917 (do ((l (cdr x) (cdr l))) ((null l) n)
918 (setq n (+ n (destructuring-size (car l)) (estimate-code-size-1 (cadr l) env)))))
919 ((member fn '(setq psetq))
920 (do ((l (cdr x) (cdr l))) ((null l) n)
921 (setq n (+ n (estimate-code-size-1 (cadr l) env) 1))))
922 ((eq fn 'go) 1)
923 ((eq fn 'function)
924 ;;This skirts the issue of implementationally-defined lambda macros
925 ;; by recognizing CL function names and nothing else.
926 (if (ext:valid-function-name-p (cadr x))
927 1
928 (throw 'duplicatable-code-p nil)))
929 ((eq fn 'multiple-value-setq) (f (length (second x)) (cddr x)))
930 ((eq fn 'return-from) (1+ (estimate-code-size-1 (third x) env)))
931 ((or (special-operator-p fn) (member fn *estimate-code-size-punt*))
932 (throw 'estimate-code-size nil))
933 (t (multiple-value-bind (new-form expanded-p) (macroexpand-1 x env)
934 (if expanded-p
935 (estimate-code-size-1 new-form env)
936 (f 3))))))))
937 (t (throw 'estimate-code-size nil)))))
938
939
940 ;;;; Loop Errors
941
942
943 (defun loop-context ()
944 (do ((l *loop-source-context* (cdr l)) (new nil (cons (car l) new)))
945 ((eq l (cdr *loop-source-code*)) (nreverse new))))
946
947
948 (defun loop-error (format-string &rest format-args)
949 #+(or Genera CLOE) (declare (dbg:error-reporter))
950 #+Genera (setq format-args (copy-list format-args)) ;Don't ask.
951 (kernel:simple-program-error (intl:gettext "~?~%Current LOOP context:~{ ~S~}.")
952 (intl:gettext format-string) format-args (loop-context)))
953
954
955 (defun loop-warn (format-string &rest format-args)
956 (warn (intl:gettext "~?~%Current LOOP context:~{ ~S~}.") (intl:gettext format-string) format-args (loop-context)))
957
958
959 (defun loop-check-data-type (specified-type required-type
960 &optional (default-type required-type))
961 (if (null specified-type)
962 default-type
963 (multiple-value-bind (a b) (subtypep specified-type required-type)
964 (cond ((not b)
965 (loop-warn _N"LOOP couldn't verify that ~S is a subtype of the required type ~S."
966 specified-type required-type))
967 ((not a)
968 (loop-error _N"Specified data type ~S is not a subtype of ~S."
969 specified-type required-type)))
970 specified-type)))
971
972
973 ;;;INTERFACE: Traditional, ANSI, Lucid.
974 (defmacro loop-finish ()
975 "Causes the iteration to terminate \"normally\", the same as implicit
976 termination by an iteration driving clause, or by use of WHILE or
977 UNTIL -- the epilogue code (if any) will be run, and any implicitly
978 collected result will be returned as the value of the LOOP."
979 '(go end-loop))
980
981
982
983 (defun subst-gensyms-for-nil (tree)
984 (declare (special *ignores*))
985 (cond
986 ((null tree) (car (push (loop-gentemp) *ignores*)))
987 ((atom tree) tree)
988 (t (cons (subst-gensyms-for-nil (car tree))
989 (subst-gensyms-for-nil (cdr tree))))))
990
991 (defun loop-build-destructuring-bindings (crocks forms)
992 (if crocks
993 (let ((*ignores* ()))
994 (declare (special *ignores*))
995 `((destructuring-bind ,(subst-gensyms-for-nil (car crocks))
996 ,(cadr crocks)
997 (declare (ignore ,@*ignores*))
998 ,@(loop-build-destructuring-bindings (cddr crocks) forms))))
999 forms))
1000
1001 (defun loop-translate (*loop-source-code* *loop-macro-environment* *loop-universe*)
1002 (let ((*loop-original-source-code* *loop-source-code*)
1003 (*loop-source-context* nil)
1004 (*loop-iteration-variables* nil)
1005 (*loop-variables* nil)
1006 (*loop-nodeclare* nil)
1007 (*loop-named-variables* nil)
1008 (*loop-declarations* nil)
1009 (*loop-desetq-crocks* nil)
1010 (*loop-bind-stack* nil)
1011 (*loop-prologue* nil)
1012 (*loop-wrappers* nil)
1013 (*loop-before-loop* nil)
1014 (*loop-body* nil)
1015 (*loop-emitted-body* nil)
1016 (*loop-after-body* nil)
1017 (*loop-epilogue* nil)
1018 (*loop-after-epilogue* nil)
1019 (*loop-final-value-culprit* nil)
1020 (*loop-inside-conditional* nil)
1021 (*loop-when-it-variable* nil)
1022 (*loop-never-stepped-variable* nil)
1023 (*loop-names* nil)
1024 (*loop-collection-cruft* nil))
1025 (loop-iteration-driver)
1026 (loop-bind-block)
1027 (let ((answer `(loop-body
1028 ,(nreverse *loop-prologue*)
1029 ,(nreverse *loop-before-loop*)
1030 ,(nreverse *loop-body*)
1031 ,(nreverse *loop-after-body*)
1032 ,(nreconc *loop-epilogue* (nreverse *loop-after-epilogue*)))))
1033 (dolist (entry *loop-bind-stack*)
1034 (let ((vars (first entry))
1035 (dcls (second entry))
1036 (crocks (third entry))
1037 (wrappers (fourth entry)))
1038 (dolist (w wrappers)
1039 (setq answer (append w (list answer))))
1040 (when (or vars dcls crocks)
1041 (let ((forms (list answer)))
1042 ;;(when crocks (push crocks forms))
1043 (when dcls (push `(declare ,@dcls) forms))
1044 (setq answer `(,(cond ((not vars) 'locally)
1045 (*loop-destructuring-hooks* (first *loop-destructuring-hooks*))
1046 (t 'let))
1047 ,vars
1048 ,@(loop-build-destructuring-bindings crocks forms)))))))
1049 (if *loop-names*
1050 (dolist (name *loop-names* answer)
1051 (setq answer `(block ,name ,answer)))
1052 `(block nil ,answer)))))
1053
1054
1055 (defun loop-iteration-driver ()
1056 (do () ((null *loop-source-code*))
1057 (let ((keyword (car *loop-source-code*)) (tem nil))
1058 (cond ((not (symbolp keyword))
1059 (loop-error _N"~S found where LOOP keyword expected." keyword))
1060 (t (setq *loop-source-context* *loop-source-code*)
1061 (loop-pop-source)
1062 (cond ((setq tem (loop-lookup-keyword keyword (loop-universe-keywords *loop-universe*)))
1063 ;;It's a "miscellaneous" toplevel LOOP keyword (do, collect, named, etc.)
1064 (apply (symbol-function (first tem)) (rest tem)))
1065 ((setq tem (loop-lookup-keyword keyword (loop-universe-iteration-keywords *loop-universe*)))
1066 (loop-hack-iteration tem))
1067 ((loop-tmember keyword '(and else))
1068 ;; Alternative is to ignore it, ie let it go around to the next keyword...
1069 (loop-error _N"Secondary clause misplaced at top level in LOOP macro: ~S ~S ~S ..."
1070 keyword (car *loop-source-code*) (cadr *loop-source-code*)))
1071 (t (loop-error _N"~S is an unknown keyword in LOOP macro." keyword))))))))
1072
1073
1074
1075 (defun loop-pop-source ()
1076 (if *loop-source-code*
1077 (pop *loop-source-code*)
1078 (loop-error _N"LOOP source code ran out when another token was expected.")))
1079
1080
1081 (defun loop-get-compound-form ()
1082 (let ((form (loop-get-form)))
1083 (unless (consp form)
1084 (loop-error _N"Compound form expected, but found ~A." form))
1085 form))
1086
1087 (defun loop-get-progn ()
1088 (do ((forms (list (loop-get-compound-form))
1089 (cons (loop-get-compound-form) forms))
1090 (nextform (car *loop-source-code*)
1091 (car *loop-source-code*)))
1092 ((atom nextform)
1093 (if (null (cdr forms)) (car forms) (cons 'progn (nreverse forms))))))
1094
1095
1096 (defun loop-get-form ()
1097 (if *loop-source-code*
1098 (loop-pop-source)
1099 (loop-error _N"LOOP code ran out where a form was expected.")))
1100
1101
1102 (defun loop-construct-return (form)
1103 `(return-from ,(car *loop-names*) ,form))
1104
1105
1106 (defun loop-pseudo-body (form)
1107 (cond ((or *loop-emitted-body* *loop-inside-conditional*) (push form *loop-body*))
1108 (t (push form *loop-before-loop*) (push form *loop-after-body*))))
1109
1110 (defun loop-emit-body (form)
1111 (setq *loop-emitted-body* t)
1112 (loop-pseudo-body form))
1113
1114 (defun loop-emit-final-value (&optional (form nil form-supplied-p))
1115 (when form-supplied-p
1116 (push (loop-construct-return form) *loop-after-epilogue*))
1117 (when *loop-final-value-culprit*
1118 (loop-warn _N"LOOP clause is providing a value for the iteration,~@
1119 however one was already established by a ~S clause."
1120 *loop-final-value-culprit*))
1121 (setq *loop-final-value-culprit* (car *loop-source-context*)))
1122
1123
1124 (defun loop-disallow-conditional (&optional kwd)
1125 #+(or Genera CLOE) (declare (dbg:error-reporter))
1126 (when *loop-inside-conditional*
1127 (loop-error _N"~:[This LOOP~;The LOOP ~:*~S~] clause is not permitted inside a conditional." kwd)))
1128
1129 (defun loop-disallow-anonymous-collectors ()
1130 (when (find-if-not 'loop-collector-name *loop-collection-cruft*)
1131 (loop-error _N"This LOOP clause is not permitted with anonymous collectors.")))
1132
1133 (defun loop-disallow-aggregate-booleans ()
1134 (when (loop-tmember *loop-final-value-culprit* '(always never thereis))
1135 (loop-error _N"This anonymous collection LOOP clause is not permitted with aggregate booleans.")))
1136
1137
1138
1139 ;;;; Loop Types
1140
1141
1142 (defun loop-typed-init (data-type)
1143 (when (and data-type (subtypep data-type 'number))
1144 (if (or (subtypep data-type 'float) (subtypep data-type '(complex float)))
1145 (coerce 0 data-type)
1146 0)))
1147
1148
1149 (defun loop-optional-type (&optional variable)
1150 ;;No variable specified implies that no destructuring is permissible.
1151 (and *loop-source-code* ;Don't get confused by NILs...
1152 (let ((z (car *loop-source-code*)))
1153 (cond ((loop-tequal z 'of-type)
1154 ;;This is the syntactically unambigous form in that the form of the
1155 ;; type specifier does not matter. Also, it is assumed that the
1156 ;; type specifier is unambiguously, and without need of translation,
1157 ;; a common lisp type specifier or pattern (matching the variable) thereof.
1158 (loop-pop-source)
1159 (loop-pop-source))
1160
1161 ((symbolp z)
1162 ;;This is the (sort of) "old" syntax, even though we didn't used to support all of
1163 ;; these type symbols.
1164 (let ((type-spec (or (gethash z (loop-universe-type-symbols *loop-universe*))
1165 (gethash (symbol-name z) (loop-universe-type-keywords *loop-universe*)))))
1166 (when type-spec
1167 (loop-pop-source)
1168 type-spec)))
1169 (t
1170 ;;This is our sort-of old syntax. But this is only valid for when we are destructuring,
1171 ;; so we will be compulsive (should we really be?) and require that we in fact be
1172 ;; doing variable destructuring here. We must translate the old keyword pattern typespec
1173 ;; into a fully-specified pattern of real type specifiers here.
1174 (if (consp variable)
1175 (unless (consp z)
1176 (loop-error
1177 _N"~S found where a LOOP keyword, LOOP type keyword, or LOOP type pattern expected."
1178 z))
1179 (loop-error _N"~S found where a LOOP keyword or LOOP type keyword expected." z))
1180 (loop-pop-source)
1181 (labels ((translate (k v)
1182 (cond ((null k) nil)
1183 ((atom k)
1184 (replicate
1185 (or (gethash k (loop-universe-type-symbols *loop-universe*))
1186 (gethash (symbol-name k) (loop-universe-type-keywords *loop-universe*))
1187 (loop-error
1188 _N"Destructuring type pattern ~S contains unrecognized type keyword ~S."
1189 z k))
1190 v))
1191 ((atom v)
1192 (loop-error
1193 _N"Destructuring type pattern ~S doesn't match variable pattern ~S."
1194 z variable))
1195 (t (cons (translate (car k) (car v)) (translate (cdr k) (cdr v))))))
1196 (replicate (typ v)
1197 (if (atom v) typ (cons (replicate typ (car v)) (replicate typ (cdr v))))))
1198 (translate z variable)))))))
1199
1200
1201
1202 ;;;; Loop Variables
1203
1204
1205 (defun loop-bind-block ()
1206 (when (or *loop-variables* *loop-declarations* *loop-wrappers*)
1207 (push (list (nreverse *loop-variables*) *loop-declarations* *loop-desetq-crocks* *loop-wrappers*)
1208 *loop-bind-stack*)
1209 (setq *loop-variables* nil
1210 *loop-declarations* nil
1211 *loop-desetq-crocks* nil
1212 *loop-wrappers* nil)))
1213
1214 (defun loop-variable-p (name)
1215 (do ((entry *loop-bind-stack* (cdr entry))) (nil)
1216 (cond ((null entry)
1217 (return nil))
1218 ((assoc name (caar entry) :test #'eq)
1219 (return t)))))
1220
1221 (defun loop-make-variable (name initialization dtype &optional iteration-variable-p)
1222 (cond ((null name)
1223 (cond ((not (null initialization))
1224 (push (list (setq name (loop-gentemp 'loop-ignore-))
1225 initialization)
1226 *loop-variables*)
1227 (push `(ignore ,name) *loop-declarations*))))
1228 ((atom name)
1229 (cond (iteration-variable-p
1230 (if (member name *loop-iteration-variables*)
1231 (loop-error _N"Duplicated LOOP iteration variable ~S." name)
1232 (push name *loop-iteration-variables*)))
1233 ((assoc name *loop-variables*)
1234 (loop-error _N"Duplicated variable ~S in LOOP parallel binding." name)))
1235 (unless (symbolp name)
1236 (loop-error _N"Bad variable ~S somewhere in LOOP." name))
1237 (loop-declare-variable name dtype)
1238 ;; We use ASSOC on this list to check for duplications (above),
1239 ;; so don't optimize out this list:
1240 (push (list name (or initialization (loop-typed-init dtype)))
1241 *loop-variables*))
1242 (initialization
1243 (cond (*loop-destructuring-hooks*
1244 (loop-declare-variable name dtype)
1245 (push (list name initialization) *loop-variables*))
1246 (t (let ((newvar (loop-gentemp 'loop-destructure-)))
1247 (loop-declare-variable name dtype)
1248 (push (list newvar initialization) *loop-variables*)
1249 ;; *LOOP-DESETQ-CROCKS* gathered in reverse order.
1250 (setq *loop-desetq-crocks*
1251 (list* name newvar *loop-desetq-crocks*))
1252 #+ignore
1253 (loop-make-variable name nil dtype iteration-variable-p)))))
1254 (t (let ((tcar nil) (tcdr nil))
1255 (if (atom dtype) (setq tcar (setq tcdr dtype))
1256 (setq tcar (car dtype) tcdr (cdr dtype)))
1257 (loop-make-variable (car name) nil tcar iteration-variable-p)
1258 (loop-make-variable (cdr name) nil tcdr iteration-variable-p))))
1259 name)
1260
1261
1262 (defun loop-make-iteration-variable (name initialization dtype)
1263 (when (and name (loop-variable-p name))
1264 (loop-error _N"Variable ~S has already been used" name))
1265 (loop-make-variable name initialization dtype t))
1266
1267
1268 (defun loop-declare-variable (name dtype)
1269 (cond ((or (null name) (null dtype) (eq dtype t)) nil)
1270 ((symbolp name)
1271 (unless (or (eq dtype t) (member (the symbol name) *loop-nodeclare*))
1272 (let ((dtype #-cmu dtype
1273 #+cmu
1274 (let ((init (loop-typed-init dtype)))
1275 (if (typep init dtype)
1276 dtype
1277 `(or (member ,init) ,dtype)))))
1278 (push `(type ,dtype ,name) *loop-declarations*))))
1279 ((consp name)
1280 (cond ((consp dtype)
1281 (loop-declare-variable (car name) (car dtype))
1282 (loop-declare-variable (cdr name) (cdr dtype)))
1283 (t (loop-declare-variable (car name) dtype)
1284 (loop-declare-variable (cdr name) dtype))))
1285 (t (error (intl:gettext "Invalid LOOP variable passed in: ~S.") name))))
1286
1287
1288 (defun loop-maybe-bind-form (form data-type)
1289 (if (loop-constantp form)
1290 form
1291 (loop-make-variable (loop-gentemp 'loop-bind-) form data-type)))
1292
1293
1294
1295 (defun loop-do-if (for negatep)
1296 (let ((form (loop-get-form))
1297 (it-p nil)
1298 (first-clause-p t) then else)
1299 (let ((*loop-inside-conditional* t))
1300 (flet ((get-clause (for)
1301 (do ((body nil)) (nil)
1302 (let ((key (car *loop-source-code*)) (*loop-body* nil) data)
1303 (cond ((not (symbolp key))
1304 (loop-error
1305 _N"~S found where keyword expected getting LOOP clause after ~S."
1306 key for))
1307 (t (setq *loop-source-context* *loop-source-code*)
1308 (loop-pop-source)
1309 (when (and (loop-tequal (car *loop-source-code*) 'it)
1310 first-clause-p)
1311 (setq *loop-source-code*
1312 (cons (or it-p (setq it-p (loop-when-it-variable)))
1313 (cdr *loop-source-code*))))
1314 (cond ((or (not (setq data (loop-lookup-keyword
1315 key (loop-universe-keywords *loop-universe*))))
1316 (progn (apply (symbol-function (car data)) (cdr data))
1317 (null *loop-body*)))
1318 (loop-error
1319 _N"~S does not introduce a LOOP clause that can follow ~S."
1320 key for))
1321 (t (setq body (nreconc *loop-body* body)))))))
1322 (setq first-clause-p nil)
1323 (if (loop-tequal (car *loop-source-code*) :and)
1324 (loop-pop-source)
1325 (return (if (cdr body) `(progn ,@(nreverse body)) (car body)))))))
1326 (setq then (get-clause for))
1327 (setq else (when (loop-tequal (car *loop-source-code*) :else)
1328 (loop-pop-source)
1329 (list (get-clause :else)))))
1330 (when (loop-tequal (car *loop-source-code*) :end)
1331 (loop-pop-source))
1332 (when it-p
1333 (setq form `(setq ,it-p ,form))))
1334 (loop-pseudo-body
1335 `(if ,(if negatep `(not ,form) form)
1336 ,then
1337 ,@else))))
1338
1339
1340 (defun loop-do-initially ()
1341 (loop-disallow-conditional :initially)
1342 (push (loop-get-progn) *loop-prologue*))
1343
1344 (defun loop-do-finally ()
1345 (loop-disallow-conditional :finally)
1346 (push (loop-get-progn) *loop-epilogue*))
1347
1348 (defun loop-do-do ()
1349 (loop-emit-body (loop-get-progn)))
1350
1351 (defun loop-do-named ()
1352 (let ((name (loop-pop-source)))
1353 (unless (symbolp name)
1354 (loop-error _N"~S is an invalid name for your LOOP." name))
1355 (when (or *loop-before-loop* *loop-body* *loop-after-epilogue* *loop-inside-conditional*)
1356 (loop-error _N"The NAMED ~S clause occurs too late." name))
1357 (when *loop-names*
1358 (loop-error _N"You may only use one NAMED clause in your loop: NAMED ~S ... NAMED ~S."
1359 (car *loop-names*) name))
1360 (setq *loop-names* (list name))))
1361
1362 (defun loop-do-return ()
1363 (loop-pseudo-body (loop-construct-return (loop-get-form))))
1364
1365
1366 ;;;; Value Accumulation: List
1367
1368
1369 (defstruct (loop-collector
1370 (:copier nil)
1371 (:predicate nil))
1372 name
1373 class
1374 (history nil)
1375 (tempvars nil)
1376 dtype
1377 (data nil)) ;collector-specific data
1378
1379
1380 (defun loop-get-collection-info (collector class default-type)
1381 (let ((form (loop-get-form))
1382 (dtype (and (not (loop-universe-ansi *loop-universe*)) (loop-optional-type)))
1383 (name (when (loop-tequal (car *loop-source-code*) 'into)
1384 (loop-pop-source)
1385 (loop-pop-source))))
1386 (when (not (symbolp name))
1387 (loop-error _N"Value accumulation recipient name, ~S, is not a symbol." name))
1388 (unless name
1389 (loop-disallow-aggregate-booleans))
1390 (unless dtype
1391 (setq dtype (or (loop-optional-type) default-type)))
1392 (let ((cruft (find (the symbol name) *loop-collection-cruft*
1393 :key #'loop-collector-name)))
1394 (cond ((not cruft)
1395 (when (and name (loop-variable-p name))
1396 (loop-error _N"Variable ~S cannot be used in INTO clause" name))
1397 (push (setq cruft (make-loop-collector
1398 :name name :class class
1399 :history (list collector) :dtype dtype))
1400 *loop-collection-cruft*))
1401 (t (unless (eq (loop-collector-class cruft) class)
1402 (loop-error
1403 _N"Incompatible kinds of LOOP value accumulation specified for collecting~@
1404 ~:[as the value of the LOOP~;~:*INTO ~S~]: ~S and ~S."
1405 name (car (loop-collector-history cruft)) collector))
1406 (unless (equal dtype (loop-collector-dtype cruft))
1407 (loop-warn
1408 _N"Unequal datatypes specified in different LOOP value accumulations~@
1409 into ~S: ~S and ~S."
1410 name dtype (loop-collector-dtype cruft))
1411 (when (eq (loop-collector-dtype cruft) t)
1412 (setf (loop-collector-dtype cruft) dtype)))
1413 (push collector (loop-collector-history cruft))))
1414 (values cruft form))))
1415
1416
1417 (defun loop-list-collection (specifically) ;NCONC, LIST, or APPEND
1418 (multiple-value-bind (lc form) (loop-get-collection-info specifically 'list 'list)
1419 (let ((tempvars (loop-collector-tempvars lc)))
1420 (unless tempvars
1421 (setf (loop-collector-tempvars lc)
1422 (setq tempvars (list* (loop-gentemp 'loop-list-head-)
1423 (loop-gentemp 'loop-list-tail-)
1424 (and (loop-collector-name lc)
1425 (list (loop-collector-name lc))))))
1426 (push `(with-loop-list-collection-head ,tempvars) *loop-wrappers*)
1427 (unless (loop-collector-name lc)
1428 (loop-emit-final-value `(loop-collect-answer ,(car tempvars) ,@(cddr tempvars)))))
1429 (ecase specifically
1430 (list (setq form `(list ,form)))
1431 (nconc nil)
1432 (append (unless (and (consp form) (eq (car form) 'list))
1433 (setq form `(loop-copylist* ,form)))))
1434 (loop-emit-body `(loop-collect-rplacd ,tempvars ,form)))))
1435
1436
1437 ;;;; Value Accumulation: max, min, sum, count.
1438
1439
1440
1441 (defun loop-sum-collection (specifically required-type default-type) ;SUM, COUNT
1442 (multiple-value-bind (lc form)
1443 (loop-get-collection-info specifically 'sum default-type)
1444 (loop-check-data-type (loop-collector-dtype lc) required-type)
1445 (let ((tempvars (loop-collector-tempvars lc)))
1446 (unless tempvars
1447 (setf (loop-collector-tempvars lc)
1448 (setq tempvars (list (loop-make-variable
1449 (or (loop-collector-name lc)
1450 (loop-gentemp 'loop-sum-))
1451 nil (loop-collector-dtype lc)))))
1452 (unless (loop-collector-name lc)
1453 (loop-emit-final-value (car (loop-collector-tempvars lc)))))
1454 (loop-emit-body
1455 (if (eq specifically 'count)
1456 `(when ,form
1457 (setq ,(car tempvars)
1458 ,(hide-variable-reference t (car tempvars) `(1+ ,(car tempvars)))))
1459 `(setq ,(car tempvars)
1460 (+ ,(hide-variable-reference t (car tempvars) (car tempvars))
1461 ,form)))))))
1462
1463
1464
1465 (defun loop-maxmin-collection (specifically)
1466 (multiple-value-bind (lc form)
1467 (loop-get-collection-info specifically 'maxmin *loop-real-data-type*)
1468 (loop-check-data-type (loop-collector-dtype lc) *loop-real-data-type*)
1469 (let ((data (loop-collector-data lc)))
1470 (unless data
1471 (setf (loop-collector-data lc)
1472 (setq data (make-loop-minimax
1473 (or (loop-collector-name lc) (loop-gentemp 'loop-maxmin-))
1474 (loop-collector-dtype lc))))
1475 (unless (loop-collector-name lc)
1476 (loop-emit-final-value (loop-minimax-answer-variable data))))
1477 (loop-note-minimax-operation specifically data)
1478 (push `(with-minimax-value ,data) *loop-wrappers*)
1479 (loop-emit-body `(loop-accumulate-minimax-value ,data ,specifically ,form))
1480 )))
1481
1482
1483 ;;;; Value Accumulation: Aggregate Booleans
1484
1485 ;;;ALWAYS and NEVER.
1486 ;;; Under ANSI these are not permitted to appear under conditionalization.
1487 (defun loop-do-always (restrictive negate)
1488 (let ((form (loop-get-form)))
1489 (when restrictive (loop-disallow-conditional))
1490 (loop-disallow-anonymous-collectors)
1491 (loop-emit-body `(,(if negate 'when 'unless) ,form
1492 ,(loop-construct-return nil)))
1493 (loop-emit-final-value t)))
1494
1495
1496
1497 ;;;THERIS.
1498 ;;; Under ANSI this is not permitted to appear under conditionalization.
1499 (defun loop-do-thereis (restrictive)
1500 (when restrictive (loop-disallow-conditional))
1501 (loop-disallow-anonymous-collectors)
1502 (loop-emit-final-value)
1503 (loop-emit-body `(when (setq ,(loop-when-it-variable) ,(loop-get-form))
1504 ,(loop-construct-return *loop-when-it-variable*))))
1505
1506
1507 (defun loop-do-while (negate kwd &aux (form (loop-get-form)))
1508 (loop-disallow-conditional kwd)
1509 (loop-pseudo-body `(,(if negate 'when 'unless) ,form (go end-loop))))
1510
1511
1512 (defun loop-do-with ()
1513 (loop-disallow-conditional :with)
1514 (do ((var) (val) (dtype)) (nil)
1515 (setq var (loop-pop-source)
1516 dtype (loop-optional-type var)
1517 val (cond ((loop-tequal (car *loop-source-code*) :=)
1518 (loop-pop-source)
1519 (loop-get-form))
1520 (t nil)))
1521 (when (and var (loop-variable-p var))
1522 (loop-error _N"Variable ~S has already been used" var))
1523 (loop-make-variable var val dtype)
1524 (if (loop-tequal (car *loop-source-code*) :and)
1525 (loop-pop-source)
1526 (return (loop-bind-block)))))
1527
1528
1529 ;;;; The iteration driver
1530
1531 (defun loop-hack-iteration (entry)
1532 (flet ((make-endtest (list-of-forms)
1533 (cond ((null list-of-forms) nil)
1534 ((member t list-of-forms) '(go end-loop))
1535 (t `(when ,(if (null (cdr (setq list-of-forms (nreverse list-of-forms))))
1536 (car list-of-forms)
1537 (cons 'or list-of-forms))
1538 (go end-loop))))))
1539 (do ((pre-step-tests nil)
1540 (steps nil)
1541 (post-step-tests nil)
1542 (pseudo-steps nil)
1543 (pre-loop-pre-step-tests nil)
1544 (pre-loop-steps nil)
1545 (pre-loop-post-step-tests nil)
1546 (pre-loop-pseudo-steps nil)
1547 (tem) (data))
1548 (nil)
1549 ;; Note we collect endtests in reverse order, but steps in correct
1550 ;; order. MAKE-ENDTEST does the nreverse for us.
1551 (setq tem (setq data (apply (symbol-function (first entry)) (rest entry))))
1552 (and (car tem) (push (car tem) pre-step-tests))
1553 (setq steps (nconc steps (loop-copylist* (car (setq tem (cdr tem))))))
1554 (and (car (setq tem (cdr tem))) (push (car tem) post-step-tests))
1555 (setq pseudo-steps (nconc pseudo-steps (loop-copylist* (car (setq tem (cdr tem))))))
1556 (setq tem (cdr tem))
1557 (when *loop-emitted-body*
1558 (loop-error _N"Iteration in LOOP follows body code."))
1559 (unless tem (setq tem data))
1560 (when (car tem) (push (car tem) pre-loop-pre-step-tests))
1561 (setq pre-loop-steps (nconc pre-loop-steps (loop-copylist* (car (setq tem (cdr tem))))))
1562 (when (car (setq tem (cdr tem))) (push (car tem) pre-loop-post-step-tests))
1563 (setq pre-loop-pseudo-steps (nconc pre-loop-pseudo-steps (loop-copylist* (cadr tem))))
1564 (unless (loop-tequal (car *loop-source-code*) :and)
1565 (setq *loop-before-loop* (list* (loop-make-desetq pre-loop-pseudo-steps)
1566 (make-endtest pre-loop-post-step-tests)
1567 (loop-make-psetq pre-loop-steps)
1568 (make-endtest pre-loop-pre-step-tests)
1569 *loop-before-loop*)
1570 *loop-after-body* (list* (loop-make-desetq pseudo-steps)
1571 (make-endtest post-step-tests)
1572 (loop-make-psetq steps)
1573 (make-endtest pre-step-tests)
1574 *loop-after-body*))
1575 (loop-bind-block)
1576 (return nil))
1577 (loop-pop-source) ; flush the "AND"
1578 (when (and (not (loop-universe-implicit-for-required *loop-universe*))
1579 (setq tem (loop-lookup-keyword
1580 (car *loop-source-code*)
1581 (loop-universe-iteration-keywords *loop-universe*))))
1582 ;;Latest ANSI clarification is that the FOR/AS after the AND must NOT be supplied.
1583 (loop-pop-source)
1584 (setq entry tem)))))
1585
1586
1587 ;;;; Main Iteration Drivers
1588
1589
1590 ;FOR variable keyword ..args..
1591 (defun loop-do-for ()
1592 (let* ((var (or (loop-pop-source) (loop-gentemp 'loop-do-for-anon-)))
1593 (data-type (loop-optional-type var))
1594 (keyword (loop-pop-source))
1595 (first-arg nil)
1596 (tem nil))
1597 (setq first-arg (loop-get-form))
1598 (unless (and (symbolp keyword)
1599 (setq tem (loop-lookup-keyword
1600 keyword
1601 (loop-universe-for-keywords *loop-universe*))))
1602 (loop-error _N"~S is an unknown keyword in FOR or AS clause in LOOP." keyword))
1603 (apply (car tem) var first-arg data-type (cdr tem))))
1604
1605 (defun loop-do-repeat ()
1606 (loop-disallow-conditional :repeat)
1607 (let ((form (loop-get-form))
1608 (type 'real))
1609 (let ((var (loop-make-variable (loop-gentemp) form type)))
1610 (push `(when (minusp (decf ,var)) (go end-loop)) *loop-before-loop*)
1611 (push `(when (minusp (decf ,var)) (go end-loop)) *loop-after-body*)
1612 ;; FIXME: What should
1613 ;; (loop count t into a
1614 ;; repeat 3
1615 ;; count t into b
1616 ;; finally (return (list a b)))
1617 ;; return: (3 3) or (4 3)? PUSHes above are for the former
1618 ;; variant, L-P-B below for the latter.
1619 #+nil (loop-pseudo-body `(when (minusp (decf ,var)) (go end-loop))))))
1620
1621 (defun loop-when-it-variable ()
1622 (or *loop-when-it-variable*
1623 (setq *loop-when-it-variable*
1624 (loop-make-variable (loop-gentemp 'loop-it-) nil nil))))
1625
1626
1627 ;;;; Various FOR/AS Subdispatches
1628
1629
1630 ;;;ANSI "FOR x = y [THEN z]" is sort of like the old Genera one when the THEN
1631 ;;; is omitted (other than being more stringent in its placement), and like
1632 ;;; the old "FOR x FIRST y THEN z" when the THEN is present. I.e., the first
1633 ;;; initialization occurs in the loop body (first-step), not in the variable binding
1634 ;;; phase.
1635 (defun loop-ansi-for-equals (var val data-type)
1636 (loop-make-iteration-variable var nil data-type)
1637 (cond ((loop-tequal (car *loop-source-code*) :then)
1638 ;;Then we are the same as "FOR x FIRST y THEN z".
1639 (loop-pop-source)
1640 `(() (,var ,(loop-get-form)) () ()
1641 () (,var ,val) () ()))
1642 (t ;;We are the same as "FOR x = y".
1643 ;; Let me document here what this is returning. Look at
1644 ;; loop-hack-iteration for more info. But anyway, we return a list of
1645 ;; 8 items, in this order: PRE-STEP-TESTS, STEPS, POST-STEP-TESTS,
1646 ;; PSEUDO-STEPS, PRE-LOOP-PRE-STEP-TESTS, PRE-LOOP-STEPS,
1647 ;; PRE-LOOP-POST-STEP-TESTS, PRE-LOOP-PSEUDO-STEPS. (We should add
1648 ;; something to make it easier to figure out what these args are!)
1649 ;;
1650 ;; For a "FOR x = y" clause without the THEN, we want the STEPS item to
1651 ;; step the variable VAR with the value VAL. This gets placed in the
1652 ;; body of the loop. The original code just did that. It seems that
1653 ;; the STEPS form is placed in *loop-before-loop* and in
1654 ;; *loop-after-loop*. Loop optimization would then see the same form
1655 ;; in both, and move them into the beginning of body. This is ok,
1656 ;; except that if there are :initially forms that were placed into the
1657 ;; loop prologue, the :initially forms might refer to incorrectly
1658 ;; initialized variables, because the optimizer moved STEPS from from
1659 ;; *loop-before-loop* into the body.
1660 ;;
1661 ;; To solve this, we add a PRE-LOOP-PSEUDO-STEP form that is identical
1662 ;; to the STEPS form. This gets placed in *loop-before-loop*. But
1663 ;; this won't match any *loop-after-loop* form, so it won't get moved,
1664 ;; and we maintain the proper sequencing such that the
1665 ;; PRE-LOOP-PSEUDO-STEP form is in *loop-before-loop*, before any
1666 ;; :initially clauses that might refer to this. So all is well. Whew.
1667 ;;
1668 ;; I hope this doesn't break anything else.
1669 `(() (,var ,val) () ()
1670 () () () (,var ,val))
1671 )))
1672
1673
1674 (defun loop-for-across (var val data-type)
1675 (loop-make-iteration-variable var nil data-type)
1676 (let ((vector-var (loop-gentemp 'loop-across-vector-))
1677 (index-var (loop-gentemp 'loop-across-index-)))
1678 (multiple-value-bind (vector-form constantp vector-value)
1679 (loop-constant-fold-if-possible val 'vector)
1680 (loop-make-variable
1681 vector-var vector-form
1682 (if (and (consp vector-form) (eq (car vector-form) 'the))
1683 (cadr vector-form)
1684 'vector))
1685 #+Genera (push `(system:array-register ,vector-var) *loop-declarations*)
1686 (loop-make-variable index-var 0 'fixnum)
1687 (let* ((length 0)
1688 (length-form (cond ((not constantp)
1689 (let ((v (loop-gentemp 'loop-across-limit-)))
1690 ;; This used to just push the length
1691 ;; computation into the prologue code. I
1692 ;; (rtoy) don't think that's right,
1693 ;; especially since the prologue is supposed
1694 ;; to happen AFTER other initializations.
1695 ;; So, this puts the computation in
1696 ;; *loop-before-body*. We need a matching
1697 ;; entry for *loop-after-body*, so stuff a
1698 ;; NIL there.
1699 (push `(setq ,v (length ,vector-var)) *loop-before-loop*)
1700 (push nil *loop-after-body*)
1701 (loop-make-variable v 0 'fixnum)))
1702 (t (setq length (length vector-value)))))
1703 (first-test `(>= ,index-var ,length-form))
1704 (other-test first-test)
1705 (step `(,var (aref ,vector-var ,index-var)))
1706 (pstep `(,index-var (1+ ,index-var))))
1707 (declare (fixnum length))
1708 (when constantp
1709 (setq first-test (= length 0))
1710 (when (<= length 1)
1711 (setq other-test t)))
1712 `(,other-test ,step () ,pstep
1713 ,@(and (not (eq first-test other-test)) `(,first-test ,step () ,pstep)))))))
1714
1715
1716
1717 ;;;; List Iteration
1718
1719
1720 (defun loop-list-step (listvar)
1721 ;;We are not equipped to analyze whether 'FOO is the same as #'FOO here in any
1722 ;; sensible fashion, so let's give an obnoxious warning whenever 'FOO is used
1723 ;; as the stepping function.
1724 ;;While a Discerning Compiler may deal intelligently with (funcall 'foo ...), not
1725 ;; recognizing FOO may defeat some LOOP optimizations.
1726 (let ((stepper (cond ((loop-tequal (car *loop-source-code*) :by)
1727 (loop-pop-source)
1728 (loop-get-form))
1729 (t '(function cdr)))))
1730 (cond ((and (consp stepper) (eq (car stepper) 'quote))
1731 (loop-warn _N"Use of QUOTE around stepping function in LOOP will be left verbatim.")
1732 (values `(funcall ,stepper ,listvar) nil))
1733 ((and (consp stepper) (eq (car stepper) 'function))
1734 (values (list (cadr stepper) listvar) (cadr stepper)))
1735 (t (values `(funcall ,(loop-make-variable (loop-gentemp 'loop-fn-) stepper 'function)
1736 ,listvar)
1737 nil)))))
1738
1739
1740 (defun loop-for-on (var val data-type)
1741 (multiple-value-bind (list constantp list-value) (loop-constant-fold-if-possible val)
1742 (let ((listvar var))
1743 (cond ((and var (symbolp var)) (loop-make-iteration-variable var list data-type))
1744 (t (loop-make-variable (setq listvar (loop-gentemp)) list 't)
1745 (loop-make-iteration-variable var nil data-type)))
1746 (multiple-value-bind (list-step step-function) (loop-list-step listvar)
1747 (declare #+(and (not LOOP-Prefer-POP) (not CLOE)) (ignore step-function))
1748 ;;@@@@ The CLOE problem above has to do with bug in macroexpansion of multiple-value-bind.
1749 (let* ((first-endtest
1750 (hide-variable-reference
1751 (eq var listvar)
1752 listvar
1753 ;; the following should use `atom' instead of `endp', per
1754 ;; [bug2428]
1755 `(atom ,listvar)))
1756 (other-endtest first-endtest))
1757 (when (and constantp (listp list-value))
1758 (setq first-endtest (null list-value)))
1759 (cond ((eq var listvar)
1760 ;;Contour of the loop is different because we use the user's variable...
1761 `(() (,listvar ,(hide-variable-reference t listvar list-step)) ,other-endtest
1762 () () () ,first-endtest ()))
1763 #+LOOP-Prefer-POP
1764 ((and step-function
1765 (let ((n (cdr (assoc step-function '((cdr . 1) (cddr . 2)
1766 (cdddr . 3) (cddddr . 4))))))
1767 (and n (do ((l var (cdr l)) (i 0 (1+ i)))
1768 ((atom l) (and (null l) (= i n)))
1769 (declare (fixnum i))))))
1770 (let ((step (mapcan #'(lambda (x) (list x `(pop ,listvar))) var)))
1771 `(,other-endtest () () ,step ,first-endtest () () ,step)))
1772 (t (let ((step `(,var ,listvar)) (pseudo `(,listvar ,list-step)))
1773 `(,other-endtest ,step () ,pseudo
1774 ,@(and (not (eq first-endtest other-endtest))
1775 `(,first-endtest ,step () ,pseudo)))))))))))
1776
1777
1778 (defun loop-for-in (var val data-type)
1779 (multiple-value-bind (list constantp list-value) (loop-constant-fold-if-possible val)
1780 (let ((listvar (loop-gentemp 'loop-list-)))
1781 (loop-make-iteration-variable var nil data-type)
1782 (loop-make-variable listvar list 'list)
1783 (multiple-value-bind (list-step step-function) (loop-list-step listvar)
1784 #-LOOP-Prefer-POP (declare (ignore step-function))
1785 (let* ((first-endtest `(endp ,listvar))
1786 (other-endtest first-endtest)
1787 (step `(,var (car ,listvar)))
1788 (pseudo-step `(,listvar ,list-step)))
1789 (when (and constantp (listp list-value))
1790 (setq first-endtest (null list-value)))
1791 #+LOOP-Prefer-POP (when (eq step-function 'cdr)
1792 (setq step `(,var (pop ,listvar)) pseudo-step nil))
1793 `(,other-endtest ,step () ,pseudo-step
1794 ,@(and (not (eq first-endtest other-endtest))
1795 `(,first-endtest ,step () ,pseudo-step))))))))
1796
1797
1798 ;;;; Iteration Paths
1799
1800
1801 (defstruct (loop-path
1802 (:copier nil)
1803 (:predicate nil))
1804 names
1805 preposition-groups
1806 inclusive-permitted
1807 function
1808 user-data)
1809
1810
1811 (defun add-loop-path (names function universe &key preposition-groups inclusive-permitted user-data)
1812 (unless (listp names) (setq names (list names)))
1813 ;; Can't do this due to CLOS bootstrapping problems.
1814 #-(or Genera (and CLOE Source-Bootstrap)) (check-type universe loop-universe)
1815 (let ((ht (loop-universe-path-keywords universe))
1816 (lp (make-loop-path
1817 :names (mapcar #'symbol-name names)
1818 :function function
1819 :user-data user-data
1820 :preposition-groups (mapcar #'(lambda (x) (if (listp x) x (list x))) preposition-groups)
1821 :inclusive-permitted inclusive-permitted)))
1822 (dolist (name names) (setf (gethash (symbol-name name) ht) lp))
1823 lp))
1824
1825
1826 ;;; Note: path functions are allowed to use loop-make-variable, hack
1827 ;;; the prologue, etc.
1828 (defun loop-for-being (var val data-type)
1829 ;; FOR var BEING each/the pathname prep-phrases using-stuff...
1830 ;; each/the = EACH or THE. Not clear if it is optional, so I guess we'll warn.
1831 (let ((path nil)
1832 (data nil)
1833 (inclusive nil)
1834 (stuff nil)
1835 (initial-prepositions nil))
1836 (cond ((loop-tmember val '(:each :the)) (setq path (loop-pop-source)))
1837 ((loop-tequal (car *loop-source-code*) :and)
1838 (loop-pop-source)
1839 (setq inclusive t)
1840 (unless (loop-tmember (car *loop-source-code*) '(:its :each :his :her))
1841 (loop-error _N"~S found where ITS or EACH expected in LOOP iteration path syntax."
1842 (car *loop-source-code*)))
1843 (loop-pop-source)
1844 (setq path (loop-pop-source))
1845 (setq initial-prepositions `((:in ,val))))
1846 (t (loop-error _N"Unrecognizable LOOP iteration path syntax. Missing EACH or THE?")))
1847 (cond ((not (symbolp path))
1848 (loop-error _N"~S found where a LOOP iteration path name was expected." path))
1849 ((not (setq data (loop-lookup-keyword path (loop-universe-path-keywords *loop-universe*))))
1850 (loop-error _N"~S is not the name of a LOOP iteration path." path))
1851 ((and inclusive (not (loop-path-inclusive-permitted data)))
1852 (loop-error _N"\"Inclusive\" iteration is not possible with the ~S LOOP iteration path." path)))
1853 (let ((fun (loop-path-function data))
1854 (preps (nconc initial-prepositions
1855 (loop-collect-prepositional-phrases (loop-path-preposition-groups data) t)))
1856 (user-data (loop-path-user-data data)))
1857 (when (symbolp fun) (setq fun (symbol-function fun)))
1858 (setq stuff (if inclusive
1859 (apply fun var data-type preps :inclusive t user-data)
1860 (apply fun var data-type preps user-data))))
1861 (when *loop-named-variables*
1862 (loop-error _N"Unused USING variables: ~S." *loop-named-variables*))
1863 ;; STUFF is now (bindings prologue-forms . stuff-to-pass-back). Protect the system from the user
1864 ;; and the user from himself.
1865 (unless (member (length stuff) '(6 10))
1866 (loop-error _N"Value passed back by LOOP iteration path function for path ~S has invalid length."
1867 path))
1868 (do ((l (car stuff) (cdr l)) (x)) ((null l))
1869 (if (atom (setq x (car l)))
1870 (loop-make-iteration-variable x nil nil)
1871 (loop-make-iteration-variable (car x) (cadr x) (caddr x))))
1872 (setq *loop-prologue* (nconc (reverse (cadr stuff)) *loop-prologue*))
1873 (cddr stuff)))
1874
1875
1876
1877 ;;;INTERFACE: Lucid, exported.
1878 ;;; i.e., this is part of our extended ansi-loop interface.
1879 (defun named-variable (name)
1880 (let ((tem (loop-tassoc name *loop-named-variables*)))
1881 (declare (list tem))
1882 (cond ((null tem) (values (loop-gentemp) nil))
1883 (t (setq *loop-named-variables* (delete tem *loop-named-variables*))
1884 (values (cdr tem) t)))))
1885
1886
1887 (defun loop-collect-prepositional-phrases (preposition-groups &optional USING-allowed initial-phrases)
1888 (flet ((in-group-p (x group) (car (loop-tmember x group))))
1889 (do ((token nil)
1890 (prepositional-phrases initial-phrases)
1891 (this-group nil nil)
1892 (this-prep nil nil)
1893 (disallowed-prepositions
1894 (mapcan #'(lambda (x)
1895 (loop-copylist*
1896 (find (car x) preposition-groups :test #'in-group-p)))
1897 initial-phrases))
1898 (used-prepositions (mapcar #'car initial-phrases)))
1899 ((null *loop-source-code*) (nreverse prepositional-phrases))
1900 (declare (symbol this-prep))
1901 (setq token (car *loop-source-code*))
1902 (dolist (group preposition-groups)
1903 (when (setq this-prep (in-group-p token group))
1904 (return (setq this-group group))))
1905 (cond (this-group
1906 (when (member this-prep disallowed-prepositions)
1907 (loop-error
1908 (if (member this-prep used-prepositions)
1909 _N"A ~S prepositional phrase occurs multiply for some LOOP clause."
1910 _N"Preposition ~S used when some other preposition has subsumed it.")
1911 token))
1912 (setq used-prepositions (if (listp this-group)
1913 (append this-group used-prepositions)
1914 (cons this-group used-prepositions)))
1915 (loop-pop-source)
1916 (push (list this-prep (loop-get-form)) prepositional-phrases))
1917 ((and USING-allowed (loop-tequal token 'using))
1918 (loop-pop-source)
1919 (do ((z (loop-pop-source) (loop-pop-source)) (tem)) (nil)
1920 (when (cadr z)
1921 (if (setq tem (loop-tassoc (car z) *loop-named-variables*))
1922 (loop-error
1923 _N"The variable substitution for ~S occurs twice in a USING phrase,~@
1924 with ~S and ~S."
1925 (car z) (cadr z) (cadr tem))
1926 (push (cons (car z) (cadr z)) *loop-named-variables*)))
1927 (when (or (null *loop-source-code*) (symbolp (car *loop-source-code*)))
1928 (return nil))))
1929 (t (return (nreverse prepositional-phrases)))))))
1930
1931
1932 ;;;; Master Sequencer Function
1933
1934
1935 (defun loop-sequencer (indexv indexv-type indexv-user-specified-p
1936 variable variable-type
1937 sequence-variable sequence-type
1938 step-hack default-top
1939 prep-phrases)
1940 (let ((endform nil) ;Form (constant or variable) with limit value.
1941 (sequencep nil) ;T if sequence arg has been provided.
1942 (testfn nil) ;endtest function
1943 (test nil) ;endtest form.
1944 (stepby (1+ (or (loop-typed-init indexv-type) 0))) ;Our increment.
1945 (stepby-constantp t)
1946 (step nil) ;step form.
1947 (dir nil) ;Direction of stepping: NIL, :UP, :DOWN.
1948 (inclusive-iteration nil) ;T if include last index.
1949 (start-given nil) ;T when prep phrase has specified start
1950 (start-value nil)
1951 (start-constantp nil)
1952 (limit-given nil) ;T when prep phrase has specified end
1953 (limit-constantp nil)
1954 (limit-value nil)
1955 )
1956 (when variable (loop-make-iteration-variable variable nil variable-type))
1957 (do ((l prep-phrases (cdr l)) (prep) (form) (odir)) ((null l))
1958 (setq prep (caar l) form (cadar l))
1959 (case prep
1960 ((:of :in)
1961 (setq sequencep t)
1962 (loop-make-variable sequence-variable form sequence-type))
1963 ((:from :downfrom :upfrom)
1964 (setq start-given t)
1965 (cond ((eq prep :downfrom) (setq dir ':down))
1966 ((eq prep :upfrom) (setq dir ':up)))
1967 (multiple-value-setq (form start-constantp start-value)
1968 (loop-constant-fold-if-possible form indexv-type))
1969 (loop-make-iteration-variable indexv form indexv-type))
1970 ((:upto :to :downto :above :below)
1971 (cond ((loop-tequal prep :upto) (setq inclusive-iteration (setq dir ':up)))
1972 ((loop-tequal prep :to) (setq inclusive-iteration t))
1973 ((loop-tequal prep :downto) (setq inclusive-iteration (setq dir ':down)))
1974 ((loop-tequal prep :above) (setq dir ':down))
1975 ((loop-tequal prep :below) (setq dir ':up)))
1976 (setq limit-given t)
1977 (multiple-value-setq (form limit-constantp limit-value)
1978 (loop-constant-fold-if-possible form indexv-type))
1979 (setq endform (if limit-constantp
1980 `',limit-value
1981 (loop-make-variable
1982 (loop-gentemp 'loop-limit-) form indexv-type))))
1983 (:by
1984 (multiple-value-setq (form stepby-constantp stepby)
1985 (loop-constant-fold-if-possible form indexv-type))
1986 (unless stepby-constantp
1987 (loop-make-variable (setq stepby (loop-gentemp 'loop-step-by-)) form indexv-type)))
1988 (t (loop-error
1989 _N"~S invalid preposition in sequencing or sequence path.~@
1990 Invalid prepositions specified in iteration path descriptor or something?"
1991 prep)))
1992 (when (and odir dir (not (eq dir odir)))
1993 (loop-error _N"Conflicting stepping directions in LOOP sequencing path"))
1994 (setq odir dir))
1995 (when (and sequence-variable (not sequencep))
1996 (loop-error _N"Missing OF or IN phrase in sequence path"))
1997 ;; Now fill in the defaults.
1998 (unless start-given
1999 (loop-make-iteration-variable
2000 indexv
2001 (setq start-constantp t start-value (or (loop-typed-init indexv-type) 0))
2002 indexv-type))
2003 (cond ((member dir '(nil :up))
2004 (when (or limit-given default-top)
2005 (unless limit-given
2006 (loop-make-variable (setq endform (loop-gentemp 'loop-seq-limit-))
2007 nil indexv-type)
2008 (push `(setq ,endform ,default-top) *loop-prologue*))
2009 (setq testfn (if inclusive-iteration '> '>=)))
2010 (setq step (if (eql stepby 1) `(1+ ,indexv) `(+ ,indexv ,stepby))))
2011 (t (unless start-given
2012 (unless default-top
2013 (loop-error _N"Don't know where to start stepping."))
2014 (push `(setq ,indexv (1- ,default-top)) *loop-prologue*))
2015 (when (and default-top (not endform))
2016 (setq endform (loop-typed-init indexv-type) inclusive-iteration t))
2017 (when endform (setq testfn (if inclusive-iteration '< '<=)))
2018 (setq step (if (eql stepby 1) `(1- ,indexv) `(- ,indexv ,stepby)))))
2019 (when testfn (setq test (hide-variable-reference t indexv `(,testfn ,indexv ,endform))))
2020 (when step-hack
2021 (setq step-hack `(,variable ,(hide-variable-reference indexv-user-specified-p indexv step-hack))))
2022 (let ((first-test test) (remaining-tests test))
2023 (when (and stepby-constantp start-constantp limit-constantp)
2024 (when (setq first-test (funcall (symbol-function testfn) start-value limit-value))
2025 (setq remaining-tests t)))
2026 `(() (,indexv ,(hide-variable-reference t indexv step)) ,remaining-tests ,step-hack
2027 () () ,first-test ,step-hack))))
2028
2029
2030 ;;;; Interfaces to the Master Sequencer
2031
2032
2033
2034 (defun loop-for-arithmetic (var val data-type kwd)
2035 (loop-sequencer
2036 var (loop-check-data-type data-type 'number) t
2037 nil nil nil nil nil nil
2038 (loop-collect-prepositional-phrases
2039 '((:from :upfrom :downfrom) (:to :upto :downto :above :below) (:by))
2040 nil (list (list kwd val)))))
2041
2042
2043 (defun loop-sequence-elements-path (variable data-type prep-phrases
2044 &key fetch-function size-function sequence-type element-type)
2045 (multiple-value-bind (indexv indexv-user-specified-p) (named-variable 'index)
2046 (let ((sequencev (named-variable 'sequence)))
2047 #+Genera (when (and sequencev
2048 (symbolp sequencev)
2049 sequence-type
2050 (subtypep sequence-type 'vector)
2051 (not (member (the symbol sequencev) *loop-nodeclare*)))
2052 (push `(sys:array-register ,sequencev) *loop-declarations*))
2053 (list* nil nil ; dummy bindings and prologue
2054 (loop-sequencer
2055 indexv 'fixnum indexv-user-specified-p
2056 variable (or data-type element-type)
2057 sequencev sequence-type
2058 `(,fetch-function ,sequencev ,indexv) `(,size-function ,sequencev)
2059 prep-phrases)))))
2060
2061
2062 ;;;; Builtin LOOP Iteration Paths
2063
2064
2065 #||
2066 (loop for v being the hash-values of ht do (print v))
2067 (loop for k being the hash-keys of ht do (print k))
2068 (loop for v being the hash-values of ht using (hash-key k) do (print (list k v)))
2069 (loop for k being the hash-keys of ht using (hash-value v) do (print (list k v)))
2070 ||#
2071
2072 (defun loop-hash-table-iteration-path (variable data-type prep-phrases &key which)
2073 (check-type which (member hash-key hash-value))
2074 (cond ((or (cdr prep-phrases) (not (member (caar prep-phrases) '(:in :of))))
2075 (loop-error _N"Too many prepositions!"))
2076 ((null prep-phrases) (loop-error _N"Missing OF or IN in ~S iteration path.")))
2077 (let ((ht-var (loop-gentemp 'loop-hashtab-))
2078 (next-fn (loop-gentemp 'loop-hashtab-next-))
2079 (dummy-predicate-var nil)
2080 (post-steps nil))
2081 (multiple-value-bind (other-var other-p)
2082 (named-variable (if (eq which 'hash-key) 'hash-value 'hash-key))
2083 ;;@@@@ named-variable returns a second value of T if the name was actually
2084 ;; specified, so clever code can throw away the gensym'ed up variable if
2085 ;; it isn't really needed.
2086 ;;The following is for those implementations in which we cannot put dummy NILs
2087 ;; into multiple-value-setq variable lists.
2088 #-Genera (setq other-p t
2089 dummy-predicate-var (loop-when-it-variable))
2090 (let* ((key-var nil)
2091 (val-var nil)
2092 (temp-val-var (loop-gentemp 'loop-hash-val-temp-))
2093 (temp-key-var (loop-gentemp 'loop-hash-key-temp-))
2094 (temp-predicate-var (loop-gentemp 'loop-hash-predicate-var-))
2095 (variable (or variable (loop-gentemp)))
2096 (bindings `((,variable nil ,data-type)
2097 (,ht-var ,(cadar prep-phrases))
2098 ,@(and other-p other-var `((,other-var nil))))))
2099 (if (eq which 'hash-key)
2100 (setq key-var variable val-var (and other-p other-var))
2101 (setq key-var (and other-p other-var) val-var variable))
2102 (push `(with-hash-table-iterator (,next-fn ,ht-var)) *loop-wrappers*)
2103 (when (consp key-var)
2104 (setq post-steps `(,key-var ,(setq key-var (loop-gentemp 'loop-hash-key-temp-))
2105 ,@post-steps))
2106 (push `(,key-var nil) bindings))
2107 (when (consp val-var)
2108 (setq post-steps `(,val-var ,(setq val-var (loop-gentemp 'loop-hash-val-temp-))
2109 ,@post-steps))
2110 (push `(,val-var nil) bindings))
2111 `(,bindings ;bindings
2112 () ;prologue
2113 () ;pre-test
2114 () ;parallel steps
2115 (not
2116 (multiple-value-bind (,temp-predicate-var ,temp-key-var ,temp-val-var)
2117 (,next-fn)
2118 ;; We use M-V-BIND instead of M-V-SETQ because we only
2119 ;; want to assign values to the key and val vars when we
2120 ;; are in the hash table. When we reach the end,
2121 ;; TEMP-PREDICATE-VAR is NIL, and so are temp-key-var and
2122 ;; temp-val-var. This might break any type declarations
2123 ;; on the key and val vars.
2124 (when ,temp-predicate-var
2125 (setq ,val-var ,temp-val-var)
2126 (setq ,key-var ,temp-key-var))
2127 (setq ,dummy-predicate-var ,temp-predicate-var)
2128 )) ;post-test
2129 ,post-steps)))))
2130
2131
2132 (defun loop-package-symbols-iteration-path (variable data-type prep-phrases &key symbol-types)
2133 (cond ((and prep-phrases (cdr prep-phrases))
2134 (loop-error _N"Too many prepositions!"))
2135 ((and prep-phrases (not (member (caar prep-phrases) '(:in :of))))
2136 (loop-error _N"Unknown preposition ~S" (caar prep-phrases))))
2137 (unless (symbolp variable)
2138 (loop-error _N"Destructuring is not valid for package symbol iteration."))
2139 (let ((pkg-var (loop-gentemp 'loop-pkgsym-))
2140 (next-fn (loop-gentemp 'loop-pkgsym-next-))
2141 (variable (or variable (loop-gentemp)))
2142 (pkg (or (cadar prep-phrases) '*package*)))
2143 (push `(with-package-iterator (,next-fn ,pkg-var ,@symbol-types)) *loop-wrappers*)
2144 `(((,variable nil ,data-type) (,pkg-var ,pkg))
2145 ()
2146 ()
2147 ()
2148 (not (multiple-value-setq (,(progn
2149 ;;@@@@ If an implementation can get away without actually
2150 ;; using a variable here, so much the better.
2151 #+Genera NIL
2152 #-Genera (loop-when-it-variable))
2153 ,variable)
2154 (,next-fn)))
2155 ())))
2156
2157 ;;;; ANSI Loop
2158
2159 (defun make-ansi-loop-universe (extended-p)
2160 (let ((w (make-standard-loop-universe
2161 :keywords `((named (loop-do-named))
2162 (initially (loop-do-initially))
2163 (finally (loop-do-finally))
2164 (do (loop-do-do))
2165 (doing (loop-do-do))
2166 (return (loop-do-return))
2167 (collect (loop-list-collection list))
2168 (collecting (loop-list-collection list))
2169 (append (loop-list-collection append))
2170 (appending (loop-list-collection append))
2171 (nconc (loop-list-collection nconc))
2172 (nconcing (loop-list-collection nconc))
2173 (count (loop-sum-collection count ,*loop-real-data-type* fixnum))
2174 (counting (loop-sum-collection count ,*loop-real-data-type* fixnum))
2175 (sum (loop-sum-collection sum number number))
2176 (summing (loop-sum-collection sum number number))
2177 (maximize (loop-maxmin-collection max))
2178 (minimize (loop-maxmin-collection min))
2179 (maximizing (loop-maxmin-collection max))
2180 (minimizing (loop-maxmin-collection min))
2181 (always (loop-do-always t nil)) ; Normal, do always
2182 (never (loop-do-always t t)) ; Negate the test on always.
2183 (thereis (loop-do-thereis t))
2184 (while (loop-do-while nil :while)) ; Normal, do while
2185 (until (loop-do-while t :until)) ; Negate the test on while
2186 (when (loop-do-if when nil)) ; Normal, do when
2187 (if (loop-do-if if nil)) ; synonymous
2188 (unless (loop-do-if unless t)) ; Negate the test on when
2189 (with (loop-do-with))
2190 (repeat (loop-do-repeat)))
2191 :for-keywords '((= (loop-ansi-for-equals))
2192 (across (loop-for-across))
2193 (in (loop-for-in))
2194 (on (loop-for-on))
2195 (from (loop-for-arithmetic :from))
2196 (downfrom (loop-for-arithmetic :downfrom))
2197 (upfrom (loop-for-arithmetic :upfrom))
2198 (below (loop-for-arithmetic :below))
2199 (above (loop-for-arithmetic :above))
2200 (to (loop-for-arithmetic :to))
2201 (upto (loop-for-arithmetic :upto))
2202 (downto (loop-for-arithmetic :downto))
2203 (by (loop-for-arithmetic :by))
2204 (being (loop-for-being)))
2205 :iteration-keywords '((for (loop-do-for))
2206 (as (loop-do-for)))
2207 :type-symbols '(array atom bignum bit bit-vector character compiled-function
2208 complex cons double-float fixnum float
2209 function hash-table integer keyword list long-float
2210 nil null number package pathname random-state
2211 ratio rational readtable sequence short-float
2212 simple-array simple-bit-vector simple-string
2213 simple-vector single-float standard-char
2214 stream string base-char
2215 symbol t vector)
2216 :type-keywords nil
2217 :ansi (if extended-p :extended t))))
2218 (add-loop-path '(hash-key hash-keys) 'loop-hash-table-iteration-path w
2219 :preposition-groups '((:of :in))
2220 :inclusive-permitted nil
2221 :user-data '(:which hash-key))
2222 (add-loop-path '(hash-value hash-values) 'loop-hash-table-iteration-path w
2223 :preposition-groups '((:of :in))
2224 :inclusive-permitted nil
2225 :user-data '(:which hash-value))
2226 (add-loop-path '(symbol symbols) 'loop-package-symbols-iteration-path w
2227 :preposition-groups '((:of :in))
2228 :inclusive-permitted nil
2229 :user-data '(:symbol-types (:internal :external :inherited)))
2230 (add-loop-path '(external-symbol external-symbols) 'loop-package-symbols-iteration-path w
2231 :preposition-groups '((:of :in))
2232 :inclusive-permitted nil
2233 :user-data '(:symbol-types (:external)))
2234 (add-loop-path '(present-symbol present-symbols) 'loop-package-symbols-iteration-path w
2235 :preposition-groups '((:of :in))
2236 :inclusive-permitted nil
2237 :user-data '(:symbol-types (:internal :external)))
2238 w))
2239
2240
2241 (defparameter *loop-ansi-universe*
2242 (make-ansi-loop-universe nil))
2243
2244
2245 (defun loop-standard-expansion (keywords-and-forms environment universe)
2246 (if (and keywords-and-forms (symbolp (car keywords-and-forms)))
2247 (loop-translate keywords-and-forms environment universe)
2248 (let ((tag (gensym)))
2249 `(block nil (tagbody ,tag (progn ,@keywords-and-forms) (go ,tag))))))
2250
2251
2252 ;;;INTERFACE: ANSI
2253 (defmacro loop (&environment env &rest keywords-and-forms)
2254 #+Genera (declare (compiler:do-not-record-macroexpansions)
2255 (zwei:indentation . zwei:indent-loop))
2256 (loop-standard-expansion keywords-and-forms env *loop-ansi-universe*))
2257
2258 #+allegro
2259 (defun excl::complex-loop-expander (body env)
2260 (loop-standard-expansion body env *loop-ansi-universe*))

  ViewVC Help
Powered by ViewVC 1.1.5