1 ;;;; the LOOP iteration macro
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This code was modified by William Harold Newman beginning
7 ;;;; 19981106, originally to conform to the new SBCL bootstrap package
8 ;;;; system and then subsequently to address other cross-compiling
9 ;;;; bootstrap issues, SBCLification (e.g. DECLARE used to check
10 ;;;; argument types), and other maintenance. Whether or not it then
11 ;;;; supported all the environments implied by the reader conditionals
12 ;;;; in the source code (e.g. #!+CLOE-RUNTIME) before that
13 ;;;; modification, it sure doesn't now. It might perhaps, by blind
14 ;;;; luck, be appropriate for some other CMU-CL-derived system, but
15 ;;;; really it only attempts to be appropriate for SBCL.
17 ;;;; This software is derived from software originally released by the
18 ;;;; Massachusetts Institute of Technology and Symbolics, Inc. Copyright and
19 ;;;; release statements follow. Later modifications to the software are in
20 ;;;; the public domain and are provided with absolutely no warranty. See the
21 ;;;; COPYING and CREDITS files for more information.
23 ;;;; Portions of LOOP are Copyright (c) 1986 by the Massachusetts Institute
24 ;;;; of Technology. All Rights Reserved.
26 ;;;; Permission to use, copy, modify and distribute this software and its
27 ;;;; documentation for any purpose and without fee is hereby granted,
28 ;;;; provided that the M.I.T. copyright notice appear in all copies and that
29 ;;;; both that copyright notice and this permission notice appear in
30 ;;;; supporting documentation. The names "M.I.T." and "Massachusetts
31 ;;;; Institute of Technology" may not be used in advertising or publicity
32 ;;;; pertaining to distribution of the software without specific, written
33 ;;;; prior permission. Notice must be given in supporting documentation that
34 ;;;; copying distribution is by permission of M.I.T. M.I.T. makes no
35 ;;;; representations about the suitability of this software for any purpose.
36 ;;;; It is provided "as is" without express or implied warranty.
38 ;;;; Massachusetts Institute of Technology
39 ;;;; 77 Massachusetts Avenue
40 ;;;; Cambridge, Massachusetts 02139
41 ;;;; United States of America
44 ;;;; Portions of LOOP are Copyright (c) 1989, 1990, 1991, 1992 by Symbolics,
45 ;;;; Inc. All Rights Reserved.
47 ;;;; Permission to use, copy, modify and distribute this software and its
48 ;;;; documentation for any purpose and without fee is hereby granted,
49 ;;;; provided that the Symbolics copyright notice appear in all copies and
50 ;;;; that both that copyright notice and this permission notice appear in
51 ;;;; supporting documentation. The name "Symbolics" may not be used in
52 ;;;; advertising or publicity pertaining to distribution of the software
53 ;;;; without specific, written prior permission. Notice must be given in
54 ;;;; supporting documentation that copying distribution is by permission of
55 ;;;; Symbolics. Symbolics makes no representations about the suitability of
56 ;;;; this software for any purpose. It is provided "as is" without express
57 ;;;; or implied warranty.
59 ;;;; Symbolics, CLOE Runtime, and Minima are trademarks, and CLOE, Genera,
60 ;;;; and Zetalisp are registered trademarks of Symbolics, Inc.
63 ;;;; 8 New England Executive Park, East
64 ;;;; Burlington, Massachusetts 01803
65 ;;;; United States of America
68 (in-package "SB!LOOP")
70 ;;;; The design of this LOOP is intended to permit, using mostly the same
71 ;;;; kernel of code, up to three different "loop" macros:
73 ;;;; (1) The unextended, unextensible ANSI standard LOOP;
75 ;;;; (2) A clean "superset" extension of the ANSI LOOP which provides
76 ;;;; functionality similar to that of the old LOOP, but "in the style of"
77 ;;;; the ANSI LOOP. For instance, user-definable iteration paths, with a
78 ;;;; somewhat cleaned-up interface.
80 ;;;; (3) Extensions provided in another file which can make this LOOP
81 ;;;; kernel behave largely compatibly with the Genera-vintage LOOP macro,
82 ;;;; with only a small addition of code (instead of two whole, separate,
85 ;;;; Each of the above three LOOP variations can coexist in the same LISP
88 ;;;; KLUDGE: In SBCL, we only really use variant (1), and any generality
89 ;;;; for the other variants is wasted. -- WHN 20000121
91 ;;;; list collection macrology
93 (sb!int
:defmacro-mundanely with-loop-list-collection-head
94 ((head-var tail-var
&optional user-head-var
) &body body
)
95 (let ((l (and user-head-var
(list (list user-head-var nil
)))))
96 `(let* ((,head-var
(list nil
)) (,tail-var
,head-var
) ,@l
)
99 (sb!int
:defmacro-mundanely loop-collect-rplacd
100 (&environment env
(head-var tail-var
&optional user-head-var
) form
)
101 (setq form
(sb!int
:%macroexpand form env
))
102 (flet ((cdr-wrap (form n
)
104 (do () ((<= n
4) (setq form
`(,(case n
110 (setq form
`(cddddr ,form
) n
(- n
4)))))
111 (let ((tail-form form
) (ncdrs nil
))
112 ;; Determine whether the form being constructed is a list of known
115 (cond ((eq (car form
) 'list
)
116 (setq ncdrs
(1- (length (cdr form
)))))
117 ((member (car form
) '(list* cons
))
118 (when (and (cddr form
) (member (car (last form
)) '(nil 'nil
)))
119 (setq ncdrs
(- (length (cdr form
)) 2))))))
122 `(when (setf (cdr ,tail-var
) ,tail-form
)
123 (setq ,tail-var
(last (cdr ,tail-var
)))))
124 ((< ncdrs
0) (return-from loop-collect-rplacd nil
))
126 ;; @@@@ Here we have a choice of two idioms:
127 ;; (RPLACD TAIL (SETQ TAIL TAIL-FORM))
128 ;; (SETQ TAIL (SETF (CDR TAIL) TAIL-FORM)).
129 ;; Genera and most others I have seen do better with the
131 `(rplacd ,tail-var
(setq ,tail-var
,tail-form
)))
132 (t `(setq ,tail-var
,(cdr-wrap `(setf (cdr ,tail-var
)
135 ;; If not using locatives or something similar to update the
136 ;; user's head variable, we've got to set it... It's harmless
137 ;; to repeatedly set it unconditionally, and probably faster
142 (setq ,user-head-var
(cdr ,head-var
)))))
145 (sb!int
:defmacro-mundanely loop-collect-answer
(head-var
146 &optional user-head-var
)
150 ;;;; maximization technology
153 The basic idea of all this minimax randomness here is that we have to
154 have constructed all uses of maximize and minimize to a particular
155 "destination" before we can decide how to code them. The goal is to not
156 have to have any kinds of flags
, by knowing both that
(1) the type is
157 something which we can provide an initial minimum or maximum value for
158 and
(2) know that a MAXIMIZE and MINIMIZE are not being combined.
160 SO
, we have a datastructure which we annotate with all sorts of things
,
161 incrementally updating it as we generate loop body code
, and then use
162 a wrapper and internal macros to do the coding when the loop has been
166 (defstruct (loop-minimax
167 (:constructor make-loop-minimax-internal
)
177 (defvar *loop-minimax-type-infinities-alist
*
178 ;; FIXME: Now that SBCL supports floating point infinities again, we
179 ;; should have floating point infinities here, as cmucl-2.4.8 did.
180 '((fixnum most-positive-fixnum most-negative-fixnum
)))
182 (defun make-loop-minimax (answer-variable type
)
183 (let ((infinity-data (cdr (assoc type
184 *loop-minimax-type-infinities-alist
*
185 :test
#'sb
!xc
:subtypep
))))
186 (make-loop-minimax-internal
187 :answer-variable answer-variable
189 :temp-variable
(gensym "LOOP-MAXMIN-TEMP-")
190 :flag-variable
(and (not infinity-data
)
191 (gensym "LOOP-MAXMIN-FLAG-"))
193 :infinity-data infinity-data
)))
195 (defun loop-note-minimax-operation (operation minimax
)
196 (pushnew (the symbol operation
) (loop-minimax-operations minimax
))
197 (when (and (cdr (loop-minimax-operations minimax
))
198 (not (loop-minimax-flag-variable minimax
)))
199 (setf (loop-minimax-flag-variable minimax
)
200 (gensym "LOOP-MAXMIN-FLAG-")))
203 (sb!int
:defmacro-mundanely with-minimax-value
(lm &body body
)
204 (let ((init (loop-typed-init (loop-minimax-type lm
)))
205 (which (car (loop-minimax-operations lm
)))
206 (infinity-data (loop-minimax-infinity-data lm
))
207 (answer-var (loop-minimax-answer-variable lm
))
208 (temp-var (loop-minimax-temp-variable lm
))
209 (flag-var (loop-minimax-flag-variable lm
))
210 (type (loop-minimax-type lm
)))
212 `(let ((,answer-var
,init
) (,temp-var
,init
) (,flag-var nil
))
213 (declare (type ,type
,answer-var
,temp-var
))
215 `(let ((,answer-var
,(if (eq which
'min
)
216 (first infinity-data
)
217 (second infinity-data
)))
219 (declare (type ,type
,answer-var
,temp-var
))
222 (sb!int
:defmacro-mundanely loop-accumulate-minimax-value
(lm operation form
)
223 (let* ((answer-var (loop-minimax-answer-variable lm
))
224 (temp-var (loop-minimax-temp-variable lm
))
225 (flag-var (loop-minimax-flag-variable lm
))
226 (test `(,(ecase operation
229 ,temp-var
,answer-var
)))
231 (setq ,temp-var
,form
)
232 (when ,(if flag-var
`(or (not ,flag-var
) ,test
) test
)
233 (setq ,@(and flag-var
`(,flag-var t
))
234 ,answer-var
,temp-var
)))))
236 ;;;; LOOP keyword tables
239 LOOP keyword tables are hash tables string keys and a test of EQUAL.
241 The actual descriptive
/dispatch structure used by LOOP is called a
"loop
242 universe" contains a few tables and parameterizations. The basic idea is
243 that we can provide a non-extensible ANSI-compatible loop environment
,
244 an extensible ANSI-superset loop environment
, and
(for such environments
245 as CLOE
) one which is
"sufficiently close" to the old Genera-vintage
246 LOOP for use by old user programs without requiring all of the old LOOP
252 ;;; Compare two "tokens". The first is the frob out of *LOOP-SOURCE-CODE*,
253 ;;; the second a symbol to check against.
254 (defun loop-tequal (x1 x2
)
255 (and (symbolp x1
) (string= x1 x2
)))
257 (defun loop-tassoc (kwd alist
)
258 (and (symbolp kwd
) (assoc kwd alist
:test
#'string
=)))
260 (defun loop-tmember (kwd list
)
261 (and (symbolp kwd
) (member kwd list
:test
#'string
=)))
263 (defun loop-lookup-keyword (loop-token table
)
264 (and (symbolp loop-token
)
265 (values (gethash (symbol-name loop-token
) table
))))
267 (sb!int
:defmacro-mundanely loop-store-table-data
(symbol table datum
)
268 `(setf (gethash (symbol-name ,symbol
) ,table
) ,datum
))
270 (defstruct (loop-universe
273 keywords
; hash table, value = (fn-name . extra-data)
274 iteration-keywords
; hash table, value = (fn-name . extra-data)
275 for-keywords
; hash table, value = (fn-name . extra-data)
276 path-keywords
; hash table, value = (fn-name . extra-data)
277 type-symbols
; hash table of type SYMBOLS, test EQ,
278 ; value = CL type specifier
279 type-keywords
) ; hash table of type STRINGS, test EQUAL,
280 ; value = CL type spec
281 (sb!int
:def
!method print-object
((u loop-universe
) stream
)
282 (print-unreadable-object (u stream
:type t
:identity t
)))
284 ;;; This is the "current" loop context in use when we are expanding a
285 ;;; loop. It gets bound on each invocation of LOOP.
286 (defvar *loop-universe
*)
288 (defun make-standard-loop-universe (&key keywords for-keywords
289 iteration-keywords path-keywords
290 type-keywords type-symbols
)
291 (flet ((maketable (entries)
292 (let* ((size (length entries
))
293 (ht (make-hash-table :size
(if (< size
10) 10 size
)
296 (setf (gethash (symbol-name (car x
)) ht
) (cadr x
)))
299 :keywords
(maketable keywords
)
300 :for-keywords
(maketable for-keywords
)
301 :iteration-keywords
(maketable iteration-keywords
)
302 :path-keywords
(maketable path-keywords
)
303 :type-keywords
(maketable type-keywords
)
304 :type-symbols
(let* ((size (length type-symbols
))
305 (ht (make-hash-table :size
(if (< size
10) 10 size
)
307 (dolist (x type-symbols
)
309 (setf (gethash x ht
) x
)
310 (setf (gethash (car x
) ht
) (cadr x
))))
313 ;;;; SETQ hackery, including destructuring ("DESETQ")
315 (defun loop-make-psetq (frobs)
319 (if (null (cddr frobs
)) (cadr frobs
)
320 `(prog1 ,(cadr frobs
)
321 ,(loop-make-psetq (cddr frobs
))))))))
323 (defun loop-make-desetq (var-val-pairs)
324 (if (null var-val-pairs
)
326 (cons 'loop-really-desetq var-val-pairs
)))
328 (defvar *loop-desetq-temporary
*
329 (make-symbol "LOOP-DESETQ-TEMP"))
331 (sb!int
:defmacro-mundanely loop-really-desetq
(&environment env
333 (labels ((find-non-null (var)
334 ;; See whether there's any non-null thing here. Recurse
335 ;; if the list element is itself a list.
336 (do ((tail var
)) ((not (consp tail
)) tail
)
337 (when (find-non-null (pop tail
)) (return t
))))
338 (loop-desetq-internal (var val
&optional temp
)
339 ;; returns a list of actions to be performed
343 ;; Don't lose possible side effects.
344 (if (eq (car val
) 'prog1
)
345 ;; These can come from PSETQ or DESETQ below.
346 ;; Throw away the value, keep the side effects.
347 ;; Special case is for handling an expanded POP.
350 (or (not (eq (car x
) 'car
))
351 (not (symbolp (cadr x
)))
352 (not (symbolp (setq x
(sb!int
:%macroexpand x env
)))))
357 (let* ((car (car var
))
359 (car-non-null (find-non-null car
))
360 (cdr-non-null (find-non-null cdr
)))
361 (when (or car-non-null cdr-non-null
)
364 (temp (or temp
*loop-desetq-temporary
*))
365 (body `(,@(loop-desetq-internal car
367 (setq ,temp
(cdr ,temp
))
368 ,@(loop-desetq-internal cdr
372 `(,@(unless (eq temp val
)
373 `((setq ,temp
,val
)))
375 `((let ((,temp
,val
))
378 (loop-desetq-internal car
`(car ,val
) temp
)))))
381 `((setq ,var
,val
)))))))
383 ((null var-val-pairs
)
384 (if (null (cdr actions
)) (car actions
) `(progn ,@(nreverse actions
))))
385 (setq actions
(revappend
386 (loop-desetq-internal (pop var-val-pairs
)
390 ;;;; LOOP-local variables
392 ;;; This is the "current" pointer into the LOOP source code.
393 (defvar *loop-source-code
*)
395 ;;; This is the pointer to the original, for things like NAMED that
396 ;;; insist on being in a particular position
397 (defvar *loop-original-source-code
*)
399 ;;; This is *loop-source-code* as of the "last" clause. It is used
400 ;;; primarily for generating error messages (see loop-error, loop-warn).
401 (defvar *loop-source-context
*)
403 ;;; list of names for the LOOP, supplied by the NAMED clause
404 (defvar *loop-names
*)
406 ;;; The macroexpansion environment given to the macro.
407 (defvar *loop-macro-environment
*)
409 ;;; This holds variable names specified with the USING clause.
410 ;;; See LOOP-NAMED-VAR.
411 (defvar *loop-named-vars
*)
413 ;;; LETlist-like list being accumulated for current group of bindings.
416 ;;; List of declarations being accumulated in parallel with
418 (defvar *loop-declarations
*)
420 ;;; Declarations for destructuring bindings
421 (defvar *loop-desetq-declarations
*)
423 ;;; This is used by LOOP for destructuring binding, if it is doing
424 ;;; that itself. See LOOP-MAKE-VAR.
425 (defvar *loop-desetq
*)
427 ;;; list of wrapping forms, innermost first, which go immediately
428 ;;; inside the current set of parallel bindings being accumulated in
429 ;;; *LOOP-VARS*. The wrappers are appended onto a body. E.g., this
430 ;;; list could conceivably have as its value
431 ;;; ((WITH-OPEN-FILE (G0001 G0002 ...))),
432 ;;; with G0002 being one of the bindings in *LOOP-VARS* (This is why
433 ;;; the wrappers go inside of the variable bindings).
434 (defvar *loop-wrappers
*)
436 ;;; This accumulates lists of previous values of *LOOP-VARS* and the
437 ;;; other lists above, for each new nesting of bindings. See
439 (defvar *loop-bind-stack
*)
441 ;;; list of prologue forms of the loop, accumulated in reverse order
442 (defvar *loop-prologue
*)
444 (defvar *loop-before-loop
*)
446 (defvar *loop-after-body
*)
448 ;;; This is T if we have emitted any body code, so that iteration
449 ;;; driving clauses can be disallowed. This is not strictly the same
450 ;;; as checking *LOOP-BODY*, because we permit some clauses such as
451 ;;; RETURN to not be considered "real" body (so as to permit the user
452 ;;; to "code" an abnormal return value "in loop").
453 (defvar *loop-emitted-body
*)
455 ;;; list of epilogue forms (supplied by FINALLY generally), accumulated
457 (defvar *loop-epilogue
*)
459 ;;; list of epilogue forms which are supplied after the above "user"
460 ;;; epilogue. "Normal" termination return values are provide by
461 ;;; putting the return form in here. Normally this is done using
462 ;;; LOOP-EMIT-FINAL-VALUE, q.v.
463 (defvar *loop-after-epilogue
*)
465 ;;; the "culprit" responsible for supplying a final value from the
466 ;;; loop. This is so LOOP-DISALLOW-AGGREGATE-BOOLEANS can moan about
467 ;;; disallowed anonymous collections.
468 (defvar *loop-final-value-culprit
*)
470 ;;; If this is true, we are in some branch of a conditional. Some
471 ;;; clauses may be disallowed.
472 (defvar *loop-inside-conditional
*)
474 ;;; If not NIL, this is a temporary bound around the loop for holding
475 ;;; the temporary value for "it" in things like "when (f) collect it".
476 ;;; It may be used as a supertemporary by some other things.
477 (defvar *loop-when-it-var
*)
479 ;;; Sometimes we decide we need to fold together parts of the loop,
480 ;;; but some part of the generated iteration code is different for the
481 ;;; first and remaining iterations. This variable will be the
482 ;;; temporary which is the flag used in the loop to tell whether we
483 ;;; are in the first or remaining iterations.
484 (defvar *loop-never-stepped-var
*)
486 ;;; list of all the value-accumulation descriptor structures in the
487 ;;; loop. See LOOP-GET-COLLECTION-INFO.
488 (defvar *loop-collection-cruft
*) ; for multiple COLLECTs (etc.)
490 ;;;; code analysis stuff
492 (defun loop-constant-fold-if-possible (form &optional expected-type
)
493 (let* ((constantp (sb!xc
:constantp form
))
494 (value (and constantp
(sb!int
:constant-form-value form
))))
495 (when (and constantp expected-type
)
496 (unless (sb!xc
:typep value expected-type
)
497 (loop-warn "~@<The form ~S evaluated to ~S, which was not of ~
498 the anticipated type ~S.~:@>"
499 form value expected-type
)
500 (setq constantp nil value nil
)))
501 (values form constantp value
)))
503 (sb!int
:defmacro-mundanely loop-body
(prologue
508 (unless (= (length before-loop
) (length after-loop
))
509 (error "LOOP-BODY called with non-synched before- and after-loop lists"))
510 ;; All our work is done from these copies, working backwards from the end
511 (let ((rbefore (reverse before-loop
))
512 (rafter (reverse after-loop
)))
513 ;; Go backwards from the ends of before-loop and after-loop
514 ;; merging all the equivalent forms into the body.
517 (not (equal (car rbefore
) (car rafter
)))))
518 (push (pop rbefore
) main-body
)
521 ,@(remove nil prologue
)
522 ,@(nreverse (remove nil rbefore
))
524 ,@(remove nil main-body
)
525 ,@(nreverse (remove nil rafter
))
528 ,@(remove nil epilogue
))))
532 (defun loop-context ()
533 (do ((l *loop-source-context
* (cdr l
)) (new nil
(cons (car l
) new
)))
534 ((eq l
(cdr *loop-source-code
*)) (nreverse new
))))
536 (defun loop-error (format-string &rest format-args
)
537 (error 'sb
!int
:simple-program-error
538 :format-control
"~?~%current LOOP context:~{ ~S~}."
539 :format-arguments
(list format-string format-args
(loop-context))))
541 (defun loop-warn (format-string &rest format-args
)
542 (warn "~?~%current LOOP context:~{ ~S~}."
547 (defun loop-check-data-type (specified-type required-type
548 &optional
(default-type required-type
))
549 (if (null specified-type
)
551 (multiple-value-bind (a b
) (sb!xc
:subtypep specified-type required-type
)
553 (loop-warn "LOOP couldn't verify that ~S is a subtype of the required type ~S."
554 specified-type required-type
))
556 (loop-error "The specified data type ~S is not a subtype of ~S."
557 specified-type required-type
)))
560 (defun subst-gensyms-for-nil (tree)
561 (declare (special *ignores
*))
563 ((null tree
) (car (push (gensym "LOOP-IGNORED-VAR-") *ignores
*)))
565 (t (cons (subst-gensyms-for-nil (car tree
))
566 (subst-gensyms-for-nil (cdr tree
))))))
568 (sb!int
:defmacro-mundanely loop-destructuring-bind
569 (lambda-list arg-list
&rest body
)
570 (let ((*ignores
* nil
))
571 (declare (special *ignores
*))
572 (let ((d-var-lambda-list (subst-gensyms-for-nil lambda-list
)))
573 `(destructuring-bind (&optional
,@d-var-lambda-list
)
575 (declare (ignore ,@*ignores
*))
578 (defun loop-build-destructuring-bindings (crocks forms
)
580 `((loop-destructuring-bind ,(car crocks
) ,(cadr crocks
)
581 ,@(loop-build-destructuring-bindings (cddr crocks
) forms
)))
584 (defun loop-translate (*loop-source-code
*
585 *loop-macro-environment
*
587 (let ((*loop-original-source-code
* *loop-source-code
*)
588 (*loop-source-context
* nil
)
590 (*loop-named-vars
* nil
)
591 (*loop-declarations
* nil
)
592 (*loop-desetq-declarations
* nil
)
594 (*loop-bind-stack
* nil
)
595 (*loop-prologue
* nil
)
596 (*loop-wrappers
* nil
)
597 (*loop-before-loop
* nil
)
599 (*loop-emitted-body
* nil
)
600 (*loop-after-body
* nil
)
601 (*loop-epilogue
* nil
)
602 (*loop-after-epilogue
* nil
)
603 (*loop-final-value-culprit
* nil
)
604 (*loop-inside-conditional
* nil
)
605 (*loop-when-it-var
* nil
)
606 (*loop-never-stepped-var
* nil
)
608 (*loop-collection-cruft
* nil
))
609 (loop-iteration-driver)
611 (let ((answer `(loop-body
612 ,(nreverse *loop-prologue
*)
613 ,(nreverse *loop-before-loop
*)
614 ,(nreverse *loop-body
*)
615 ,(nreverse *loop-after-body
*)
616 ,(nreconc *loop-epilogue
*
617 (nreverse *loop-after-epilogue
*)))))
618 (dolist (entry *loop-bind-stack
*)
619 (destructuring-bind (vars dcls desetq desetq-decls wrappers
) entry
621 (setq answer
(append w
(list answer
))))
622 (when (or vars dcls desetq
)
623 (let ((forms (list answer
)))
625 (push `(declare ,@desetq-decls
) forms
))
626 (setq answer
`(,(if vars
'let
'locally
)
629 ,@(loop-build-destructuring-bindings desetq
632 (setq answer
`(block ,(pop *loop-names
*) ,answer
))
633 (unless *loop-names
* (return nil
)))
636 (defun loop-iteration-driver ()
638 ((null *loop-source-code
*))
639 (let ((keyword (car *loop-source-code
*)) (tem nil
))
640 (cond ((not (symbolp keyword
))
641 (loop-error "~S found where LOOP keyword expected" keyword
))
642 (t (setq *loop-source-context
* *loop-source-code
*)
645 (loop-lookup-keyword keyword
646 (loop-universe-keywords
648 ;; It's a "miscellaneous" toplevel LOOP keyword (DO,
649 ;; COLLECT, NAMED, etc.)
650 (apply (symbol-function (first tem
)) (rest tem
)))
652 (loop-lookup-keyword keyword
653 (loop-universe-iteration-keywords *loop-universe
*)))
654 (loop-hack-iteration tem
))
655 ((loop-tmember keyword
'(and else
))
656 ;; The alternative is to ignore it, i.e. let it go
657 ;; around to the next keyword...
658 (loop-error "secondary clause misplaced at top level in LOOP macro: ~S ~S ~S ..."
660 (car *loop-source-code
*)
661 (cadr *loop-source-code
*)))
662 (t (loop-error "unknown LOOP keyword: ~S" keyword
))))))))
664 (defun loop-pop-source ()
665 (if *loop-source-code
*
666 (pop *loop-source-code
*)
667 (loop-error "LOOP source code ran out when another token was expected.")))
669 (defun loop-get-form ()
670 (if *loop-source-code
*
672 (loop-error "LOOP code ran out where a form was expected.")))
674 (defun loop-get-compound-form ()
675 (let ((form (loop-get-form)))
677 (loop-error "A compound form was expected, but ~S found." form
))
680 (defun loop-get-progn ()
681 (do ((forms (list (loop-get-compound-form))
682 (cons (loop-get-compound-form) forms
))
683 (nextform (car *loop-source-code
*)
684 (car *loop-source-code
*)))
686 (if (null (cdr forms
)) (car forms
) (cons 'progn
(nreverse forms
))))))
688 (defun loop-construct-return (form)
689 `(return-from ,(car *loop-names
*) ,form
))
691 (defun loop-pseudo-body (form)
692 (cond ((or *loop-emitted-body
* *loop-inside-conditional
*)
693 (push form
*loop-body
*))
694 (t (push form
*loop-before-loop
*) (push form
*loop-after-body
*))))
696 (defun loop-emit-body (form)
697 (setq *loop-emitted-body
* t
)
698 (loop-pseudo-body form
))
700 (defun loop-emit-final-value (&optional
(form nil form-supplied-p
))
701 (when form-supplied-p
702 (push (loop-construct-return form
) *loop-after-epilogue
*))
703 (setq *loop-final-value-culprit
* (car *loop-source-context
*)))
705 (defun loop-disallow-conditional (&optional kwd
)
706 (when *loop-inside-conditional
*
707 (loop-error "~:[This LOOP~;The LOOP ~:*~S~] clause is not permitted inside a conditional." kwd
)))
709 (defun loop-disallow-anonymous-collectors ()
710 (when (find-if-not 'loop-collector-name
*loop-collection-cruft
*)
711 (loop-error "This LOOP clause is not permitted with anonymous collectors.")))
713 (defun loop-disallow-aggregate-booleans ()
714 (when (loop-tmember *loop-final-value-culprit
* '(always never thereis
))
715 (loop-error "This anonymous collection LOOP clause is not permitted with aggregate booleans.")))
719 (defun loop-typed-init (data-type &optional step-var-p
)
720 (cond ((null data-type
)
722 ((sb!xc
:subtypep data-type
'number
)
723 (let ((init (if step-var-p
1 0)))
724 (flet ((like (&rest types
)
725 (coerce init
(find-if (lambda (type)
726 (sb!xc
:subtypep data-type type
))
728 (cond ((sb!xc
:subtypep data-type
'float
)
729 (like 'single-float
'double-float
730 'short-float
'long-float
'float
))
731 ((sb!xc
:subtypep data-type
'(complex float
))
732 (like '(complex single-float
)
733 '(complex double-float
)
734 '(complex short-float
)
735 '(complex long-float
)
739 ((sb!xc
:subtypep data-type
'vector
)
740 (let ((ctype (sb!kernel
:specifier-type data-type
)))
741 (when (sb!kernel
:array-type-p ctype
)
742 (let ((etype (sb!kernel
:type-
*-to-t
743 (sb!kernel
:array-type-specialized-element-type ctype
))))
744 (make-array 0 :element-type
(sb!kernel
:type-specifier etype
))))))
745 ((sb!xc
:typep
#\x data-type
)
750 (defun loop-optional-type (&optional variable
)
751 ;; No variable specified implies that no destructuring is permissible.
752 (and *loop-source-code
* ; Don't get confused by NILs..
753 (let ((z (car *loop-source-code
*)))
754 (cond ((loop-tequal z
'of-type
)
755 ;; This is the syntactically unambigous form in that
756 ;; the form of the type specifier does not matter.
757 ;; Also, it is assumed that the type specifier is
758 ;; unambiguously, and without need of translation, a
759 ;; common lisp type specifier or pattern (matching the
760 ;; variable) thereof.
765 ;; This is the (sort of) "old" syntax, even though we
766 ;; didn't used to support all of these type symbols.
767 (let ((type-spec (or (gethash z
768 (loop-universe-type-symbols
770 (gethash (symbol-name z
)
771 (loop-universe-type-keywords
777 ;; This is our sort-of old syntax. But this is only
778 ;; valid for when we are destructuring, so we will be
779 ;; compulsive (should we really be?) and require that
780 ;; we in fact be doing variable destructuring here. We
781 ;; must translate the old keyword pattern typespec
782 ;; into a fully-specified pattern of real type
787 "~S found where a LOOP keyword, LOOP type keyword, or LOOP type pattern expected"
789 (loop-error "~S found where a LOOP keyword or LOOP type keyword expected" z
))
791 (labels ((translate (k v
)
796 (loop-universe-type-symbols
798 (gethash (symbol-name k
)
799 (loop-universe-type-keywords
802 "The destructuring type pattern ~S contains the unrecognized type keyword ~S."
807 "The destructuring type pattern ~S doesn't match the variable pattern ~S."
809 (t (cons (translate (car k
) (car v
))
810 (translate (cdr k
) (cdr v
))))))
814 (cons (replicate typ
(car v
))
815 (replicate typ
(cdr v
))))))
816 (translate z variable
)))))))
820 (defun loop-bind-block ()
821 (when (or *loop-vars
* *loop-declarations
* *loop-wrappers
*
823 (push (list (nreverse *loop-vars
*)
826 *loop-desetq-declarations
*
829 (setq *loop-vars
* nil
830 *loop-declarations
* nil
832 *loop-desetq-declarations
* nil
833 *loop-wrappers
* nil
)))
835 (defun check-var-name (name &optional
(context ""))
836 (labels ((map-name (function name
)
837 (do ((x (pop name
) (pop name
)))
841 (cons (map-name function x
))
842 (symbol (funcall function x
))
844 (loop-error "Bad variable ~s~a" x context
)))
850 (funcall function name
)
853 (loop-error "Bad variable ~s~a" name context
)))))
855 (loop-error "Duplicated variable ~s~a" x context
))
856 (find-in-desetq (name desetqs
)
857 (do* ((desetq desetqs
(cddr desetq
))
858 (var (car desetq
) (car desetq
)))
860 (map-name (lambda (x)
865 (map-name (lambda (x) (check-var-name x context
)) name
))
866 ((assoc name
*loop-vars
*)
868 ((find-in-desetq name
*loop-desetq
*))
870 (do ((entry *loop-bind-stack
* (cdr entry
)))
873 ((null entry
) (return nil
))
874 ((assoc name
(caar entry
) :test
#'eq
)
877 (find-in-desetq name
(caddar entry
)))))))))
879 (defun loop-make-var (name initialization dtype
&optional step-var-p
)
881 (setq name
(gensym "LOOP-IGNORE-"))
882 (push (list name
(or initialization
(loop-typed-init dtype step-var-p
)))
884 (push `(ignore ,name
) *loop-declarations
*)
885 (loop-declare-var name dtype
))
887 (check-var-name name
)
888 (loop-declare-var name dtype
:step-var-p step-var-p
889 :initialization initialization
)
890 ;; We use ASSOC on this list to check for duplications (above),
891 ;; so don't optimize out this list:
892 (push (list name
(or initialization
(loop-typed-init dtype step-var-p
)))
895 (check-var-name name
)
896 (let ((newvar (gensym "LOOP-DESTRUCTURE-")))
897 (loop-declare-var name dtype
:desetq t
)
898 (push (list newvar initialization
) *loop-vars
*)
899 ;; *LOOP-DESETQ* gathered in reverse order.
901 (list* name newvar
*loop-desetq
*))))
903 (let ((tcar nil
) (tcdr nil
))
904 (if (atom dtype
) (setq tcar
(setq tcdr dtype
))
905 (setq tcar
(car dtype
) tcdr
(cdr dtype
)))
906 (loop-make-var (car name
) nil tcar
)
908 (loop-make-var (cdr name
) nil tcdr
)))))
911 (defun loop-declare-var (name dtype
&key step-var-p initialization
913 (cond ((or (null name
) (null dtype
) (eq dtype t
)) nil
)
915 (unless (or (sb!xc
:subtypep t dtype
)
916 (and (eq (find-package :cl
) (symbol-package name
))
917 (eq :special
(sb!int
:info
:variable
:kind name
))))
918 (let ((dtype `(type ,(if initialization
920 (let ((init (loop-typed-init dtype step-var-p
)))
921 (if (sb!xc
:typep init dtype
)
923 `(or ,(type-of init
) ,dtype
))))
926 (push dtype
*loop-desetq-declarations
*)
927 (push dtype
*loop-declarations
*)))))
930 (loop-declare-var (car name
) (car dtype
)
932 (loop-declare-var (cdr name
) (cdr dtype
)
934 (t (loop-declare-var (car name
) dtype
936 (loop-declare-var (cdr name
) dtype
938 (t (error "invalid LOOP variable passed in: ~S" name
))))
940 (defun loop-maybe-bind-form (form data-type
)
943 (loop-make-var (gensym "LOOP-BIND-") form data-type
)))
945 (defun loop-do-if (for negatep
)
946 (let ((form (loop-get-form))
947 (*loop-inside-conditional
* t
)
950 (flet ((get-clause (for)
951 (do ((body nil
)) (nil)
952 (let ((key (car *loop-source-code
*)) (*loop-body
* nil
) data
)
953 (cond ((not (symbolp key
))
955 "~S found where keyword expected getting LOOP clause after ~S"
957 (t (setq *loop-source-context
* *loop-source-code
*)
959 (when (and (loop-tequal (car *loop-source-code
*) 'it
)
961 (setq *loop-source-code
*
965 (cdr *loop-source-code
*))))
966 (cond ((or (not (setq data
(loop-lookup-keyword
967 key
(loop-universe-keywords *loop-universe
*))))
968 (progn (apply (symbol-function (car data
))
972 "~S does not introduce a LOOP clause that can follow ~S."
974 (t (setq body
(nreconc *loop-body
* body
)))))))
975 (setq first-clause-p nil
)
976 (if (loop-tequal (car *loop-source-code
*) :and
)
978 (return (if (cdr body
)
979 `(progn ,@(nreverse body
))
981 (let ((then (get-clause for
))
982 (else (when (loop-tequal (car *loop-source-code
*) :else
)
984 (list (get-clause :else
)))))
985 (when (loop-tequal (car *loop-source-code
*) :end
)
987 (when it-p
(setq form
`(setq ,it-p
,form
)))
989 `(if ,(if negatep
`(not ,form
) form
)
993 (defun loop-do-initially ()
994 (loop-disallow-conditional :initially
)
995 (push (loop-get-progn) *loop-prologue
*))
997 (defun loop-do-finally ()
998 (loop-disallow-conditional :finally
)
999 (push (loop-get-progn) *loop-epilogue
*))
1001 (defun loop-do-do ()
1002 (loop-emit-body (loop-get-progn)))
1004 (defun loop-do-named ()
1005 (let ((name (loop-pop-source)))
1006 (unless (symbolp name
)
1007 (loop-error "~S is an invalid name for your LOOP" name
))
1008 (when (or *loop-before-loop
* *loop-body
* *loop-after-epilogue
* *loop-inside-conditional
*)
1009 (loop-error "The NAMED ~S clause occurs too late." name
))
1011 (loop-error "You may only use one NAMED clause in your loop: NAMED ~S ... NAMED ~S."
1012 (car *loop-names
*) name
))
1013 (setq *loop-names
* (list name
))))
1015 (defun loop-do-return ()
1016 (loop-emit-body (loop-construct-return (loop-get-form))))
1018 ;;;; value accumulation: LIST
1020 (defstruct (loop-collector
1029 (data nil
)) ;collector-specific data
1031 (sb!int
:defmacro-mundanely with-sum-count
(lc &body body
)
1032 (let ((type (loop-collector-dtype lc
))
1033 (temp-var (car (loop-collector-tempvars lc
))))
1034 `(let ((,temp-var
,(loop-typed-init type
)))
1035 (declare (type ,type
,temp-var
))
1038 (defun loop-get-collection-info (collector class default-type
)
1039 (let ((form (loop-get-form))
1040 (name (when (loop-tequal (car *loop-source-code
*) 'into
)
1042 (loop-pop-source))))
1043 (when (not (symbolp name
))
1044 (loop-error "The value accumulation recipient name, ~S, is not a symbol." name
))
1046 (loop-disallow-aggregate-booleans))
1047 (let* ((specified-type (loop-optional-type))
1048 (dtype (or specified-type default-type
))
1049 (cruft (find (the symbol name
) *loop-collection-cruft
*
1050 :key
#'loop-collector-name
)))
1052 (check-var-name name
" in INTO clause")
1053 (push (setq cruft
(make-loop-collector
1054 :name name
:class class
1055 :history
(list collector
)
1056 :specified-type specified-type
1058 *loop-collection-cruft
*))
1059 (t (unless (eq (loop-collector-class cruft
) class
)
1061 "incompatible kinds of LOOP value accumulation specified for collecting~@
1062 ~:[as the value of the LOOP~;~:*INTO ~S~]: ~S and ~S"
1063 name
(car (loop-collector-history cruft
)) collector
))
1064 (cond ((equal dtype
(loop-collector-dtype cruft
)))
1065 ((and (null specified-type
)
1066 (null (loop-collector-specified-type cruft
)))
1067 ;; Unionize types only for default types, most
1068 ;; likely, SUM and COUNT which have number and
1069 ;; fixnum respectively.
1070 (setf (loop-collector-dtype cruft
)
1071 (sb!kernel
:type-specifier
1072 (sb!kernel
:type-union
1073 (sb!kernel
:specifier-type dtype
)
1074 (sb!kernel
:specifier-type
(loop-collector-dtype cruft
))))))
1077 "unequal datatypes specified in different LOOP value accumulations~@
1079 name dtype
(loop-collector-dtype cruft
))
1080 (when (eq (loop-collector-dtype cruft
) t
)
1081 (setf (loop-collector-dtype cruft
) dtype
))))
1082 (push collector
(loop-collector-history cruft
))))
1083 (values cruft form
))))
1085 (defun loop-list-collection (specifically) ; NCONC, LIST, or APPEND
1086 (multiple-value-bind (lc form
)
1087 (loop-get-collection-info specifically
'list
'list
)
1088 (let ((tempvars (loop-collector-tempvars lc
)))
1090 (setf (loop-collector-tempvars lc
)
1091 (setq tempvars
(list* (gensym "LOOP-LIST-HEAD-")
1092 (gensym "LOOP-LIST-TAIL-")
1093 (and (loop-collector-name lc
)
1094 (list (loop-collector-name lc
))))))
1095 (push `(with-loop-list-collection-head ,tempvars
) *loop-wrappers
*)
1096 (unless (loop-collector-name lc
)
1097 (loop-emit-final-value `(loop-collect-answer ,(car tempvars
)
1098 ,@(cddr tempvars
)))))
1100 (list (setq form
`(list ,form
)))
1102 (append (unless (and (consp form
) (eq (car form
) 'list
))
1103 (setq form
`(copy-list ,form
)))))
1104 (loop-emit-body `(loop-collect-rplacd ,tempvars
,form
)))))
1106 ;;;; value accumulation: MAX, MIN, SUM, COUNT
1108 (defun loop-sum-collection (specifically required-type default-type
);SUM, COUNT
1109 (multiple-value-bind (lc form
)
1110 (loop-get-collection-info specifically
'sum default-type
)
1111 (loop-check-data-type (loop-collector-dtype lc
) required-type
)
1112 (let ((tempvars (loop-collector-tempvars lc
)))
1114 (setf (loop-collector-tempvars lc
)
1115 (setq tempvars
(list (or (loop-collector-name lc
)
1116 (gensym "LOOP-SUM-")))))
1117 (unless (loop-collector-name lc
)
1118 (loop-emit-final-value (car (loop-collector-tempvars lc
))))
1119 (push `(with-sum-count ,lc
) *loop-wrappers
*))
1121 (if (eq specifically
'count
)
1123 (setq ,(car tempvars
)
1124 (1+ ,(car tempvars
))))
1125 `(setq ,(car tempvars
)
1129 (defun loop-maxmin-collection (specifically)
1130 (multiple-value-bind (lc form
)
1131 (loop-get-collection-info specifically
'maxmin
'real
)
1132 (loop-check-data-type (loop-collector-dtype lc
) 'real
)
1133 (let ((data (loop-collector-data lc
)))
1135 (setf (loop-collector-data lc
)
1136 (setq data
(make-loop-minimax
1137 (or (loop-collector-name lc
)
1138 (gensym "LOOP-MAXMIN-"))
1139 (loop-collector-dtype lc
))))
1140 (unless (loop-collector-name lc
)
1141 (loop-emit-final-value (loop-minimax-answer-variable data
)))
1142 (push `(with-minimax-value ,data
) *loop-wrappers
*))
1143 (loop-note-minimax-operation specifically data
)
1144 (loop-emit-body `(loop-accumulate-minimax-value ,data
1148 ;;;; value accumulation: aggregate booleans
1150 ;;; handling the ALWAYS and NEVER loop keywords
1152 ;;; Under ANSI these are not permitted to appear under conditionalization.
1153 (defun loop-do-always (restrictive negate
)
1154 (let ((form (loop-get-form)))
1155 (when restrictive
(loop-disallow-conditional))
1156 (loop-disallow-anonymous-collectors)
1157 (loop-emit-body `(,(if negate
'when
'unless
) ,form
1158 ,(loop-construct-return nil
)))
1159 (loop-emit-final-value t
)))
1161 ;;; handling the THEREIS loop keyword
1163 ;;; Under ANSI this is not permitted to appear under conditionalization.
1164 (defun loop-do-thereis (restrictive)
1165 (when restrictive
(loop-disallow-conditional))
1166 (loop-disallow-anonymous-collectors)
1167 (loop-emit-final-value)
1168 (loop-emit-body `(when (setq ,(loop-when-it-var) ,(loop-get-form))
1169 ,(loop-construct-return *loop-when-it-var
*))))
1171 (defun loop-do-while (negate kwd
&aux
(form (loop-get-form)))
1172 (loop-disallow-conditional kwd
)
1173 (loop-pseudo-body `(,(if negate
'when
'unless
) ,form
(go end-loop
))))
1175 (defun loop-do-repeat ()
1176 (loop-disallow-conditional :repeat
)
1177 (let* ((form (loop-get-form))
1178 (type (if (realp form
)
1179 `(mod ,(1+ (ceiling form
)))
1181 (let ((var (loop-make-var (gensym "LOOP-REPEAT-") `(ceiling ,form
) type
)))
1182 (push `(if (<= ,var
0) (go end-loop
) (decf ,var
)) *loop-before-loop
*)
1183 (push `(if (<= ,var
0) (go end-loop
) (decf ,var
)) *loop-after-body
*)
1184 ;; FIXME: What should
1185 ;; (loop count t into a
1188 ;; finally (return (list a b)))
1189 ;; return: (3 3) or (4 3)? PUSHes above are for the former
1190 ;; variant, L-P-B below for the latter.
1191 #+nil
(loop-pseudo-body `(when (minusp (decf ,var
)) (go end-loop
))))))
1193 (defun loop-do-with ()
1194 (loop-disallow-conditional :with
)
1195 (do ((var) (val) (dtype))
1197 (setq var
(loop-pop-source)
1198 dtype
(loop-optional-type var
)
1199 val
(cond ((loop-tequal (car *loop-source-code
*) :=)
1203 (loop-make-var var val dtype
)
1204 (if (loop-tequal (car *loop-source-code
*) :and
)
1206 (return (loop-bind-block)))))
1208 ;;;; the iteration driver
1210 (defun loop-hack-iteration (entry)
1211 (flet ((make-endtest (list-of-forms)
1212 (cond ((null list-of-forms
) nil
)
1213 ((member t list-of-forms
) '(go end-loop
))
1214 (t `(when ,(if (null (cdr (setq list-of-forms
1215 (nreverse list-of-forms
))))
1217 (cons 'or list-of-forms
))
1219 (do ((pre-step-tests nil
)
1221 (post-step-tests nil
)
1223 (pre-loop-pre-step-tests nil
)
1224 (pre-loop-steps nil
)
1225 (pre-loop-post-step-tests nil
)
1226 (pre-loop-pseudo-steps nil
)
1229 ;; Note that we collect endtests in reverse order, but steps in correct
1230 ;; order. MAKE-ENDTEST does the nreverse for us.
1231 (setq tem
(setq data
1232 (apply (symbol-function (first entry
)) (rest entry
))))
1233 (and (car tem
) (push (car tem
) pre-step-tests
))
1234 (setq steps
(nconc steps
(copy-list (car (setq tem
(cdr tem
))))))
1235 (and (car (setq tem
(cdr tem
))) (push (car tem
) post-step-tests
))
1237 (nconc pseudo-steps
(copy-list (car (setq tem
(cdr tem
))))))
1238 (setq tem
(cdr tem
))
1239 (when *loop-emitted-body
*
1240 (loop-error "iteration in LOOP follows body code"))
1241 (unless tem
(setq tem data
))
1242 (when (car tem
) (push (car tem
) pre-loop-pre-step-tests
))
1243 ;; FIXME: This (SETF FOO (NCONC FOO BAR)) idiom appears often enough
1244 ;; that it might be worth making it into an NCONCF macro.
1245 (setq pre-loop-steps
1246 (nconc pre-loop-steps
(copy-list (car (setq tem
(cdr tem
))))))
1247 (when (car (setq tem
(cdr tem
)))
1248 (push (car tem
) pre-loop-post-step-tests
))
1249 (setq pre-loop-pseudo-steps
1250 (nconc pre-loop-pseudo-steps
(copy-list (cadr tem
))))
1251 (unless (loop-tequal (car *loop-source-code
*) :and
)
1252 (setq *loop-before-loop
*
1253 (list* (loop-make-desetq pre-loop-pseudo-steps
)
1254 (make-endtest pre-loop-post-step-tests
)
1255 (loop-make-psetq pre-loop-steps
)
1256 (make-endtest pre-loop-pre-step-tests
)
1257 *loop-before-loop
*))
1258 (setq *loop-after-body
*
1259 (list* (loop-make-desetq pseudo-steps
)
1260 (make-endtest post-step-tests
)
1261 (loop-make-psetq steps
)
1262 (make-endtest pre-step-tests
)
1266 (loop-pop-source)))) ; Flush the "AND".
1268 ;;;; main iteration drivers
1270 ;;; FOR variable keyword ..args..
1271 (defun loop-do-for ()
1272 (let* ((var (loop-pop-source))
1273 (data-type (loop-optional-type var
))
1274 (keyword (loop-pop-source))
1277 (setq first-arg
(loop-get-form))
1278 (unless (and (symbolp keyword
)
1279 (setq tem
(loop-lookup-keyword
1281 (loop-universe-for-keywords *loop-universe
*))))
1282 (loop-error "~S is an unknown keyword in FOR or AS clause in LOOP."
1284 (apply (car tem
) var first-arg data-type
(cdr tem
))))
1286 (defun loop-when-it-var ()
1287 (or *loop-when-it-var
*
1288 (setq *loop-when-it-var
*
1289 (loop-make-var (gensym "LOOP-IT-") nil nil
))))
1291 ;;;; various FOR/AS subdispatches
1293 ;;; ANSI "FOR x = y [THEN z]" is sort of like the old Genera one when
1294 ;;; the THEN is omitted (other than being more stringent in its
1295 ;;; placement), and like the old "FOR x FIRST y THEN z" when the THEN
1296 ;;; is present. I.e., the first initialization occurs in the loop body
1297 ;;; (first-step), not in the variable binding phase.
1298 (defun loop-ansi-for-equals (var val data-type
)
1299 (loop-make-var var nil data-type
)
1300 (cond ((loop-tequal (car *loop-source-code
*) :then
)
1301 ;; Then we are the same as "FOR x FIRST y THEN z".
1303 `(() (,var
,(loop-get-form)) () ()
1304 () (,var
,val
) () ()))
1305 (t ;; We are the same as "FOR x = y".
1306 `(() (,var
,val
) () ()))))
1308 (defun loop-for-across (var val data-type
)
1309 (loop-make-var var nil data-type
)
1310 (let ((vector-var (gensym "LOOP-ACROSS-VECTOR-"))
1311 (index-var (gensym "LOOP-ACROSS-INDEX-")))
1312 (multiple-value-bind (vector-form constantp vector-value
)
1313 (loop-constant-fold-if-possible val
'vector
)
1315 vector-var vector-form
1316 (if (and (consp vector-form
) (eq (car vector-form
) 'the
))
1319 (loop-make-var index-var
0 'fixnum
)
1321 (length-form (cond ((not constantp
)
1322 (let ((v (gensym "LOOP-ACROSS-LIMIT-")))
1323 (push `(setq ,v
(length ,vector-var
))
1325 (loop-make-var v
0 'fixnum
)))
1326 (t (setq length
(length vector-value
)))))
1327 (first-test `(>= ,index-var
,length-form
))
1328 (other-test first-test
)
1329 (step `(,var
(aref ,vector-var
,index-var
)))
1330 (pstep `(,index-var
(1+ ,index-var
))))
1331 (declare (fixnum length
))
1333 (setq first-test
(= length
0))
1335 (setq other-test t
)))
1336 `(,other-test
,step
() ,pstep
1337 ,@(and (not (eq first-test other-test
))
1338 `(,first-test
,step
() ,pstep
)))))))
1342 (defun loop-list-step (listvar)
1343 ;; We are not equipped to analyze whether 'FOO is the same as #'FOO
1344 ;; here in any sensible fashion, so let's give an obnoxious warning
1345 ;; whenever 'FOO is used as the stepping function.
1347 ;; While a Discerning Compiler may deal intelligently with
1348 ;; (FUNCALL 'FOO ...), not recognizing FOO may defeat some LOOP
1350 (let ((stepper (cond ((loop-tequal (car *loop-source-code
*) :by
)
1353 (t '(function cdr
)))))
1354 (cond ((and (consp stepper
) (eq (car stepper
) 'quote
))
1355 (loop-warn "Use of QUOTE around stepping function in LOOP will be left verbatim.")
1356 `(funcall ,stepper
,listvar
))
1357 ((and (consp stepper
) (eq (car stepper
) 'function
))
1358 (list (cadr stepper
) listvar
))
1360 `(funcall ,(loop-make-var (gensym "LOOP-FN-") stepper
'function
)
1363 (defun loop-for-on (var val data-type
)
1364 (multiple-value-bind (list constantp list-value
)
1365 (loop-constant-fold-if-possible val
)
1366 (let ((listvar var
))
1367 (cond ((and var
(symbolp var
))
1368 (loop-make-var var list data-type
))
1370 (loop-make-var (setq listvar
(gensym)) list
't
)
1371 (loop-make-var var nil data-type
)))
1372 (let ((list-step (loop-list-step listvar
)))
1373 (let* ((first-endtest
1374 ;; mysterious comment from original CMU CL sources:
1375 ;; the following should use `atom' instead of `endp',
1378 (other-endtest first-endtest
))
1379 (when (and constantp
(listp list-value
))
1380 (setq first-endtest
(null list-value
)))
1381 (cond ((eq var listvar
)
1382 ;; The contour of the loop is different because we
1383 ;; use the user's variable...
1384 `(() (,listvar
,list-step
)
1385 ,other-endtest
() () () ,first-endtest
()))
1386 (t (let ((step `(,var
,listvar
))
1387 (pseudo `(,listvar
,list-step
)))
1388 `(,other-endtest
,step
() ,pseudo
1389 ,@(and (not (eq first-endtest other-endtest
))
1390 `(,first-endtest
,step
() ,pseudo
)))))))))))
1392 (defun loop-for-in (var val data-type
)
1393 (multiple-value-bind (list constantp list-value
)
1394 (loop-constant-fold-if-possible val
)
1395 (let ((listvar (gensym "LOOP-LIST-")))
1396 (loop-make-var var nil data-type
)
1397 (loop-make-var listvar list
'list
)
1398 (let ((list-step (loop-list-step listvar
)))
1399 (let* ((first-endtest `(endp ,listvar
))
1400 (other-endtest first-endtest
)
1401 (step `(,var
(car ,listvar
)))
1402 (pseudo-step `(,listvar
,list-step
)))
1403 (when (and constantp
(listp list-value
))
1404 (setq first-endtest
(null list-value
)))
1405 `(,other-endtest
,step
() ,pseudo-step
1406 ,@(and (not (eq first-endtest other-endtest
))
1407 `(,first-endtest
,step
() ,pseudo-step
))))))))
1409 ;;;; iteration paths
1411 (defstruct (loop-path
1420 (defun add-loop-path (names function universe
1421 &key preposition-groups inclusive-permitted user-data
)
1422 (declare (type loop-universe universe
))
1423 (unless (listp names
)
1424 (setq names
(list names
)))
1425 (let ((ht (loop-universe-path-keywords universe
))
1427 :names
(mapcar #'symbol-name names
)
1429 :user-data user-data
1430 :preposition-groups
(mapcar (lambda (x)
1431 (if (listp x
) x
(list x
)))
1433 :inclusive-permitted inclusive-permitted
)))
1434 (dolist (name names
)
1435 (setf (gethash (symbol-name name
) ht
) lp
))
1438 ;;; Note: Path functions are allowed to use LOOP-MAKE-VAR, hack
1439 ;;; the prologue, etc.
1440 (defun loop-for-being (var val data-type
)
1441 ;; FOR var BEING each/the pathname prep-phrases using-stuff... each/the =
1442 ;; EACH or THE. Not clear if it is optional, so I guess we'll warn.
1447 (initial-prepositions nil
))
1448 (cond ((loop-tmember val
'(:each
:the
)) (setq path
(loop-pop-source)))
1449 ((loop-tequal (car *loop-source-code
*) :and
)
1452 (unless (loop-tmember (car *loop-source-code
*)
1453 '(:its
:each
:his
:her
))
1454 (loop-error "~S was found where ITS or EACH expected in LOOP iteration path syntax."
1455 (car *loop-source-code
*)))
1457 (setq path
(loop-pop-source))
1458 (setq initial-prepositions
`((:in
,val
))))
1459 (t (loop-error "unrecognizable LOOP iteration path syntax: missing EACH or THE?")))
1460 (cond ((not (symbolp path
))
1462 "~S was found where a LOOP iteration path name was expected."
1464 ((not (setq data
(loop-lookup-keyword path
(loop-universe-path-keywords *loop-universe
*))))
1465 (loop-error "~S is not the name of a LOOP iteration path." path
))
1466 ((and inclusive
(not (loop-path-inclusive-permitted data
)))
1467 (loop-error "\"Inclusive\" iteration is not possible with the ~S LOOP iteration path." path
)))
1468 (let ((fun (loop-path-function data
))
1469 (preps (nconc initial-prepositions
1470 (loop-collect-prepositional-phrases
1471 (loop-path-preposition-groups data
)
1473 (user-data (loop-path-user-data data
)))
1474 (when (symbolp fun
) (setq fun
(symbol-function fun
)))
1475 (setq stuff
(if inclusive
1476 (apply fun var data-type preps
:inclusive t user-data
)
1477 (apply fun var data-type preps user-data
))))
1478 (when *loop-named-vars
*
1479 (loop-error "Unused USING vars: ~S." *loop-named-vars
*))
1480 ;; STUFF is now (bindings prologue-forms . stuff-to-pass-back).
1481 ;; Protect the system from the user and the user from himself.
1482 (unless (member (length stuff
) '(6 10))
1483 (loop-error "Value passed back by LOOP iteration path function for path ~S has invalid length."
1485 (do ((l (car stuff
) (cdr l
)) (x)) ((null l
))
1486 (if (atom (setq x
(car l
)))
1487 (loop-make-var x nil nil
)
1488 (loop-make-var (car x
) (cadr x
) (caddr x
))))
1489 (setq *loop-prologue
* (nconc (reverse (cadr stuff
)) *loop-prologue
*))
1492 (defun loop-named-var (name)
1493 (let ((tem (loop-tassoc name
*loop-named-vars
*)))
1494 (declare (list tem
))
1495 (cond ((null tem
) (values (gensym) nil
))
1496 (t (setq *loop-named-vars
* (delete tem
*loop-named-vars
*))
1497 (values (cdr tem
) t
)))))
1499 (defun loop-collect-prepositional-phrases (preposition-groups
1503 (flet ((in-group-p (x group
) (car (loop-tmember x group
))))
1505 (prepositional-phrases initial-phrases
)
1506 (this-group nil nil
)
1508 (disallowed-prepositions
1511 (find (car x
) preposition-groups
:test
#'in-group-p
)))
1513 (used-prepositions (mapcar #'car initial-phrases
)))
1514 ((null *loop-source-code
*) (nreverse prepositional-phrases
))
1515 (declare (symbol this-prep
))
1516 (setq token
(car *loop-source-code
*))
1517 (dolist (group preposition-groups
)
1518 (when (setq this-prep
(in-group-p token group
))
1519 (return (setq this-group group
))))
1521 (when (member this-prep disallowed-prepositions
)
1523 (if (member this-prep used-prepositions
)
1524 "A ~S prepositional phrase occurs multiply for some LOOP clause."
1525 "Preposition ~S was used when some other preposition has subsumed it.")
1527 (setq used-prepositions
(if (listp this-group
)
1528 (append this-group used-prepositions
)
1529 (cons this-group used-prepositions
)))
1531 (push (list this-prep
(loop-get-form)) prepositional-phrases
))
1532 ((and using-allowed
(loop-tequal token
'using
))
1534 (do ((z (loop-pop-source) (loop-pop-source)) (tem)) (nil)
1536 (if (setq tem
(loop-tassoc (car z
) *loop-named-vars
*))
1538 "The variable substitution for ~S occurs twice in a USING phrase,~@
1540 (car z
) (cadr z
) (cadr tem
))
1541 (push (cons (car z
) (cadr z
)) *loop-named-vars
*)))
1542 (when (or (null *loop-source-code
*)
1543 (symbolp (car *loop-source-code
*)))
1545 (t (return (nreverse prepositional-phrases
)))))))
1547 ;;;; master sequencer function
1549 (defun loop-sequencer (indexv indexv-type
1550 variable variable-type
1551 sequence-variable sequence-type
1552 step-hack default-top
1554 (let ((endform nil
) ; form (constant or variable) with limit value
1555 (sequencep nil
) ; T if sequence arg has been provided
1556 (testfn nil
) ; endtest function
1557 (test nil
) ; endtest form
1558 (stepby (1+ (or (loop-typed-init indexv-type
) 0))) ; our increment
1559 (stepby-constantp t
)
1560 (step nil
) ; step form
1561 (dir nil
) ; direction of stepping: NIL, :UP, :DOWN
1562 (inclusive-iteration nil
) ; T if include last index
1563 (start-given nil
) ; T when prep phrase has specified start
1565 (start-constantp nil
)
1566 (limit-given nil
) ; T when prep phrase has specified end
1567 (limit-constantp nil
)
1569 (flet ((assert-index-for-arithmetic (index)
1570 (unless (atom index
)
1571 (loop-error "Arithmetic index must be an atom."))))
1572 (when variable
(loop-make-var variable nil variable-type
))
1573 (do ((l prep-phrases
(cdr l
)) (prep) (form) (odir)) ((null l
))
1574 (setq prep
(caar l
) form
(cadar l
))
1578 (loop-make-var sequence-variable form sequence-type
))
1579 ((:from
:downfrom
:upfrom
)
1580 (setq start-given t
)
1581 (cond ((eq prep
:downfrom
) (setq dir
':down
))
1582 ((eq prep
:upfrom
) (setq dir
':up
)))
1583 (multiple-value-setq (form start-constantp start-value
)
1584 (loop-constant-fold-if-possible form indexv-type
))
1585 (assert-index-for-arithmetic indexv
)
1586 ;; KLUDGE: loop-make-var generates a temporary symbol for
1587 ;; indexv if it is NIL. We have to use it to have the index
1589 (setq indexv
(loop-make-var indexv form indexv-type
)))
1590 ((:upto
:to
:downto
:above
:below
)
1591 (cond ((loop-tequal prep
:upto
) (setq inclusive-iteration
1593 ((loop-tequal prep
:to
) (setq inclusive-iteration t
))
1594 ((loop-tequal prep
:downto
) (setq inclusive-iteration
1596 ((loop-tequal prep
:above
) (setq dir
':down
))
1597 ((loop-tequal prep
:below
) (setq dir
':up
)))
1598 (setq limit-given t
)
1599 (multiple-value-setq (form limit-constantp limit-value
)
1600 (loop-constant-fold-if-possible form
`(and ,indexv-type real
)))
1601 (setq endform
(if limit-constantp
1604 (gensym "LOOP-LIMIT-") form
1605 `(and ,indexv-type real
)))))
1607 (multiple-value-setq (form stepby-constantp stepby
)
1608 (loop-constant-fold-if-possible form
1609 `(and ,indexv-type
(real (0)))))
1610 (unless stepby-constantp
1611 (loop-make-var (setq stepby
(gensym "LOOP-STEP-BY-"))
1613 `(and ,indexv-type
(real (0)))
1616 "~S invalid preposition in sequencing or sequence path;~@
1617 maybe invalid prepositions were specified in iteration path descriptor?"
1619 (when (and odir dir
(not (eq dir odir
)))
1621 "conflicting stepping directions in LOOP sequencing path"))
1623 (when (and sequence-variable
(not sequencep
))
1624 (loop-error "missing OF or IN phrase in sequence path"))
1625 ;; Now fill in the defaults.
1626 (cond ((not start-given
)
1628 ;; DUPLICATE KLUDGE: loop-make-var generates a temporary
1629 ;; symbol for indexv if it is NIL. See also the comment in
1630 ;; the (:from :downfrom :upfrom) case
1631 (assert-index-for-arithmetic indexv
)
1635 (setq start-constantp t
1636 start-value
(or (loop-typed-init indexv-type
) 0))
1637 `(and ,indexv-type real
))))
1639 ;; if both start and limit are given, they had better both
1640 ;; be REAL. We already enforce the REALness of LIMIT,
1641 ;; above; here's the KLUDGE to enforce the type of START.
1642 (flet ((type-declaration-of (x)
1643 (and (eq (car x
) 'type
) (caddr x
))))
1644 (let ((decl (find indexv
*loop-declarations
*
1645 :key
#'type-declaration-of
))
1646 (%decl
(find indexv
*loop-declarations
*
1647 :key
#'type-declaration-of
1649 (sb!int
:aver
(eq decl %decl
))
1652 `(and real
,(cadr decl
))))))))
1653 (cond ((member dir
'(nil :up
))
1654 (when (or limit-given default-top
)
1656 (loop-make-var (setq endform
(gensym "LOOP-SEQ-LIMIT-"))
1659 (push `(setq ,endform
,default-top
) *loop-prologue
*))
1660 (setq testfn
(if inclusive-iteration
'> '>=)))
1661 (setq step
(if (eql stepby
1) `(1+ ,indexv
) `(+ ,indexv
,stepby
))))
1662 (t (unless start-given
1664 (loop-error "don't know where to start stepping"))
1665 (push `(setq ,indexv
(1- ,default-top
)) *loop-prologue
*))
1666 (when (and default-top
(not endform
))
1667 (setq endform
(loop-typed-init indexv-type
)
1668 inclusive-iteration t
))
1669 (when endform
(setq testfn
(if inclusive-iteration
'< '<=)))
1671 (if (eql stepby
1) `(1- ,indexv
) `(- ,indexv
,stepby
)))))
1674 `(,testfn
,indexv
,endform
)))
1677 `(,variable
,step-hack
)))
1678 (let ((first-test test
) (remaining-tests test
))
1679 ;; As far as I can tell, the effect of the following code is
1680 ;; to detect cases where we know statically whether the first
1681 ;; iteration of the loop will be executed. Depending on the
1682 ;; situation, we can either:
1683 ;; a) save one jump and one comparison per loop (not per iteration)
1684 ;; when it will get executed
1685 ;; b) remove the loop body completely when it won't be executed
1687 ;; Noble goals. However, the code generated in case a) will
1688 ;; fool the loop induction variable detection, and cause
1689 ;; code like (LOOP FOR I TO 10 ...) to use generic addition
1692 ;; Since the gain in case a) is rather minimal and Python is
1693 ;; generally smart enough to handle b) without any extra
1694 ;; support from the loop macro, I've disabled this code for
1695 ;; now. The code and the comment left here in case somebody
1696 ;; extends the induction variable bound detection to work
1697 ;; with code where the stepping precedes the test.
1698 ;; -- JES 2005-11-30
1700 (when (and stepby-constantp start-constantp limit-constantp
1701 (realp start-value
) (realp limit-value
))
1702 (when (setq first-test
1703 (funcall (symbol-function testfn
)
1706 (setq remaining-tests t
)))
1707 `(() (,indexv
,step
)
1708 ,remaining-tests
,step-hack
() () ,first-test
,step-hack
)))))
1710 ;;;; interfaces to the master sequencer
1712 (defun loop-for-arithmetic (var val data-type kwd
)
1714 var
(loop-check-data-type data-type
'number
)
1715 nil nil nil nil nil nil
1716 (loop-collect-prepositional-phrases
1717 '((:from
:upfrom
:downfrom
) (:to
:upto
:downto
:above
:below
) (:by
))
1718 nil
(list (list kwd val
)))))
1721 ;;;; builtin LOOP iteration paths
1724 (loop for v being the hash-values of ht do
(print v
))
1725 (loop for k being the hash-keys of ht do
(print k
))
1726 (loop for v being the hash-values of ht using
(hash-key k
) do
(print (list k v
)))
1727 (loop for k being the hash-keys of ht using
(hash-value v
) do
(print (list k v
)))
1730 (defun loop-hash-table-iteration-path (variable data-type prep-phrases
1731 &key
(which (sb!int
:missing-arg
)))
1732 (declare (type (member :hash-key
:hash-value
) which
))
1733 (cond ((or (cdr prep-phrases
) (not (member (caar prep-phrases
) '(:in
:of
))))
1734 (loop-error "too many prepositions!"))
1735 ((null prep-phrases
)
1736 (loop-error "missing OF or IN in ~S iteration path")))
1737 (let ((ht-var (gensym "LOOP-HASHTAB-"))
1738 (next-fn (gensym "LOOP-HASHTAB-NEXT-"))
1739 (dummy-predicate-var nil
)
1741 (multiple-value-bind (other-var other-p
)
1742 (loop-named-var (ecase which
1743 (:hash-key
'hash-value
)
1744 (:hash-value
'hash-key
)))
1745 ;; @@@@ LOOP-NAMED-VAR returns a second value of T if the name
1746 ;; was actually specified, so clever code can throw away the
1747 ;; GENSYM'ed-up variable if it isn't really needed. The
1748 ;; following is for those implementations in which we cannot put
1749 ;; dummy NILs into MULTIPLE-VALUE-SETQ variable lists.
1751 dummy-predicate-var
(loop-when-it-var))
1752 (let* ((key-var nil
)
1754 (variable (or variable
(gensym "LOOP-HASH-VAR-TEMP-")))
1755 (bindings `((,variable nil
,data-type
)
1756 (,ht-var
,(cadar prep-phrases
))
1757 ,@(and other-p other-var
`((,other-var nil
))))))
1759 (:hash-key
(setq key-var variable
1760 val-var
(and other-p other-var
)))
1761 (:hash-value
(setq key-var
(and other-p other-var
)
1763 (push `(with-hash-table-iterator (,next-fn
,ht-var
)) *loop-wrappers
*)
1764 (when (or (consp key-var
) data-type
)
1766 `(,key-var
,(setq key-var
(gensym "LOOP-HASH-KEY-TEMP-"))
1768 (push `(,key-var nil
) bindings
))
1769 (when (or (consp val-var
) data-type
)
1771 `(,val-var
,(setq val-var
(gensym "LOOP-HASH-VAL-TEMP-"))
1773 (push `(,val-var nil
) bindings
))
1774 `(,bindings
;bindings
1778 (not (multiple-value-setq (,dummy-predicate-var
,key-var
,val-var
)
1779 (,next-fn
))) ;post-test
1782 (defun loop-package-symbols-iteration-path (variable data-type prep-phrases
1784 (cond ((and prep-phrases
(cdr prep-phrases
))
1785 (loop-error "Too many prepositions!"))
1786 ((and prep-phrases
(not (member (caar prep-phrases
) '(:in
:of
))))
1787 (sb!int
:bug
"Unknown preposition ~S." (caar prep-phrases
))))
1788 (unless (symbolp variable
)
1789 (loop-error "Destructuring is not valid for package symbol iteration."))
1790 (let ((pkg-var (gensym "LOOP-PKGSYM-"))
1791 (next-fn (gensym "LOOP-PKGSYM-NEXT-"))
1792 (variable (or variable
(gensym "LOOP-PKGSYM-VAR-")))
1793 (package (or (cadar prep-phrases
) '*package
*)))
1794 (push `(with-package-iterator (,next-fn
,pkg-var
,@symbol-types
))
1796 `(((,variable nil
,data-type
) (,pkg-var
,package
))
1800 (not (multiple-value-setq (,(loop-when-it-var)
1807 (defun make-ansi-loop-universe ()
1808 (let ((w (make-standard-loop-universe
1809 :keywords
'((named (loop-do-named))
1810 (initially (loop-do-initially))
1811 (finally (loop-do-finally))
1813 (doing (loop-do-do))
1814 (return (loop-do-return))
1815 (collect (loop-list-collection list
))
1816 (collecting (loop-list-collection list
))
1817 (append (loop-list-collection append
))
1818 (appending (loop-list-collection append
))
1819 (nconc (loop-list-collection nconc
))
1820 (nconcing (loop-list-collection nconc
))
1821 (count (loop-sum-collection count
1822 ;; This could be REAL, but when
1823 ;; combined with SUM, it has to be
1827 (counting (loop-sum-collection count
1830 (sum (loop-sum-collection sum number number
))
1831 (summing (loop-sum-collection sum number number
))
1832 (maximize (loop-maxmin-collection max
))
1833 (minimize (loop-maxmin-collection min
))
1834 (maximizing (loop-maxmin-collection max
))
1835 (minimizing (loop-maxmin-collection min
))
1836 (always (loop-do-always t nil
)) ; Normal, do always
1837 (never (loop-do-always t t
)) ; Negate test on always.
1838 (thereis (loop-do-thereis t
))
1839 (while (loop-do-while nil
:while
)) ; Normal, do while
1840 (until (loop-do-while t
:until
)) ;Negate test on while
1841 (when (loop-do-if when nil
)) ; Normal, do when
1842 (if (loop-do-if if nil
)) ; synonymous
1843 (unless (loop-do-if unless t
)) ; Negate test on when
1844 (with (loop-do-with))
1845 (repeat (loop-do-repeat)))
1846 :for-keywords
'((= (loop-ansi-for-equals))
1847 (across (loop-for-across))
1850 (from (loop-for-arithmetic :from
))
1851 (downfrom (loop-for-arithmetic :downfrom
))
1852 (upfrom (loop-for-arithmetic :upfrom
))
1853 (below (loop-for-arithmetic :below
))
1854 (above (loop-for-arithmetic :above
))
1855 (to (loop-for-arithmetic :to
))
1856 (upto (loop-for-arithmetic :upto
))
1857 (downto (loop-for-arithmetic :downto
))
1858 (by (loop-for-arithmetic :by
))
1859 (being (loop-for-being)))
1860 :iteration-keywords
'((for (loop-do-for))
1862 :type-symbols
'(array atom bignum bit bit-vector character
1863 compiled-function complex cons double-float
1864 fixnum float function hash-table integer
1865 keyword list long-float nil null number
1866 package pathname random-state ratio rational
1867 readtable sequence short-float simple-array
1868 simple-bit-vector simple-string simple-vector
1869 single-float standard-char stream string
1870 base-char symbol t vector
)
1871 :type-keywords nil
)))
1872 (add-loop-path '(hash-key hash-keys
) 'loop-hash-table-iteration-path w
1873 :preposition-groups
'((:of
:in
))
1874 :inclusive-permitted nil
1875 :user-data
'(:which
:hash-key
))
1876 (add-loop-path '(hash-value hash-values
) 'loop-hash-table-iteration-path w
1877 :preposition-groups
'((:of
:in
))
1878 :inclusive-permitted nil
1879 :user-data
'(:which
:hash-value
))
1880 (add-loop-path '(symbol symbols
) 'loop-package-symbols-iteration-path w
1881 :preposition-groups
'((:of
:in
))
1882 :inclusive-permitted nil
1883 :user-data
'(:symbol-types
(:internal
1886 (add-loop-path '(external-symbol external-symbols
)
1887 'loop-package-symbols-iteration-path w
1888 :preposition-groups
'((:of
:in
))
1889 :inclusive-permitted nil
1890 :user-data
'(:symbol-types
(:external
)))
1891 (add-loop-path '(present-symbol present-symbols
)
1892 'loop-package-symbols-iteration-path w
1893 :preposition-groups
'((:of
:in
))
1894 :inclusive-permitted nil
1895 :user-data
'(:symbol-types
(:internal
1899 (defparameter *loop-ansi-universe
*
1900 (make-ansi-loop-universe))
1902 (defun loop-standard-expansion (keywords-and-forms environment universe
)
1903 (if (and keywords-and-forms
(symbolp (car keywords-and-forms
)))
1904 (loop-translate keywords-and-forms environment universe
)
1905 (let ((tag (gensym)))
1906 `(block nil
(tagbody ,tag
(progn ,@keywords-and-forms
) (go ,tag
))))))
1908 (sb!int
:defmacro-mundanely loop
(&environment env
&rest keywords-and-forms
)
1909 (loop-standard-expansion keywords-and-forms env
*loop-ansi-universe
*))
1911 (sb!int
:defmacro-mundanely loop-finish
()
1913 "Cause the iteration to terminate \"normally\", the same as implicit
1914 termination by an iteration driving clause, or by use of WHILE or
1915 UNTIL -- the epilogue code (if any) will be run, and any implicitly
1916 collected result will be returned as the value of the LOOP."