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!xc
:defmacro 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!xc
:defmacro 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!xc
:defmacro 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!xc
:defmacro 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!xc
:defmacro 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!xc
:defmacro loop-store-table-data
(symbol table datum
)
268 `(setf (gethash (symbol-name ,symbol
) ,table
) ,datum
))
270 (defstruct (loop-universe
271 (:constructor
!make-loop-universe
)
274 keywords
; hash table, value = (fn-name . extra-data)
275 iteration-keywords
; hash table, value = (fn-name . extra-data)
276 for-keywords
; hash table, value = (fn-name . extra-data)
277 path-keywords
; hash table, value = (fn-name . extra-data)
278 type-symbols
; hash table of type SYMBOLS, test EQ,
279 ; value = CL type specifier
280 type-keywords
) ; hash table of type STRINGS, test EQUAL,
281 ; value = CL type spec
282 (defmethod print-object ((u loop-universe
) stream
)
283 (print-unreadable-object (u stream
:type t
:identity t
)))
285 ;;; This is the "current" loop context in use when we are expanding a
286 ;;; loop. It gets bound on each invocation of LOOP.
287 (defvar *loop-universe
*)
289 (defun !make-standard-loop-universe
(&key keywords for-keywords
290 iteration-keywords path-keywords
291 type-keywords type-symbols
)
292 (flet ((maketable (entries)
293 (let* ((size (length entries
))
294 (ht (make-hash-table :size
(if (< size
10) 10 size
)
297 (setf (gethash (symbol-name (car x
)) ht
) (cadr x
)))
300 :keywords
(maketable keywords
)
301 :for-keywords
(maketable for-keywords
)
302 :iteration-keywords
(maketable iteration-keywords
)
303 :path-keywords
(maketable path-keywords
)
304 :type-keywords
(maketable type-keywords
)
305 :type-symbols
(let* ((size (length type-symbols
))
306 (ht (make-hash-table :size
(if (< size
10) 10 size
)
308 (dolist (x type-symbols
)
310 (setf (gethash x ht
) x
)
311 (setf (gethash (car x
) ht
) (cadr x
))))
314 ;;;; SETQ hackery, including destructuring ("DESETQ")
316 (defun loop-make-psetq (frobs)
320 (if (null (cddr frobs
)) (cadr frobs
)
321 `(prog1 ,(cadr frobs
)
322 ,(loop-make-psetq (cddr frobs
))))))))
324 (defun loop-make-desetq (var-val-pairs)
325 (if (null var-val-pairs
)
327 (cons 'loop-really-desetq var-val-pairs
)))
329 (defvar *loop-desetq-temporary
*
330 (make-symbol "LOOP-DESETQ-TEMP"))
332 (sb!xc
:defmacro loop-really-desetq
(&environment env
334 (labels ((find-non-null (var)
335 ;; See whether there's any non-null thing here. Recurse
336 ;; if the list element is itself a list.
337 (do ((tail var
)) ((not (consp tail
)) tail
)
338 (when (find-non-null (pop tail
)) (return t
))))
339 (loop-desetq-internal (var val
&optional temp
)
340 ;; returns a list of actions to be performed
344 ;; Don't lose possible side effects.
345 (if (eq (car val
) 'prog1
)
346 ;; These can come from PSETQ or DESETQ below.
347 ;; Throw away the value, keep the side effects.
348 ;; Special case is for handling an expanded POP.
351 (or (not (eq (car x
) 'car
))
352 (not (symbolp (cadr x
)))
353 (not (symbolp (setq x
(sb!int
:%macroexpand x env
)))))
358 (let* ((car (car var
))
360 (car-non-null (find-non-null car
))
361 (cdr-non-null (find-non-null cdr
)))
362 (when (or car-non-null cdr-non-null
)
365 (temp (or temp
*loop-desetq-temporary
*))
366 (body `(,@(loop-desetq-internal car
368 (setq ,temp
(cdr ,temp
))
369 ,@(loop-desetq-internal cdr
373 `(,@(unless (eq temp val
)
374 `((setq ,temp
,val
)))
376 `((let ((,temp
,val
))
379 (loop-desetq-internal car
`(car ,val
) temp
)))))
382 `((setq ,var
,val
)))))))
384 ((null var-val-pairs
)
385 (if (null (cdr actions
)) (car actions
) `(progn ,@(nreverse actions
))))
386 (setq actions
(revappend
387 (loop-desetq-internal (pop var-val-pairs
)
391 ;;;; LOOP-local variables
393 ;;; This is the "current" pointer into the LOOP source code.
394 (defvar *loop-source-code
*)
396 ;;; This is the pointer to the original, for things like NAMED that
397 ;;; insist on being in a particular position
398 (defvar *loop-original-source-code
*)
400 ;;; This is *loop-source-code* as of the "last" clause. It is used
401 ;;; primarily for generating error messages (see loop-error, loop-warn).
402 (defvar *loop-source-context
*)
404 ;;; list of names for the LOOP, supplied by the NAMED clause
405 (defvar *loop-names
*)
407 ;;; The macroexpansion environment given to the macro.
408 (defvar *loop-macro-environment
*)
410 ;;; This holds variable names specified with the USING clause.
411 ;;; See LOOP-NAMED-VAR.
412 (defvar *loop-named-vars
*)
414 ;;; LETlist-like list being accumulated for current group of bindings.
417 ;;; List of declarations being accumulated in parallel with
419 (defvar *loop-declarations
*)
421 ;;; Declarations for destructuring bindings
422 (defvar *loop-desetq-declarations
*)
424 ;;; This is used by LOOP for destructuring binding, if it is doing
425 ;;; that itself. See LOOP-MAKE-VAR.
426 (defvar *loop-desetq
*)
428 ;;; list of wrapping forms, innermost first, which go immediately
429 ;;; inside the current set of parallel bindings being accumulated in
430 ;;; *LOOP-VARS*. The wrappers are appended onto a body. E.g., this
431 ;;; list could conceivably have as its value
432 ;;; ((WITH-OPEN-FILE (G0001 G0002 ...))),
433 ;;; with G0002 being one of the bindings in *LOOP-VARS* (This is why
434 ;;; the wrappers go inside of the variable bindings).
435 (defvar *loop-wrappers
*)
437 ;;; This accumulates lists of previous values of *LOOP-VARS* and the
438 ;;; other lists above, for each new nesting of bindings. See
440 (defvar *loop-bind-stack
*)
442 ;;; list of prologue forms of the loop, accumulated in reverse order
443 (defvar *loop-prologue
*)
445 (defvar *loop-before-loop
*)
447 (defvar *loop-after-body
*)
449 ;;; This is T if we have emitted any body code, so that iteration
450 ;;; driving clauses can be disallowed. This is not strictly the same
451 ;;; as checking *LOOP-BODY*, because we permit some clauses such as
452 ;;; RETURN to not be considered "real" body (so as to permit the user
453 ;;; to "code" an abnormal return value "in loop").
454 (defvar *loop-emitted-body
*)
456 ;;; list of epilogue forms (supplied by FINALLY generally), accumulated
458 (defvar *loop-epilogue
*)
460 ;;; list of epilogue forms which are supplied after the above "user"
461 ;;; epilogue. "Normal" termination return values are provide by
462 ;;; putting the return form in here. Normally this is done using
463 ;;; LOOP-EMIT-FINAL-VALUE, q.v.
464 (defvar *loop-after-epilogue
*)
466 ;;; the "culprit" responsible for supplying a final value from the
467 ;;; loop. This is so LOOP-DISALLOW-AGGREGATE-BOOLEANS can moan about
468 ;;; disallowed anonymous collections.
469 (defvar *loop-final-value-culprit
*)
471 ;;; If this is true, we are in some branch of a conditional. Some
472 ;;; clauses may be disallowed.
473 (defvar *loop-inside-conditional
*)
475 ;;; If not NIL, this is a temporary bound around the loop for holding
476 ;;; the temporary value for "it" in things like "when (f) collect it".
477 ;;; It may be used as a supertemporary by some other things.
478 (defvar *loop-when-it-var
*)
480 ;;; Sometimes we decide we need to fold together parts of the loop,
481 ;;; but some part of the generated iteration code is different for the
482 ;;; first and remaining iterations. This variable will be the
483 ;;; temporary which is the flag used in the loop to tell whether we
484 ;;; are in the first or remaining iterations.
485 (defvar *loop-never-stepped-var
*)
487 ;;; list of all the value-accumulation descriptor structures in the
488 ;;; loop. See LOOP-GET-COLLECTION-INFO.
489 (defvar *loop-collection-cruft
*) ; for multiple COLLECTs (etc.)
491 ;;;; code analysis stuff
493 (defun loop-constant-fold-if-possible (form &optional expected-type
)
494 (let* ((constantp (sb!xc
:constantp form
))
495 (value (and constantp
(sb!int
:constant-form-value form
))))
496 (when (and constantp expected-type
)
497 (unless (sb!xc
:typep value expected-type
)
498 (loop-warn "~@<The form ~S evaluated to ~S, which was not of ~
499 the anticipated type ~S.~:@>"
500 form value expected-type
)
501 (setq constantp nil value nil
)))
502 (values form constantp value
)))
504 (sb!xc
:defmacro loop-body
(prologue
509 (unless (= (length before-loop
) (length after-loop
))
510 (error "LOOP-BODY called with non-synched before- and after-loop lists"))
511 ;; All our work is done from these copies, working backwards from the end
512 (let ((rbefore (reverse before-loop
))
513 (rafter (reverse after-loop
)))
514 ;; Go backwards from the ends of before-loop and after-loop
515 ;; merging all the equivalent forms into the body.
518 (not (equal (car rbefore
) (car rafter
)))))
519 (push (pop rbefore
) main-body
)
522 ,@(remove nil prologue
)
523 ,@(nreverse (remove nil rbefore
))
525 ,@(remove nil main-body
)
526 ,@(nreverse (remove nil rafter
))
529 ,@(remove nil epilogue
))))
533 (defun loop-context ()
534 (do ((l *loop-source-context
* (cdr l
)) (new nil
(cons (car l
) new
)))
535 ((eq l
(cdr *loop-source-code
*)) (nreverse new
))))
537 (defun loop-error (format-string &rest format-args
)
538 (error 'sb
!int
:simple-program-error
539 :format-control
"~?~%current LOOP context:~{ ~S~}."
540 :format-arguments
(list format-string format-args
(loop-context))))
542 (defun loop-warn (format-string &rest format-args
)
543 (warn "~?~%current LOOP context:~{ ~S~}."
548 (defun loop-check-data-type (specified-type required-type
549 &optional
(default-type required-type
))
550 (if (null specified-type
)
552 (multiple-value-bind (a b
) (sb!xc
:subtypep specified-type required-type
)
554 (loop-warn "LOOP couldn't verify that ~S is a subtype of the required type ~S."
555 specified-type required-type
))
557 (loop-error "The specified data type ~S is not a subtype of ~S."
558 specified-type required-type
)))
561 ;;; Transform the LOOP kind of destructuring into the DESTRUCTURING-BIND kind
562 ;;; basically by adding &optional and ignored &rest dotted list
563 (defun transform-destructuring (tree)
565 (labels ((transform (tree)
566 (do ((result (list '&optional
))
567 (cdr tree
(cdr cdr
)))
570 (return (nreconc result
571 (car (push (gensym "LOOP-IGNORED-")
574 (return (nreconc result cdr
)))
576 (push (list (transform (car cdr
))) result
))
578 (push (car (push (gensym "LOOP-IGNORED-")
582 (push (car cdr
) result
))))))
583 (values (transform tree
) ignores
))))
585 (sb!xc
:defmacro loop-destructuring-bind
586 (lambda-list args
&rest body
)
587 (multiple-value-bind (d-lambda-list ignores
)
588 (transform-destructuring lambda-list
)
589 `(destructuring-bind ,d-lambda-list
,args
590 (declare (ignore ,@ignores
))
593 (defun loop-build-destructuring-bindings (crocks forms
)
595 `((loop-destructuring-bind ,(car crocks
) ,(cadr crocks
)
596 ,@(loop-build-destructuring-bindings (cddr crocks
) forms
)))
599 (defun loop-translate (*loop-source-code
*
600 *loop-macro-environment
*
602 (let ((*loop-original-source-code
* *loop-source-code
*)
603 (*loop-source-context
* nil
)
605 (*loop-named-vars
* nil
)
606 (*loop-declarations
* nil
)
607 (*loop-desetq-declarations
* nil
)
609 (*loop-bind-stack
* nil
)
610 (*loop-prologue
* nil
)
611 (*loop-wrappers
* nil
)
612 (*loop-before-loop
* nil
)
614 (*loop-emitted-body
* nil
)
615 (*loop-after-body
* nil
)
616 (*loop-epilogue
* nil
)
617 (*loop-after-epilogue
* nil
)
618 (*loop-final-value-culprit
* nil
)
619 (*loop-inside-conditional
* nil
)
620 (*loop-when-it-var
* nil
)
621 (*loop-never-stepped-var
* nil
)
623 (*loop-collection-cruft
* nil
))
624 (loop-iteration-driver)
626 (let ((answer `(loop-body
627 ,(nreverse *loop-prologue
*)
628 ,(nreverse *loop-before-loop
*)
629 ,(nreverse *loop-body
*)
630 ,(nreverse *loop-after-body
*)
631 ,(nreconc *loop-epilogue
*
632 (nreverse *loop-after-epilogue
*)))))
633 (dolist (entry *loop-bind-stack
*)
634 (destructuring-bind (vars dcls desetq desetq-decls wrappers
) entry
636 (setq answer
(append w
(list answer
))))
637 (when (or vars dcls desetq
)
638 (let ((forms (list answer
)))
640 (push `(declare ,@desetq-decls
) forms
))
641 (setq answer
`(,(if vars
'let
'locally
)
644 ,@(loop-build-destructuring-bindings desetq
647 (setq answer
`(block ,(pop *loop-names
*) ,answer
))
648 (unless *loop-names
* (return nil
)))
651 (defun loop-iteration-driver ()
653 ((null *loop-source-code
*))
654 (let ((keyword (car *loop-source-code
*)) (tem nil
))
655 (cond ((not (symbolp keyword
))
656 (loop-error "~S found where LOOP keyword expected" keyword
))
657 (t (setq *loop-source-context
* *loop-source-code
*)
660 (loop-lookup-keyword keyword
661 (loop-universe-keywords
663 ;; It's a "miscellaneous" toplevel LOOP keyword (DO,
664 ;; COLLECT, NAMED, etc.)
665 (apply (symbol-function (first tem
)) (rest tem
)))
667 (loop-lookup-keyword keyword
668 (loop-universe-iteration-keywords *loop-universe
*)))
669 (loop-hack-iteration tem
))
670 ((loop-tmember keyword
'(and else
))
671 ;; The alternative is to ignore it, i.e. let it go
672 ;; around to the next keyword...
673 (loop-error "secondary clause misplaced at top level in LOOP macro: ~S ~S ~S ..."
675 (car *loop-source-code
*)
676 (cadr *loop-source-code
*)))
677 (t (loop-error "unknown LOOP keyword: ~S" keyword
))))))))
679 (defun loop-pop-source ()
680 (if *loop-source-code
*
681 (pop *loop-source-code
*)
682 (loop-error "LOOP source code ran out when another token was expected.")))
684 (defun loop-get-form ()
685 (if *loop-source-code
*
687 (loop-error "LOOP code ran out where a form was expected.")))
689 (defun loop-get-compound-form ()
690 (let ((form (loop-get-form)))
692 (loop-error "A compound form was expected, but ~S found." form
))
695 (defun loop-get-progn ()
696 (do ((forms (list (loop-get-compound-form))
697 (cons (loop-get-compound-form) forms
))
698 (nextform (car *loop-source-code
*)
699 (car *loop-source-code
*)))
701 (if (null (cdr forms
)) (car forms
) (cons 'progn
(nreverse forms
))))))
703 (defun loop-construct-return (form)
704 `(return-from ,(car *loop-names
*) ,form
))
706 (defun loop-pseudo-body (form)
707 (cond ((or *loop-emitted-body
* *loop-inside-conditional
*)
708 (push form
*loop-body
*))
709 (t (push form
*loop-before-loop
*) (push form
*loop-after-body
*))))
711 (defun loop-emit-body (form)
712 (setq *loop-emitted-body
* t
)
713 (loop-pseudo-body form
))
715 (defun loop-emit-final-value (&optional
(form nil form-supplied-p
))
716 (when form-supplied-p
717 (push (loop-construct-return form
) *loop-after-epilogue
*))
718 (setq *loop-final-value-culprit
* (car *loop-source-context
*)))
720 (defun loop-disallow-conditional (&optional kwd
)
721 (when *loop-inside-conditional
*
722 (loop-error "~:[This LOOP~;The LOOP ~:*~S~] clause is not permitted inside a conditional." kwd
)))
724 (defun loop-disallow-anonymous-collectors ()
725 (when (find-if-not 'loop-collector-name
*loop-collection-cruft
*)
726 (loop-error "This LOOP clause is not permitted with anonymous collectors.")))
728 (defun loop-disallow-aggregate-booleans ()
729 (when (loop-tmember *loop-final-value-culprit
* '(always never thereis
))
730 (loop-error "This anonymous collection LOOP clause is not permitted with aggregate booleans.")))
734 (defun loop-typed-init (data-type &optional step-var-p
)
735 ;; FIXME: can't tell if unsupplied or NIL, but it has to be rare.
737 (let ((ctype (sb!kernel
:specifier-type data-type
)))
738 ;; FIXME: use the ctype for the rest of the type operations, now
740 (cond ((eql ctype sb
!kernel
:*empty-type
*)
742 ((sb!xc
:subtypep data-type
'number
)
743 (let ((init (if step-var-p
1 0)))
744 (flet ((like (&rest types
)
745 (coerce init
(find-if (lambda (type)
746 (sb!xc
:subtypep data-type type
))
748 (cond ((sb!xc
:subtypep data-type
'float
)
749 (like 'single-float
'double-float
750 'short-float
'long-float
'float
))
751 ((sb!xc
:subtypep data-type
'(complex float
))
752 (like '(complex single-float
)
753 '(complex double-float
)
754 '(complex short-float
)
755 '(complex long-float
)
759 ((sb!xc
:subtypep data-type
'vector
)
760 (when (sb!kernel
:array-type-p ctype
)
761 (let ((etype (sb!kernel
:type-
*-to-t
762 (sb!kernel
:array-type-specialized-element-type ctype
))))
763 (make-array 0 :element-type
(sb!kernel
:type-specifier etype
)))))
765 ((sb!xc
:subtypep data-type
'extended-char
)
766 (code-char sb
!int
:base-char-code-limit
))
767 ((sb!xc
:subtypep data-type
'character
)
772 (defun loop-optional-type (&optional variable
)
773 ;; No variable specified implies that no destructuring is permissible.
774 (and *loop-source-code
* ; Don't get confused by NILs..
775 (let ((z (car *loop-source-code
*)))
776 (cond ((loop-tequal z
'of-type
)
777 ;; This is the syntactically unambigous form in that
778 ;; the form of the type specifier does not matter.
779 ;; Also, it is assumed that the type specifier is
780 ;; unambiguously, and without need of translation, a
781 ;; common lisp type specifier or pattern (matching the
782 ;; variable) thereof.
787 ;; This is the (sort of) "old" syntax, even though we
788 ;; didn't used to support all of these type symbols.
789 (let ((type-spec (or (gethash z
790 (loop-universe-type-symbols
792 (gethash (symbol-name z
)
793 (loop-universe-type-keywords
799 ;; This is our sort-of old syntax. But this is only
800 ;; valid for when we are destructuring, so we will be
801 ;; compulsive (should we really be?) and require that
802 ;; we in fact be doing variable destructuring here. We
803 ;; must translate the old keyword pattern typespec
804 ;; into a fully-specified pattern of real type
809 "~S found where a LOOP keyword, LOOP type keyword, or LOOP type pattern expected"
811 (loop-error "~S found where a LOOP keyword or LOOP type keyword expected" z
))
813 (labels ((translate (k v
)
818 (loop-universe-type-symbols
820 (gethash (symbol-name k
)
821 (loop-universe-type-keywords
824 "The destructuring type pattern ~S contains the unrecognized type keyword ~S."
829 "The destructuring type pattern ~S doesn't match the variable pattern ~S."
831 (t (cons (translate (car k
) (car v
))
832 (translate (cdr k
) (cdr v
))))))
836 (cons (replicate typ
(car v
))
837 (replicate typ
(cdr v
))))))
838 (translate z variable
)))))))
842 (defun loop-bind-block ()
843 (when (or *loop-vars
* *loop-declarations
* *loop-wrappers
*
845 (push (list (nreverse *loop-vars
*)
848 *loop-desetq-declarations
*
851 (setq *loop-vars
* nil
852 *loop-declarations
* nil
854 *loop-desetq-declarations
* nil
855 *loop-wrappers
* nil
)))
857 (defun check-var-name (name &optional
(context ""))
858 (labels ((map-name (function name
)
859 (do ((x (pop name
) (pop name
)))
863 (cons (map-name function x
))
864 (symbol (funcall function x
))
866 (loop-error "Bad variable ~s~a" x context
)))
872 (funcall function name
)
875 (loop-error "Bad variable ~s~a" name context
)))))
877 (loop-error "Duplicated variable ~s~a" x context
))
878 (find-in-desetq (name desetqs
)
879 (do* ((desetq desetqs
(cddr desetq
))
880 (var (car desetq
) (car desetq
)))
882 (map-name (lambda (x)
887 (map-name (lambda (x) (check-var-name x context
)) name
))
888 ((assoc name
*loop-vars
*)
890 ((find-in-desetq name
*loop-desetq
*))
892 (do ((entry *loop-bind-stack
* (cdr entry
)))
895 ((null entry
) (return nil
))
896 ((assoc name
(caar entry
) :test
#'eq
)
899 (find-in-desetq name
(caddar entry
)))))))))
901 (defun loop-make-var (name initialization dtype
&optional step-var-p
)
903 (setq name
(gensym "LOOP-IGNORE-"))
904 (push (list name
(or initialization
(loop-typed-init dtype step-var-p
)))
906 (push `(ignore ,name
) *loop-declarations
*)
907 (loop-declare-var name dtype
))
909 (check-var-name name
)
910 (loop-declare-var name dtype
:step-var-p step-var-p
911 :initialization initialization
)
912 ;; We use ASSOC on this list to check for duplications (above),
913 ;; so don't optimize out this list:
914 (push (list name
(or initialization
(loop-typed-init dtype step-var-p
)))
917 (check-var-name name
)
918 (let ((newvar (gensym "LOOP-DESTRUCTURE-")))
919 (loop-declare-var name dtype
:desetq t
)
920 (push (list newvar initialization
) *loop-vars
*)
921 ;; *LOOP-DESETQ* gathered in reverse order.
923 (list* name newvar
*loop-desetq
*))))
925 (let ((tcar nil
) (tcdr nil
))
926 (if (atom dtype
) (setq tcar
(setq tcdr dtype
))
927 (setq tcar
(car dtype
) tcdr
(cdr dtype
)))
928 (loop-make-var (car name
) nil tcar
)
930 (loop-make-var (cdr name
) nil tcdr
)))))
933 ;;; Find a suitable type for default initialization
934 (defun type-for-default-init (type &optional step-var-p
)
935 (multiple-value-bind (init empty-type
)
936 (loop-typed-init type step-var-p
)
939 ;; Don't wrap empty types `(or ...), otherwise the will no
940 ;; longer be empty and the compiler won't produce
943 ((sb!xc
:typep init type
)
945 ((sb!xc
:typep init
'(simple-array * (*)))
946 ;; type-of lets the size in
947 `(or (simple-array ,(array-element-type init
) (*)) ,type
))
949 `(or ,(sb!xc
:type-of init
) ,type
)))
952 (defun loop-declare-var (name dtype
&key step-var-p initialization
954 (cond ((or (null name
) (null dtype
) (eq dtype t
)) nil
)
956 (unless (or (sb!xc
:subtypep t dtype
)
957 (and (eq (find-package :cl
) (symbol-package name
))
958 (eq :special
(sb!int
:info
:variable
:kind name
))))
959 (let ((dtype `(type ,(if initialization
961 (type-for-default-init dtype step-var-p
))
964 (push dtype
*loop-desetq-declarations
*)
965 (push dtype
*loop-declarations
*)))))
968 (loop-declare-var (car name
) (car dtype
)
970 (loop-declare-var (cdr name
) (cdr dtype
)
972 (t (loop-declare-var (car name
) dtype
974 (loop-declare-var (cdr name
) dtype
976 (t (error "invalid LOOP variable passed in: ~S" name
))))
978 (defun loop-maybe-bind-form (form data-type
)
981 (loop-make-var (gensym "LOOP-BIND-") form data-type
)))
983 (defun loop-do-if (for negatep
)
984 (let ((form (loop-get-form))
985 (*loop-inside-conditional
* t
)
988 (flet ((get-clause (for)
989 (do ((body nil
)) (nil)
990 (let ((key (car *loop-source-code
*)) (*loop-body
* nil
) data
)
991 (cond ((not (symbolp key
))
993 "~S found where keyword expected getting LOOP clause after ~S"
995 (t (setq *loop-source-context
* *loop-source-code
*)
997 (when (and (loop-tequal (car *loop-source-code
*) 'it
)
999 (setq *loop-source-code
*
1002 (loop-when-it-var)))
1003 (cdr *loop-source-code
*))))
1004 (cond ((or (not (setq data
(loop-lookup-keyword
1005 key
(loop-universe-keywords *loop-universe
*))))
1006 (progn (apply (symbol-function (car data
))
1008 (null *loop-body
*)))
1010 "~S does not introduce a LOOP clause that can follow ~S."
1012 (t (setq body
(nreconc *loop-body
* body
)))))))
1013 (setq first-clause-p nil
)
1014 (if (loop-tequal (car *loop-source-code
*) :and
)
1016 (return (if (cdr body
)
1017 `(progn ,@(nreverse body
))
1019 (let ((then (get-clause for
))
1020 (else (when (loop-tequal (car *loop-source-code
*) :else
)
1022 (list (get-clause :else
)))))
1023 (when (loop-tequal (car *loop-source-code
*) :end
)
1025 (when it-p
(setq form
`(setq ,it-p
,form
)))
1027 `(if ,(if negatep
`(not ,form
) form
)
1031 (defun loop-do-initially ()
1032 (loop-disallow-conditional :initially
)
1033 (push (loop-get-progn) *loop-prologue
*))
1035 (defun loop-do-finally ()
1036 (loop-disallow-conditional :finally
)
1037 (push (loop-get-progn) *loop-epilogue
*))
1039 (defun loop-do-do ()
1040 (loop-emit-body (loop-get-progn)))
1042 (defun loop-do-named ()
1043 (let ((name (loop-pop-source)))
1044 (unless (symbolp name
)
1045 (loop-error "~S is an invalid name for your LOOP" name
))
1046 (when (or *loop-before-loop
* *loop-body
* *loop-after-epilogue
* *loop-inside-conditional
*)
1047 (loop-error "The NAMED ~S clause occurs too late." name
))
1049 (loop-error "You may only use one NAMED clause in your loop: NAMED ~S ... NAMED ~S."
1050 (car *loop-names
*) name
))
1051 (setq *loop-names
* (list name
))))
1053 (defun loop-do-return ()
1054 (loop-emit-body (loop-construct-return (loop-get-form))))
1056 ;;;; value accumulation: LIST
1058 (defstruct (loop-collector
1067 (data nil
)) ;collector-specific data
1069 (sb!xc
:defmacro with-sum-count
(lc &body body
)
1070 (let* ((type (loop-collector-dtype lc
))
1071 (temp-var (car (loop-collector-tempvars lc
))))
1072 (multiple-value-bind (type init
)
1073 (type-for-default-init type
)
1074 `(let ((,temp-var
,init
))
1075 (declare (type ,type
,temp-var
))
1078 (defun loop-get-collection-info (collector class default-type
)
1079 (let ((form (loop-get-form))
1080 (name (when (loop-tequal (car *loop-source-code
*) 'into
)
1082 (loop-pop-source))))
1083 (when (not (symbolp name
))
1084 (loop-error "The value accumulation recipient name, ~S, is not a symbol." name
))
1086 (loop-disallow-aggregate-booleans))
1087 (let* ((specified-type (loop-optional-type))
1088 (dtype (or specified-type default-type
))
1089 (cruft (find (the symbol name
) *loop-collection-cruft
*
1090 :key
#'loop-collector-name
)))
1092 (check-var-name name
" in INTO clause")
1093 (push (setq cruft
(make-loop-collector
1094 :name name
:class class
1095 :history
(list collector
)
1096 :specified-type specified-type
1098 *loop-collection-cruft
*))
1099 (t (unless (eq (loop-collector-class cruft
) class
)
1101 "incompatible kinds of LOOP value accumulation specified for collecting~@
1102 ~:[as the value of the LOOP~;~:*INTO ~S~]: ~S and ~S"
1103 name
(car (loop-collector-history cruft
)) collector
))
1104 (cond ((equal dtype
(loop-collector-dtype cruft
)))
1105 ((and (null specified-type
)
1106 (null (loop-collector-specified-type cruft
)))
1107 ;; Unionize types only for default types, most
1108 ;; likely, SUM and COUNT which have number and
1109 ;; fixnum respectively.
1110 (setf (loop-collector-dtype cruft
)
1111 (sb!kernel
:type-specifier
1112 (sb!kernel
:type-union
1113 (sb!kernel
:specifier-type dtype
)
1114 (sb!kernel
:specifier-type
(loop-collector-dtype cruft
))))))
1117 "unequal datatypes specified in different LOOP value accumulations~@
1119 name dtype
(loop-collector-dtype cruft
))
1120 (when (eq (loop-collector-dtype cruft
) t
)
1121 (setf (loop-collector-dtype cruft
) dtype
))))
1122 (push collector
(loop-collector-history cruft
))))
1123 (values cruft form
))))
1125 (defun loop-list-collection (specifically) ; NCONC, LIST, or APPEND
1126 (multiple-value-bind (lc form
)
1127 (loop-get-collection-info specifically
'list
'list
)
1128 (let ((tempvars (loop-collector-tempvars lc
)))
1130 (setf (loop-collector-tempvars lc
)
1131 (setq tempvars
(list* (gensym "LOOP-LIST-HEAD-")
1132 (gensym "LOOP-LIST-TAIL-")
1133 (and (loop-collector-name lc
)
1134 (list (loop-collector-name lc
))))))
1135 (push `(with-loop-list-collection-head ,tempvars
) *loop-wrappers
*)
1136 (unless (loop-collector-name lc
)
1137 (loop-emit-final-value `(loop-collect-answer ,(car tempvars
)
1138 ,@(cddr tempvars
)))))
1140 (list (setq form
`(list ,form
)))
1142 (append (unless (and (consp form
) (eq (car form
) 'list
))
1143 (setq form
`(copy-list ,form
)))))
1144 (loop-emit-body `(loop-collect-rplacd ,tempvars
,form
)))))
1146 ;;;; value accumulation: MAX, MIN, SUM, COUNT
1148 (defun loop-sum-collection (specifically required-type default-type
);SUM, COUNT
1149 (multiple-value-bind (lc form
)
1150 (loop-get-collection-info specifically
'sum default-type
)
1151 (loop-check-data-type (loop-collector-dtype lc
) required-type
)
1152 (let ((tempvars (loop-collector-tempvars lc
)))
1154 (setf (loop-collector-tempvars lc
)
1155 (setq tempvars
(list (or (loop-collector-name lc
)
1156 (gensym "LOOP-SUM-")))))
1157 (unless (loop-collector-name lc
)
1158 (loop-emit-final-value (car (loop-collector-tempvars lc
))))
1159 (push `(with-sum-count ,lc
) *loop-wrappers
*))
1161 (if (eq specifically
'count
)
1163 (setq ,(car tempvars
)
1164 (1+ ,(car tempvars
))))
1165 `(setq ,(car tempvars
)
1169 (defun loop-maxmin-collection (specifically)
1170 (multiple-value-bind (lc form
)
1171 (loop-get-collection-info specifically
'maxmin
'real
)
1172 (loop-check-data-type (loop-collector-dtype lc
) 'real
)
1173 (let ((data (loop-collector-data lc
)))
1175 (setf (loop-collector-data lc
)
1176 (setq data
(make-loop-minimax
1177 (or (loop-collector-name lc
)
1178 (gensym "LOOP-MAXMIN-"))
1179 (loop-collector-dtype lc
))))
1180 (unless (loop-collector-name lc
)
1181 (loop-emit-final-value (loop-minimax-answer-variable data
)))
1182 (push `(with-minimax-value ,data
) *loop-wrappers
*))
1183 (loop-note-minimax-operation specifically data
)
1184 (loop-emit-body `(loop-accumulate-minimax-value ,data
1188 ;;;; value accumulation: aggregate booleans
1190 ;;; handling the ALWAYS and NEVER loop keywords
1192 ;;; Under ANSI these are not permitted to appear under conditionalization.
1193 (defun loop-do-always (restrictive negate
)
1194 (let ((form (loop-get-form)))
1195 (when restrictive
(loop-disallow-conditional))
1196 (loop-disallow-anonymous-collectors)
1197 (loop-emit-body `(,(if negate
'when
'unless
) ,form
1198 ,(loop-construct-return nil
)))
1199 (loop-emit-final-value t
)))
1201 ;;; handling the THEREIS loop keyword
1203 ;;; Under ANSI this is not permitted to appear under conditionalization.
1204 (defun loop-do-thereis (restrictive)
1205 (when restrictive
(loop-disallow-conditional))
1206 (loop-disallow-anonymous-collectors)
1207 (loop-emit-final-value)
1208 (loop-emit-body `(when (setq ,(loop-when-it-var) ,(loop-get-form))
1209 ,(loop-construct-return *loop-when-it-var
*))))
1211 (defun loop-do-while (negate kwd
&aux
(form (loop-get-form)))
1212 (loop-disallow-conditional kwd
)
1213 (loop-pseudo-body `(,(if negate
'when
'unless
) ,form
(go end-loop
))))
1215 (defun loop-do-repeat ()
1216 (loop-disallow-conditional :repeat
)
1217 (let* ((form (loop-get-form))
1218 (count (and (constantp form
) ; FIXME: lexical environment constants
1219 (sb!int
:constant-form-value form
)))
1220 (type (cond ((not (realp count
))
1223 `(mod ,(1+ (ceiling count
))))
1225 `(integer ,(ceiling count
))))))
1226 (let ((var (loop-make-var (gensym "LOOP-REPEAT-") `(ceiling ,form
) type
)))
1227 (push `(if (<= ,var
0) (go end-loop
) (decf ,var
)) *loop-before-loop
*)
1228 (push `(if (<= ,var
0) (go end-loop
) (decf ,var
)) *loop-after-body
*)
1229 ;; FIXME: What should
1230 ;; (loop count t into a
1233 ;; finally (return (list a b)))
1234 ;; return: (3 3) or (4 3)? PUSHes above are for the former
1235 ;; variant, L-P-B below for the latter.
1236 #+nil
(loop-pseudo-body `(when (minusp (decf ,var
)) (go end-loop
))))))
1238 (defun loop-do-with ()
1239 (loop-disallow-conditional :with
)
1240 (do ((var) (val) (dtype))
1242 (setq var
(loop-pop-source)
1243 dtype
(loop-optional-type var
)
1244 val
(cond ((loop-tequal (car *loop-source-code
*) :=)
1248 (loop-make-var var val dtype
)
1249 (if (loop-tequal (car *loop-source-code
*) :and
)
1251 (return (loop-bind-block)))))
1253 ;;;; the iteration driver
1255 (defun loop-hack-iteration (entry)
1256 (flet ((make-endtest (list-of-forms)
1257 (cond ((null list-of-forms
) nil
)
1258 ((member t list-of-forms
) '(go end-loop
))
1259 (t `(when ,(if (null (cdr (setq list-of-forms
1260 (nreverse list-of-forms
))))
1262 (cons 'or list-of-forms
))
1264 (do ((pre-step-tests nil
)
1266 (post-step-tests nil
)
1268 (pre-loop-pre-step-tests nil
)
1269 (pre-loop-steps nil
)
1270 (pre-loop-post-step-tests nil
)
1271 (pre-loop-pseudo-steps nil
)
1274 ;; Note that we collect endtests in reverse order, but steps in correct
1275 ;; order. MAKE-ENDTEST does the nreverse for us.
1276 (setq tem
(setq data
1277 (apply (symbol-function (first entry
)) (rest entry
))))
1278 (and (car tem
) (push (car tem
) pre-step-tests
))
1279 (setq steps
(nconc steps
(copy-list (car (setq tem
(cdr tem
))))))
1280 (and (car (setq tem
(cdr tem
))) (push (car tem
) post-step-tests
))
1282 (nconc pseudo-steps
(copy-list (car (setq tem
(cdr tem
))))))
1283 (setq tem
(cdr tem
))
1284 (when *loop-emitted-body
*
1285 (loop-error "iteration in LOOP follows body code"))
1286 (unless tem
(setq tem data
))
1287 (when (car tem
) (push (car tem
) pre-loop-pre-step-tests
))
1288 ;; FIXME: This (SETF FOO (NCONC FOO BAR)) idiom appears often enough
1289 ;; that it might be worth making it into an NCONCF macro.
1290 (setq pre-loop-steps
1291 (nconc pre-loop-steps
(copy-list (car (setq tem
(cdr tem
))))))
1292 (when (car (setq tem
(cdr tem
)))
1293 (push (car tem
) pre-loop-post-step-tests
))
1294 (setq pre-loop-pseudo-steps
1295 (nconc pre-loop-pseudo-steps
(copy-list (cadr tem
))))
1296 (unless (loop-tequal (car *loop-source-code
*) :and
)
1297 (setq *loop-before-loop
*
1298 (list* (loop-make-desetq pre-loop-pseudo-steps
)
1299 (make-endtest pre-loop-post-step-tests
)
1300 (loop-make-psetq pre-loop-steps
)
1301 (make-endtest pre-loop-pre-step-tests
)
1302 *loop-before-loop
*))
1303 (setq *loop-after-body
*
1304 (list* (loop-make-desetq pseudo-steps
)
1305 (make-endtest post-step-tests
)
1306 (loop-make-psetq steps
)
1307 (make-endtest pre-step-tests
)
1311 (loop-pop-source)))) ; Flush the "AND".
1313 ;;;; main iteration drivers
1315 ;;; FOR variable keyword ..args..
1316 (defun loop-do-for ()
1317 (let* ((var (loop-pop-source))
1318 (data-type (loop-optional-type var
))
1319 (keyword (loop-pop-source))
1322 (setq first-arg
(loop-get-form))
1323 (unless (and (symbolp keyword
)
1324 (setq tem
(loop-lookup-keyword
1326 (loop-universe-for-keywords *loop-universe
*))))
1327 (loop-error "~S is an unknown keyword in FOR or AS clause in LOOP."
1329 (apply (car tem
) var first-arg data-type
(cdr tem
))))
1331 (defun loop-when-it-var ()
1332 (or *loop-when-it-var
*
1333 (setq *loop-when-it-var
*
1334 (loop-make-var (gensym "LOOP-IT-") nil nil
))))
1336 ;;;; various FOR/AS subdispatches
1338 ;;; ANSI "FOR x = y [THEN z]" is sort of like the old Genera one when
1339 ;;; the THEN is omitted (other than being more stringent in its
1340 ;;; placement), and like the old "FOR x FIRST y THEN z" when the THEN
1341 ;;; is present. I.e., the first initialization occurs in the loop body
1342 ;;; (first-step), not in the variable binding phase.
1343 (defun loop-ansi-for-equals (var val data-type
)
1344 (loop-make-var var nil data-type
)
1345 (cond ((loop-tequal (car *loop-source-code
*) :then
)
1346 ;; Then we are the same as "FOR x FIRST y THEN z".
1348 `(() (,var
,(loop-get-form)) () ()
1349 () (,var
,val
) () ()))
1350 (t ;; We are the same as "FOR x = y".
1351 `(() (,var
,val
) () ()))))
1353 (defun loop-for-across (var val data-type
)
1354 (loop-make-var var nil data-type
)
1355 (let ((vector-var (gensym "LOOP-ACROSS-VECTOR-"))
1356 (index-var (gensym "LOOP-ACROSS-INDEX-")))
1357 (multiple-value-bind (vector-form constantp vector-value
)
1358 (loop-constant-fold-if-possible val
'vector
)
1360 vector-var vector-form
1361 (if (and (consp vector-form
) (eq (car vector-form
) 'the
))
1364 (loop-make-var index-var
0 'fixnum
)
1366 (length-form (cond ((not constantp
)
1367 (let ((v (gensym "LOOP-ACROSS-LIMIT-")))
1368 (push `(setq ,v
(length ,vector-var
))
1370 (loop-make-var v
0 'fixnum
)))
1371 (t (setq length
(length vector-value
)))))
1372 (first-test `(>= ,index-var
,length-form
))
1373 (other-test first-test
)
1374 (step `(,var
(aref ,vector-var
,index-var
)))
1375 (pstep `(,index-var
(1+ ,index-var
))))
1376 (declare (fixnum length
))
1378 (setq first-test
(= length
0))
1380 (setq other-test t
)))
1381 `(,other-test
,step
() ,pstep
1382 ,@(and (not (eq first-test other-test
))
1383 `(,first-test
,step
() ,pstep
)))))))
1387 (defun loop-list-step (listvar)
1388 ;; We are not equipped to analyze whether 'FOO is the same as #'FOO
1389 ;; here in any sensible fashion, so let's give an obnoxious warning
1390 ;; whenever 'FOO is used as the stepping function.
1392 ;; While a Discerning Compiler may deal intelligently with
1393 ;; (FUNCALL 'FOO ...), not recognizing FOO may defeat some LOOP
1395 (let ((stepper (cond ((loop-tequal (car *loop-source-code
*) :by
)
1398 (t '(function cdr
)))))
1399 (cond ((and (consp stepper
) (eq (car stepper
) 'quote
))
1400 (loop-warn "Use of QUOTE around stepping function in LOOP will be left verbatim.")
1401 `(funcall ,stepper
,listvar
))
1402 ((and (consp stepper
) (eq (car stepper
) 'function
))
1403 (list (cadr stepper
) listvar
))
1405 `(funcall ,(loop-make-var (gensym "LOOP-FN-") stepper
'function
)
1408 (defun loop-for-on (var val data-type
)
1409 (multiple-value-bind (list constantp list-value
)
1410 (loop-constant-fold-if-possible val
)
1411 (let ((listvar var
))
1412 (cond ((and var
(symbolp var
))
1413 (loop-make-var var list data-type
))
1415 (loop-make-var (setq listvar
(gensym)) list
't
)
1416 (loop-make-var var nil data-type
)))
1417 (let ((list-step (loop-list-step listvar
)))
1418 (let* ((first-endtest
1419 ;; mysterious comment from original CMU CL sources:
1420 ;; the following should use `atom' instead of `endp',
1423 (other-endtest first-endtest
))
1424 (when (and constantp
(listp list-value
))
1425 (setq first-endtest
(null list-value
)))
1426 (cond ((eq var listvar
)
1427 ;; The contour of the loop is different because we
1428 ;; use the user's variable...
1429 `(() (,listvar
,list-step
)
1430 ,other-endtest
() () () ,first-endtest
()))
1431 (t (let ((step `(,var
,listvar
))
1432 (pseudo `(,listvar
,list-step
)))
1433 `(,other-endtest
,step
() ,pseudo
1434 ,@(and (not (eq first-endtest other-endtest
))
1435 `(,first-endtest
,step
() ,pseudo
)))))))))))
1437 (defun loop-for-in (var val data-type
)
1438 (multiple-value-bind (list constantp list-value
)
1439 (loop-constant-fold-if-possible val
)
1440 (let ((listvar (gensym "LOOP-LIST-")))
1441 (loop-make-var var nil data-type
)
1442 (loop-make-var listvar list
'list
)
1443 (let ((list-step (loop-list-step listvar
)))
1444 (let* ((first-endtest `(endp ,listvar
))
1445 (other-endtest first-endtest
)
1446 (step `(,var
(car ,listvar
)))
1447 (pseudo-step `(,listvar
,list-step
)))
1448 (when (and constantp
(listp list-value
))
1449 (setq first-endtest
(null list-value
)))
1450 `(,other-endtest
,step
() ,pseudo-step
1451 ,@(and (not (eq first-endtest other-endtest
))
1452 `(,first-endtest
,step
() ,pseudo-step
))))))))
1454 ;;;; iteration paths
1456 (defstruct (loop-path
1465 (defun add-loop-path (names function universe
1466 &key preposition-groups inclusive-permitted user-data
)
1467 (declare (type loop-universe universe
))
1468 (let* ((names (sb!int
:ensure-list names
))
1469 (ht (loop-universe-path-keywords universe
))
1471 :names
(mapcar #'symbol-name names
)
1473 :user-data user-data
1474 :preposition-groups
(mapcar #'sb
!int
:ensure-list preposition-groups
)
1475 :inclusive-permitted inclusive-permitted
)))
1476 (dolist (name names
)
1477 (setf (gethash (symbol-name name
) ht
) lp
))
1480 ;;; Note: Path functions are allowed to use LOOP-MAKE-VAR, hack
1481 ;;; the prologue, etc.
1482 (defun loop-for-being (var val data-type
)
1483 ;; FOR var BEING each/the pathname prep-phrases using-stuff... each/the =
1484 ;; EACH or THE. Not clear if it is optional, so I guess we'll warn.
1489 (initial-prepositions nil
))
1490 (cond ((loop-tmember val
'(:each
:the
)) (setq path
(loop-pop-source)))
1491 ((loop-tequal (car *loop-source-code
*) :and
)
1494 (unless (loop-tmember (car *loop-source-code
*)
1495 '(:its
:each
:his
:her
))
1496 (loop-error "~S was found where ITS or EACH expected in LOOP iteration path syntax."
1497 (car *loop-source-code
*)))
1499 (setq path
(loop-pop-source))
1500 (setq initial-prepositions
`((:in
,val
))))
1501 (t (loop-error "unrecognizable LOOP iteration path syntax: missing EACH or THE?")))
1502 (cond ((not (symbolp path
))
1504 "~S was found where a LOOP iteration path name was expected."
1506 ((not (setq data
(loop-lookup-keyword path
(loop-universe-path-keywords *loop-universe
*))))
1507 (loop-error "~S is not the name of a LOOP iteration path." path
))
1508 ((and inclusive
(not (loop-path-inclusive-permitted data
)))
1509 (loop-error "\"Inclusive\" iteration is not possible with the ~S LOOP iteration path." path
)))
1510 (let ((fun (loop-path-function data
))
1511 (preps (nconc initial-prepositions
1512 (loop-collect-prepositional-phrases
1513 (loop-path-preposition-groups data
)
1515 (user-data (loop-path-user-data data
)))
1516 (when (symbolp fun
) (setq fun
(symbol-function fun
)))
1517 (setq stuff
(if inclusive
1518 (apply fun var data-type preps
:inclusive t user-data
)
1519 (apply fun var data-type preps user-data
))))
1520 (when *loop-named-vars
*
1521 (loop-error "Unused USING vars: ~S." *loop-named-vars
*))
1522 ;; STUFF is now (bindings prologue-forms . stuff-to-pass-back).
1523 ;; Protect the system from the user and the user from himself.
1524 (unless (member (length stuff
) '(6 10))
1525 (loop-error "Value passed back by LOOP iteration path function for path ~S has invalid length."
1527 (do ((l (car stuff
) (cdr l
)) (x)) ((null l
))
1528 (if (atom (setq x
(car l
)))
1529 (loop-make-var x nil nil
)
1530 (loop-make-var (car x
) (cadr x
) (caddr x
))))
1531 (setq *loop-prologue
* (nconc (reverse (cadr stuff
)) *loop-prologue
*))
1534 (defun loop-named-var (name)
1535 (let ((tem (loop-tassoc name
*loop-named-vars
*)))
1536 (declare (list tem
))
1537 (cond ((null tem
) (values (gensym) nil
))
1538 (t (setq *loop-named-vars
* (delete tem
*loop-named-vars
*))
1539 (values (cdr tem
) t
)))))
1541 (defun loop-collect-prepositional-phrases (preposition-groups
1545 (flet ((in-group-p (x group
) (car (loop-tmember x group
))))
1547 (prepositional-phrases initial-phrases
)
1548 (this-group nil nil
)
1550 (disallowed-prepositions
1553 (find (car x
) preposition-groups
:test
#'in-group-p
)))
1555 (used-prepositions (mapcar #'car initial-phrases
)))
1556 ((null *loop-source-code
*) (nreverse prepositional-phrases
))
1557 (declare (symbol this-prep
))
1558 (setq token
(car *loop-source-code
*))
1559 (dolist (group preposition-groups
)
1560 (when (setq this-prep
(in-group-p token group
))
1561 (return (setq this-group group
))))
1563 (when (member this-prep disallowed-prepositions
)
1565 (if (member this-prep used-prepositions
)
1566 "A ~S prepositional phrase occurs multiply for some LOOP clause."
1567 "Preposition ~S was used when some other preposition has subsumed it.")
1569 (setq used-prepositions
(if (listp this-group
)
1570 (append this-group used-prepositions
)
1571 (cons this-group used-prepositions
)))
1573 (push (list this-prep
(loop-get-form)) prepositional-phrases
))
1574 ((and using-allowed
(loop-tequal token
'using
))
1576 (do ((z (loop-pop-source) (loop-pop-source)) (tem)) (nil)
1578 (if (setq tem
(loop-tassoc (car z
) *loop-named-vars
*))
1580 "The variable substitution for ~S occurs twice in a USING phrase,~@
1582 (car z
) (cadr z
) (cadr tem
))
1583 (push (cons (car z
) (cadr z
)) *loop-named-vars
*)))
1584 (when (or (null *loop-source-code
*)
1585 (symbolp (car *loop-source-code
*)))
1587 (t (return (nreverse prepositional-phrases
)))))))
1589 ;;;; master sequencer function
1591 (defun loop-sequencer (indexv indexv-type
1592 variable variable-type
1593 sequence-variable sequence-type
1594 step-hack default-top
1596 (let ((endform nil
) ; form (constant or variable) with limit value
1597 (sequencep nil
) ; T if sequence arg has been provided
1598 (testfn nil
) ; endtest function
1599 (test nil
) ; endtest form
1600 (stepby (1+ (or (loop-typed-init indexv-type
) 0))) ; our increment
1601 (stepby-constantp t
)
1602 (step nil
) ; step form
1603 (dir nil
) ; direction of stepping: NIL, :UP, :DOWN
1604 (inclusive-iteration nil
) ; T if include last index
1605 (start-given nil
) ; T when prep phrase has specified start
1607 (start-constantp nil
)
1608 (limit-given nil
) ; T when prep phrase has specified end
1609 (limit-constantp nil
)
1611 #+ccl
(progn start-constantp start-value
) ; bogus "Unused" warnings
1612 (flet ((assert-index-for-arithmetic (index)
1613 (unless (atom index
)
1614 (loop-error "Arithmetic index must be an atom."))))
1615 (when variable
(loop-make-var variable nil variable-type
))
1616 (do ((l prep-phrases
(cdr l
)) (prep) (form) (odir)) ((null l
))
1617 (setq prep
(caar l
) form
(cadar l
))
1621 (loop-make-var sequence-variable form sequence-type
))
1622 ((:from
:downfrom
:upfrom
)
1623 (setq start-given t
)
1624 (cond ((eq prep
:downfrom
) (setq dir
':down
))
1625 ((eq prep
:upfrom
) (setq dir
':up
)))
1626 (multiple-value-setq (form start-constantp start-value
)
1627 (loop-constant-fold-if-possible form indexv-type
))
1628 (assert-index-for-arithmetic indexv
)
1629 ;; KLUDGE: loop-make-var generates a temporary symbol for
1630 ;; indexv if it is NIL. We have to use it to have the index
1632 (setq indexv
(loop-make-var indexv form indexv-type
)))
1633 ((:upto
:to
:downto
:above
:below
)
1634 (cond ((loop-tequal prep
:upto
) (setq inclusive-iteration
1636 ((loop-tequal prep
:to
) (setq inclusive-iteration t
))
1637 ((loop-tequal prep
:downto
) (setq inclusive-iteration
1639 ((loop-tequal prep
:above
) (setq dir
':down
))
1640 ((loop-tequal prep
:below
) (setq dir
':up
)))
1641 (setq limit-given t
)
1642 (multiple-value-setq (form limit-constantp limit-value
)
1643 (loop-constant-fold-if-possible form
`(and ,indexv-type real
)))
1644 (setq endform
(if limit-constantp
1647 (gensym "LOOP-LIMIT-") form
1648 `(and ,indexv-type real
)))))
1650 (multiple-value-setq (form stepby-constantp stepby
)
1651 (loop-constant-fold-if-possible form
1652 `(and ,indexv-type
(real (0)))))
1653 (unless stepby-constantp
1654 (loop-make-var (setq stepby
(gensym "LOOP-STEP-BY-"))
1656 `(and ,indexv-type
(real (0)))
1659 "~S invalid preposition in sequencing or sequence path;~@
1660 maybe invalid prepositions were specified in iteration path descriptor?"
1662 (when (and odir dir
(not (eq dir odir
)))
1664 "conflicting stepping directions in LOOP sequencing path"))
1666 (when (and sequence-variable
(not sequencep
))
1667 (loop-error "missing OF or IN phrase in sequence path"))
1668 ;; Now fill in the defaults.
1669 (cond ((not start-given
)
1671 ;; DUPLICATE KLUDGE: loop-make-var generates a temporary
1672 ;; symbol for indexv if it is NIL. See also the comment in
1673 ;; the (:from :downfrom :upfrom) case
1674 (assert-index-for-arithmetic indexv
)
1678 (setq start-constantp t
1679 start-value
(or (loop-typed-init indexv-type
) 0))
1680 `(and ,indexv-type real
))))
1682 ;; if both start and limit are given, they had better both
1683 ;; be REAL. We already enforce the REALness of LIMIT,
1684 ;; above; here's the KLUDGE to enforce the type of START.
1685 (flet ((type-declaration-of (x)
1686 (and (eq (car x
) 'type
) (caddr x
))))
1687 (let ((decl (find indexv
*loop-declarations
*
1688 :key
#'type-declaration-of
))
1689 (%decl
(find indexv
*loop-declarations
*
1690 :key
#'type-declaration-of
1692 (sb!int
:aver
(eq decl %decl
))
1695 `(and real
,(cadr decl
))))))))
1696 (cond ((member dir
'(nil :up
))
1697 (when (or limit-given default-top
)
1699 (loop-make-var (setq endform
(gensym "LOOP-SEQ-LIMIT-"))
1702 (push `(setq ,endform
,default-top
) *loop-prologue
*))
1703 (setq testfn
(if inclusive-iteration
'> '>=)))
1704 (setq step
(if (eql stepby
1) `(1+ ,indexv
) `(+ ,indexv
,stepby
))))
1705 (t (unless start-given
1707 (loop-error "don't know where to start stepping"))
1708 (push `(setq ,indexv
(1- ,default-top
)) *loop-prologue
*))
1709 (when (and default-top
(not endform
))
1710 (setq endform
(loop-typed-init indexv-type
)
1711 inclusive-iteration t
))
1712 (when endform
(setq testfn
(if inclusive-iteration
'< '<=)))
1714 (if (eql stepby
1) `(1- ,indexv
) `(- ,indexv
,stepby
)))))
1717 `(,testfn
,indexv
,endform
)))
1720 `(,variable
,step-hack
)))
1721 (let ((first-test test
) (remaining-tests test
))
1722 ;; As far as I can tell, the effect of the following code is
1723 ;; to detect cases where we know statically whether the first
1724 ;; iteration of the loop will be executed. Depending on the
1725 ;; situation, we can either:
1726 ;; a) save one jump and one comparison per loop (not per iteration)
1727 ;; when it will get executed
1728 ;; b) remove the loop body completely when it won't be executed
1730 ;; Noble goals. However, the code generated in case a) will
1731 ;; fool the loop induction variable detection, and cause
1732 ;; code like (LOOP FOR I TO 10 ...) to use generic addition
1735 ;; Since the gain in case a) is rather minimal and Python is
1736 ;; generally smart enough to handle b) without any extra
1737 ;; support from the loop macro, I've disabled this code for
1738 ;; now. The code and the comment left here in case somebody
1739 ;; extends the induction variable bound detection to work
1740 ;; with code where the stepping precedes the test.
1741 ;; -- JES 2005-11-30
1743 (when (and stepby-constantp start-constantp limit-constantp
1744 (realp start-value
) (realp limit-value
))
1745 (when (setq first-test
1746 (funcall (symbol-function testfn
)
1749 (setq remaining-tests t
)))
1750 `(() (,indexv
,step
)
1751 ,remaining-tests
,step-hack
() () ,first-test
,step-hack
)))))
1753 ;;;; interfaces to the master sequencer
1755 (defun loop-for-arithmetic (var val data-type kwd
)
1757 var
(loop-check-data-type data-type
'number
)
1758 nil nil nil nil nil nil
1759 (loop-collect-prepositional-phrases
1760 '((:from
:upfrom
:downfrom
) (:to
:upto
:downto
:above
:below
) (:by
))
1761 nil
(list (list kwd val
)))))
1764 ;;;; builtin LOOP iteration paths
1767 (loop for v being the hash-values of ht do
(print v
))
1768 (loop for k being the hash-keys of ht do
(print k
))
1769 (loop for v being the hash-values of ht using
(hash-key k
) do
(print (list k v
)))
1770 (loop for k being the hash-keys of ht using
(hash-value v
) do
(print (list k v
)))
1773 (defun loop-hash-table-iteration-path (variable data-type prep-phrases
1774 &key
(which (sb!int
:missing-arg
)))
1775 (declare (type (member :hash-key
:hash-value
) which
))
1776 (cond ((or (cdr prep-phrases
) (not (member (caar prep-phrases
) '(:in
:of
))))
1777 (loop-error "too many prepositions!"))
1778 ((null prep-phrases
)
1779 (loop-error "missing OF or IN in ~S iteration path")))
1780 (let ((ht-var (gensym "LOOP-HASHTAB-"))
1781 (next-fn (gensym "LOOP-HASHTAB-NEXT-"))
1782 (dummy-predicate-var nil
)
1784 (multiple-value-bind (other-var other-p
)
1785 (loop-named-var (ecase which
1786 (:hash-key
'hash-value
)
1787 (:hash-value
'hash-key
)))
1788 ;; @@@@ LOOP-NAMED-VAR returns a second value of T if the name
1789 ;; was actually specified, so clever code can throw away the
1790 ;; GENSYM'ed-up variable if it isn't really needed. The
1791 ;; following is for those implementations in which we cannot put
1792 ;; dummy NILs into MULTIPLE-VALUE-SETQ variable lists.
1794 dummy-predicate-var
(loop-when-it-var))
1795 (let* ((key-var nil
)
1797 (variable (or variable
(gensym "LOOP-HASH-VAR-TEMP-")))
1798 (bindings `((,variable nil
,data-type
)
1799 (,ht-var
,(cadar prep-phrases
))
1800 ,@(and other-p other-var
`((,other-var nil
))))))
1802 (:hash-key
(setq key-var variable
1803 val-var
(and other-p other-var
)))
1804 (:hash-value
(setq key-var
(and other-p other-var
)
1806 (push `(with-hash-table-iterator (,next-fn
,ht-var
)) *loop-wrappers
*)
1807 (when (or (consp key-var
) data-type
)
1809 `(,key-var
,(setq key-var
(gensym "LOOP-HASH-KEY-TEMP-"))
1811 (push `(,key-var nil
) bindings
))
1812 (when (or (consp val-var
) data-type
)
1814 `(,val-var
,(setq val-var
(gensym "LOOP-HASH-VAL-TEMP-"))
1816 (push `(,val-var nil
) bindings
))
1817 `(,bindings
;bindings
1821 (not (multiple-value-setq (,dummy-predicate-var
,key-var
,val-var
)
1822 (,next-fn
))) ;post-test
1825 (defun loop-package-symbols-iteration-path (variable data-type prep-phrases
1827 (cond ((and prep-phrases
(cdr prep-phrases
))
1828 (loop-error "Too many prepositions!"))
1829 ((and prep-phrases
(not (member (caar prep-phrases
) '(:in
:of
))))
1830 (sb!int
:bug
"Unknown preposition ~S." (caar prep-phrases
))))
1831 (unless (symbolp variable
)
1832 (loop-error "Destructuring is not valid for package symbol iteration."))
1833 (let ((pkg-var (gensym "LOOP-PKGSYM-"))
1834 (next-fn (gensym "LOOP-PKGSYM-NEXT-"))
1835 (variable (or variable
(gensym "LOOP-PKGSYM-VAR-")))
1836 (package (or (cadar prep-phrases
) '*package
*)))
1837 (push `(with-package-iterator (,next-fn
,pkg-var
,@symbol-types
))
1839 `(((,variable nil
,data-type
) (,pkg-var
,package
))
1843 (not (multiple-value-setq (,(loop-when-it-var)
1850 (defun !make-ansi-loop-universe
()
1851 (let ((w (!make-standard-loop-universe
1852 :keywords
'((named (loop-do-named))
1853 (initially (loop-do-initially))
1854 (finally (loop-do-finally))
1856 (doing (loop-do-do))
1857 (return (loop-do-return))
1858 (collect (loop-list-collection list
))
1859 (collecting (loop-list-collection list
))
1860 (append (loop-list-collection append
))
1861 (appending (loop-list-collection append
))
1862 (nconc (loop-list-collection nconc
))
1863 (nconcing (loop-list-collection nconc
))
1864 (count (loop-sum-collection count
1865 ;; This could be REAL, but when
1866 ;; combined with SUM, it has to be
1870 (counting (loop-sum-collection count
1873 (sum (loop-sum-collection sum number number
))
1874 (summing (loop-sum-collection sum number number
))
1875 (maximize (loop-maxmin-collection max
))
1876 (minimize (loop-maxmin-collection min
))
1877 (maximizing (loop-maxmin-collection max
))
1878 (minimizing (loop-maxmin-collection min
))
1879 (always (loop-do-always t nil
)) ; Normal, do always
1880 (never (loop-do-always t t
)) ; Negate test on always.
1881 (thereis (loop-do-thereis t
))
1882 (while (loop-do-while nil
:while
)) ; Normal, do while
1883 (until (loop-do-while t
:until
)) ;Negate test on while
1884 (when (loop-do-if when nil
)) ; Normal, do when
1885 (if (loop-do-if if nil
)) ; synonymous
1886 (unless (loop-do-if unless t
)) ; Negate test on when
1887 (with (loop-do-with))
1888 (repeat (loop-do-repeat)))
1889 :for-keywords
'((= (loop-ansi-for-equals))
1890 (across (loop-for-across))
1893 (from (loop-for-arithmetic :from
))
1894 (downfrom (loop-for-arithmetic :downfrom
))
1895 (upfrom (loop-for-arithmetic :upfrom
))
1896 (below (loop-for-arithmetic :below
))
1897 (above (loop-for-arithmetic :above
))
1898 (to (loop-for-arithmetic :to
))
1899 (upto (loop-for-arithmetic :upto
))
1900 (downto (loop-for-arithmetic :downto
))
1901 (by (loop-for-arithmetic :by
))
1902 (being (loop-for-being)))
1903 :iteration-keywords
'((for (loop-do-for))
1905 :type-symbols sb
!kernel
::*!standard-type-names
*
1906 :type-keywords nil
)))
1907 (add-loop-path '(hash-key hash-keys
) 'loop-hash-table-iteration-path w
1908 :preposition-groups
'((:of
:in
))
1909 :inclusive-permitted nil
1910 :user-data
'(:which
:hash-key
))
1911 (add-loop-path '(hash-value hash-values
) 'loop-hash-table-iteration-path w
1912 :preposition-groups
'((:of
:in
))
1913 :inclusive-permitted nil
1914 :user-data
'(:which
:hash-value
))
1915 (add-loop-path '(symbol symbols
) 'loop-package-symbols-iteration-path w
1916 :preposition-groups
'((:of
:in
))
1917 :inclusive-permitted nil
1918 :user-data
'(:symbol-types
(:internal
1921 (add-loop-path '(external-symbol external-symbols
)
1922 'loop-package-symbols-iteration-path w
1923 :preposition-groups
'((:of
:in
))
1924 :inclusive-permitted nil
1925 :user-data
'(:symbol-types
(:external
)))
1926 (add-loop-path '(present-symbol present-symbols
)
1927 'loop-package-symbols-iteration-path w
1928 :preposition-groups
'((:of
:in
))
1929 :inclusive-permitted nil
1930 :user-data
'(:symbol-types
(:internal
1934 (defparameter *loop-ansi-universe
*
1935 (!make-ansi-loop-universe
))
1937 (defun loop-standard-expansion (keywords-and-forms environment universe
)
1938 (if (and keywords-and-forms
(symbolp (car keywords-and-forms
)))
1939 (loop-translate keywords-and-forms environment universe
)
1940 (let ((tag (gensym)))
1941 `(block nil
(tagbody ,tag
(progn ,@keywords-and-forms
) (go ,tag
))))))
1943 (sb!xc
:defmacro loop
(&environment env
&rest keywords-and-forms
)
1944 (loop-standard-expansion keywords-and-forms env
*loop-ansi-universe
*))
1946 (sb!xc
:defmacro loop-finish
()
1948 "Cause the iteration to terminate \"normally\", the same as implicit
1949 termination by an iteration driving clause, or by use of WHILE or
1950 UNTIL -- the epilogue code (if any) will be run, and any implicitly
1951 collected result will be returned as the value of the LOOP."