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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.20 - (show annotations)
Fri Feb 8 13:34:07 1991 UTC (23 years, 2 months ago) by ram
Branch: MAIN
Changes since 1.19: +9 -5 lines
New file header with RCS header FILE-COMMENT.
1 ;;; -*- Log: code.log; Package: Lisp -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;; If you want to use this code or any part of CMU Common Lisp, please contact
7 ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
8 ;;;
9 (ext:file-comment
10 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/macros.lisp,v 1.20 1991/02/08 13:34:07 ram Exp $")
11 ;;;
12 ;;; **********************************************************************
13 ;;;
14 ;;; $Header: /tiger/var/lib/cvsroots/cmucl/src/code/macros.lisp,v 1.20 1991/02/08 13:34:07 ram Exp $
15 ;;;
16 ;;; This file contains the macros that are part of the standard
17 ;;; Spice Lisp environment.
18 ;;;
19 ;;; Written by Scott Fahlman and Rob MacLachlan.
20 ;;; Modified by Bill Chiles to adhere to
21 ;;;
22 (in-package "LISP")
23 (export '(defvar defparameter defconstant when unless setf
24 defsetf define-setf-method psetf shiftf rotatef push pushnew pop
25 incf decf remf case typecase with-open-file
26 with-open-stream with-input-from-string with-output-to-string
27 locally etypecase ctypecase ecase ccase
28 get-setf-method get-setf-method-multiple-value
29 define-modify-macro destructuring-bind nth-value
30 otherwise)) ; Sacred to CASE and related macros.
31
32 (in-package "EXTENSIONS")
33 (export '(do-anonymous collect iterate))
34
35 (in-package "LISP")
36
37
38 ;;; Parse-Body -- Public
39 ;;;
40 ;;; Parse out declarations and doc strings, *not* expanding macros.
41 ;;; Eventually the environment arg should be flushed, since macros can't expand
42 ;;; into declarations anymore.
43 ;;;
44 (defun parse-body (body environment &optional (doc-string-allowed t))
45 "This function is to parse the declarations and doc-string out of the body of
46 a defun-like form. Body is the list of stuff which is to be parsed.
47 Environment is ignored. If Doc-String-Allowed is true, then a doc string
48 will be parsed out of the body and returned. If it is false then a string
49 will terminate the search for declarations. Three values are returned: the
50 tail of Body after the declarations and doc strings, a list of declare forms,
51 and the doc-string, or NIL if none."
52 (declare (ignore environment))
53 (let ((decls ())
54 (doc nil))
55 (do ((tail body (cdr tail)))
56 ((endp tail)
57 (values tail (nreverse decls) doc))
58 (let ((form (car tail)))
59 (cond ((and (stringp form) (cdr tail))
60 (if doc-string-allowed
61 (setq doc form)
62 (return (values tail (nreverse decls) doc))))
63 ((not (and (consp form) (symbolp (car form))))
64 (return (values tail (nreverse decls) doc)))
65 ((eq (car form) 'declare)
66 (push form decls))
67 (t
68 (return (values tail (nreverse decls) doc))))))))
69
70
71 ;;;; DEFMACRO:
72
73 ;;; Defmacro -- Public
74 ;;;
75 ;;; Parse the definition and make an expander function. The actual
76 ;;; definition is done by %defmacro which we expand into.
77 ;;;
78 (defmacro defmacro (name lambda-list &body body)
79 (let ((whole (gensym "WHOLE-"))
80 (environment (gensym "ENV-")))
81 (multiple-value-bind
82 (body local-decs doc)
83 (parse-defmacro lambda-list whole body name 'defmacro
84 :environment environment)
85 (let ((def `(lambda (,whole ,environment)
86 ,@local-decs
87 (block ,name
88 ,body))))
89 `(c::%defmacro ',name #',def ',lambda-list ,doc)))))
90
91
92 ;;; %Defmacro, %%Defmacro -- Internal
93 ;;;
94 ;;; Defmacro expands into %Defmacro which is a function that is treated
95 ;;; magically the compiler. After the compiler has gotten the information it
96 ;;; wants out of macro definition, it compiles a call to %%Defmacro which
97 ;;; happens at load time. We have a %Defmacro function which just calls
98 ;;; %%Defmacro in order to keep the interpreter happy.
99 ;;;
100 ;;; Eventually %%Defmacro should deal with clearing old compiler information
101 ;;; for the functional value.
102 ;;;
103 (defun c::%defmacro (name definition lambda-list doc)
104 (assert (eval:interpreted-function-p definition))
105 (setf (eval:interpreted-function-name definition)
106 (format nil "DEFMACRO ~S" name))
107 (setf (eval:interpreted-function-arglist definition) lambda-list)
108 (c::%%defmacro name definition doc))
109 ;;;
110 (defun c::%%defmacro (name definition doc)
111 (clear-info function where-from name)
112 (setf (macro-function name) definition)
113 (setf (documentation name 'function) doc)
114 name)
115
116
117 ;;; DEFTYPE is a lot like DEFMACRO.
118
119 (defmacro deftype (name arglist &body body)
120 "Syntax like DEFMACRO, but defines a new type."
121 (unless (symbolp name)
122 (error "~S -- Type name not a symbol." name))
123
124 (let ((whole (gensym "WHOLE-")))
125 (multiple-value-bind (body local-decs doc)
126 (parse-defmacro arglist whole body name 'deftype
127 :default-default ''*)
128 `(eval-when (compile load eval)
129 (%deftype ',name
130 #'(lambda (,whole)
131 ,@local-decs
132 (block ,name ,body))
133 ,@(when doc `(,doc)))))))
134 ;;;
135 (defun %deftype (name expander &optional doc)
136 (ecase (info type kind name)
137 (:primitive
138 (error "Illegal to redefine standard type: ~S." name))
139 (:structure
140 (warn "Redefining structure type ~S with DEFTYPE." name)
141 (c::undefine-structure (info type structure-info name)))
142 ((nil :defined)))
143 (setf (info type kind name) :defined)
144 (setf (info type expander name) expander)
145 (when doc
146 (setf (documentation name 'type) doc))
147 ;; ### Bootstrap hack -- we need to define types before %note-type-defined
148 ;; is defined.
149 (when (fboundp 'c::%note-type-defined)
150 (c::%note-type-defined name))
151 name)
152
153
154 ;;; And so is DEFINE-SETF-METHOD.
155
156 (defparameter defsetf-error-string "Setf expander for ~S cannot be called with ~S args.")
157
158 (defmacro define-setf-method (access-fn lambda-list &body body)
159 "Syntax like DEFMACRO, but creates a Setf-Method generator. The body
160 must be a form that returns the five magical values."
161 (unless (symbolp access-fn)
162 (error "~S -- Access-function name not a symbol in DEFINE-SETF-METHOD."
163 access-fn))
164
165 (let ((whole (gensym "WHOLE-"))
166 (environment (gensym "ENV-")))
167 (multiple-value-bind (body local-decs doc)
168 (parse-defmacro lambda-list whole body access-fn
169 'define-setf-method
170 :environment environment)
171 `(eval-when (load compile eval)
172 (setf (info setf inverse ',access-fn) nil)
173 (setf (info setf expander ',access-fn)
174 #'(lambda (,whole ,environment)
175 ,@local-decs
176 (block ,access-fn ,body)))
177 ,@(when doc
178 `((setf (documentation ',access-fn 'setf) ,doc)))
179 ',access-fn))))
180
181
182 ;;;; Destructuring-bind
183
184 (defmacro destructuring-bind (lambda-list arg-list &rest body)
185 "Bind the variables in LAMBDA-LIST to the contents of ARG-LIST."
186 (let* ((arg-list-name (gensym "ARG-LIST-")))
187 (multiple-value-bind
188 (body local-decls)
189 (parse-defmacro lambda-list arg-list-name body nil 'destructuring-bind
190 :annonymousp t :doc-string-allowed nil)
191 `(let ((,arg-list-name ,arg-list))
192 ,@local-decls
193 ,body))))
194
195
196 ;;;; Defun, Defvar, Defparameter, Defconstant:
197
198 ;;; Defun -- Public
199 ;;;
200 ;;; Very similar to Defmacro, but simpler. We don't have to parse the
201 ;;; lambda-list.
202 ;;;
203 (defmacro defun (&whole source name lambda-list &body (body decls doc))
204 (let ((def `(lambda ,lambda-list
205 ,@decls
206 (block ,(if (and (consp name) (eq (car name) 'setf))
207 (cadr name)
208 name)
209 ,@body))))
210 `(c::%defun ',name #',def ,doc ',source)))
211
212
213 ;;; %Defun, %%Defun -- Internal
214 ;;;
215 ;;; Similar to %Defmacro, ...
216 ;;;
217 (defun c::%%defun (name def doc &optional inline-expansion)
218 (setf (fdefinition name) def)
219 (when doc
220 (if (and (consp name) (eq (first name) 'setf))
221 (setf (documentation (second name) 'setf) doc)
222 (setf (documentation name 'function) doc)))
223 (c::define-function-name name)
224 (when (eq (info function where-from name) :assumed)
225 (setf (info function where-from name) :defined)
226 (when (info function assumed-type name)
227 (setf (info function assumed-type name) nil)))
228 (when (or inline-expansion
229 (info function inline-expansion name))
230 (setf (info function inline-expansion name) inline-expansion))
231 name)
232 ;;;
233 (defun c::%defun (name def doc source)
234 (declare (ignore source))
235 (assert (eval:interpreted-function-p def))
236 (setf (eval:interpreted-function-name def) name)
237 (c::%%defun name def doc))
238
239
240 ;;; DEFCONSTANT -- Public
241 ;;;
242 (defmacro defconstant (var val &optional doc)
243 "For defining global constants at top level. The DEFCONSTANT says that the
244 value is constant and may be compiled into code. If the variable already has
245 a value, and this is not equal to the init, an error is signalled. The third
246 argument is an optional documentation string for the variable."
247 `(c::%defconstant ',var ,val ',doc))
248
249 ;;; %Defconstant, %%Defconstant -- Internal
250 ;;;
251 ;;; Like the other %mumbles except that we currently actually do something
252 ;;; interesting at load time, namely checking if the constant is being
253 ;;; redefined.
254 ;;;
255 (defun c::%defconstant (name value doc)
256 (c::%%defconstant name value doc))
257 ;;;
258 (defun c::%%defconstant (name value doc)
259 (when doc
260 (setf (documentation name 'variable) doc))
261 (when (boundp name)
262 (unless (equalp (symbol-value name) value)
263 (cerror "Go ahead and change the value."
264 "Constant ~S being redefined." name)))
265 (setf (symbol-value name) value)
266 (setf (info variable kind name) :constant)
267 (clear-info variable constant-value name)
268 name)
269
270
271 (defmacro defvar (var &optional (val nil valp) (doc nil docp))
272 "For defining global variables at top level. Declares the variable
273 SPECIAL and, optionally, initializes it. If the variable already has a
274 value, the old value is not clobbered. The third argument is an optional
275 documentation string for the variable."
276 `(progn
277 (proclaim '(special ,var))
278 ,@(when valp
279 `((unless (boundp ',var)
280 (setq ,var ,val))))
281 ,@(when docp
282 `((setf (documentation ',var 'variable) ',doc)))
283 ',var))
284
285 (defmacro defparameter (var val &optional (doc nil docp))
286 "Defines a parameter that is not normally changed by the program,
287 but that may be changed without causing an error. Declares the
288 variable special and sets its value to VAL. The third argument is
289 an optional documentation string for the parameter."
290 `(progn
291 (proclaim '(special ,var))
292 (setq ,var ,val)
293 ,@(when docp
294 `((setf (documentation ',var 'variable) ',doc)))
295 ',var))
296
297
298 ;;;; ASSORTED CONTROL STRUCTURES
299
300
301 (defmacro when (test &body forms)
302 "First arg is a predicate. If it is non-null, the rest of the forms are
303 evaluated as a PROGN."
304 `(cond (,test nil ,@forms)))
305
306 (defmacro unless (test &rest forms)
307 "First arg is a predicate. If it is null, the rest of the forms are
308 evaluated as a PROGN."
309 `(cond ((not ,test) nil ,@forms)))
310
311
312 (defmacro return (&optional (value nil))
313 `(return-from nil ,value))
314
315 (defmacro prog (varlist &body (body decls))
316 `(block nil
317 (let ,varlist
318 ,@decls
319 (tagbody ,@body))))
320
321 (defmacro prog* (varlist &body (body decls))
322 `(block nil
323 (let* ,varlist
324 ,@decls
325 (tagbody ,@body))))
326
327
328 ;;; Prog1, Prog2 -- Public
329 ;;;
330 ;;; These just turn into a Let.
331 ;;;
332 (defmacro prog1 (result &rest body)
333 (let ((n-result (gensym)))
334 `(let ((,n-result ,result))
335 ,@body
336 ,n-result)))
337 ;;;
338 (defmacro prog2 (form1 result &rest body)
339 `(prog1 (progn ,form1 ,result) ,@body))
340
341
342 ;;; And, Or -- Public
343 ;;;
344 ;;; AND and OR are defined in terms of IF.
345 ;;;
346 (defmacro and (&rest forms)
347 (cond ((endp forms) t)
348 ((endp (rest forms)) (first forms))
349 (t
350 `(if ,(first forms)
351 (and ,@(rest forms))
352 nil))))
353 ;;;
354 (defmacro or (&rest forms)
355 (cond ((endp forms) nil)
356 ((endp (rest forms)) (first forms))
357 (t
358 (let ((n-result (gensym)))
359 `(let ((,n-result ,(first forms)))
360 (if ,n-result
361 ,n-result
362 (or ,@(rest forms))))))))
363
364
365 ;;; Cond -- Public
366 ;;;
367 ;;; COND also turns into IF.
368 ;;;
369 (defmacro cond (&rest clauses)
370 (if (endp clauses)
371 nil
372 (let ((clause (first clauses)))
373 (when (atom clause)
374 (error "Cond clause is not a list: ~S." clause))
375 (let ((test (first clause))
376 (forms (rest clause)))
377 (if (endp forms)
378 (let ((n-result (gensym)))
379 `(let ((,n-result ,test))
380 (if ,n-result
381 ,n-result
382 (cond ,@(rest clauses)))))
383 `(if ,test
384 (progn ,@forms)
385 (cond ,@(rest clauses))))))))
386
387
388 ;;;; Multiple value macros:
389
390 ;;; Multiple-Value-XXX -- Public
391 ;;;
392 ;;; All the multiple-value receiving forms are defined in terms of
393 ;;; Multiple-Value-Call.
394 ;;;
395 (defmacro multiple-value-setq (varlist value-form)
396 (unless (and (listp varlist) (every #'symbolp varlist))
397 (error "Varlist is not a list of symbols: ~S." varlist))
398 (let ((temps (mapcar #'(lambda (x) (declare (ignore x)) (gensym)) varlist)))
399 `(multiple-value-bind ,temps ,value-form
400 ,@(mapcar #'(lambda (var temp)
401 `(setq ,var ,temp))
402 varlist temps)
403 ,(car temps))))
404 ;;;
405 (defmacro multiple-value-bind (varlist value-form &body body)
406 (unless (and (listp varlist) (every #'symbolp varlist))
407 (error "Varlist is not a list of symbols: ~S." varlist))
408 (if (= (length varlist) 1)
409 `(let ((,(car varlist) ,value-form))
410 ,@body)
411 (let ((ignore (gensym)))
412 `(multiple-value-call #'(lambda (&optional ,@varlist &rest ,ignore)
413 (declare (ignore ,ignore))
414 ,@body)
415 ,value-form))))
416 ;;;
417 (defmacro multiple-value-list (value-form)
418 `(multiple-value-call #'list ,value-form))
419
420
421 (defmacro nth-value (n form)
422 "Evaluates FORM and returns the Nth value (zero based). This involves no
423 consing when N is a trivial constant integer."
424 (if (integerp n)
425 (let ((dummy-list nil)
426 (wendy (gensym)))
427 ;; We build DUMMY-LIST, a list of variables to bind to useless
428 ;; values, then we explicitly IGNORE those bindings and return
429 ;; WENDY, the only thing we're really interested in right now.
430 (dotimes (i n)
431 (push (gensym) dummy-list))
432 `(multiple-value-bind (,@dummy-list ,wendy)
433 ,form
434 (declare (ignore ,@dummy-list))
435 ,wendy))
436 `(nth ,n (multiple-value-list ,form))))
437
438
439 ;;;; SETF and friends.
440
441 ;;; Note: The expansions for SETF and friends sometimes create needless
442 ;;; LET-bindings of argument values. The compiler will remove most of
443 ;;; these spurious bindings, so SETF doesn't worry too much about creating
444 ;;; them.
445
446 ;;; The inverse for a generalized-variable reference function is stored in
447 ;;; one of two ways:
448 ;;;
449 ;;; A SETF-INVERSE property corresponds to the short form of DEFSETF. It is
450 ;;; the name of a function takes the same args as the reference form, plus a
451 ;;; new-value arg at the end.
452 ;;;
453 ;;; A SETF-METHOD-EXPANDER property is created by the long form of DEFSETF or
454 ;;; by DEFINE-SETF-METHOD. It is a function that is called on the reference
455 ;;; form and that produces five values: a list of temporary variables, a list
456 ;;; of value forms, a list of the single store-value form, a storing function,
457 ;;; and an accessing function.
458
459 ;;; Left over in case someone is still trying to call this.
460 (defun foo-get-setf-method (form &optional environment)
461 (get-setf-method form environment))
462
463 (eval-when (compile load eval)
464 ;;;
465 (defun get-setf-method (form &optional environment)
466 "Returns five values needed by the SETF machinery: a list of temporary
467 variables, a list of values with which to fill them, the temporary for the
468 new value in a list, the setting function, and the accessing function."
469 (let (temp)
470 (cond ((symbolp form)
471 (let ((new-var (gensym)))
472 (values nil nil (list new-var) `(setq ,form ,new-var) form)))
473 ((atom form)
474 (error "~S illegal atomic form for GET-SETF-METHOD." form))
475 ;;
476 ;; ### Bootstrap hack...
477 ;; Ignore any DEFSETF info for structure accessors.
478 ((info function accessor-for (car form))
479 (get-setf-method-inverse form `(funcall #'(setf ,(car form))) t))
480 ((setq temp (info setf inverse (car form)))
481 (get-setf-method-inverse form `(,temp) nil))
482 ((setq temp (info setf expander (car form)))
483 (funcall temp form environment))
484 (t
485 (multiple-value-bind (res win)
486 (macroexpand-1 form environment)
487 (if win
488 (get-setf-method res environment)
489 (get-setf-method-inverse form
490 `(funcall #'(setf ,(car form)))
491 t)))))))
492
493 (defun get-setf-method-inverse (form inverse setf-function)
494 (let ((new-var (gensym))
495 (vars nil)
496 (vals nil))
497 (dolist (x (cdr form))
498 (push (gensym) vars)
499 (push x vals))
500 (setq vals (nreverse vals))
501 (values vars vals (list new-var)
502 (if setf-function
503 `(,@inverse ,new-var ,@vars)
504 `(,@inverse ,@vars ,new-var))
505 `(,(car form) ,@vars))))
506
507
508 (defun get-setf-method-multiple-value (form &optional environment)
509 "Like Get-Setf-Method, but may return multiple new-value variables."
510 (get-setf-method form environment))
511
512 (defun defsetter (fn rest)
513 (let ((arglist (car rest))
514 (arglist-var (gensym "ARGS-"))
515 (new-var (car (cadr rest))))
516 (multiple-value-bind
517 (body local-decs doc)
518 (parse-defmacro arglist arglist-var (cddr rest) fn 'defsetf)
519 (values
520 `(lambda (,arglist-var ,new-var)
521 ,@local-decs
522 ,body)
523 doc))))
524
525 ) ; End of Eval-When.
526
527
528 (defmacro defsetf (access-fn &rest rest)
529 "Associates a SETF update function or macro with the specified access
530 function or macro. The format is complex. See the manual for
531 details."
532 (cond ((not (listp (car rest)))
533 `(eval-when (load compile eval)
534 (setf (info setf inverse ',access-fn) ',(car rest))
535 (setf (info setf expander ',access-fn) nil)
536 ,@(if (and (car rest) (stringp (cadr rest)))
537 `((eval-when (load eval)
538 (%put ',access-fn '%setf-documentation ,(cadr rest)))))
539 ',access-fn))
540 ((and (listp (car rest)) (cdr rest) (listp (cadr rest)))
541 (if (not (= (length (cadr rest)) 1))
542 (cerror "Ignore the extra items in the list."
543 "Only one new-value variable allowed in DEFSETF."))
544 (multiple-value-bind (setting-form-generator doc)
545 (defsetter access-fn rest)
546 `(eval-when (load compile eval)
547 (setf (info setf inverse ',access-fn) nil)
548 (setf (info setf expander ',access-fn)
549 #'(lambda (access-form environment)
550 (declare (ignore environment))
551 (do* ((args (cdr access-form) (cdr args))
552 (dummies nil (cons (gensym) dummies))
553 (newval-var (gensym))
554 (new-access-form nil))
555 ((atom args)
556 (setq new-access-form
557 (cons (car access-form) dummies))
558 (values
559 dummies
560 (cdr access-form)
561 (list newval-var)
562 (funcall (function ,setting-form-generator)
563 new-access-form newval-var)
564 new-access-form)))))
565 ,@(if doc
566 `((eval-when (load eval)
567 (setf (info setf documentation ',access-fn) ',doc)))
568 `((eval-when (load eval)
569 (or (clear-info setf documentation ',access-fn)
570 (setf (info setf documentation ',access-fn)
571 nil)))))
572 ',access-fn)))
573 (t (error "Ill-formed DEFSETF for ~S." access-fn))))
574
575 (defmacro setf (&rest args &environment env)
576 "Takes pairs of arguments like SETQ. The first is a place and the second
577 is the value that is supposed to go into that place. Returns the last
578 value. The place argument may be any of the access forms for which SETF
579 knows a corresponding setting form."
580 (let ((temp (length args)))
581 (cond ((= temp 2)
582 (cond ((atom (car args))
583 `(setq ,(car args) ,(cadr args)))
584 ((info function accessor-for (caar args))
585 `(funcall #'(setf ,(caar args)) ,(cadr args) ,@(cdar args)))
586 ((setq temp (info setf inverse (caar args)))
587 `(,temp ,@(cdar args) ,(cadr args)))
588 (t (multiple-value-bind (dummies vals newval setter getter)
589 (get-setf-method (car args) env)
590 (declare (ignore getter))
591 (do* ((d dummies (cdr d))
592 (v vals (cdr v))
593 (let-list nil))
594 ((null d)
595 (setq let-list
596 (nreverse (cons (list (car newval)
597 (cadr args))
598 let-list)))
599 `(let* ,let-list ,setter))
600 (setq let-list
601 (cons (list (car d) (car v)) let-list)))))))
602 ((oddp temp)
603 (error "Odd number of args to SETF."))
604 (t (do ((a args (cddr a)) (l nil))
605 ((null a) `(progn ,@(nreverse l)))
606 (setq l (cons (list 'setf (car a) (cadr a)) l)))))))
607
608
609 (defmacro psetf (&rest args &environment env)
610 "This is to SETF as PSETQ is to SETQ. Args are alternating place
611 expressions and values to go into those places. All of the subforms and
612 values are determined, left to right, and only then are the locations
613 updated. Returns NIL."
614 (do ((a args (cddr a))
615 (let-list nil)
616 (setf-list nil))
617 ((atom a)
618 `(let* ,(nreverse let-list) ,@(nreverse setf-list) nil))
619 (if (atom (cdr a))
620 (error "Odd number of args to PSETF."))
621 (multiple-value-bind (dummies vals newval setter getter)
622 (get-setf-method (car a) env)
623 (declare (ignore getter))
624 (do* ((d dummies (cdr d))
625 (v vals (cdr v)))
626 ((null d))
627 (push (list (car d) (car v)) let-list))
628 (push (list (car newval) (cadr a)) let-list)
629 (push setter setf-list))))
630
631
632
633 (defmacro shiftf (&rest args &environment env)
634 "One or more SETF-style place expressions, followed by a single
635 value expression. Evaluates all of the expressions in turn, then
636 assigns the value of each expression to the place on its left,
637 returning the value of the leftmost."
638 (if (< (length args) 2)
639 (error "Too few argument forms to a SHIFTF."))
640 (let ((leftmost (gensym)))
641 (do ((a args (cdr a))
642 (let-list nil)
643 (setf-list nil)
644 (next-var leftmost))
645 ((atom (cdr a))
646 (push (list next-var (car a)) let-list)
647 `(let* ,(nreverse let-list) ,@(nreverse setf-list) ,leftmost))
648 (multiple-value-bind (dummies vals newval setter getter)
649 (get-setf-method (car a) env)
650 (do* ((d dummies (cdr d))
651 (v vals (cdr v)))
652 ((null d))
653 (push (list (car d) (car v)) let-list))
654 (push (list next-var getter) let-list)
655 (push setter setf-list)
656 (setq next-var (car newval))))))
657
658
659 (defmacro rotatef (&rest args &environment env)
660 "Takes any number of SETF-style place expressions. Evaluates all of the
661 expressions in turn, then assigns to each place the value of the form to
662 its right. The rightmost form gets the value of the leftmost. Returns NIL."
663 (cond ((null args) nil)
664 ((null (cdr args)) `(progn ,(car args) nil))
665 (t (do ((a args (cdr a))
666 (let-list nil)
667 (setf-list nil)
668 (next-var nil)
669 (fix-me nil))
670 ((atom a)
671 (rplaca fix-me next-var)
672 `(let* ,(nreverse let-list) ,@(nreverse setf-list) nil))
673 (multiple-value-bind (dummies vals newval setter getter)
674 (get-setf-method (car a) env)
675 (do ((d dummies (cdr d))
676 (v vals (cdr v)))
677 ((null d))
678 (push (list (car d) (car v)) let-list))
679 (push (list next-var getter) let-list)
680 ;; We don't know the newval variable for the last form yet,
681 ;; so fake it for the first getter and fix it at the end.
682 (unless fix-me (setq fix-me (car let-list)))
683 (push setter setf-list)
684 (setq next-var (car newval)))))))
685
686
687 (defmacro define-modify-macro (name lambda-list function &optional doc-string)
688 "Creates a new read-modify-write macro like PUSH or INCF."
689 (let ((other-args nil)
690 (rest-arg nil)
691 (env (gensym))
692 (reference (gensym)))
693
694 ;; Parse out the variable names and rest arg from the lambda list.
695 (do ((ll lambda-list (cdr ll))
696 (arg nil))
697 ((null ll))
698 (setq arg (car ll))
699 (cond ((eq arg '&optional))
700 ((eq arg '&rest)
701 (if (symbolp (cadr ll))
702 (setq rest-arg (cadr ll))
703 (error "Non-symbol &rest arg in definition of ~S." name))
704 (if (null (cddr ll))
705 (return nil)
706 (error "Illegal stuff after &rest arg in Define-Modify-Macro.")))
707 ((memq arg '(&key &allow-other-keys &aux))
708 (error "~S not allowed in Define-Modify-Macro lambda list." arg))
709 ((symbolp arg)
710 (push arg other-args))
711 ((and (listp arg) (symbolp (car arg)))
712 (push (car arg) other-args))
713 (t (error "Illegal stuff in lambda list of Define-Modify-Macro."))))
714 (setq other-args (nreverse other-args))
715 `(defmacro ,name (,reference ,@lambda-list &environment ,env)
716 ,doc-string
717 (multiple-value-bind (dummies vals newval setter getter)
718 (get-setf-method ,reference ,env)
719 (do ((d dummies (cdr d))
720 (v vals (cdr v))
721 (let-list nil (cons (list (car d) (car v)) let-list)))
722 ((null d)
723 (push
724 (list (car newval)
725 ,(if rest-arg
726 `(list* ',function getter ,@other-args ,rest-arg)
727 `(list ',function getter ,@other-args)))
728 let-list)
729 `(let* ,(nreverse let-list)
730 ,setter)))))))
731
732
733
734 (defmacro push (obj place &environment env)
735 "Takes an object and a location holding a list. Conses the object onto
736 the list, returning the modified list."
737 (if (symbolp place)
738 `(setq ,place (cons ,obj ,place))
739 (multiple-value-bind (dummies vals newval setter getter)
740 (get-setf-method place env)
741 (do* ((d dummies (cdr d))
742 (v vals (cdr v))
743 (let-list nil))
744 ((null d)
745 (push (list (car newval) `(cons ,obj ,getter))
746 let-list)
747 `(let* ,(nreverse let-list)
748 ,setter))
749 (push (list (car d) (car v)) let-list)))))
750
751
752 (defmacro pushnew (obj place &rest keys &environment env)
753 "Takes an object and a location holding a list. If the object is already
754 in the list, does nothing. Else, conses the object onto the list. Returns
755 NIL. If there is a :TEST keyword, this is used for the comparison."
756 (if (symbolp place)
757 `(setq ,place (adjoin ,obj ,place ,@keys))
758 (multiple-value-bind (dummies vals newval setter getter)
759 (get-setf-method place env)
760 (do* ((d dummies (cdr d))
761 (v vals (cdr v))
762 (let-list nil))
763 ((null d)
764 (push (list (car newval) `(adjoin ,obj ,getter ,@keys))
765 let-list)
766 `(let* ,(nreverse let-list)
767 ,setter))
768 (push (list (car d) (car v)) let-list)))))
769
770
771 (defmacro pop (place &environment env)
772 "The argument is a location holding a list. Pops one item off the front
773 of the list and returns it."
774 (if (symbolp place)
775 `(prog1 (car ,place) (setq ,place (cdr ,place)))
776 (multiple-value-bind (dummies vals newval setter getter)
777 (get-setf-method place env)
778 (do* ((d dummies (cdr d))
779 (v vals (cdr v))
780 (let-list nil))
781 ((null d)
782 (push (list (car newval) getter) let-list)
783 `(let* ,(nreverse let-list)
784 (prog1 (car ,(car newval))
785 (setq ,(car newval) (cdr ,(car newval)))
786 ,setter)))
787 (push (list (car d) (car v)) let-list)))))
788
789
790 (define-modify-macro incf (&optional (delta 1)) +
791 "The first argument is some location holding a number. This number is
792 incremented by the second argument, DELTA, which defaults to 1.")
793
794
795 (define-modify-macro decf (&optional (delta 1)) -
796 "The first argument is some location holding a number. This number is
797 decremented by the second argument, DELTA, which defaults to 1.")
798
799
800 (defmacro remf (place indicator &environment env)
801 "Place may be any place expression acceptable to SETF, and is expected
802 to hold a property list or (). This list is destructively altered to
803 remove the property specified by the indicator. Returns T if such a
804 property was present, NIL if not."
805 (multiple-value-bind (dummies vals newval setter getter)
806 (get-setf-method place env)
807 (do* ((d dummies (cdr d))
808 (v vals (cdr v))
809 (let-list nil)
810 (ind-temp (gensym))
811 (local1 (gensym))
812 (local2 (gensym)))
813 ((null d)
814 (push (list (car newval) getter) let-list)
815 (push (list ind-temp indicator) let-list)
816 `(let* ,(nreverse let-list)
817 (do ((,local1 ,(car newval) (cddr ,local1))
818 (,local2 nil ,local1))
819 ((atom ,local1) nil)
820 (cond ((atom (cdr ,local1))
821 (error "Odd-length property list in REMF."))
822 ((eq (car ,local1) ,ind-temp)
823 (cond (,local2
824 (rplacd (cdr ,local2) (cddr ,local1))
825 (return t))
826 (t (setq ,(car newval) (cddr ,(car newval)))
827 ,setter
828 (return t))))))))
829 (push (list (car d) (car v)) let-list))))
830
831
832 ;;; The built-in DEFSETFs.
833
834 (defsetf car %rplaca)
835 (defsetf cdr %rplacd)
836 (defsetf caar (x) (v) `(%rplaca (car ,x) ,v))
837 (defsetf cadr (x) (v) `(%rplaca (cdr ,x) ,v))
838 (defsetf cdar (x) (v) `(%rplacd (car ,x) ,v))
839 (defsetf cddr (x) (v) `(%rplacd (cdr ,x) ,v))
840 (defsetf caaar (x) (v) `(%rplaca (caar ,x) ,v))
841 (defsetf cadar (x) (v) `(%rplaca (cdar ,x) ,v))
842 (defsetf cdaar (x) (v) `(%rplacd (caar ,x) ,v))
843 (defsetf cddar (x) (v) `(%rplacd (cdar ,x) ,v))
844 (defsetf caadr (x) (v) `(%rplaca (cadr ,x) ,v))
845 (defsetf caddr (x) (v) `(%rplaca (cddr ,x) ,v))
846 (defsetf cdadr (x) (v) `(%rplacd (cadr ,x) ,v))
847 (defsetf cdddr (x) (v) `(%rplacd (cddr ,x) ,v))
848 (defsetf caaaar (x) (v) `(%rplaca (caaar ,x) ,v))
849 (defsetf cadaar (x) (v) `(%rplaca (cdaar ,x) ,v))
850 (defsetf cdaaar (x) (v) `(%rplacd (caaar ,x) ,v))
851 (defsetf cddaar (x) (v) `(%rplacd (cdaar ,x) ,v))
852 (defsetf caadar (x) (v) `(%rplaca (cadar ,x) ,v))
853 (defsetf caddar (x) (v) `(%rplaca (cddar ,x) ,v))
854 (defsetf cdadar (x) (v) `(%rplacd (cadar ,x) ,v))
855 (defsetf cdddar (x) (v) `(%rplacd (cddar ,x) ,v))
856 (defsetf caaadr (x) (v) `(%rplaca (caadr ,x) ,v))
857 (defsetf cadadr (x) (v) `(%rplaca (cdadr ,x) ,v))
858 (defsetf cdaadr (x) (v) `(%rplacd (caadr ,x) ,v))
859 (defsetf cddadr (x) (v) `(%rplacd (cdadr ,x) ,v))
860 (defsetf caaddr (x) (v) `(%rplaca (caddr ,x) ,v))
861 (defsetf cadddr (x) (v) `(%rplaca (cdddr ,x) ,v))
862 (defsetf cdaddr (x) (v) `(%rplacd (caddr ,x) ,v))
863 (defsetf cddddr (x) (v) `(%rplacd (cdddr ,x) ,v))
864
865 (defsetf first %rplaca)
866 (defsetf second (x) (v) `(%rplaca (cdr ,x) ,v))
867 (defsetf third (x) (v) `(%rplaca (cddr ,x) ,v))
868 (defsetf fourth (x) (v) `(%rplaca (cdddr ,x) ,v))
869 (defsetf fifth (x) (v) `(%rplaca (cddddr ,x) ,v))
870 (defsetf sixth (x) (v) `(%rplaca (cdr (cddddr ,x)) ,v))
871 (defsetf seventh (x) (v) `(%rplaca (cddr (cddddr ,x)) ,v))
872 (defsetf eighth (x) (v) `(%rplaca (cdddr (cddddr ,x)) ,v))
873 (defsetf ninth (x) (v) `(%rplaca (cddddr (cddddr ,x)) ,v))
874 (defsetf tenth (x) (v) `(%rplaca (cdr (cddddr (cddddr ,x))) ,v))
875 (defsetf rest %rplacd)
876
877 (defsetf elt %setelt)
878 (defsetf aref %aset)
879 (defsetf row-major-aref %set-row-major-aref)
880 (defsetf svref %svset)
881 (defsetf char %charset)
882 (defsetf bit %bitset)
883 (defsetf schar %scharset)
884 (defsetf sbit %sbitset)
885 (defsetf %array-dimension %set-array-dimension)
886 (defsetf %raw-bits %set-raw-bits)
887 (defsetf symbol-value set)
888 (defsetf symbol-function %sp-set-definition)
889 (defsetf symbol-plist %sp-set-plist)
890 (defsetf documentation %set-documentation)
891 (defsetf nth %setnth)
892 (defsetf fill-pointer %set-fill-pointer)
893 (defsetf search-list %set-search-list)
894
895 (defsetf sap-ref-8 %set-sap-ref-8)
896 (defsetf signed-sap-ref-8 %set-sap-ref-8)
897 (defsetf sap-ref-16 %set-sap-ref-16)
898 (defsetf signed-sap-ref-16 %set-sap-ref-16)
899 (defsetf sap-ref-32 %set-sap-ref-32)
900 (defsetf signed-sap-ref-32 %set-sap-ref-32)
901 (defsetf sap-ref-sap %set-sap-ref-sap)
902 (defsetf sap-ref-single %set-sap-ref-single)
903 (defsetf sap-ref-double %set-sap-ref-double)
904
905 (define-setf-method getf (place prop &optional default &environment env)
906 (multiple-value-bind (temps values stores set get)
907 (get-setf-method place env)
908 (let ((newval (gensym))
909 (ptemp (gensym))
910 (def-temp (gensym)))
911 (values `(,@temps ,(car stores) ,ptemp ,@(if default `(,def-temp)))
912 `(,@values ,get ,prop ,@(if default `(,default)))
913 `(,newval)
914 `(progn (setq ,(car stores)
915 (%putf ,(car stores) ,ptemp ,newval))
916 ,set
917 ,newval)
918 `(getf ,(car stores) ,ptemp ,@(if default `(,def-temp)))))))
919
920 (define-setf-method get (symbol prop &optional default)
921 "Get turns into %put. Don't put in the default unless it really is supplied and
922 non-nil, so that we can transform into the get instruction whenever possible."
923 (let ((symbol-temp (gensym))
924 (prop-temp (gensym))
925 (def-temp (gensym))
926 (newval (gensym)))
927 (values `(,symbol-temp ,prop-temp ,@(if default `(,def-temp)))
928 `(,symbol ,prop ,@(if default `(,default)))
929 (list newval)
930 `(%put ,symbol-temp ,prop-temp ,newval)
931 `(get ,symbol-temp ,prop-temp ,@(if default `(,def-temp))))))
932
933 (define-setf-method gethash (key hashtable &optional default)
934 (let ((key-temp (gensym))
935 (hashtable-temp (gensym))
936 (default-temp (gensym))
937 (new-value-temp (gensym)))
938 (values
939 `(,key-temp ,hashtable-temp ,@(if default `(,default-temp)))
940 `(,key ,hashtable ,@(if default `(,default)))
941 `(,new-value-temp)
942 `(%puthash ,key-temp ,hashtable-temp ,new-value-temp)
943 `(gethash ,key-temp ,hashtable-temp ,@(if default `(,default-temp))))))
944
945 (defsetf subseq (sequence start &optional (end nil)) (v)
946 `(progn (replace ,sequence ,v :start1 ,start :end1 ,end)
947 ,v))
948
949
950 ;;; Evil hack invented by the gnomes of Vassar Street. The function
951 ;;; arg must be constant. Get a setf method for this function, pretending
952 ;;; that the final (list) arg to apply is just a normal arg. If the
953 ;;; setting and access forms produced in this way reference this arg at
954 ;;; the end, then just splice the APPLY back onto the front and the right
955 ;;; thing happens.
956
957 (define-setf-method apply (function &rest args &environment env)
958 (if (and (listp function)
959 (= (list-length function) 2)
960 (eq (first function) 'function)
961 (symbolp (second function)))
962 (setq function (second function))
963 (error
964 "Setf of Apply is only defined for function args of form #'symbol."))
965 (multiple-value-bind (dummies vals newval setter getter)
966 (get-setf-method (cons function args) env)
967 ;; Special case aref and svref.
968 (cond ((or (eq function 'aref) (eq function 'svref))
969 (let ((nargs (subseq setter 0 (1- (length setter))))
970 (fcn (if (eq function 'aref) 'lisp::%apply-aset 'lisp::%apply-svset)))
971 (values dummies vals newval
972 `(apply (function ,fcn) ,(car newval) ,@(cdr nargs))
973 `(apply (function ,function) ,@(cdr getter)))))
974 ;; Make sure the place is one that we can handle.
975 (T (unless (and (eq (car (last args)) (car (last vals)))
976 (eq (car (last getter)) (car (last dummies)))
977 (eq (car (last setter)) (car (last dummies))))
978 (error "Apply of ~S not understood as a location for Setf."
979 function))
980 (values dummies vals newval
981 `(apply (function ,(car setter)) ,@(cdr setter))
982 `(apply (function ,(car getter)) ,@(cdr getter)))))))
983
984
985 ;;; Special-case a BYTE bytespec so that the compiler can recognize it.
986 ;;;
987 (define-setf-method ldb (bytespec place &environment env)
988 "The first argument is a byte specifier. The second is any place form
989 acceptable to SETF. Replaces the specified byte of the number in this
990 place with bits from the low-order end of the new value."
991 (multiple-value-bind (dummies vals newval setter getter)
992 (get-setf-method place env)
993 (if (and (consp bytespec) (eq (car bytespec) 'byte))
994 (let ((n-size (gensym))
995 (n-pos (gensym))
996 (n-new (gensym)))
997 (values (list* n-size n-pos dummies)
998 (list* (second bytespec) (third bytespec) vals)
999 (list n-new)
1000 `(let ((,(car newval) (dpb ,n-new (byte ,n-size ,n-pos)
1001 ,getter)))
1002 ,setter
1003 ,n-new)
1004 `(ldb (byte ,n-size ,n-pos) ,getter)))
1005 (let ((btemp (gensym))
1006 (gnuval (gensym)))
1007 (values (cons btemp dummies)
1008 (cons bytespec vals)
1009 (list gnuval)
1010 `(let ((,(car newval) (dpb ,gnuval ,btemp ,getter)))
1011 ,setter
1012 ,gnuval)
1013 `(ldb ,btemp ,getter))))))
1014
1015
1016 (define-setf-method mask-field (bytespec place &environment env)
1017 "The first argument is a byte specifier. The second is any place form
1018 acceptable to SETF. Replaces the specified byte of the number in this place
1019 with bits from the corresponding position in the new value."
1020 (multiple-value-bind (dummies vals newval setter getter)
1021 (get-setf-method place env)
1022 (let ((btemp (gensym))
1023 (gnuval (gensym)))
1024 (values (cons btemp dummies)
1025 (cons bytespec vals)
1026 (list gnuval)
1027 `(let ((,(car newval) (deposit-field ,gnuval ,btemp ,getter)))
1028 ,setter
1029 ,gnuval)
1030 `(mask-field ,btemp ,getter)))))
1031
1032
1033 (define-setf-method the (type place &environment env)
1034 (multiple-value-bind (dummies vals newval setter getter)
1035 (get-setf-method place env)
1036 (values dummies
1037 vals
1038 newval
1039 (subst `(the ,type ,(car newval)) (car newval) setter)
1040 `(the ,type ,getter))))
1041
1042
1043
1044 ;;;; CASE, TYPECASE, & Friends.
1045
1046 (eval-when (compile load eval)
1047
1048 ;;; CASE-BODY returns code for all the standard "case" macros. Name is the
1049 ;;; macro name, and keyform is the thing to case on. Multi-p indicates whether
1050 ;;; a branch may fire off a list of keys; otherwise, a key that is a list is
1051 ;;; interpreted in some way as a single key. When multi-p, test is applied to
1052 ;;; the value of keyform and each key for a given branch; otherwise, test is
1053 ;;; applied to the value of keyform and the entire first element, instead of
1054 ;;; each part, of the case branch. When errorp, no t or otherwise branch is
1055 ;;; permitted, and an ERROR form is generated. When proceedp, it is an error
1056 ;;; to omit errorp, and the ERROR form generated is executed within a
1057 ;;; RESTART-CASE allowing keyform to be set and retested.
1058 ;;;
1059 (defun case-body (name keyform cases multi-p test errorp proceedp)
1060 (let ((keyform-value (gensym))
1061 (clauses ())
1062 (keys ()))
1063 (dolist (case cases)
1064 (cond ((atom case)
1065 (error "~S -- Bad clause in ~S." case name))
1066 ((memq (car case) '(t otherwise))
1067 (if errorp
1068 (error "No default clause allowed in ~S: ~S" name case)
1069 (push `(t nil ,@(rest case)) clauses)))
1070 ((and multi-p (listp (first case)))
1071 (setf keys (append (first case) keys))
1072 (push `((or ,@(mapcar #'(lambda (key)
1073 `(,test ,keyform-value ',key))
1074 (first case)))
1075 nil ,@(rest case))
1076 clauses))
1077 (t
1078 (push (first case) keys)
1079 (push `((,test ,keyform-value
1080 ',(first case)) nil ,@(rest case)) clauses))))
1081 (case-body-aux name keyform keyform-value clauses keys errorp proceedp
1082 `(,(if multi-p 'member 'or) ,@keys))))
1083
1084 ;;; CASE-BODY-AUX provides the expansion once CASE-BODY has groveled all the
1085 ;;; cases. Note: it is not necessary that the resulting code signal
1086 ;;; case-failure conditions, but that's what KMP's prototype code did. We call
1087 ;;; CASE-BODY-ERROR, because of how closures are compiled. RESTART-CASE has
1088 ;;; forms with closures that the compiler causes to be generated at the top of
1089 ;;; any function using the case macros, regardless of whether they are needed.
1090 ;;;
1091 (defun case-body-aux (name keyform keyform-value clauses keys
1092 errorp proceedp expected-type)
1093 (if proceedp
1094 (let ((block (gensym))
1095 (again (gensym)))
1096 `(let ((,keyform-value ,keyform))
1097 (block ,block
1098 (tagbody
1099 ,again
1100 (return-from
1101 ,block
1102 (cond ,@(nreverse clauses)
1103 (t
1104 (setf ,keyform-value
1105 (setf ,keyform
1106 (case-body-error
1107 ',name ',keyform ,keyform-value
1108 ',expected-type ',keys)))
1109 (go ,again))))))))
1110 `(let ((,keyform-value ,keyform))
1111 (cond
1112 ,@(nreverse clauses)
1113 ,@(if errorp
1114 `((t (error 'conditions::case-failure
1115 :name ',name
1116 :datum ,keyform-value
1117 :expected-type ',expected-type
1118 :possibilities ',keys))))))))
1119
1120 ); eval-when
1121
1122 (defun case-body-error (name keyform keyform-value expected-type keys)
1123 (restart-case
1124 (error 'conditions::case-failure
1125 :name name
1126 :datum keyform-value
1127 :expected-type expected-type
1128 :possibilities keys)
1129 (store-value (value)
1130 :report (lambda (stream)
1131 (format stream "Supply a new value for ~S." keyform))
1132 :interactive read-evaluated-form
1133 value)))
1134
1135
1136
1137 (defmacro case (keyform &body cases)
1138 "CASE Keyform {({(Key*) | Key} Form*)}*
1139 Evaluates the Forms in the first clause with a Key EQL to the value of
1140 Keyform. If a singleton key is T then the clause is a default clause."
1141 (case-body 'case keyform cases t 'eql nil nil))
1142
1143 (defmacro ccase (keyform &body cases)
1144 "CCASE Keyform {({(Key*) | Key} Form*)}*
1145 Evaluates the Forms in the first clause with a Key EQL to the value of
1146 Keyform. If none of the keys matches then a correctable error is
1147 signalled."
1148 (case-body 'ccase keyform cases t 'eql t t))
1149
1150 (defmacro ecase (keyform &body cases)
1151 "ECASE Keyform {({(Key*) | Key} Form*)}*
1152 Evaluates the Forms in the first clause with a Key EQL to the value of
1153 Keyform. If none of the keys matches then an error is signalled."
1154 (case-body 'ecase keyform cases t 'eql t nil))
1155
1156 (defmacro typecase (keyform &body cases)
1157 "TYPECASE Keyform {(Type Form*)}*
1158 Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
1159 is true."
1160 (case-body 'typecase keyform cases nil 'typep nil nil))
1161
1162 (defmacro ctypecase (keyform &body cases)
1163 "CTYPECASE Keyform {(Type Form*)}*
1164 Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
1165 is true. If no form is satisfied then a correctable error is signalled."
1166 (case-body 'ctypecase keyform cases nil 'typep t t))
1167
1168 (defmacro etypecase (keyform &body cases)
1169 "ETYPECASE Keyform {(Type Form*)}*
1170 Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
1171 is true. If no form is satisfied then an error is signalled."
1172 (case-body 'etypecase keyform cases nil 'typep t nil))
1173
1174
1175 ;;;; ASSERT and CHECK-TYPE.
1176
1177 ;;; ASSERT is written this way, to call ASSERT-ERROR, because of how closures
1178 ;;; are compiled. RESTART-CASE has forms with closures that the compiler
1179 ;;; causes to be generated at the top of any function using ASSERT, regardless
1180 ;;; of whether they are needed.
1181 ;;;
1182 (defmacro assert (test-form &optional places datum &rest arguments)
1183 "Signals an error if the value of test-form is nil. Continuing from this
1184 error using the CONTINUE restart will allow the user to alter the value of
1185 some locations known to SETF, starting over with test-form. Returns nil."
1186 `(loop
1187 (when ,test-form (return nil))
1188 (assert-error ',test-form ',places ,datum ,@arguments)
1189 ,@(mapcar #'(lambda (place)
1190 `(setf ,place (assert-prompt ',place ,place)))
1191 places)))
1192
1193 (defun assert-error (test-form places datum &rest arguments)
1194 (restart-case (if datum
1195 (apply #'error datum arguments)
1196 (simple-assertion-failure test-form))
1197 (continue ()
1198 :report (lambda (stream) (assert-report places stream))
1199 nil)))
1200
1201 (defun simple-assertion-failure (assertion)
1202 (error 'simple-type-error
1203 :datum assertion
1204 :expected-type nil ;this needs some work in next revision. -kmp
1205 :format-string "The assertion ~S failed."
1206 :format-arguments (list assertion)))
1207
1208 (defun assert-report (names stream)
1209 (format stream "Retry assertion")
1210 (if names
1211 (format stream " with new value~P for ~{~S~^, ~}."
1212 (length names) names)
1213 (format stream ".")))
1214
1215 (defun assert-prompt (name value)
1216 (cond ((y-or-n-p "The old value of ~S is ~S.~
1217 ~%Do you want to supply a new value? "
1218 name value)
1219 (format *query-io* "~&Type a form to be evaluated:~%")
1220 (flet ((read-it () (eval (read *query-io*))))
1221 (if (symbolp name) ;help user debug lexical variables
1222 (progv (list name) (list value) (read-it))
1223 (read-it))))
1224 (t value)))
1225
1226
1227 ;;; CHECK-TYPE is written this way, to call CHECK-TYPE-ERROR, because of how
1228 ;;; closures are compiled. RESTART-CASE has forms with closures that the
1229 ;;; compiler causes to be generated at the top of any function using
1230 ;;; CHECK-TYPE, regardless of whether they are needed. Because it would be
1231 ;;; nice if this were cheap to use, and some things can't afford this excessive
1232 ;;; consing (e.g., READ-CHAR), we bend backwards a little.
1233 ;;;
1234
1235 (defmacro check-type (place type &optional type-string)
1236 "Signals an error of type type-error if the contents of place are not of the
1237 specified type. If an error is signaled, this can only return if
1238 STORE-VALUE is invoked. It will store into place and start over."
1239 (let ((place-value (gensym)))
1240 `(loop
1241 (let ((,place-value ,place))
1242 (when (typep ,place-value ',type) (return nil))
1243 (setf ,place
1244 (check-type-error ',place ,place-value ',type ,type-string))))))
1245
1246 (defun check-type-error (place place-value type type-string)
1247 (restart-case (if type-string
1248 (error 'simple-type-error
1249 :datum place :expected-type type
1250 :format-string
1251 "The value of ~S is ~S, which is not ~A."
1252 :format-arguments
1253 (list place place-value type-string))
1254 (error 'simple-type-error
1255 :datum place :expected-type type
1256 :format-string
1257 "The value of ~S is ~S, which is not of type ~S."
1258 :format-arguments
1259 (list place place-value type)))
1260 (store-value (value)
1261 :report (lambda (stream)
1262 (format stream "Supply a new value of ~S."
1263 place))
1264 :interactive read-evaluated-form
1265 value)))
1266
1267 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
1268 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
1269 ;;; and by CHECK-TYPE.
1270 ;;;
1271 (defun read-evaluated-form ()
1272 (format *query-io* "~&Type a form to be evaluated:~%")
1273 (list (eval (read *query-io*))))
1274
1275
1276 ;;;; With-XXX
1277
1278 (defmacro with-open-file ((var &rest open-args) &body (forms decls))
1279 "Bindspec is of the form (Stream File-Name . Options). The file whose
1280 name is File-Name is opened using the Options and bound to the variable
1281 Stream. If the call to open is unsuccessful, the forms are not
1282 evaluated. The Forms are executed, and when they terminate, normally or
1283 otherwise, the file is closed."
1284 (let ((abortp (gensym)))
1285 `(let ((,var (open ,@open-args))
1286 (,abortp t))
1287 ,@decls
1288 (when ,var
1289 (unwind-protect
1290 (multiple-value-prog1
1291 (progn ,@forms)
1292 (setq ,abortp nil))
1293 (close ,var :abort ,abortp))))))
1294
1295
1296
1297 (defmacro with-open-stream ((var stream) &body (forms decls))
1298 "The form stream should evaluate to a stream. VAR is bound
1299 to the stream and the forms are evaluated as an implicit
1300 progn. The stream is closed upon exit."
1301 (let ((abortp (gensym)))
1302 `(let ((,var ,stream)
1303 (,abortp t))
1304 ,@decls
1305 (unwind-protect
1306 (multiple-value-prog1
1307 (progn ,@forms)
1308 (setq ,abortp nil))
1309 (when ,var
1310 (close ,var :abort ,abortp))))))
1311
1312
1313 (defmacro with-input-from-string ((var string &key index start end) &body (forms decls))
1314 "Binds the Var to an input stream that returns characters from String and
1315 executes the body. See manual for details."
1316 `(let ((,var
1317 ,(if end
1318 `(make-string-input-stream ,string ,(or start 0) ,end)
1319 `(make-string-input-stream ,string ,(or start 0)))))
1320 ,@decls
1321 (unwind-protect
1322 (progn ,@forms)
1323 (close ,var)
1324 ,@(if index `((setf ,index (string-input-stream-current ,var)))))))
1325
1326
1327 (defmacro with-output-to-string ((var &optional string) &body (forms decls))
1328 "If *string* is specified, it must be a string with a fill pointer;
1329 the output is incrementally appended to the string (as if by use of
1330 VECTOR-PUSH-EXTEND)."
1331 (if string
1332 `(let ((,var (make-fill-pointer-output-stream ,string)))
1333 ,@decls
1334 (unwind-protect
1335 (progn ,@forms)
1336 (close ,var)))
1337 `(let ((,var (make-string-output-stream)))
1338 ,@decls
1339 (unwind-protect
1340 (progn ,@forms)
1341 (close ,var))
1342 (get-output-stream-string ,var))))
1343
1344
1345 ;;;; Iteration macros:
1346
1347 (defmacro dotimes ((var count &optional (result nil)) &body body)
1348 (cond ((numberp count)
1349 `(do ((,var 0 (1+ ,var)))
1350 ((>= ,var ,count) ,result)
1351 (declare (type unsigned-byte ,var))
1352 ,@body))
1353 (t (let ((v1 (gensym)))
1354 `(do ((,var 0 (1+ ,var)) (,v1 ,count))
1355 ((>= ,var ,v1) ,result)
1356 (declare (type unsigned-byte ,var))
1357 ,@body)))))
1358
1359
1360 ;;; We repeatedly bind the var instead of setting it so that we never give the
1361 ;;; var a random value such as NIL (which might conflict with a declaration).
1362 ;;; ### Might not be legal...
1363 ;;;
1364 (defmacro dolist ((var list &optional (result nil)) &body body)
1365 (let ((n-list (gensym)))
1366 `(do ((,n-list ,list (cdr ,n-list)))
1367 ((endp ,n-list)
1368 (let ((,var nil))
1369 (declare (ignorable ,var))
1370 ,result))
1371 (let ((,var (car ,n-list)))
1372 ,@body))))
1373
1374
1375 (defmacro do (varlist endlist &body (body decls))
1376 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
1377 Iteration construct. Each Var is initialized in parallel to the value of the
1378 specified Init form. On subsequent iterations, the Vars are assigned the
1379 value of the Step form (if any) in paralell. The Test is evaluated before
1380 each evaluation of the body Forms. When the Test is true, the the Exit-Forms
1381 are evaluated as a PROGN, with the result being the value of the DO. A block
1382 named NIL is established around the entire expansion, allowing RETURN to be
1383 used as an laternate exit mechanism."
1384
1385 (do-do-body varlist endlist body decls 'let 'psetq 'do nil))
1386
1387
1388 (defmacro do* (varlist endlist &body (body decls))
1389 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
1390 Iteration construct. Each Var is initialized sequentially (like LET*) to the
1391 value of the specified Init form. On subsequent iterations, the Vars are
1392 sequentially assigned the value of the Step form (if any). The Test is
1393 evaluated before each evaluation of the body Forms. When the Test is true,
1394 the the Exit-Forms are evaluated as a PROGN, with the result being the value
1395 of the DO. A block named NIL is established around the entire expansion,
1396 allowing RETURN to be used as an laternate exit mechanism."
1397 (do-do-body varlist endlist body decls 'let* 'setq 'do* nil))
1398
1399
1400 ;;;; Miscellaneous macros:
1401
1402 (defmacro locally (&rest forms)
1403 "A form providing a container for locally-scoped variables."
1404 `(let () ,@forms))
1405
1406 (defmacro psetq (&rest pairs)
1407 (do ((lets nil)
1408 (setqs nil)
1409 (pairs pairs (cddr pairs)))
1410 ((atom (cdr pairs))
1411 `(let ,(nreverse lets) (setq ,@(nreverse setqs))))
1412 (let ((gen (gensym)))
1413 (push `(,gen ,(cadr pairs)) lets)
1414 (push (car pairs) setqs)
1415 (push gen setqs))))
1416
1417
1418 ;;;; With-Compilation-Unit:
1419
1420 ;;; True if we are within a With-Compilation-Unit form, which normally causes
1421 ;;; nested uses to be NOOPS.
1422 ;;;
1423 (defvar *in-compilation-unit* nil)
1424
1425 ;;; Count of the number of compilation units dynamically enclosed by the
1426 ;;; current active WITH-COMPILATION-UNIT that were unwound out of.
1427 ;;;
1428 (defvar *aborted-compilation-units*)
1429
1430 ;;; With-Compilation-Unit -- Public
1431 ;;;
1432 ;;;
1433 (defmacro with-compilation-unit (options &body body)
1434 (let ((force nil)
1435 (n-fun (gensym))
1436 (n-abort-p (gensym)))
1437 (when (oddp (length options))
1438 (error "Odd number of key/value pairs: ~S." options))
1439 (do ((opt options (cddr opt)))
1440 ((null opt))
1441 (case (first opt)
1442 (:force
1443 (setq force (second opt)))
1444 (t
1445 (warn "Ignoring unknown option: ~S." (first opt)))))
1446
1447 `(flet ((,n-fun () ,@body))
1448 (if (or ,force (not *in-compilation-unit*))
1449 (let ((c::*undefined-warnings* nil)
1450 (c::*compiler-error-count* 0)
1451 (c::*compiler-warning-count* 0)
1452 (c::*compiler-note-count* 0)
1453 (*in-compilation-unit* t)
1454 (*aborted-compilation-units* 0)
1455 (,n-abort-p t))
1456 (handler-bind ((c::parse-unknown-type
1457 #'(lambda (c)
1458 (c::note-undefined-reference
1459 (c::parse-unknown-type-specifier c)
1460 :type))))
1461 (unwind-protect
1462 (multiple-value-prog1
1463 (,n-fun)
1464 (setq ,n-abort-p nil))
1465 (c::print-summary ,n-abort-p *aborted-compilation-units*))))
1466 (let ((,n-abort-p t))
1467 (unwind-protect
1468 (multiple-value-prog1
1469 (,n-fun)
1470 (setq ,n-abort-p nil))
1471 (when ,n-abort-p
1472 (incf *aborted-compilation-units*))))))))

  ViewVC Help
Powered by ViewVC 1.1.5