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
(%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
)
170 (answer-variable nil
:read-only t
)
171 (type nil
:read-only t
)
172 (temp-variable nil
:read-only t
)
175 (infinity-data nil
:read-only t
))
177 (defconstant-eqx +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
))
183 (defun make-loop-minimax (answer-variable type
)
184 (let ((infinity-data (cdr (assoc type
185 +loop-minimax-type-infinities-alist
+
186 :test
#'sb
!xc
:subtypep
))))
187 (make-loop-minimax-internal
188 :answer-variable answer-variable
190 :temp-variable
(gensym "LOOP-MAXMIN-TEMP-")
191 :flag-variable
(and (not infinity-data
)
192 (gensym "LOOP-MAXMIN-FLAG-"))
194 :infinity-data infinity-data
)))
196 (defun loop-note-minimax-operation (operation minimax
)
197 (pushnew (the symbol operation
) (loop-minimax-operations minimax
))
198 (when (and (cdr (loop-minimax-operations minimax
))
199 (not (loop-minimax-flag-variable minimax
)))
200 (setf (loop-minimax-flag-variable minimax
)
201 (gensym "LOOP-MAXMIN-FLAG-")))
204 (sb!xc
:defmacro with-minimax-value
(lm &body body
)
205 (let ((init (loop-typed-init (loop-minimax-type lm
)))
206 (which (car (loop-minimax-operations lm
)))
207 (infinity-data (loop-minimax-infinity-data lm
))
208 (answer-var (loop-minimax-answer-variable lm
))
209 (temp-var (loop-minimax-temp-variable lm
))
210 (flag-var (loop-minimax-flag-variable lm
))
211 (type (loop-minimax-type lm
)))
213 `(let ((,answer-var
,init
) (,temp-var
,init
) (,flag-var nil
))
214 (declare (type ,type
,answer-var
,temp-var
))
216 `(let ((,answer-var
,(if (eq which
'min
)
217 (first infinity-data
)
218 (second infinity-data
)))
220 (declare (type ,type
,answer-var
,temp-var
))
223 (sb!xc
:defmacro loop-accumulate-minimax-value
(lm operation form
)
224 (let* ((answer-var (loop-minimax-answer-variable lm
))
225 (temp-var (loop-minimax-temp-variable lm
))
226 (flag-var (loop-minimax-flag-variable lm
))
227 (test `(,(ecase operation
230 ,temp-var
,answer-var
)))
232 (setq ,temp-var
,form
)
233 (when ,(if flag-var
`(or (not ,flag-var
) ,test
) test
)
234 (setq ,@(and flag-var
`(,flag-var t
))
235 ,answer-var
,temp-var
)))))
237 ;;;; LOOP keyword tables
240 LOOP keyword tables are hash tables string keys and a test of EQUAL.
242 The actual descriptive
/dispatch structure used by LOOP is called a
"loop
243 universe" contains a few tables and parameterizations. The basic idea is
244 that we can provide a non-extensible ANSI-compatible loop environment
,
245 an extensible ANSI-superset loop environment
, and
(for such environments
246 as CLOE
) one which is
"sufficiently close" to the old Genera-vintage
247 LOOP for use by old user programs without requiring all of the old LOOP
253 ;;; Compare two "tokens". The first is the frob out of (SOURCE-CODE *LOOP*),
254 ;;; the second a symbol to check against.
255 (defun loop-tequal (x1 x2
)
256 (and (symbolp x1
) (string= x1 x2
)))
258 (defun loop-tassoc (kwd alist
)
259 (and (symbolp kwd
) (assoc kwd alist
:test
#'string
=)))
261 (defun loop-tmember (kwd list
)
262 (and (symbolp kwd
) (member kwd list
:test
#'string
=)))
264 (defun loop-lookup-keyword (loop-token table
)
265 (and (symbolp loop-token
)
266 (values (gethash (symbol-name loop-token
) table
))))
268 (sb!xc
:defmacro loop-store-table-data
(symbol table datum
)
269 `(setf (gethash (symbol-name ,symbol
) ,table
) ,datum
))
271 ;;; "4.2.3 Type Specifiers" lists the standardized atomic type specifiers.
272 ;;; While in theory we might want to include all of them,
273 ;;; in practice it seems silly to allow "for x arithmetic-error in ..."
274 (defun std-atom-type-specifier-p (symbol)
275 (and (eq (symbol-package symbol
) *cl-package
*)
276 ;; Check for primitiveness of the type, since some symbols in the CL
277 ;; package are private-use typedefs (e.g. ARRAY-RANK, CHAR-CODE).
278 (or (eq (info :type
:kind symbol
) :primitive
)
279 ;; allow certain :instance types, but not all of them
280 (member symbol
'(hash-table package pathname random-state readtable
)))
284 (defstruct (loop-universe
285 (:constructor
!make-loop-universe
)
288 keywords
; hash table, value = (fn-name . extra-data)
289 iteration-keywords
; hash table, value = (fn-name . extra-data)
290 for-keywords
; hash table, value = (fn-name . extra-data)
291 path-keywords
) ; hash table, value = (fn-name . extra-data)
292 (defmethod print-object ((u loop-universe
) stream
)
293 (print-unreadable-object (u stream
:type t
:identity t
)))
295 (defun !make-standard-loop-universe
(&key keywords for-keywords
296 iteration-keywords path-keywords
)
297 (flet ((maketable (entries)
298 (let ((ht (make-hash-table :size
(max 10 (length entries
))
301 (setf (gethash (symbol-name (car x
)) ht
) (cadr x
)))
304 :keywords
(maketable keywords
)
305 :for-keywords
(maketable for-keywords
)
306 :iteration-keywords
(maketable iteration-keywords
)
307 :path-keywords
(maketable path-keywords
))))
309 ;;;; SETQ hackery, including destructuring ("DESETQ")
311 (defun loop-make-psetq (frobs)
315 (if (null (cddr frobs
)) (cadr frobs
)
316 `(prog1 ,(cadr frobs
)
317 ,(loop-make-psetq (cddr frobs
))))))))
319 (defun loop-make-desetq (var-val-pairs)
320 (if var-val-pairs
(cons 'loop-desetq var-val-pairs
)))
322 (sb!ext
:defglobal
*loop-desetq-temporary
*
323 (make-symbol "LOOP-DESETQ-TEMP"))
325 (sb!xc
:defmacro loop-desetq
(&environment env
&rest var-val-pairs
)
326 (labels ((find-non-null (var)
327 ;; See whether there's any non-null thing here. Recurse
328 ;; if the list element is itself a list.
329 (do ((tail var
)) ((not (consp tail
)) tail
)
330 (when (find-non-null (pop tail
)) (return t
))))
331 (loop-desetq-internal (var val
&optional temp
)
332 ;; Check for well-formed use of MULTIPLE-VALUE-LIST
333 (when (and (typep val
'(cons (eql multiple-value-list
) (cons t null
)))
335 (return-from loop-desetq-internal
336 (let ((temps (make-gensym-list (length var
))))
337 `((multiple-value-bind ,temps
,(cadr val
)
338 ,@(when (member nil var
)
339 `((declare (ignore ,@(mapcan (lambda (var val
)
340 (unless var
(list val
)))
342 ,@(mapcan (lambda (var val
)
343 (if var
(list `(loop-desetq ,var
,val
))))
345 ;; returns a list of actions to be performed
349 ;; Don't lose possible side effects.
350 ;; FIXME: this special case is just wrong. Either (DESETQ NIL THING)
351 ;; evaluates THING, or it doesn't. If it does, then it mustn't touch
352 ;; THING at all, because the user could have written
353 ;; (LOOP FOR NIL = (PROG1 AAA BBB)) where AAA and BBB are
354 ;; symbol-macros the expansions of which are, let's say, (INCF C)
355 ;; and (WHEN (PLUSP C) (RETURN)) respectively. The desired effect is
356 ;; to perform the incf, ignore it, and then return. But we turn that
357 ;; whole form into NIL because of a PROG1. Wtf???
358 (if (eq (car val
) 'prog1
)
359 ;; These can come from PSETQ or DESETQ below.
360 ;; Throw away the value, keep the side effects.
361 ;; Special case is for handling an expanded POP.
364 (or (neq (car x
) 'car
)
365 (not (symbolp (cadr x
)))
366 (not (symbolp (setq x
(%macroexpand x env
)))))
371 (let* ((car (car var
))
373 (car-non-null (find-non-null car
))
374 (cdr-non-null (find-non-null cdr
)))
375 (when (or car-non-null cdr-non-null
)
378 (temp (or temp
*loop-desetq-temporary
*))
379 (body `(,@(loop-desetq-internal car
381 (setq ,temp
(cdr ,temp
))
382 ,@(loop-desetq-internal cdr
386 `(,@(unless (eq temp val
)
387 `((setq ,temp
,val
)))
389 `((let ((,temp
,val
))
392 (loop-desetq-internal car
`(car ,val
) temp
)))))
395 `((setq ,var
,val
)))))))
397 ((null var-val-pairs
)
398 (if (null (cdr actions
)) (car actions
) `(progn ,@(nreverse actions
))))
399 (setq actions
(revappend
400 (loop-desetq-internal (pop var-val-pairs
)
404 ;;;; LOOP-local variables
406 (defstruct (macro-state
407 (:copier nil
) (:predicate nil
) (:conc-name nil
)
408 (:constructor make-loop
(source-code macro-environment universe
)))
409 ;;; This is the "current" pointer into the LOOP source code.
412 ;;; This is the pointer to the original, for things like NAMED that
413 ;;; insist on being in a particular position
414 (original-source-code)
416 ;;; This is (source-code *loop*) as of the "last" clause. It is used
417 ;;; primarily for generating error messages (see loop-error, loop-warn).
420 ;;; list of names for the LOOP, supplied by the NAMED clause
423 ;;; The macroexpansion environment given to the macro.
426 ;;; This holds variable names specified with the USING clause.
427 ;;; See LOOP-NAMED-VAR.
430 ;;; LETlist-like list being accumulated for current group of bindings.
433 ;;; List of declarations being accumulated in parallel with
437 ;;; Declarations for destructuring bindings
438 (desetq-declarations)
440 ;;; This is used by LOOP for destructuring binding, if it is doing
441 ;;; that itself. See LOOP-MAKE-VAR.
444 ;;; list of wrapping forms, innermost first, which go immediately
445 ;;; inside the current set of parallel bindings being accumulated in
446 ;;; VARS. The wrappers are appended onto a body. E.g., this list could
447 ;;; conceivably have as its value
448 ;;; ((WITH-OPEN-FILE (G0001 G0002 ...))),
449 ;;; with G0002 being one of the bindings in VARS (This is why the
450 ;;; wrappers go inside of the variable bindings).
453 ;;; This accumulates lists of previous values of VARS and the other
454 ;;; lists above, for each new nesting of bindings. See
458 ;;; list of prologue forms of the loop, accumulated in reverse order
464 ;;; This is T if we have emitted any body code, so that iteration
465 ;;; driving clauses can be disallowed. This is not strictly the same
466 ;;; as checking (BODY *LOOP*), because we permit some clauses such as
467 ;;; RETURN to not be considered "real" body (so as to permit the user
468 ;;; to "code" an abnormal return value "in loop").
471 ;;; list of epilogue forms (supplied by FINALLY generally), accumulated
475 ;;; list of epilogue forms which are supplied after the above "user"
476 ;;; epilogue. "Normal" termination return values are provide by
477 ;;; putting the return form in here. Normally this is done using
478 ;;; LOOP-EMIT-FINAL-VALUE, q.v.
481 ;;; the "culprit" responsible for supplying a final value from the
482 ;;; loop. This is so LOOP-DISALLOW-AGGREGATE-BOOLEANS can moan about
483 ;;; disallowed anonymous collections.
484 (final-value-culprit)
486 ;;; If not NIL, this is a temporary bound around the loop for holding
487 ;;; the temporary value for "it" in things like "when (f) collect it".
488 ;;; It may be used as a supertemporary by some other things.
491 ;;; Sometimes we decide we need to fold together parts of the loop,
492 ;;; but some part of the generated iteration code is different for the
493 ;;; first and remaining iterations. This variable will be the
494 ;;; temporary which is the flag used in the loop to tell whether we
495 ;;; are in the first or remaining iterations.
498 ;;; list of all the value-accumulation descriptor structures in the
499 ;;; loop. See LOOP-GET-COLLECTION-INFO.
500 (collection-cruft) ; for multiple COLLECTs (etc.)
502 ;;; This is the "current" loop context in use when we are expanding a
503 ;;; loop. It gets bound on each invocation of LOOP.
504 (universe nil
:type loop-universe
))
507 (declaim (type macro-state
*loop
*))
511 ;;; If this is true, we are in some branch of a conditional. Some
512 ;;; clauses may be disallowed.
513 (defvar *loop-inside-conditional
*)
515 ;;;; code analysis stuff
517 (defun loop-constant-fold-if-possible (form &optional expected-type
)
518 (let* ((constantp (sb!xc
:constantp form
))
519 (value (and constantp
(constant-form-value form
))))
520 (when (and constantp expected-type
)
521 (unless (sb!xc
:typep value expected-type
)
522 (loop-warn "~@<The form ~S evaluated to ~S, which was not of ~
523 the anticipated type ~S.~:@>"
524 form value expected-type
)
525 (setq constantp nil value nil
)))
526 (values form constantp value
)))
528 (defun gen-loop-body (prologue before-loop main-body after-loop epilogue
)
529 (unless (= (length before-loop
) (length after-loop
))
530 ;; FIXME: should be (bug) ?
531 (error "GEN-LOOP-BODY called with non-synched before- and after-loop lists"))
532 ;; All our work is done from these copies, working backwards from the end
533 (let ((rbefore (reverse before-loop
))
534 (rafter (reverse after-loop
)))
535 ;; Go backwards from the ends of before-loop and after-loop
536 ;; merging all the equivalent forms into the body.
539 (not (equal (car rbefore
) (car rafter
)))))
540 (push (pop rbefore
) main-body
)
543 ,@(remove nil prologue
)
544 ,@(nreverse (remove nil rbefore
))
546 ,@(remove nil main-body
)
547 ,@(nreverse (remove nil rafter
))
550 ,@(remove nil epilogue
))))
554 (defun loop-context (&aux
(loop *loop
*))
555 (do ((l (source-context loop
) (cdr l
)) (new nil
(cons (car l
) new
)))
556 ((eq l
(cdr (source-code loop
))) (nreverse new
))))
558 (defun loop-error (format-string &rest format-args
)
559 #-sb-xc-host
(declare (optimize allow-non-returning-tail-call
))
560 (%program-error
"~?~%current LOOP context:~{ ~S~}."
561 format-string format-args
(loop-context)))
563 (defun loop-warn (format-string &rest format-args
)
564 (warn "~?~%current LOOP context:~{ ~S~}."
569 (defun loop-check-data-type (specified-type required-type
570 &optional
(default-type required-type
))
571 (if (null specified-type
)
573 (multiple-value-bind (a b
) (sb!xc
:subtypep specified-type required-type
)
575 (loop-warn "LOOP couldn't verify that ~S is a subtype of the required type ~S."
576 specified-type required-type
))
578 (loop-error "The specified data type ~S is not a subtype of ~S."
579 specified-type required-type
)))
582 ;;; Transform the LOOP kind of destructuring into the DESTRUCTURING-BIND kind
583 ;;; basically by adding &optional and ignored &rest dotted list
584 (defun transform-destructuring (tree)
586 (labels ((transform (tree)
587 (do ((result (list '&optional
))
588 (cdr tree
(cdr cdr
)))
591 (return (nreconc result
592 (car (push (gensym "LOOP-IGNORED-")
595 (return (nreconc result cdr
)))
597 (push (list (transform (car cdr
))) result
))
599 (push (car (push (gensym "LOOP-IGNORED-")
603 (push (car cdr
) result
))))))
604 (values (transform tree
) ignores
))))
606 (sb!xc
:defmacro loop-destructuring-bind
607 (lambda-list args
&rest body
)
608 (multiple-value-bind (d-lambda-list ignores
)
609 (transform-destructuring lambda-list
)
610 `(destructuring-bind ,d-lambda-list
,args
611 (declare (ignore ,@ignores
))
614 (defun loop-build-destructuring-bindings (crocks forms
)
616 `((loop-destructuring-bind ,(car crocks
) ,(cadr crocks
)
617 ,@(loop-build-destructuring-bindings (cddr crocks
) forms
)))
620 (defun loop-translate (loop &aux
(*loop
* loop
))
621 (let ((*loop-body
* nil
)
622 (*loop-inside-conditional
* nil
))
623 (loop-iteration-driver loop
)
625 (let ((answer (gen-loop-body
626 (nreverse (prologue loop
))
627 (nreverse (before-loop loop
))
628 (nreverse *loop-body
*)
629 (nreverse (after-body loop
))
630 (nreconc (epilogue loop
)
631 (nreverse (after-epilogue loop
))))))
632 (dolist (entry (bind-stack loop
))
633 (destructuring-bind (vars dcls desetq desetq-decls wrappers
) entry
635 (setq answer
(append w
(list answer
))))
636 (when (or vars dcls desetq
)
637 (let ((forms (list answer
)))
639 (push `(declare ,@desetq-decls
) forms
))
640 (setq answer
`(,(if vars
'let
'locally
)
643 ,@(loop-build-destructuring-bindings desetq
646 (setq answer
`(block ,(pop (names loop
)) ,answer
))
647 (unless (names loop
) (return nil
)))
650 (defun loop-iteration-driver (loop &aux
(universe (universe loop
)))
652 ((null (source-code loop
)))
653 (let ((keyword (car (source-code loop
))) (tem nil
))
654 (cond ((not (symbolp keyword
))
655 (loop-error "~S found where LOOP keyword expected" keyword
))
656 (t (setf (source-context loop
) (source-code loop
))
659 (loop-lookup-keyword keyword
660 (loop-universe-keywords universe
)))
661 ;; It's a "miscellaneous" toplevel LOOP keyword (DO,
662 ;; COLLECT, NAMED, etc.)
663 (apply (symbol-function (first tem
)) (rest tem
)))
665 (loop-lookup-keyword keyword
666 (loop-universe-iteration-keywords universe
)))
667 (loop-hack-iteration tem
))
668 ((loop-tmember keyword
'(and else
))
669 ;; The alternative is to ignore it, i.e. let it go
670 ;; around to the next keyword...
671 (loop-error "secondary clause misplaced at top level in LOOP macro: ~S ~S ~S ..."
673 (car (source-code loop
))
674 (cadr (source-code loop
))))
675 (t (loop-error "unknown LOOP keyword: ~S" keyword
))))))))
677 (defun loop-pop-source (&aux
(loop *loop
*))
678 (if (source-code loop
)
679 (pop (source-code loop
))
680 (loop-error "LOOP source code ran out when another token was expected.")))
682 (defun loop-get-form ()
683 (if (source-code *loop
*)
685 (loop-error "LOOP code ran out where a form was expected.")))
687 (defun loop-get-compound-form ()
688 (let ((form (loop-get-form)))
690 (loop-error "A compound form was expected, but ~S found." form
))
693 (defun loop-get-progn (&aux
(loop *loop
*))
694 (do ((forms (list (loop-get-compound-form))
695 (cons (loop-get-compound-form) forms
))
696 (nextform (car (source-code loop
))
697 (car (source-code loop
))))
699 (if (null (cdr forms
)) (car forms
) (cons 'progn
(nreverse forms
))))))
701 (defun loop-construct-return (form)
702 `(return-from ,(car (names *loop
*)) ,form
))
704 (defun loop-pseudo-body (form &aux
(loop *loop
*))
705 (cond ((or (emitted-body loop
) *loop-inside-conditional
*)
706 (push form
*loop-body
*))
707 (t (push form
(before-loop loop
)) (push form
(after-body loop
)))))
709 (defun loop-emit-body (form)
710 (setf (emitted-body *loop
*) t
)
711 (loop-pseudo-body form
))
713 (defun loop-emit-final-value (&optional
(form nil form-supplied-p
) &aux
(loop *loop
*))
714 (when form-supplied-p
715 (push (loop-construct-return form
) (after-epilogue loop
)))
716 (setf (final-value-culprit loop
) (car (source-context loop
))))
718 (defun loop-disallow-conditional (&optional kwd
)
719 (when *loop-inside-conditional
*
720 (loop-error "~:[This LOOP~;The LOOP ~:*~S~] clause is not permitted inside a conditional." kwd
)))
722 (defun loop-disallow-anonymous-collectors ()
723 (when (find-if-not 'loop-collector-name
(collection-cruft *loop
*))
724 (loop-error "This LOOP clause is not permitted with anonymous collectors.")))
726 (defun loop-disallow-aggregate-booleans ()
727 (when (loop-tmember (final-value-culprit *loop
*) '(always never thereis
))
728 (loop-error "This anonymous collection LOOP clause is not permitted with aggregate booleans.")))
732 (defun loop-typed-init (data-type &optional step-var-p
)
733 ;; FIXME: can't tell if unsupplied or NIL, but it has to be rare.
735 (let ((ctype (specifier-type data-type
)))
736 ;; FIXME: use the ctype for the rest of the type operations, now
738 (cond ((eql ctype
*empty-type
*)
740 ((sb!xc
:subtypep data-type
'number
)
741 (let ((init (if step-var-p
1 0)))
742 (flet ((like (&rest types
)
743 (coerce init
(find-if (lambda (type)
744 (sb!xc
:subtypep data-type type
))
746 (cond ((sb!xc
:subtypep data-type
'float
)
747 (like 'single-float
'double-float
748 'short-float
'long-float
'float
))
749 ((sb!xc
:subtypep data-type
'(complex float
))
750 (like '(complex single-float
)
751 '(complex double-float
)
752 '(complex short-float
)
753 '(complex long-float
)
757 ((sb!xc
:subtypep data-type
'vector
)
758 (when (array-type-p ctype
)
759 (let ((etype (type-*-to-t
760 (array-type-specialized-element-type ctype
))))
761 (make-array 0 :element-type
(type-specifier etype
)))))
763 ((sb!xc
:subtypep data-type
'extended-char
)
764 (code-char base-char-code-limit
))
765 ((sb!xc
:subtypep data-type
'character
)
770 (defun loop-optional-type (&optional variable
&aux
(loop *loop
*))
771 ;; No variable specified implies that no destructuring is permissible.
772 (and (source-code loop
) ; Don't get confused by NILs..
773 (let ((z (car (source-code loop
))))
774 (cond ((loop-tequal z
'of-type
)
775 ;; This is the syntactically unambigous form in that
776 ;; the form of the type specifier does not matter.
777 ;; Also, it is assumed that the type specifier is
778 ;; unambiguously, and without need of translation, a
779 ;; common lisp type specifier or pattern (matching the
780 ;; variable) thereof.
785 ;; This is the (sort of) "old" syntax, even though we
786 ;; didn't used to support all of these type symbols.
787 (let ((type-spec (std-atom-type-specifier-p z
)))
792 ;; This is our sort-of old syntax. But this is only
793 ;; valid for when we are destructuring, so we will be
794 ;; compulsive (should we really be?) and require that
795 ;; we in fact be doing variable destructuring here. We
796 ;; must translate the old keyword pattern typespec
797 ;; into a fully-specified pattern of real type
802 "~S found where a LOOP keyword, LOOP type keyword, or LOOP type pattern expected"
804 (loop-error "~S found where a LOOP keyword or LOOP type keyword expected" z
))
806 (labels ((translate (k v
)
810 (or (std-atom-type-specifier-p k
)
812 "The destructuring type pattern ~S contains the unrecognized type keyword ~S."
817 "The destructuring type pattern ~S doesn't match the variable pattern ~S."
819 (t (cons (translate (car k
) (car v
))
820 (translate (cdr k
) (cdr v
))))))
824 (cons (replicate typ
(car v
))
825 (replicate typ
(cdr v
))))))
826 (translate z variable
)))))))
830 (defun loop-bind-block (&aux
(loop *loop
*))
831 (when (or (vars loop
) (declarations loop
) (wrappers loop
) (desetq loop
))
832 (push (list (nreverse (vars loop
))
835 (desetq-declarations loop
)
838 (setf (vars loop
) nil
839 (declarations loop
) nil
841 (desetq-declarations loop
) nil
842 (wrappers loop
) nil
)))
844 (defun check-var-name (name &optional
(context "") &aux
(loop *loop
*))
845 (labels ((map-name (function name
)
846 (do ((x (pop name
) (pop name
)))
850 (cons (map-name function x
))
851 (symbol (funcall function x
))
853 (loop-error "Bad variable ~s~a" x context
)))
859 (funcall function name
)
862 (loop-error "Bad variable ~s~a" name context
)))))
864 (loop-error "Duplicated variable ~s~a" x context
))
865 (find-in-desetq (name desetqs
)
866 (do* ((desetq desetqs
(cddr desetq
))
867 (var (car desetq
) (car desetq
)))
869 (map-name (lambda (x)
874 (map-name (lambda (x) (check-var-name x context
)) name
))
875 ((assoc name
(vars loop
))
877 ((find-in-desetq name
(desetq loop
)))
879 (do ((entry (bind-stack loop
) (cdr entry
)))
882 ((null entry
) (return nil
))
883 ((assoc name
(caar entry
) :test
#'eq
)
886 (find-in-desetq name
(caddar entry
)))))))))
888 (defun loop-make-var (name initialization dtype
&optional step-var-p
891 (setq name
(gensym "LOOP-IGNORE-"))
892 (push (list name
(or initialization
(loop-typed-init dtype step-var-p
)))
894 (push `(ignore ,name
) (declarations loop
))
895 (loop-declare-var name dtype
))
897 (check-var-name name
)
898 (loop-declare-var name dtype
:step-var-p step-var-p
899 :initialization initialization
)
900 ;; We use ASSOC on this list to check for duplications (above),
901 ;; so don't optimize out this list:
902 (push (list name
(or initialization
(loop-typed-init dtype step-var-p
)))
905 (check-var-name name
)
906 (let ((newvar (gensym "LOOP-DESTRUCTURE-")))
907 (loop-declare-var name dtype
:desetq t
)
908 (push (list newvar initialization
) (vars loop
))
909 ;; (DESETQ *LOOP*) gathered in reverse order.
911 (list* name newvar
(desetq loop
)))))
913 (let ((tcar nil
) (tcdr nil
))
914 (if (atom dtype
) (setq tcar
(setq tcdr dtype
))
915 (setq tcar
(car dtype
) tcdr
(cdr dtype
)))
916 (loop-make-var (car name
) nil tcar
)
918 (loop-make-var (cdr name
) nil tcdr
)))))
921 ;;; Find a suitable type for default initialization
922 (defun type-for-default-init (type &optional step-var-p
)
923 (multiple-value-bind (init empty-type
)
924 (loop-typed-init type step-var-p
)
927 ;; Don't wrap empty types `(or ...), otherwise the will no
928 ;; longer be empty and the compiler won't produce
931 ((sb!xc
:typep init type
)
933 ((sb!xc
:typep init
'(simple-array * (*)))
934 ;; type-of lets the size in
935 `(or (simple-array ,(array-element-type init
) (*)) ,type
))
937 `(or ,(sb!xc
:type-of init
) ,type
)))
940 (defun loop-declare-var (name dtype
&key step-var-p initialization
941 desetq
&aux
(loop *loop
*))
942 (cond ((or (null name
) (null dtype
) (eq dtype t
)) nil
)
944 (unless (or (sb!xc
:subtypep t dtype
)
945 (and (eq (find-package :cl
) (symbol-package name
))
946 (eq :special
(info :variable
:kind name
))))
947 (let ((dtype `(type ,(if initialization
949 (type-for-default-init dtype step-var-p
))
952 (push dtype
(desetq-declarations loop
))
953 (push dtype
(declarations loop
))))))
956 (loop-declare-var (car name
) (car dtype
)
958 (loop-declare-var (cdr name
) (cdr dtype
)
960 (t (loop-declare-var (car name
) dtype
962 (loop-declare-var (cdr name
) dtype
964 (t (error "invalid LOOP variable passed in: ~S" name
))))
966 (defun loop-maybe-bind-form (form data-type
)
969 (loop-make-var (gensym "LOOP-BIND-") form data-type
)))
971 (defun loop-do-if (for negatep
&aux
(loop *loop
*) (universe (universe loop
)))
972 (let ((form (loop-get-form))
973 (*loop-inside-conditional
* t
)
976 (flet ((get-clause (for)
977 (do ((body nil
)) (nil)
978 (let ((key (car (source-code loop
))) (*loop-body
* nil
) data
)
979 (cond ((not (symbolp key
))
981 "~S found where keyword expected getting LOOP clause after ~S"
983 (t (setf (source-context loop
) (source-code loop
))
985 (when (and (loop-tequal (car (source-code loop
)) 'it
)
987 (setf (source-code loop
)
991 (cdr (source-code loop
)))))
992 (cond ((or (not (setq data
(loop-lookup-keyword
993 key
(loop-universe-keywords universe
))))
994 (progn (apply (symbol-function (car data
))
998 "~S does not introduce a LOOP clause that can follow ~S."
1000 (t (setq body
(nreconc *loop-body
* body
)))))))
1001 (setq first-clause-p nil
)
1002 (if (loop-tequal (car (source-code loop
)) :and
)
1004 (return (if (cdr body
)
1005 `(progn ,@(nreverse body
))
1007 (let ((then (get-clause for
))
1008 (else (when (loop-tequal (car (source-code loop
)) :else
)
1010 (list (get-clause :else
)))))
1011 (when (loop-tequal (car (source-code loop
)) :end
)
1013 (when it-p
(setq form
`(setq ,it-p
,form
)))
1015 `(if ,(if negatep
`(not ,form
) form
)
1019 (defun loop-do-initially ()
1020 (loop-disallow-conditional :initially
)
1021 (push (loop-get-progn) (prologue *loop
*)))
1023 (defun loop-do-finally ()
1024 (loop-disallow-conditional :finally
)
1025 (push (loop-get-progn) (epilogue *loop
*)))
1027 (defun loop-do-do ()
1028 (loop-emit-body (loop-get-progn)))
1030 (defun loop-do-named (&aux
(loop *loop
*))
1031 (let ((name (loop-pop-source)))
1032 (unless (symbolp name
)
1033 (loop-error "~S is an invalid name for your LOOP" name
))
1034 (when (or (before-loop loop
) *loop-body
* (after-epilogue loop
) *loop-inside-conditional
*)
1035 (loop-error "The NAMED ~S clause occurs too late." name
))
1037 (loop-error "You may only use one NAMED clause in your loop: NAMED ~S ... NAMED ~S."
1038 (car (names loop
)) name
))
1039 (setf (names loop
) (list name
))))
1041 (defun loop-do-return ()
1042 (loop-emit-body (loop-construct-return (loop-get-form))))
1044 ;;;; value accumulation: LIST
1046 (defstruct (loop-collector
1055 (data nil
)) ;collector-specific data
1057 (sb!xc
:defmacro with-sum-count
(lc &body body
)
1058 (let* ((type (loop-collector-dtype lc
))
1059 (temp-var (car (loop-collector-tempvars lc
))))
1060 (multiple-value-bind (type init
)
1061 (type-for-default-init type
)
1062 `(let ((,temp-var
,init
))
1063 (declare (type ,type
,temp-var
))
1066 (defun loop-get-collection-info (collector class default-type
&aux
(loop *loop
*))
1067 (let ((form (loop-get-form))
1068 (name (when (loop-tequal (car (source-code loop
)) 'into
)
1070 (loop-pop-source))))
1071 (when (not (symbolp name
))
1072 (loop-error "The value accumulation recipient name, ~S, is not a symbol." name
))
1074 (loop-disallow-aggregate-booleans))
1075 (let* ((specified-type (loop-optional-type))
1076 (dtype (or specified-type default-type
))
1077 (cruft (find (the symbol name
) (collection-cruft loop
)
1078 :key
#'loop-collector-name
)))
1080 (check-var-name name
" in INTO clause")
1081 (push (setq cruft
(make-loop-collector
1082 :name name
:class class
1083 :history
(list collector
)
1084 :specified-type specified-type
1086 (collection-cruft loop
)))
1087 (t (unless (eq (loop-collector-class cruft
) class
)
1089 "incompatible kinds of LOOP value accumulation specified for collecting~@
1090 ~:[as the value of the LOOP~;~:*INTO ~S~]: ~S and ~S"
1091 name
(car (loop-collector-history cruft
)) collector
))
1092 (cond ((equal dtype
(loop-collector-dtype cruft
)))
1093 ((and (null specified-type
)
1094 (null (loop-collector-specified-type cruft
)))
1095 ;; Unionize types only for default types, most
1096 ;; likely, SUM and COUNT which have number and
1097 ;; fixnum respectively.
1098 (setf (loop-collector-dtype cruft
)
1101 (specifier-type dtype
)
1102 (specifier-type (loop-collector-dtype cruft
))))))
1105 "unequal datatypes specified in different LOOP value accumulations~@
1107 name dtype
(loop-collector-dtype cruft
))
1108 (when (eq (loop-collector-dtype cruft
) t
)
1109 (setf (loop-collector-dtype cruft
) dtype
))))
1110 (push collector
(loop-collector-history cruft
))))
1111 (values cruft form
))))
1113 (defun loop-list-collection (specifically) ; NCONC, LIST, or APPEND
1114 (multiple-value-bind (lc form
)
1115 (loop-get-collection-info specifically
'list
'list
)
1116 (let ((tempvars (loop-collector-tempvars lc
)))
1118 (setf (loop-collector-tempvars lc
)
1119 (setq tempvars
(list* (gensym "LOOP-LIST-HEAD-")
1120 (gensym "LOOP-LIST-TAIL-")
1121 (and (loop-collector-name lc
)
1122 (list (loop-collector-name lc
))))))
1123 (push `(with-loop-list-collection-head ,tempvars
) (wrappers *loop
*))
1124 (unless (loop-collector-name lc
)
1125 (loop-emit-final-value `(loop-collect-answer ,(car tempvars
)
1126 ,@(cddr tempvars
)))))
1128 (list (setq form
`(list ,form
)))
1130 (append (unless (and (consp form
) (eq (car form
) 'list
))
1131 (setq form
`(copy-list ,form
)))))
1132 (loop-emit-body `(loop-collect-rplacd ,tempvars
,form
)))))
1134 ;;;; value accumulation: MAX, MIN, SUM, COUNT
1136 (defun loop-sum-collection (specifically required-type default-type
);SUM, COUNT
1137 (multiple-value-bind (lc form
)
1138 (loop-get-collection-info specifically
'sum default-type
)
1139 (loop-check-data-type (loop-collector-dtype lc
) required-type
)
1140 (let ((tempvars (loop-collector-tempvars lc
)))
1142 (setf (loop-collector-tempvars lc
)
1143 (setq tempvars
(list (or (loop-collector-name lc
)
1144 (gensym "LOOP-SUM-")))))
1145 (unless (loop-collector-name lc
)
1146 (loop-emit-final-value (car (loop-collector-tempvars lc
))))
1147 (push `(with-sum-count ,lc
) (wrappers *loop
*)))
1149 (if (eq specifically
'count
)
1151 (setq ,(car tempvars
)
1152 (1+ ,(car tempvars
))))
1153 `(setq ,(car tempvars
)
1157 (defun loop-maxmin-collection (specifically)
1158 (multiple-value-bind (lc form
)
1159 (loop-get-collection-info specifically
'maxmin
'real
)
1160 (loop-check-data-type (loop-collector-dtype lc
) 'real
)
1161 (let ((data (loop-collector-data lc
)))
1163 (setf (loop-collector-data lc
)
1164 (setq data
(make-loop-minimax
1165 (or (loop-collector-name lc
)
1166 (gensym "LOOP-MAXMIN-"))
1167 (loop-collector-dtype lc
))))
1168 (unless (loop-collector-name lc
)
1169 (loop-emit-final-value (loop-minimax-answer-variable data
)))
1170 (push `(with-minimax-value ,data
) (wrappers *loop
*)))
1171 (loop-note-minimax-operation specifically data
)
1172 (loop-emit-body `(loop-accumulate-minimax-value ,data
1176 ;;;; value accumulation: aggregate booleans
1178 ;;; handling the ALWAYS and NEVER loop keywords
1180 ;;; Under ANSI these are not permitted to appear under conditionalization.
1181 (defun loop-do-always (restrictive negate
)
1182 (let ((form (loop-get-form)))
1183 (when restrictive
(loop-disallow-conditional))
1184 (loop-disallow-anonymous-collectors)
1185 (loop-emit-body `(,(if negate
'when
'unless
) ,form
1186 ,(loop-construct-return nil
)))
1187 (loop-emit-final-value t
)))
1189 ;;; handling the THEREIS loop keyword
1191 ;;; Under ANSI this is not permitted to appear under conditionalization.
1192 (defun loop-do-thereis (restrictive)
1193 (when restrictive
(loop-disallow-conditional))
1194 (loop-disallow-anonymous-collectors)
1195 (loop-emit-final-value)
1196 (loop-emit-body `(when (setq ,(loop-when-it-var) ,(loop-get-form))
1197 ,(loop-construct-return (when-it-var *loop
*)))))
1199 (defun loop-do-while (negate kwd
&aux
(form (loop-get-form)))
1200 (loop-disallow-conditional kwd
)
1201 (loop-pseudo-body `(,(if negate
'when
'unless
) ,form
(go end-loop
))))
1203 (defun loop-do-repeat (&aux
(loop *loop
*))
1204 (loop-disallow-conditional :repeat
)
1205 (let* ((form (loop-get-form))
1206 (count (and (constantp form
) ; FIXME: lexical environment constants
1207 (constant-form-value form
)))
1208 (type (cond ((not (realp count
))
1211 `(mod ,(1+ (ceiling count
))))
1213 `(integer ,(ceiling count
))))))
1214 (let ((var (loop-make-var (gensym "LOOP-REPEAT-") `(ceiling ,form
) type
)))
1215 (push `(if (<= ,var
0) (go end-loop
) (decf ,var
)) (before-loop loop
))
1216 (push `(if (<= ,var
0) (go end-loop
) (decf ,var
)) (after-body loop
))
1217 ;; FIXME: What should
1218 ;; (loop count t into a
1221 ;; finally (return (list a b)))
1222 ;; return: (3 3) or (4 3)? PUSHes above are for the former
1223 ;; variant, L-P-B below for the latter.
1224 #+nil
(loop-pseudo-body `(when (minusp (decf ,var
)) (go end-loop
))))))
1226 (defun loop-do-with (&aux
(loop *loop
*))
1227 (loop-disallow-conditional :with
)
1228 (do ((var) (val) (dtype))
1230 (setq var
(loop-pop-source)
1231 dtype
(loop-optional-type var
)
1232 val
(cond ((loop-tequal (car (source-code loop
)) :=)
1236 (loop-make-var var val dtype
)
1237 (if (loop-tequal (car (source-code loop
)) :and
)
1239 (return (loop-bind-block)))))
1241 ;;;; the iteration driver
1243 (defun loop-hack-iteration (entry &aux
(loop *loop
*))
1244 (flet ((make-endtest (list-of-forms)
1245 (cond ((null list-of-forms
) nil
)
1246 ((member t list-of-forms
) '(go end-loop
))
1247 (t `(when ,(if (null (cdr (setq list-of-forms
1248 (nreverse list-of-forms
))))
1250 (cons 'or list-of-forms
))
1252 (do ((pre-step-tests nil
)
1254 (post-step-tests nil
)
1256 (pre-loop-pre-step-tests nil
)
1257 (pre-loop-steps nil
)
1258 (pre-loop-post-step-tests nil
)
1259 (pre-loop-pseudo-steps nil
)
1262 ;; Note that we collect endtests in reverse order, but steps in correct
1263 ;; order. MAKE-ENDTEST does the nreverse for us.
1264 (setq tem
(setq data
1265 (apply (symbol-function (first entry
)) (rest entry
))))
1266 (and (car tem
) (push (car tem
) pre-step-tests
))
1267 (setq steps
(nconc steps
(copy-list (car (setq tem
(cdr tem
))))))
1268 (and (car (setq tem
(cdr tem
))) (push (car tem
) post-step-tests
))
1270 (nconc pseudo-steps
(copy-list (car (setq tem
(cdr tem
))))))
1271 (setq tem
(cdr tem
))
1272 (when (emitted-body loop
)
1273 (loop-error "iteration in LOOP follows body code"))
1274 (unless tem
(setq tem data
))
1275 (when (car tem
) (push (car tem
) pre-loop-pre-step-tests
))
1276 ;; FIXME: This (SETF FOO (NCONC FOO BAR)) idiom appears often enough
1277 ;; that it might be worth making it into an NCONCF macro.
1278 (setq pre-loop-steps
1279 (nconc pre-loop-steps
(copy-list (car (setq tem
(cdr tem
))))))
1280 (when (car (setq tem
(cdr tem
)))
1281 (push (car tem
) pre-loop-post-step-tests
))
1282 (setq pre-loop-pseudo-steps
1283 (nconc pre-loop-pseudo-steps
(copy-list (cadr tem
))))
1284 (unless (loop-tequal (car (source-code loop
)) :and
)
1285 (setf (before-loop loop
)
1286 (list* (loop-make-desetq pre-loop-pseudo-steps
)
1287 (make-endtest pre-loop-post-step-tests
)
1288 (loop-make-psetq pre-loop-steps
)
1289 (make-endtest pre-loop-pre-step-tests
)
1290 (before-loop loop
)))
1291 (setf (after-body loop
)
1292 (list* (loop-make-desetq pseudo-steps
)
1293 (make-endtest post-step-tests
)
1294 (loop-make-psetq steps
)
1295 (make-endtest pre-step-tests
)
1299 (loop-pop-source)))) ; Flush the "AND".
1301 ;;;; main iteration drivers
1303 ;;; FOR variable keyword ..args..
1304 (defun loop-do-for ()
1305 (let* ((var (loop-pop-source))
1306 (data-type (loop-optional-type var
))
1307 (keyword (loop-pop-source))
1310 (setq first-arg
(loop-get-form))
1311 (unless (and (symbolp keyword
)
1312 (setq tem
(loop-lookup-keyword
1314 (loop-universe-for-keywords (universe *loop
*)))))
1315 (loop-error "~S is an unknown keyword in FOR or AS clause in LOOP."
1317 (apply (car tem
) var first-arg data-type
(cdr tem
))))
1319 (defun loop-when-it-var (&aux
(loop *loop
*))
1320 (or (when-it-var loop
)
1321 (setf (when-it-var loop
)
1322 (loop-make-var (gensym "LOOP-IT-") nil nil
))))
1324 ;;;; various FOR/AS subdispatches
1326 ;;; ANSI "FOR x = y [THEN z]" is sort of like the old Genera one when
1327 ;;; the THEN is omitted (other than being more stringent in its
1328 ;;; placement), and like the old "FOR x FIRST y THEN z" when the THEN
1329 ;;; is present. I.e., the first initialization occurs in the loop body
1330 ;;; (first-step), not in the variable binding phase.
1331 (defun loop-ansi-for-equals (var val data-type
)
1332 (loop-make-var var nil data-type
)
1333 (cond ((loop-tequal (car (source-code *loop
*)) :then
)
1334 ;; Then we are the same as "FOR x FIRST y THEN z".
1336 `(() (,var
,(loop-get-form)) () ()
1337 () (,var
,val
) () ()))
1338 (t ;; We are the same as "FOR x = y".
1339 `(() (,var
,val
) () ()))))
1341 (defun loop-for-across (var val data-type
)
1342 (loop-make-var var nil data-type
)
1343 (let ((vector-var (gensym "LOOP-ACROSS-VECTOR-"))
1344 (index-var (gensym "LOOP-ACROSS-INDEX-")))
1345 (multiple-value-bind (vector-form constantp vector-value
)
1346 (loop-constant-fold-if-possible val
'vector
)
1348 vector-var vector-form
1349 (if (and (consp vector-form
) (eq (car vector-form
) 'the
))
1352 (loop-make-var index-var
0 'fixnum
)
1354 (length-form (cond ((not constantp
)
1355 (let ((v (gensym "LOOP-ACROSS-LIMIT-")))
1356 (push `(setq ,v
(length ,vector-var
))
1358 (loop-make-var v
0 'fixnum
)))
1359 (t (setq length
(length vector-value
)))))
1360 (first-test `(>= ,index-var
,length-form
))
1361 (other-test first-test
)
1362 (step `(,var
(aref ,vector-var
,index-var
)))
1363 (pstep `(,index-var
(1+ ,index-var
))))
1364 (declare (fixnum length
))
1366 (setq first-test
(= length
0))
1368 (setq other-test t
)))
1369 `(,other-test
,step
() ,pstep
1370 ,@(and (neq first-test other-test
)
1371 `(,first-test
,step
() ,pstep
)))))))
1375 (defun loop-list-step (listvar)
1376 ;; We are not equipped to analyze whether 'FOO is the same as #'FOO
1377 ;; here in any sensible fashion, so let's give an obnoxious warning
1378 ;; whenever 'FOO is used as the stepping function.
1380 ;; While a Discerning Compiler may deal intelligently with
1381 ;; (FUNCALL 'FOO ...), not recognizing FOO may defeat some LOOP
1383 (let ((stepper (cond ((loop-tequal (car (source-code *loop
*)) :by
)
1386 (t '(function cdr
)))))
1387 (cond ((and (consp stepper
) (eq (car stepper
) 'quote
))
1388 (loop-warn "Use of QUOTE around stepping function in LOOP will be left verbatim.")
1389 `(funcall ,stepper
,listvar
))
1390 ((and (consp stepper
) (eq (car stepper
) 'function
))
1391 (list (cadr stepper
) listvar
))
1393 `(funcall ,(loop-make-var (gensym "LOOP-FN-") stepper
'function
)
1396 (defun loop-for-on (var val data-type
)
1397 (multiple-value-bind (list constantp list-value
)
1398 (loop-constant-fold-if-possible val
)
1399 (let ((listvar var
))
1400 (cond ((and var
(symbolp var
))
1401 (loop-make-var var list data-type
))
1403 (loop-make-var (setq listvar
(gensym)) list
't
)
1404 (loop-make-var var nil data-type
)))
1405 (let ((list-step (loop-list-step listvar
)))
1406 (let* ((first-endtest
1407 ;; mysterious comment from original CMU CL sources:
1408 ;; the following should use `atom' instead of `endp',
1411 (other-endtest first-endtest
))
1412 (when (and constantp
(listp list-value
))
1413 (setq first-endtest
(null list-value
)))
1414 (cond ((eq var listvar
)
1415 ;; The contour of the loop is different because we
1416 ;; use the user's variable...
1417 `(() (,listvar
,list-step
)
1418 ,other-endtest
() () () ,first-endtest
()))
1419 (t (let ((step `(,var
,listvar
))
1420 (pseudo `(,listvar
,list-step
)))
1421 `(,other-endtest
,step
() ,pseudo
1422 ,@(and (neq first-endtest other-endtest
)
1423 `(,first-endtest
,step
() ,pseudo
)))))))))))
1425 (defun loop-for-in (var val data-type
)
1426 (multiple-value-bind (list constantp list-value
)
1427 (loop-constant-fold-if-possible val
)
1428 (let ((listvar (gensym "LOOP-LIST-")))
1429 (loop-make-var var nil data-type
)
1430 (loop-make-var listvar list
'list
)
1431 (let ((list-step (loop-list-step listvar
)))
1432 (let* ((first-endtest `(endp ,listvar
))
1433 (other-endtest first-endtest
)
1434 (step `(,var
(car ,listvar
)))
1435 (pseudo-step `(,listvar
,list-step
)))
1436 (when (and constantp
(listp list-value
))
1437 (setq first-endtest
(null list-value
)))
1438 `(,other-endtest
,step
() ,pseudo-step
1439 ,@(and (neq first-endtest other-endtest
)
1440 `(,first-endtest
,step
() ,pseudo-step
))))))))
1442 ;;;; iteration paths
1444 (defstruct (loop-path
1447 (names nil
:read-only t
)
1448 (preposition-groups nil
:read-only t
)
1449 (inclusive-permitted nil
:read-only t
)
1450 (function nil
:read-only t
)
1451 (user-data nil
:read-only t
))
1453 (defun add-loop-path (names function universe
1454 &key preposition-groups inclusive-permitted user-data
)
1455 (declare (type loop-universe universe
))
1456 (let* ((names (ensure-list names
))
1457 (ht (loop-universe-path-keywords universe
))
1459 :names
(mapcar #'symbol-name names
)
1461 :user-data user-data
1462 :preposition-groups
(mapcar #'ensure-list preposition-groups
)
1463 :inclusive-permitted inclusive-permitted
)))
1464 (dolist (name names
)
1465 (setf (gethash (symbol-name name
) ht
) lp
))
1468 ;;; Note: Path functions are allowed to use LOOP-MAKE-VAR, hack
1469 ;;; the prologue, etc.
1470 (defun loop-for-being (var val data-type
&aux
(loop *loop
*) (universe (universe loop
)))
1471 ;; FOR var BEING each/the pathname prep-phrases using-stuff... each/the =
1472 ;; EACH or THE. Not clear if it is optional, so I guess we'll warn.
1477 (initial-prepositions nil
))
1478 (cond ((loop-tmember val
'(:each
:the
)) (setq path
(loop-pop-source)))
1479 ((loop-tequal (car (source-code loop
)) :and
)
1482 (unless (loop-tmember (car (source-code loop
))
1483 '(:its
:each
:his
:her
))
1484 (loop-error "~S was found where ITS or EACH expected in LOOP iteration path syntax."
1485 (car (source-code loop
))))
1487 (setq path
(loop-pop-source))
1488 (setq initial-prepositions
`((:in
,val
))))
1489 (t (loop-error "unrecognizable LOOP iteration path syntax: missing EACH or THE?")))
1490 (cond ((not (symbolp path
))
1492 "~S was found where a LOOP iteration path name was expected."
1494 ((not (setq data
(loop-lookup-keyword path
(loop-universe-path-keywords universe
))))
1495 (loop-error "~S is not the name of a LOOP iteration path." path
))
1496 ((and inclusive
(not (loop-path-inclusive-permitted data
)))
1497 (loop-error "\"Inclusive\" iteration is not possible with the ~S LOOP iteration path." path
)))
1498 (let ((fun (loop-path-function data
))
1499 (preps (nconc initial-prepositions
1500 (loop-collect-prepositional-phrases
1501 (loop-path-preposition-groups data
)
1503 (user-data (loop-path-user-data data
)))
1504 (when (symbolp fun
) (setq fun
(symbol-function fun
)))
1505 (setq stuff
(if inclusive
1506 (apply fun var data-type preps
:inclusive t user-data
)
1507 (apply fun var data-type preps user-data
))))
1508 (when (named-vars loop
)
1509 (loop-error "Unused USING vars: ~S." (named-vars loop
)))
1510 ;; STUFF is now (bindings prologue-forms . stuff-to-pass-back).
1511 ;; Protect the system from the user and the user from himself.
1512 (unless (member (length stuff
) '(6 10))
1513 (loop-error "Value passed back by LOOP iteration path function for path ~S has invalid length."
1515 (do ((l (car stuff
) (cdr l
)) (x)) ((null l
))
1516 (if (atom (setq x
(car l
)))
1517 (loop-make-var x nil nil
)
1518 (loop-make-var (car x
) (cadr x
) (caddr x
))))
1519 (setf (prologue loop
) (nconc (reverse (cadr stuff
)) (prologue loop
)))
1522 (defun loop-named-var (name &aux
(loop *loop
*))
1523 (let ((tem (loop-tassoc name
(named-vars loop
))))
1524 (declare (list tem
))
1525 (cond ((null tem
) (values (gensym) nil
))
1526 (t (setf (named-vars loop
) (delete tem
(named-vars loop
)))
1527 (values (cdr tem
) t
)))))
1529 (defun loop-collect-prepositional-phrases (preposition-groups
1532 initial-phrases
&aux
(loop *loop
*))
1533 (flet ((in-group-p (x group
) (car (loop-tmember x group
))))
1535 (prepositional-phrases initial-phrases
)
1536 (this-group nil nil
)
1538 (disallowed-prepositions
1541 (find (car x
) preposition-groups
:test
#'in-group-p
)))
1543 (used-prepositions (mapcar #'car initial-phrases
)))
1544 ((null (source-code loop
)) (nreverse prepositional-phrases
))
1545 (declare (symbol this-prep
))
1546 (setq token
(car (source-code loop
)))
1547 (dolist (group preposition-groups
)
1548 (when (setq this-prep
(in-group-p token group
))
1549 (return (setq this-group group
))))
1551 (when (member this-prep disallowed-prepositions
)
1553 (if (member this-prep used-prepositions
)
1554 "A ~S prepositional phrase occurs multiply for some LOOP clause."
1555 "Preposition ~S was used when some other preposition has subsumed it.")
1557 (setq used-prepositions
(if (listp this-group
)
1558 (append this-group used-prepositions
)
1559 (cons this-group used-prepositions
)))
1561 (push (list this-prep
(loop-get-form)) prepositional-phrases
))
1562 ((and using-allowed
(loop-tequal token
'using
))
1564 (do ((z (loop-pop-source) (loop-pop-source)) (tem)) (nil)
1566 (if (setq tem
(loop-tassoc (car z
) (named-vars loop
)))
1568 "The variable substitution for ~S occurs twice in a USING phrase,~@
1570 (car z
) (cadr z
) (cadr tem
))
1571 (push (cons (car z
) (cadr z
)) (named-vars loop
))))
1572 (when (or (null (source-code loop
))
1573 (symbolp (car (source-code loop
))))
1575 (t (return (nreverse prepositional-phrases
)))))))
1577 ;;;; master sequencer function
1579 (defun loop-sequencer (indexv indexv-type
1580 variable variable-type
1581 sequence-variable sequence-type
1582 step-hack default-top
1583 prep-phrases
&aux
(loop *loop
*))
1584 (let ((endform nil
) ; form (constant or variable) with limit value
1585 (sequencep nil
) ; T if sequence arg has been provided
1586 (testfn nil
) ; endtest function
1587 (test nil
) ; endtest form
1588 (stepby (1+ (or (loop-typed-init indexv-type
) 0))) ; our increment
1589 (stepby-constantp t
)
1590 (step nil
) ; step form
1591 (dir nil
) ; direction of stepping: NIL, :UP, :DOWN
1592 (inclusive-iteration nil
) ; T if include last index
1593 (start-given nil
) ; T when prep phrase has specified start
1595 (start-constantp nil
)
1596 (limit-given nil
) ; T when prep phrase has specified end
1597 (limit-constantp nil
)
1599 #+ccl
(progn start-constantp start-value
) ; bogus "Unused" warnings
1600 (flet ((assert-index-for-arithmetic (index)
1601 (unless (atom index
)
1602 (loop-error "Arithmetic index must be an atom."))))
1603 (when variable
(loop-make-var variable nil variable-type
))
1604 (do ((l prep-phrases
(cdr l
)) (prep) (form) (odir)) ((null l
))
1605 (setq prep
(caar l
) form
(cadar l
))
1609 (loop-make-var sequence-variable form sequence-type
))
1610 ((:from
:downfrom
:upfrom
)
1611 (setq start-given t
)
1612 (cond ((eq prep
:downfrom
) (setq dir
':down
))
1613 ((eq prep
:upfrom
) (setq dir
':up
)))
1614 (multiple-value-setq (form start-constantp start-value
)
1615 (loop-constant-fold-if-possible form indexv-type
))
1616 (assert-index-for-arithmetic indexv
)
1617 ;; KLUDGE: loop-make-var generates a temporary symbol for
1618 ;; indexv if it is NIL. We have to use it to have the index
1620 (setq indexv
(loop-make-var indexv form indexv-type
)))
1621 ((:upto
:to
:downto
:above
:below
)
1622 (cond ((loop-tequal prep
:upto
) (setq inclusive-iteration
1624 ((loop-tequal prep
:to
) (setq inclusive-iteration t
))
1625 ((loop-tequal prep
:downto
) (setq inclusive-iteration
1627 ((loop-tequal prep
:above
) (setq dir
':down
))
1628 ((loop-tequal prep
:below
) (setq dir
':up
)))
1629 (setq limit-given t
)
1630 (multiple-value-setq (form limit-constantp limit-value
)
1631 (loop-constant-fold-if-possible form
`(and ,indexv-type real
)))
1632 (setq endform
(if limit-constantp
1635 (gensym "LOOP-LIMIT-") form
1636 `(and ,indexv-type real
)))))
1638 (multiple-value-setq (form stepby-constantp stepby
)
1639 (loop-constant-fold-if-possible form
1640 `(and ,indexv-type
(real (0)))))
1641 (unless stepby-constantp
1642 (loop-make-var (setq stepby
(gensym "LOOP-STEP-BY-"))
1644 `(and ,indexv-type
(real (0)))
1647 "~S invalid preposition in sequencing or sequence path;~@
1648 maybe invalid prepositions were specified in iteration path descriptor?"
1650 (when (and odir dir
(neq dir odir
))
1652 "conflicting stepping directions in LOOP sequencing path"))
1654 (when (and sequence-variable
(not sequencep
))
1655 (loop-error "missing OF or IN phrase in sequence path"))
1656 ;; Now fill in the defaults.
1657 (cond ((not start-given
)
1659 ;; DUPLICATE KLUDGE: loop-make-var generates a temporary
1660 ;; symbol for indexv if it is NIL. See also the comment in
1661 ;; the (:from :downfrom :upfrom) case
1662 (assert-index-for-arithmetic indexv
)
1666 (setq start-constantp t
1667 start-value
(or (loop-typed-init indexv-type
) 0))
1668 `(and ,indexv-type real
))))
1670 ;; if both start and limit are given, they had better both
1671 ;; be REAL. We already enforce the REALness of LIMIT,
1672 ;; above; here's the KLUDGE to enforce the type of START.
1673 (flet ((type-declaration-of (x)
1674 (and (eq (car x
) 'type
) (caddr x
))))
1675 (let ((decl (find indexv
(declarations loop
)
1676 :key
#'type-declaration-of
))
1677 (%decl
(find indexv
(declarations loop
)
1678 :key
#'type-declaration-of
1680 (aver (eq decl %decl
))
1683 `(and real
,(cadr decl
))))))))
1684 (cond ((member dir
'(nil :up
))
1685 (when (or limit-given default-top
)
1687 (loop-make-var (setq endform
(gensym "LOOP-SEQ-LIMIT-"))
1690 (push `(setq ,endform
,default-top
) (prologue loop
)))
1691 (setq testfn
(if inclusive-iteration
'> '>=)))
1692 (setq step
(if (eql stepby
1) `(1+ ,indexv
) `(+ ,indexv
,stepby
))))
1693 (t (unless start-given
1695 (loop-error "don't know where to start stepping"))
1696 (push `(setq ,indexv
(1- ,default-top
)) (prologue loop
)))
1697 (when (and default-top
(not endform
))
1698 (setq endform
(loop-typed-init indexv-type
)
1699 inclusive-iteration t
))
1700 (when endform
(setq testfn
(if inclusive-iteration
'< '<=)))
1702 (if (eql stepby
1) `(1- ,indexv
) `(- ,indexv
,stepby
)))))
1705 `(,testfn
,indexv
,endform
)))
1708 `(,variable
,step-hack
)))
1709 (let ((first-test test
) (remaining-tests test
))
1710 ;; As far as I can tell, the effect of the following code is
1711 ;; to detect cases where we know statically whether the first
1712 ;; iteration of the loop will be executed. Depending on the
1713 ;; situation, we can either:
1714 ;; a) save one jump and one comparison per loop (not per iteration)
1715 ;; when it will get executed
1716 ;; b) remove the loop body completely when it won't be executed
1718 ;; Noble goals. However, the code generated in case a) will
1719 ;; fool the loop induction variable detection, and cause
1720 ;; code like (LOOP FOR I TO 10 ...) to use generic addition
1723 ;; Since the gain in case a) is rather minimal and Python is
1724 ;; generally smart enough to handle b) without any extra
1725 ;; support from the loop macro, I've disabled this code for
1726 ;; now. The code and the comment left here in case somebody
1727 ;; extends the induction variable bound detection to work
1728 ;; with code where the stepping precedes the test.
1729 ;; -- JES 2005-11-30
1731 (when (and stepby-constantp start-constantp limit-constantp
1732 (realp start-value
) (realp limit-value
))
1733 (when (setq first-test
1734 (funcall (symbol-function testfn
)
1737 (setq remaining-tests t
)))
1738 `(() (,indexv
,step
)
1739 ,remaining-tests
,step-hack
() () ,first-test
,step-hack
)))))
1741 ;;;; interfaces to the master sequencer
1743 (defun loop-for-arithmetic (var val data-type kwd
)
1745 var
(loop-check-data-type data-type
'number
)
1746 nil nil nil nil nil nil
1747 (loop-collect-prepositional-phrases
1748 '((:from
:upfrom
:downfrom
) (:to
:upto
:downto
:above
:below
) (:by
))
1749 nil
(list (list kwd val
)))))
1752 ;;;; builtin LOOP iteration paths
1755 (loop for v being the hash-values of ht do
(print v
))
1756 (loop for k being the hash-keys of ht do
(print k
))
1757 (loop for v being the hash-values of ht using
(hash-key k
) do
(print (list k v
)))
1758 (loop for k being the hash-keys of ht using
(hash-value v
) do
(print (list k v
)))
1761 (defun loop-hash-table-iteration-path (variable data-type prep-phrases
1762 &key
(which (missing-arg)))
1763 (declare (type (member :hash-key
:hash-value
) which
))
1764 (cond ((or (cdr prep-phrases
) (not (member (caar prep-phrases
) '(:in
:of
))))
1765 (loop-error "Too many prepositions!"))
1766 ((null prep-phrases
)
1767 (loop-error "missing OF or IN in ~S iteration path")))
1768 (let ((ht-var (gensym "LOOP-HASHTAB-"))
1769 (next-fn (gensym "LOOP-HASHTAB-NEXT-"))
1770 (dummy-predicate-var nil
)
1772 (multiple-value-bind (other-var other-p
)
1773 (loop-named-var (ecase which
1774 (:hash-key
'hash-value
)
1775 (:hash-value
'hash-key
)))
1776 ;; @@@@ LOOP-NAMED-VAR returns a second value of T if the name
1777 ;; was actually specified, so clever code can throw away the
1778 ;; GENSYM'ed-up variable if it isn't really needed. The
1779 ;; following is for those implementations in which we cannot put
1780 ;; dummy NILs into MULTIPLE-VALUE-SETQ variable lists.
1782 dummy-predicate-var
(loop-when-it-var))
1783 (let* ((key-var nil
)
1785 (variable (or variable
(gensym "LOOP-HASH-VAR-TEMP-")))
1786 (bindings `((,variable nil
,data-type
)
1787 (,ht-var
,(cadar prep-phrases
))
1788 ,@(and other-p other-var
`((,other-var nil
))))))
1790 (:hash-key
(setq key-var variable
1791 val-var
(and other-p other-var
)))
1792 (:hash-value
(setq key-var
(and other-p other-var
)
1794 (push `(with-hash-table-iterator (,next-fn
,ht-var
)) (wrappers *loop
*))
1795 (when (or (consp key-var
) data-type
)
1797 `(,key-var
,(setq key-var
(gensym "LOOP-HASH-KEY-TEMP-"))
1799 (push `(,key-var nil
) bindings
))
1800 (when (or (consp val-var
) data-type
)
1802 `(,val-var
,(setq val-var
(gensym "LOOP-HASH-VAL-TEMP-"))
1804 (push `(,val-var nil
) bindings
))
1805 `(,bindings
;bindings
1809 (not (multiple-value-setq (,dummy-predicate-var
,key-var
,val-var
)
1810 (,next-fn
))) ;post-test
1813 (defun loop-package-symbols-iteration-path (variable data-type prep-phrases
1815 (cond ((and prep-phrases
(cdr prep-phrases
))
1816 (loop-error "Too many prepositions!"))
1817 ((and prep-phrases
(not (member (caar prep-phrases
) '(:in
:of
))))
1818 (bug "Unknown preposition ~S." (caar prep-phrases
))))
1819 (unless (symbolp variable
)
1820 (loop-error "Destructuring is not valid for package symbol iteration."))
1821 (let ((pkg-var (gensym "LOOP-PKGSYM-"))
1822 (next-fn (gensym "LOOP-PKGSYM-NEXT-"))
1823 (variable (or variable
(gensym "LOOP-PKGSYM-VAR-")))
1824 (package (or (cadar prep-phrases
) '*package
*)))
1825 (push `(with-package-iterator (,next-fn
,pkg-var
,@symbol-types
))
1827 `(((,variable nil
,data-type
) (,pkg-var
,package
))
1831 (not (multiple-value-setq (,(loop-when-it-var)
1838 (sb!ext
:define-load-time-global
*loop-ansi-universe
*
1839 (let ((w (!make-standard-loop-universe
1840 :keywords
'((named (loop-do-named))
1841 (initially (loop-do-initially))
1842 (finally (loop-do-finally))
1844 (doing (loop-do-do))
1845 (return (loop-do-return))
1846 (collect (loop-list-collection list
))
1847 (collecting (loop-list-collection list
))
1848 (append (loop-list-collection append
))
1849 (appending (loop-list-collection append
))
1850 (nconc (loop-list-collection nconc
))
1851 (nconcing (loop-list-collection nconc
))
1852 (count (loop-sum-collection count
1853 ;; This could be REAL, but when
1854 ;; combined with SUM, it has to be
1858 (counting (loop-sum-collection count
1861 (sum (loop-sum-collection sum number number
))
1862 (summing (loop-sum-collection sum number number
))
1863 (maximize (loop-maxmin-collection max
))
1864 (minimize (loop-maxmin-collection min
))
1865 (maximizing (loop-maxmin-collection max
))
1866 (minimizing (loop-maxmin-collection min
))
1867 (always (loop-do-always t nil
)) ; Normal, do always
1868 (never (loop-do-always t t
)) ; Negate test on always.
1869 (thereis (loop-do-thereis t
))
1870 (while (loop-do-while nil
:while
)) ; Normal, do while
1871 (until (loop-do-while t
:until
)) ;Negate test on while
1872 (when (loop-do-if when nil
)) ; Normal, do when
1873 (if (loop-do-if if nil
)) ; synonymous
1874 (unless (loop-do-if unless t
)) ; Negate test on when
1875 (with (loop-do-with))
1876 (repeat (loop-do-repeat)))
1877 :for-keywords
'((= (loop-ansi-for-equals))
1878 (across (loop-for-across))
1881 (from (loop-for-arithmetic :from
))
1882 (downfrom (loop-for-arithmetic :downfrom
))
1883 (upfrom (loop-for-arithmetic :upfrom
))
1884 (below (loop-for-arithmetic :below
))
1885 (above (loop-for-arithmetic :above
))
1886 (to (loop-for-arithmetic :to
))
1887 (upto (loop-for-arithmetic :upto
))
1888 (downto (loop-for-arithmetic :downto
))
1889 (by (loop-for-arithmetic :by
))
1890 (being (loop-for-being)))
1891 :iteration-keywords
'((for (loop-do-for))
1892 (as (loop-do-for))))))
1893 (add-loop-path '(hash-key hash-keys
) 'loop-hash-table-iteration-path w
1894 :preposition-groups
'((:of
:in
))
1895 :inclusive-permitted nil
1896 :user-data
'(:which
:hash-key
))
1897 (add-loop-path '(hash-value hash-values
) 'loop-hash-table-iteration-path w
1898 :preposition-groups
'((:of
:in
))
1899 :inclusive-permitted nil
1900 :user-data
'(:which
:hash-value
))
1901 (add-loop-path '(symbol symbols
) 'loop-package-symbols-iteration-path w
1902 :preposition-groups
'((:of
:in
))
1903 :inclusive-permitted nil
1904 :user-data
'(:symbol-types
(:internal
1907 (add-loop-path '(external-symbol external-symbols
)
1908 'loop-package-symbols-iteration-path w
1909 :preposition-groups
'((:of
:in
))
1910 :inclusive-permitted nil
1911 :user-data
'(:symbol-types
(:external
)))
1912 (add-loop-path '(present-symbol present-symbols
)
1913 'loop-package-symbols-iteration-path w
1914 :preposition-groups
'((:of
:in
))
1915 :inclusive-permitted nil
1916 :user-data
'(:symbol-types
(:internal
1918 (add-loop-path '(element elements
)
1919 'loop-elements-iteration-path w
1920 :preposition-groups
'((:of
:in
))
1921 :inclusive-permitted nil
)
1924 (defun loop-standard-expansion (keywords-and-forms environment universe
)
1925 (if (and keywords-and-forms
(symbolp (car keywords-and-forms
)))
1926 (loop-translate (make-loop keywords-and-forms environment universe
))
1927 (let ((tag (gensym)))
1928 `(block nil
(tagbody ,tag
(progn ,@keywords-and-forms
) (go ,tag
))))))
1930 (sb!xc
:defmacro loop
(&environment env
&rest keywords-and-forms
)
1931 (loop-standard-expansion keywords-and-forms
1933 *loop-ansi-universe
*))
1935 (sb!xc
:defmacro loop-finish
()
1936 "Cause the iteration to terminate \"normally\", the same as implicit
1937 termination by an iteration driving clause, or by use of WHILE or
1938 UNTIL -- the epilogue code (if any) will be run, and any implicitly
1939 collected result will be returned as the value of the LOOP."
1943 (define-symbol-macro *loop-epilogue
* (epilogue *loop
*))