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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5