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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5