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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.18 - (hide annotations)
Wed Jan 30 15:50:26 1991 UTC (23 years, 2 months ago) by ram
Branch: MAIN
Changes since 1.17: +20 -2 lines
Added Dan's NTH-VALUE macro.
1 ram 1.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 ram 1.18 ;;; $Header: /tiger/var/lib/cvsroots/cmucl/src/code/macros.lisp,v 1.18 1991/01/30 15:50:26 ram Exp $
11 wlott 1.10 ;;;
12 ram 1.1 ;;; 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 wlott 1.10 (in-package "LISP")
19 wlott 1.13 (export '(defvar defparameter defconstant when unless setf
20 ram 1.1 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 ram 1.18 define-modify-macro destructuring-bind nth-value
26 ram 1.1 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 wlott 1.13 (let ((whole (gensym "WHOLE-"))
76     (environment (gensym "ENV-")))
77 ram 1.1 (multiple-value-bind
78     (body local-decs doc)
79 wlott 1.13 (parse-defmacro lambda-list whole body name 'defmacro
80     :environment environment)
81 ram 1.1 (let ((def `(lambda (,whole ,environment)
82     ,@local-decs
83     (block ,name
84     ,body))))
85 wlott 1.13 `(c::%defmacro ',name #',def ',lambda-list ,doc)))))
86 ram 1.1
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 wlott 1.13 (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 ram 1.1 (c::%%defmacro name definition doc))
105     ;;;
106     (defun c::%%defmacro (name definition doc)
107     (clear-info function where-from name)
108 wlott 1.10 (setf (macro-function name) definition)
109 ram 1.1 (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 wlott 1.13 (let ((whole (gensym "WHOLE-")))
121 ram 1.1 (multiple-value-bind (body local-decs doc)
122 wlott 1.13 (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 ram 1.7 ;;;
131     (defun %deftype (name expander &optional doc)
132 ram 1.12 (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 wlott 1.13 (setf (info type kind name) :defined)
140     (setf (info type expander name) expander)
141 ram 1.7 (when doc
142     (setf (documentation name 'type) doc))
143 wlott 1.10 ;; ### Bootstrap hack -- we need to define types before %note-type-defined
144     ;; is defined.
145 ram 1.12 (when (fboundp 'c::%note-type-defined)
146 wlott 1.10 (c::%note-type-defined name))
147 ram 1.7 name)
148    
149 ram 1.1
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 wlott 1.13 (let ((whole (gensym "WHOLE-"))
162     (environment (gensym "ENV-")))
163 ram 1.1 (multiple-value-bind (body local-decs doc)
164     (parse-defmacro lambda-list whole body access-fn
165 wlott 1.13 'define-setf-method
166     :environment environment)
167 ram 1.1 `(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 wlott 1.13
178     ;;;; Destructuring-bind
179 ram 1.1
180 wlott 1.13 (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 wlott 1.14 (body local-decls)
185 wlott 1.17 (parse-defmacro lambda-list arg-list-name body nil 'destructuring-bind
186 wlott 1.13 :annonymousp t :doc-string-allowed nil)
187     `(let ((,arg-list-name ,arg-list))
188 wlott 1.14 ,@local-decls
189 wlott 1.13 ,body))))
190    
191 ram 1.1
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 wlott 1.13 (defmacro defun (&whole source name lambda-list &body (body decls doc))
200 ram 1.1 (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 ram 1.12 (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 ram 1.1 (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 ram 1.3 (assert (eval:interpreted-function-p def))
232     (setf (eval:interpreted-function-name def) name)
233 ram 1.1 (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 ram 1.18
416    
417     (defmacro nth-value (n form)
418     "Evaluates FORM and returns the Nth value (zero based). This involves no
419     consing when N is a trivial constant integer."
420     (if (integerp n)
421     (let ((dummy-list nil)
422     (wendy (gensym)))
423     ;; We build DUMMY-LIST, a list of variables to bind to useless
424     ;; values, then we explicitly IGNORE those bindings and return
425     ;; WENDY, the only thing we're really interested in right now.
426     (dotimes (i n)
427     (push (gensym) dummy-list))
428     `(multiple-value-bind (,@dummy-list ,wendy)
429     ,form
430     (declare (ingore ,@dummy-list))
431     ,wendy))
432     `(nth ,n (multiple-value-list ,form))))
433 ram 1.1
434    
435     ;;;; SETF and friends.
436    
437     ;;; Note: The expansions for SETF and friends sometimes create needless
438     ;;; LET-bindings of argument values. The compiler will remove most of
439     ;;; these spurious bindings, so SETF doesn't worry too much about creating
440     ;;; them.
441    
442     ;;; The inverse for a generalized-variable reference function is stored in
443     ;;; one of two ways:
444     ;;;
445     ;;; A SETF-INVERSE property corresponds to the short form of DEFSETF. It is
446     ;;; the name of a function takes the same args as the reference form, plus a
447     ;;; new-value arg at the end.
448     ;;;
449     ;;; A SETF-METHOD-EXPANDER property is created by the long form of DEFSETF or
450     ;;; by DEFINE-SETF-METHOD. It is a function that is called on the reference
451     ;;; form and that produces five values: a list of temporary variables, a list
452     ;;; of value forms, a list of the single store-value form, a storing function,
453     ;;; and an accessing function.
454    
455 wlott 1.13 ;;; Left over in case someone is still trying to call this.
456     (defun foo-get-setf-method (form &optional environment)
457     (get-setf-method form environment))
458 wlott 1.11
459     (eval-when (compile load eval)
460 ram 1.1 ;;;
461 wlott 1.13 (defun get-setf-method (form &optional environment)
462 ram 1.1 "Returns five values needed by the SETF machinery: a list of temporary
463     variables, a list of values with which to fill them, the temporary for the
464     new value in a list, the setting function, and the accessing function."
465     (let (temp)
466     (cond ((symbolp form)
467     (let ((new-var (gensym)))
468     (values nil nil (list new-var) `(setq ,form ,new-var) form)))
469     ((atom form)
470     (error "~S illegal atomic form for GET-SETF-METHOD." form))
471     ;;
472     ;; ### Bootstrap hack...
473     ;; Ignore any DEFSETF info for structure accessors.
474     ((info function accessor-for (car form))
475 wlott 1.10 (get-setf-method-inverse form `(funcall #'(setf ,(car form))) t))
476 ram 1.1 ((setq temp (info setf inverse (car form)))
477 wlott 1.10 (get-setf-method-inverse form `(,temp) nil))
478 ram 1.1 ((setq temp (info setf expander (car form)))
479     (funcall temp form environment))
480     (t
481     (multiple-value-bind (res win)
482     (macroexpand-1 form environment)
483     (if win
484 wlott 1.13 (get-setf-method res environment)
485 wlott 1.10 (get-setf-method-inverse form
486     `(funcall #'(setf ,(car form)))
487     t)))))))
488 ram 1.1
489 wlott 1.10 (defun get-setf-method-inverse (form inverse setf-function)
490 ram 1.1 (let ((new-var (gensym))
491     (vars nil)
492     (vals nil))
493     (dolist (x (cdr form))
494     (push (gensym) vars)
495     (push x vals))
496     (setq vals (nreverse vals))
497     (values vars vals (list new-var)
498 wlott 1.10 (if setf-function
499     `(,@inverse ,new-var ,@vars)
500     `(,@inverse ,@vars ,new-var))
501 ram 1.1 `(,(car form) ,@vars))))
502    
503    
504     (defun get-setf-method-multiple-value (form &optional environment)
505     "Like Get-Setf-Method, but may return multiple new-value variables."
506     (get-setf-method form environment))
507    
508 wlott 1.16 (defun defsetter (fn rest)
509 wlott 1.14 (let ((arglist (car rest))
510     (arglist-var (gensym "ARGS-"))
511     (new-var (car (cadr rest))))
512     (multiple-value-bind
513     (body local-decs doc)
514 wlott 1.16 (parse-defmacro arglist arglist-var (cddr rest) fn 'defsetf)
515 ram 1.1 (values
516 wlott 1.14 `(lambda (,arglist-var ,new-var)
517     ,@local-decs
518 wlott 1.16 ,body)
519 wlott 1.15 doc))))
520 ram 1.1
521     ) ; End of Eval-When.
522    
523    
524 wlott 1.16 (defmacro defsetf (access-fn &rest rest)
525 ram 1.1 "Associates a SETF update function or macro with the specified access
526     function or macro. The format is complex. See the manual for
527     details."
528     (cond ((not (listp (car rest)))
529     `(eval-when (load compile eval)
530     (setf (info setf inverse ',access-fn) ',(car rest))
531     (setf (info setf expander ',access-fn) nil)
532     ,@(if (and (car rest) (stringp (cadr rest)))
533     `((eval-when (load eval)
534     (%put ',access-fn '%setf-documentation ,(cadr rest)))))
535     ',access-fn))
536     ((and (listp (car rest)) (cdr rest) (listp (cadr rest)))
537     (if (not (= (length (cadr rest)) 1))
538     (cerror "Ignore the extra items in the list."
539     "Only one new-value variable allowed in DEFSETF."))
540     (multiple-value-bind (setting-form-generator doc)
541 wlott 1.16 (defsetter access-fn rest)
542 ram 1.1 `(eval-when (load compile eval)
543     (setf (info setf inverse ',access-fn) nil)
544     (setf (info setf expander ',access-fn)
545     #'(lambda (access-form environment)
546     (declare (ignore environment))
547     (do* ((args (cdr access-form) (cdr args))
548     (dummies nil (cons (gensym) dummies))
549     (newval-var (gensym))
550     (new-access-form nil))
551     ((atom args)
552     (setq new-access-form
553     (cons (car access-form) dummies))
554     (values
555     dummies
556     (cdr access-form)
557     (list newval-var)
558     (funcall (function ,setting-form-generator)
559     new-access-form newval-var)
560     new-access-form)))))
561     ,@(if doc
562     `((eval-when (load eval)
563 wlott 1.10 (setf (info setf documentation ',access-fn) ',doc)))
564     `((eval-when (load eval)
565     (or (clear-info setf documentation ',access-fn)
566     (setf (info setf documentation ',access-fn)
567     nil)))))
568 ram 1.1 ',access-fn)))
569     (t (error "Ill-formed DEFSETF for ~S." access-fn))))
570    
571     (defmacro setf (&rest args &environment env)
572     "Takes pairs of arguments like SETQ. The first is a place and the second
573     is the value that is supposed to go into that place. Returns the last
574     value. The place argument may be any of the access forms for which SETF
575     knows a corresponding setting form."
576     (let ((temp (length args)))
577     (cond ((= temp 2)
578     (cond ((atom (car args))
579     `(setq ,(car args) ,(cadr args)))
580     ((info function accessor-for (caar args))
581 wlott 1.10 `(funcall #'(setf ,(caar args)) ,(cadr args) ,@(cdar args)))
582 ram 1.1 ((setq temp (info setf inverse (caar args)))
583     `(,temp ,@(cdar args) ,(cadr args)))
584     (t (multiple-value-bind (dummies vals newval setter getter)
585 wlott 1.13 (get-setf-method (car args) env)
586 ram 1.1 (declare (ignore getter))
587     (do* ((d dummies (cdr d))
588     (v vals (cdr v))
589     (let-list nil))
590     ((null d)
591     (setq let-list
592     (nreverse (cons (list (car newval)
593     (cadr args))
594     let-list)))
595     `(let* ,let-list ,setter))
596     (setq let-list
597     (cons (list (car d) (car v)) let-list)))))))
598     ((oddp temp)
599     (error "Odd number of args to SETF."))
600     (t (do ((a args (cddr a)) (l nil))
601     ((null a) `(progn ,@(nreverse l)))
602     (setq l (cons (list 'setf (car a) (cadr a)) l)))))))
603    
604    
605     (defmacro psetf (&rest args &environment env)
606     "This is to SETF as PSETQ is to SETQ. Args are alternating place
607     expressions and values to go into those places. All of the subforms and
608     values are determined, left to right, and only then are the locations
609     updated. Returns NIL."
610     (do ((a args (cddr a))
611     (let-list nil)
612     (setf-list nil))
613     ((atom a)
614     `(let* ,(nreverse let-list) ,@(nreverse setf-list) nil))
615     (if (atom (cdr a))
616     (error "Odd number of args to PSETF."))
617     (multiple-value-bind (dummies vals newval setter getter)
618 wlott 1.13 (get-setf-method (car a) env)
619 ram 1.1 (declare (ignore getter))
620     (do* ((d dummies (cdr d))
621     (v vals (cdr v)))
622     ((null d))
623     (push (list (car d) (car v)) let-list))
624     (push (list (car newval) (cadr a)) let-list)
625     (push setter setf-list))))
626    
627    
628    
629     (defmacro shiftf (&rest args &environment env)
630     "One or more SETF-style place expressions, followed by a single
631     value expression. Evaluates all of the expressions in turn, then
632     assigns the value of each expression to the place on its left,
633     returning the value of the leftmost."
634     (if (< (length args) 2)
635     (error "Too few argument forms to a SHIFTF."))
636     (let ((leftmost (gensym)))
637     (do ((a args (cdr a))
638     (let-list nil)
639     (setf-list nil)
640     (next-var leftmost))
641     ((atom (cdr a))
642     (push (list next-var (car a)) let-list)
643     `(let* ,(nreverse let-list) ,@(nreverse setf-list) ,leftmost))
644     (multiple-value-bind (dummies vals newval setter getter)
645 wlott 1.13 (get-setf-method (car a) env)
646 ram 1.1 (do* ((d dummies (cdr d))
647     (v vals (cdr v)))
648     ((null d))
649     (push (list (car d) (car v)) let-list))
650     (push (list next-var getter) let-list)
651     (push setter setf-list)
652     (setq next-var (car newval))))))
653    
654    
655     (defmacro rotatef (&rest args &environment env)
656     "Takes any number of SETF-style place expressions. Evaluates all of the
657     expressions in turn, then assigns to each place the value of the form to
658     its right. The rightmost form gets the value of the leftmost. Returns NIL."
659     (cond ((null args) nil)
660     ((null (cdr args)) `(progn ,(car args) nil))
661     (t (do ((a args (cdr a))
662     (let-list nil)
663     (setf-list nil)
664     (next-var nil)
665     (fix-me nil))
666     ((atom a)
667     (rplaca fix-me next-var)
668     `(let* ,(nreverse let-list) ,@(nreverse setf-list) nil))
669     (multiple-value-bind (dummies vals newval setter getter)
670 wlott 1.13 (get-setf-method (car a) env)
671 ram 1.1 (do ((d dummies (cdr d))
672     (v vals (cdr v)))
673     ((null d))
674     (push (list (car d) (car v)) let-list))
675     (push (list next-var getter) let-list)
676     ;; We don't know the newval variable for the last form yet,
677     ;; so fake it for the first getter and fix it at the end.
678     (unless fix-me (setq fix-me (car let-list)))
679     (push setter setf-list)
680     (setq next-var (car newval)))))))
681    
682    
683     (defmacro define-modify-macro (name lambda-list function &optional doc-string)
684     "Creates a new read-modify-write macro like PUSH or INCF."
685     (let ((other-args nil)
686     (rest-arg nil)
687     (env (gensym))
688     (reference (gensym)))
689    
690     ;; Parse out the variable names and rest arg from the lambda list.
691     (do ((ll lambda-list (cdr ll))
692     (arg nil))
693     ((null ll))
694     (setq arg (car ll))
695     (cond ((eq arg '&optional))
696     ((eq arg '&rest)
697     (if (symbolp (cadr ll))
698     (setq rest-arg (cadr ll))
699     (error "Non-symbol &rest arg in definition of ~S." name))
700     (if (null (cddr ll))
701     (return nil)
702     (error "Illegal stuff after &rest arg in Define-Modify-Macro.")))
703     ((memq arg '(&key &allow-other-keys &aux))
704     (error "~S not allowed in Define-Modify-Macro lambda list." arg))
705     ((symbolp arg)
706     (push arg other-args))
707     ((and (listp arg) (symbolp (car arg)))
708     (push (car arg) other-args))
709     (t (error "Illegal stuff in lambda list of Define-Modify-Macro."))))
710     (setq other-args (nreverse other-args))
711     `(defmacro ,name (,reference ,@lambda-list &environment ,env)
712     ,doc-string
713     (multiple-value-bind (dummies vals newval setter getter)
714 wlott 1.13 (get-setf-method ,reference ,env)
715 ram 1.1 (do ((d dummies (cdr d))
716     (v vals (cdr v))
717     (let-list nil (cons (list (car d) (car v)) let-list)))
718     ((null d)
719     (push
720     (list (car newval)
721     ,(if rest-arg
722     `(list* ',function getter ,@other-args ,rest-arg)
723     `(list ',function getter ,@other-args)))
724     let-list)
725     `(let* ,(nreverse let-list)
726     ,setter)))))))
727    
728    
729    
730     (defmacro push (obj place &environment env)
731     "Takes an object and a location holding a list. Conses the object onto
732     the list, returning the modified list."
733     (if (symbolp place)
734     `(setq ,place (cons ,obj ,place))
735     (multiple-value-bind (dummies vals newval setter getter)
736 wlott 1.13 (get-setf-method place env)
737 ram 1.1 (do* ((d dummies (cdr d))
738     (v vals (cdr v))
739     (let-list nil))
740     ((null d)
741     (push (list (car newval) `(cons ,obj ,getter))
742     let-list)
743     `(let* ,(nreverse let-list)
744     ,setter))
745     (push (list (car d) (car v)) let-list)))))
746    
747    
748     (defmacro pushnew (obj place &rest keys &environment env)
749     "Takes an object and a location holding a list. If the object is already
750     in the list, does nothing. Else, conses the object onto the list. Returns
751     NIL. If there is a :TEST keyword, this is used for the comparison."
752     (if (symbolp place)
753     `(setq ,place (adjoin ,obj ,place ,@keys))
754     (multiple-value-bind (dummies vals newval setter getter)
755 wlott 1.13 (get-setf-method place env)
756 ram 1.1 (do* ((d dummies (cdr d))
757     (v vals (cdr v))
758     (let-list nil))
759     ((null d)
760     (push (list (car newval) `(adjoin ,obj ,getter ,@keys))
761     let-list)
762     `(let* ,(nreverse let-list)
763     ,setter))
764     (push (list (car d) (car v)) let-list)))))
765    
766    
767     (defmacro pop (place &environment env)
768     "The argument is a location holding a list. Pops one item off the front
769     of the list and returns it."
770     (if (symbolp place)
771     `(prog1 (car ,place) (setq ,place (cdr ,place)))
772     (multiple-value-bind (dummies vals newval setter getter)
773 wlott 1.13 (get-setf-method place env)
774 ram 1.1 (do* ((d dummies (cdr d))
775     (v vals (cdr v))
776     (let-list nil))
777     ((null d)
778     (push (list (car newval) getter) let-list)
779     `(let* ,(nreverse let-list)
780     (prog1 (car ,(car newval))
781     (setq ,(car newval) (cdr ,(car newval)))
782     ,setter)))
783     (push (list (car d) (car v)) let-list)))))
784    
785    
786     (define-modify-macro incf (&optional (delta 1)) +
787     "The first argument is some location holding a number. This number is
788     incremented by the second argument, DELTA, which defaults to 1.")
789    
790    
791     (define-modify-macro decf (&optional (delta 1)) -
792     "The first argument is some location holding a number. This number is
793     decremented by the second argument, DELTA, which defaults to 1.")
794    
795    
796     (defmacro remf (place indicator &environment env)
797     "Place may be any place expression acceptable to SETF, and is expected
798     to hold a property list or (). This list is destructively altered to
799     remove the property specified by the indicator. Returns T if such a
800     property was present, NIL if not."
801     (multiple-value-bind (dummies vals newval setter getter)
802 wlott 1.13 (get-setf-method place env)
803 ram 1.1 (do* ((d dummies (cdr d))
804     (v vals (cdr v))
805     (let-list nil)
806     (ind-temp (gensym))
807     (local1 (gensym))
808     (local2 (gensym)))
809     ((null d)
810     (push (list (car newval) getter) let-list)
811     (push (list ind-temp indicator) let-list)
812     `(let* ,(nreverse let-list)
813     (do ((,local1 ,(car newval) (cddr ,local1))
814     (,local2 nil ,local1))
815     ((atom ,local1) nil)
816     (cond ((atom (cdr ,local1))
817     (error "Odd-length property list in REMF."))
818     ((eq (car ,local1) ,ind-temp)
819     (cond (,local2
820     (rplacd (cdr ,local2) (cddr ,local1))
821     (return t))
822     (t (setq ,(car newval) (cddr ,(car newval)))
823     ,setter
824     (return t))))))))
825     (push (list (car d) (car v)) let-list))))
826    
827    
828     ;;; The built-in DEFSETFs.
829    
830     (defsetf car %rplaca)
831     (defsetf cdr %rplacd)
832     (defsetf caar (x) (v) `(%rplaca (car ,x) ,v))
833     (defsetf cadr (x) (v) `(%rplaca (cdr ,x) ,v))
834     (defsetf cdar (x) (v) `(%rplacd (car ,x) ,v))
835     (defsetf cddr (x) (v) `(%rplacd (cdr ,x) ,v))
836     (defsetf caaar (x) (v) `(%rplaca (caar ,x) ,v))
837     (defsetf cadar (x) (v) `(%rplaca (cdar ,x) ,v))
838     (defsetf cdaar (x) (v) `(%rplacd (caar ,x) ,v))
839     (defsetf cddar (x) (v) `(%rplacd (cdar ,x) ,v))
840     (defsetf caadr (x) (v) `(%rplaca (cadr ,x) ,v))
841     (defsetf caddr (x) (v) `(%rplaca (cddr ,x) ,v))
842     (defsetf cdadr (x) (v) `(%rplacd (cadr ,x) ,v))
843     (defsetf cdddr (x) (v) `(%rplacd (cddr ,x) ,v))
844     (defsetf caaaar (x) (v) `(%rplaca (caaar ,x) ,v))
845     (defsetf cadaar (x) (v) `(%rplaca (cdaar ,x) ,v))
846     (defsetf cdaaar (x) (v) `(%rplacd (caaar ,x) ,v))
847     (defsetf cddaar (x) (v) `(%rplacd (cdaar ,x) ,v))
848     (defsetf caadar (x) (v) `(%rplaca (cadar ,x) ,v))
849     (defsetf caddar (x) (v) `(%rplaca (cddar ,x) ,v))
850     (defsetf cdadar (x) (v) `(%rplacd (cadar ,x) ,v))
851     (defsetf cdddar (x) (v) `(%rplacd (cddar ,x) ,v))
852     (defsetf caaadr (x) (v) `(%rplaca (caadr ,x) ,v))
853     (defsetf cadadr (x) (v) `(%rplaca (cdadr ,x) ,v))
854     (defsetf cdaadr (x) (v) `(%rplacd (caadr ,x) ,v))
855     (defsetf cddadr (x) (v) `(%rplacd (cdadr ,x) ,v))
856     (defsetf caaddr (x) (v) `(%rplaca (caddr ,x) ,v))
857     (defsetf cadddr (x) (v) `(%rplaca (cdddr ,x) ,v))
858     (defsetf cdaddr (x) (v) `(%rplacd (caddr ,x) ,v))
859     (defsetf cddddr (x) (v) `(%rplacd (cdddr ,x) ,v))
860    
861     (defsetf first %rplaca)
862     (defsetf second (x) (v) `(%rplaca (cdr ,x) ,v))
863     (defsetf third (x) (v) `(%rplaca (cddr ,x) ,v))
864     (defsetf fourth (x) (v) `(%rplaca (cdddr ,x) ,v))
865     (defsetf fifth (x) (v) `(%rplaca (cddddr ,x) ,v))
866     (defsetf sixth (x) (v) `(%rplaca (cdr (cddddr ,x)) ,v))
867     (defsetf seventh (x) (v) `(%rplaca (cddr (cddddr ,x)) ,v))
868     (defsetf eighth (x) (v) `(%rplaca (cdddr (cddddr ,x)) ,v))
869     (defsetf ninth (x) (v) `(%rplaca (cddddr (cddddr ,x)) ,v))
870     (defsetf tenth (x) (v) `(%rplaca (cdr (cddddr (cddddr ,x))) ,v))
871     (defsetf rest %rplacd)
872    
873     (defsetf elt %setelt)
874     (defsetf aref %aset)
875 wlott 1.10 (defsetf row-major-aref %set-row-major-aref)
876 ram 1.1 (defsetf svref %svset)
877     (defsetf char %charset)
878     (defsetf bit %bitset)
879     (defsetf schar %scharset)
880     (defsetf sbit %sbitset)
881 wlott 1.10 (defsetf %array-dimension %set-array-dimension)
882     (defsetf %raw-bits %set-raw-bits)
883 ram 1.1 (defsetf symbol-value set)
884     (defsetf symbol-function %sp-set-definition)
885     (defsetf symbol-plist %sp-set-plist)
886     (defsetf documentation %set-documentation)
887     (defsetf nth %setnth)
888     (defsetf fill-pointer %set-fill-pointer)
889     (defsetf search-list %set-search-list)
890    
891 wlott 1.10 (defsetf sap-ref-8 %set-sap-ref-8)
892     (defsetf signed-sap-ref-8 %set-sap-ref-8)
893     (defsetf sap-ref-16 %set-sap-ref-16)
894     (defsetf signed-sap-ref-16 %set-sap-ref-16)
895     (defsetf sap-ref-32 %set-sap-ref-32)
896     (defsetf signed-sap-ref-32 %set-sap-ref-32)
897     (defsetf sap-ref-sap %set-sap-ref-sap)
898     (defsetf sap-ref-single %set-sap-ref-single)
899     (defsetf sap-ref-double %set-sap-ref-double)
900 ram 1.1
901     (define-setf-method getf (place prop &optional default &environment env)
902     (multiple-value-bind (temps values stores set get)
903 wlott 1.13 (get-setf-method place env)
904 ram 1.1 (let ((newval (gensym))
905     (ptemp (gensym))
906     (def-temp (gensym)))
907     (values `(,@temps ,(car stores) ,ptemp ,@(if default `(,def-temp)))
908     `(,@values ,get ,prop ,@(if default `(,default)))
909     `(,newval)
910     `(progn (setq ,(car stores)
911 ram 1.9 (%putf ,(car stores) ,ptemp ,newval))
912 ram 1.1 ,set
913     ,newval)
914     `(getf ,(car stores) ,ptemp ,@(if default `(,def-temp)))))))
915    
916     (define-setf-method get (symbol prop &optional default)
917     "Get turns into %put. Don't put in the default unless it really is supplied and
918     non-nil, so that we can transform into the get instruction whenever possible."
919     (let ((symbol-temp (gensym))
920     (prop-temp (gensym))
921     (def-temp (gensym))
922     (newval (gensym)))
923     (values `(,symbol-temp ,prop-temp ,@(if default `(,def-temp)))
924     `(,symbol ,prop ,@(if default `(,default)))
925     (list newval)
926     `(%put ,symbol-temp ,prop-temp ,newval)
927     `(get ,symbol-temp ,prop-temp ,@(if default `(,def-temp))))))
928    
929     (define-setf-method gethash (key hashtable &optional default)
930     (let ((key-temp (gensym))
931     (hashtable-temp (gensym))
932     (default-temp (gensym))
933     (new-value-temp (gensym)))
934     (values
935     `(,key-temp ,hashtable-temp ,@(if default `(,default-temp)))
936     `(,key ,hashtable ,@(if default `(,default)))
937     `(,new-value-temp)
938     `(%puthash ,key-temp ,hashtable-temp ,new-value-temp)
939     `(gethash ,key-temp ,hashtable-temp ,@(if default `(,default-temp))))))
940    
941     (defsetf subseq (sequence start &optional (end nil)) (v)
942     `(progn (replace ,sequence ,v :start1 ,start :end1 ,end)
943     ,v))
944    
945    
946     ;;; Evil hack invented by the gnomes of Vassar Street. The function
947     ;;; arg must be constant. Get a setf method for this function, pretending
948     ;;; that the final (list) arg to apply is just a normal arg. If the
949     ;;; setting and access forms produced in this way reference this arg at
950     ;;; the end, then just splice the APPLY back onto the front and the right
951     ;;; thing happens.
952    
953     (define-setf-method apply (function &rest args &environment env)
954     (if (and (listp function)
955     (= (list-length function) 2)
956     (eq (first function) 'function)
957     (symbolp (second function)))
958     (setq function (second function))
959     (error
960     "Setf of Apply is only defined for function args of form #'symbol."))
961     (multiple-value-bind (dummies vals newval setter getter)
962 wlott 1.13 (get-setf-method (cons function args) env)
963 ram 1.1 ;; Special case aref and svref.
964     (cond ((or (eq function 'aref) (eq function 'svref))
965     (let ((nargs (subseq setter 0 (1- (length setter))))
966     (fcn (if (eq function 'aref) 'lisp::%apply-aset 'lisp::%apply-svset)))
967     (values dummies vals newval
968     `(apply (function ,fcn) ,(car newval) ,@(cdr nargs))
969     `(apply (function ,function) ,@(cdr getter)))))
970     ;; Make sure the place is one that we can handle.
971     (T (unless (and (eq (car (last args)) (car (last vals)))
972     (eq (car (last getter)) (car (last dummies)))
973     (eq (car (last setter)) (car (last dummies))))
974     (error "Apply of ~S not understood as a location for Setf."
975     function))
976     (values dummies vals newval
977     `(apply (function ,(car setter)) ,@(cdr setter))
978     `(apply (function ,(car getter)) ,@(cdr getter)))))))
979    
980    
981 wlott 1.10 ;;; Special-case a BYTE bytespec so that the compiler can recognize it.
982     ;;;
983 ram 1.1 (define-setf-method ldb (bytespec place &environment env)
984     "The first argument is a byte specifier. The second is any place form
985     acceptable to SETF. Replaces the specified byte of the number in this
986     place with bits from the low-order end of the new value."
987     (multiple-value-bind (dummies vals newval setter getter)
988 wlott 1.13 (get-setf-method place env)
989 wlott 1.10 (if (and (consp bytespec) (eq (car bytespec) 'byte))
990     (let ((n-size (gensym))
991     (n-pos (gensym))
992     (n-new (gensym)))
993     (values (list* n-size n-pos dummies)
994     (list* (second bytespec) (third bytespec) vals)
995     (list n-new)
996     `(let ((,(car newval) (dpb ,n-new (byte ,n-size ,n-pos)
997     ,getter)))
998     ,setter
999     ,n-new)
1000     `(ldb (byte ,n-size ,n-pos) ,getter)))
1001     (let ((btemp (gensym))
1002     (gnuval (gensym)))
1003     (values (cons btemp dummies)
1004     (cons bytespec vals)
1005     (list gnuval)
1006     `(let ((,(car newval) (dpb ,gnuval ,btemp ,getter)))
1007     ,setter
1008     ,gnuval)
1009     `(ldb ,btemp ,getter))))))
1010 ram 1.1
1011    
1012     (define-setf-method mask-field (bytespec place &environment env)
1013     "The first argument is a byte specifier. The second is any place form
1014     acceptable to SETF. Replaces the specified byte of the number in this place
1015     with bits from the corresponding position in the new value."
1016     (multiple-value-bind (dummies vals newval setter getter)
1017 wlott 1.13 (get-setf-method place env)
1018 ram 1.1 (let ((btemp (gensym))
1019     (gnuval (gensym)))
1020     (values (cons btemp dummies)
1021     (cons bytespec vals)
1022     (list gnuval)
1023     `(let ((,(car newval) (deposit-field ,gnuval ,btemp ,getter)))
1024     ,setter
1025     ,gnuval)
1026     `(mask-field ,btemp ,getter)))))
1027    
1028    
1029     (define-setf-method the (type place &environment env)
1030     (multiple-value-bind (dummies vals newval setter getter)
1031 wlott 1.13 (get-setf-method place env)
1032 ram 1.1 (values dummies
1033     vals
1034     newval
1035     (subst `(the ,type ,(car newval)) (car newval) setter)
1036     `(the ,type ,getter))))
1037    
1038    
1039    
1040     ;;;; CASE, TYPECASE, & Friends.
1041    
1042     (eval-when (compile load eval)
1043    
1044     ;;; CASE-BODY returns code for all the standard "case" macros. Name is the
1045     ;;; macro name, and keyform is the thing to case on. Multi-p indicates whether
1046     ;;; a branch may fire off a list of keys; otherwise, a key that is a list is
1047     ;;; interpreted in some way as a single key. When multi-p, test is applied to
1048     ;;; the value of keyform and each key for a given branch; otherwise, test is
1049     ;;; applied to the value of keyform and the entire first element, instead of
1050     ;;; each part, of the case branch. When errorp, no t or otherwise branch is
1051     ;;; permitted, and an ERROR form is generated. When proceedp, it is an error
1052     ;;; to omit errorp, and the ERROR form generated is executed within a
1053     ;;; RESTART-CASE allowing keyform to be set and retested.
1054     ;;;
1055     (defun case-body (name keyform cases multi-p test errorp proceedp)
1056     (let ((keyform-value (gensym))
1057     (clauses ())
1058     (keys ()))
1059     (dolist (case cases)
1060     (cond ((atom case)
1061     (error "~S -- Bad clause in ~S." case name))
1062     ((memq (car case) '(t otherwise))
1063     (if errorp
1064     (error "No default clause allowed in ~S: ~S" name case)
1065     (push `(t nil ,@(rest case)) clauses)))
1066     ((and multi-p (listp (first case)))
1067     (setf keys (append (first case) keys))
1068     (push `((or ,@(mapcar #'(lambda (key)
1069     `(,test ,keyform-value ',key))
1070     (first case)))
1071     nil ,@(rest case))
1072     clauses))
1073     (t
1074     (push (first case) keys)
1075     (push `((,test ,keyform-value
1076     ',(first case)) nil ,@(rest case)) clauses))))
1077     (case-body-aux name keyform keyform-value clauses keys errorp proceedp
1078     `(,(if multi-p 'member 'or) ,@keys))))
1079    
1080     ;;; CASE-BODY-AUX provides the expansion once CASE-BODY has groveled all the
1081     ;;; cases. Note: it is not necessary that the resulting code signal
1082     ;;; case-failure conditions, but that's what KMP's prototype code did. We call
1083     ;;; CASE-BODY-ERROR, because of how closures are compiled. RESTART-CASE has
1084     ;;; forms with closures that the compiler causes to be generated at the top of
1085     ;;; any function using the case macros, regardless of whether they are needed.
1086     ;;;
1087     (defun case-body-aux (name keyform keyform-value clauses keys
1088     errorp proceedp expected-type)
1089     (if proceedp
1090     (let ((block (gensym))
1091     (again (gensym)))
1092     `(let ((,keyform-value ,keyform))
1093     (block ,block
1094     (tagbody
1095     ,again
1096     (return-from
1097     ,block
1098     (cond ,@(nreverse clauses)
1099     (t
1100     (setf ,keyform-value
1101     (setf ,keyform
1102     (case-body-error
1103     ',name ',keyform ,keyform-value
1104     ',expected-type ',keys)))
1105     (go ,again))))))))
1106     `(let ((,keyform-value ,keyform))
1107     (cond
1108     ,@(nreverse clauses)
1109     ,@(if errorp
1110     `((t (error 'conditions::case-failure
1111     :name ',name
1112     :datum ,keyform-value
1113     :expected-type ',expected-type
1114     :possibilities ',keys))))))))
1115    
1116     ); eval-when
1117    
1118     (defun case-body-error (name keyform keyform-value expected-type keys)
1119     (restart-case
1120     (error 'conditions::case-failure
1121     :name name
1122     :datum keyform-value
1123     :expected-type expected-type
1124     :possibilities keys)
1125     (store-value (value)
1126     :report (lambda (stream)
1127     (format stream "Supply a new value for ~S." keyform))
1128     :interactive read-evaluated-form
1129     value)))
1130    
1131    
1132    
1133     (defmacro case (keyform &body cases)
1134     "CASE Keyform {({(Key*) | Key} Form*)}*
1135     Evaluates the Forms in the first clause with a Key EQL to the value of
1136     Keyform. If a singleton key is T then the clause is a default clause."
1137     (case-body 'case keyform cases t 'eql nil nil))
1138    
1139     (defmacro ccase (keyform &body cases)
1140     "CCASE 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 a correctable error is
1143     signalled."
1144     (case-body 'ccase keyform cases t 'eql t t))
1145    
1146     (defmacro ecase (keyform &body cases)
1147     "ECASE Keyform {({(Key*) | Key} Form*)}*
1148     Evaluates the Forms in the first clause with a Key EQL to the value of
1149     Keyform. If none of the keys matches then an error is signalled."
1150     (case-body 'ecase keyform cases t 'eql t nil))
1151    
1152     (defmacro typecase (keyform &body cases)
1153     "TYPECASE Keyform {(Type Form*)}*
1154     Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
1155     is true."
1156     (case-body 'typecase keyform cases nil 'typep nil nil))
1157    
1158     (defmacro ctypecase (keyform &body cases)
1159     "CTYPECASE Keyform {(Type Form*)}*
1160     Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
1161     is true. If no form is satisfied then a correctable error is signalled."
1162     (case-body 'ctypecase keyform cases nil 'typep t t))
1163    
1164     (defmacro etypecase (keyform &body cases)
1165     "ETYPECASE Keyform {(Type Form*)}*
1166     Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
1167     is true. If no form is satisfied then an error is signalled."
1168     (case-body 'etypecase keyform cases nil 'typep t nil))
1169    
1170    
1171     ;;;; ASSERT and CHECK-TYPE.
1172    
1173     ;;; ASSERT is written this way, to call ASSERT-ERROR, because of how closures
1174     ;;; are compiled. RESTART-CASE has forms with closures that the compiler
1175     ;;; causes to be generated at the top of any function using ASSERT, regardless
1176     ;;; of whether they are needed.
1177     ;;;
1178     (defmacro assert (test-form &optional places datum &rest arguments)
1179     "Signals an error if the value of test-form is nil. Continuing from this
1180     error using the CONTINUE restart will allow the user to alter the value of
1181     some locations known to SETF, starting over with test-form. Returns nil."
1182     `(loop
1183     (when ,test-form (return nil))
1184     (assert-error ',test-form ',places ,datum ,@arguments)
1185     ,@(mapcar #'(lambda (place)
1186     `(setf ,place (assert-prompt ',place ,place)))
1187     places)))
1188    
1189     (defun assert-error (test-form places datum &rest arguments)
1190     (restart-case (if datum
1191     (apply #'error datum arguments)
1192     (simple-assertion-failure test-form))
1193     (continue ()
1194     :report (lambda (stream) (assert-report places stream))
1195     nil)))
1196    
1197     (defun simple-assertion-failure (assertion)
1198     (error 'simple-type-error
1199     :datum assertion
1200     :expected-type nil ;this needs some work in next revision. -kmp
1201     :format-string "The assertion ~S failed."
1202     :format-arguments (list assertion)))
1203    
1204     (defun assert-report (names stream)
1205     (format stream "Retry assertion")
1206     (if names
1207     (format stream " with new value~P for ~{~S~^, ~}."
1208     (length names) names)
1209     (format stream ".")))
1210    
1211     (defun assert-prompt (name value)
1212     (cond ((y-or-n-p "The old value of ~S is ~S.~
1213     ~%Do you want to supply a new value? "
1214     name value)
1215     (format *query-io* "~&Type a form to be evaluated:~%")
1216     (flet ((read-it () (eval (read *query-io*))))
1217     (if (symbolp name) ;help user debug lexical variables
1218     (progv (list name) (list value) (read-it))
1219     (read-it))))
1220     (t value)))
1221    
1222    
1223     ;;; CHECK-TYPE is written this way, to call CHECK-TYPE-ERROR, because of how
1224     ;;; closures are compiled. RESTART-CASE has forms with closures that the
1225     ;;; compiler causes to be generated at the top of any function using
1226     ;;; CHECK-TYPE, regardless of whether they are needed. Because it would be
1227     ;;; nice if this were cheap to use, and some things can't afford this excessive
1228     ;;; consing (e.g., READ-CHAR), we bend backwards a little.
1229     ;;;
1230    
1231     (defmacro check-type (place type &optional type-string)
1232     "Signals an error of type type-error if the contents of place are not of the
1233     specified type. If an error is signaled, this can only return if
1234     STORE-VALUE is invoked. It will store into place and start over."
1235     (let ((place-value (gensym)))
1236     `(loop
1237     (let ((,place-value ,place))
1238     (when (typep ,place-value ',type) (return nil))
1239     (setf ,place
1240     (check-type-error ',place ,place-value ',type ,type-string))))))
1241    
1242     (defun check-type-error (place place-value type type-string)
1243     (restart-case (if type-string
1244     (error 'simple-type-error
1245     :datum place :expected-type type
1246     :format-string
1247     "The value of ~S is ~S, which is not ~A."
1248     :format-arguments
1249     (list place place-value type-string))
1250     (error 'simple-type-error
1251     :datum place :expected-type type
1252     :format-string
1253     "The value of ~S is ~S, which is not of type ~S."
1254     :format-arguments
1255     (list place place-value type)))
1256     (store-value (value)
1257     :report (lambda (stream)
1258     (format stream "Supply a new value of ~S."
1259     place))
1260     :interactive read-evaluated-form
1261     value)))
1262    
1263     ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
1264     ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
1265     ;;; and by CHECK-TYPE.
1266     ;;;
1267     (defun read-evaluated-form ()
1268     (format *query-io* "~&Type a form to be evaluated:~%")
1269     (list (eval (read *query-io*))))
1270    
1271    
1272     ;;;; With-XXX
1273    
1274     (defmacro with-open-file ((var &rest open-args) &body (forms decls))
1275 ram 1.5 "Bindspec is of the form (Stream File-Name . Options). The file whose
1276     name is File-Name is opened using the Options and bound to the variable
1277     Stream. If the call to open is unsuccessful, the forms are not
1278     evaluated. The Forms are executed, and when they terminate, normally or
1279     otherwise, the file is closed."
1280 ram 1.1 (let ((abortp (gensym)))
1281     `(let ((,var (open ,@open-args))
1282     (,abortp t))
1283     ,@decls
1284 ram 1.5 (when ,var
1285     (unwind-protect
1286     (multiple-value-prog1
1287     (progn ,@forms)
1288     (setq ,abortp nil))
1289 ram 1.1 (close ,var :abort ,abortp))))))
1290    
1291    
1292 ram 1.5
1293 ram 1.1 (defmacro with-open-stream ((var stream) &body (forms decls))
1294     "The form stream should evaluate to a stream. VAR is bound
1295     to the stream and the forms are evaluated as an implicit
1296     progn. The stream is closed upon exit."
1297     (let ((abortp (gensym)))
1298     `(let ((,var ,stream)
1299     (,abortp t))
1300     ,@decls
1301     (unwind-protect
1302     (multiple-value-prog1
1303     (progn ,@forms)
1304     (setq ,abortp nil))
1305     (when ,var
1306     (close ,var :abort ,abortp))))))
1307    
1308    
1309     (defmacro with-input-from-string ((var string &key index start end) &body (forms decls))
1310     "Binds the Var to an input stream that returns characters from String and
1311     executes the body. See manual for details."
1312     `(let ((,var
1313     ,(if end
1314     `(make-string-input-stream ,string ,(or start 0) ,end)
1315     `(make-string-input-stream ,string ,(or start 0)))))
1316     ,@decls
1317     (unwind-protect
1318     (progn ,@forms)
1319     (close ,var)
1320     ,@(if index `((setf ,index (string-input-stream-current ,var)))))))
1321    
1322    
1323     (defmacro with-output-to-string ((var &optional string) &body (forms decls))
1324 ram 1.5 "If *string* is specified, it must be a string with a fill pointer;
1325     the output is incrementally appended to the string (as if by use of
1326     VECTOR-PUSH-EXTEND)."
1327 ram 1.1 (if string
1328     `(let ((,var (make-fill-pointer-output-stream ,string)))
1329     ,@decls
1330     (unwind-protect
1331     (progn ,@forms)
1332     (close ,var)))
1333     `(let ((,var (make-string-output-stream)))
1334     ,@decls
1335     (unwind-protect
1336     (progn ,@forms)
1337     (close ,var))
1338     (get-output-stream-string ,var))))
1339    
1340    
1341     ;;;; Iteration macros:
1342    
1343     (defmacro dotimes ((var count &optional (result nil)) &body body)
1344     (cond ((numberp count)
1345     `(do ((,var 0 (1+ ,var)))
1346     ((>= ,var ,count) ,result)
1347 ram 1.8 (declare (type unsigned-byte ,var))
1348 ram 1.1 ,@body))
1349     (t (let ((v1 (gensym)))
1350     `(do ((,var 0 (1+ ,var)) (,v1 ,count))
1351     ((>= ,var ,v1) ,result)
1352 ram 1.8 (declare (type unsigned-byte ,var))
1353 ram 1.1 ,@body)))))
1354    
1355    
1356     ;;; We repeatedly bind the var instead of setting it so that we never give the
1357     ;;; var a random value such as NIL (which might conflict with a declaration).
1358     ;;; ### Might not be legal...
1359     ;;;
1360     (defmacro dolist ((var list &optional (result nil)) &body body)
1361     (let ((n-list (gensym)))
1362     `(do ((,n-list ,list (cdr ,n-list)))
1363     ((endp ,n-list)
1364     (let ((,var nil))
1365     (declare (ignorable ,var))
1366     ,result))
1367     (let ((,var (car ,n-list)))
1368     ,@body))))
1369    
1370    
1371     (defmacro do (varlist endlist &body (body decls))
1372     "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
1373     Iteration construct. Each Var is initialized in parallel to the value of the
1374     specified Init form. On subsequent iterations, the Vars are assigned the
1375     value of the Step form (if any) in paralell. The Test is evaluated before
1376     each evaluation of the body Forms. When the Test is true, the the Exit-Forms
1377     are evaluated as a PROGN, with the result being the value of the DO. A block
1378     named NIL is established around the entire expansion, allowing RETURN to be
1379     used as an laternate exit mechanism."
1380    
1381     (do-do-body varlist endlist body decls 'let 'psetq 'do nil))
1382    
1383    
1384     (defmacro do* (varlist endlist &body (body decls))
1385     "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
1386     Iteration construct. Each Var is initialized sequentially (like LET*) to the
1387     value of the specified Init form. On subsequent iterations, the Vars are
1388     sequentially assigned the value of the Step form (if any). The Test is
1389     evaluated before each evaluation of the body Forms. When the Test is true,
1390     the the Exit-Forms are evaluated as a PROGN, with the result being the value
1391     of the DO. A block named NIL is established around the entire expansion,
1392     allowing RETURN to be used as an laternate exit mechanism."
1393     (do-do-body varlist endlist body decls 'let* 'setq 'do* nil))
1394    
1395    
1396     ;;;; Miscellaneous macros:
1397    
1398     (defmacro locally (&rest forms)
1399     "A form providing a container for locally-scoped variables."
1400     `(let () ,@forms))
1401    
1402     (defmacro psetq (&rest pairs)
1403     (do ((lets nil)
1404     (setqs nil)
1405     (pairs pairs (cddr pairs)))
1406     ((atom (cdr pairs))
1407     `(let ,(nreverse lets) (setq ,@(nreverse setqs))))
1408     (let ((gen (gensym)))
1409     (push `(,gen ,(cadr pairs)) lets)
1410     (push (car pairs) setqs)
1411     (push gen setqs))))
1412    
1413    
1414     ;;;; With-Compilation-Unit:
1415    
1416     ;;; True if we are within a With-Compilation-Unit form, which normally causes
1417     ;;; nested uses to be NOOPS.
1418     ;;;
1419     (defvar *in-compilation-unit* nil)
1420    
1421     ;;; Count of the number of compilation units dynamically enclosed by the
1422     ;;; current active WITH-COMPILATION-UNIT that were unwound out of.
1423     ;;;
1424     (defvar *aborted-compilation-units*)
1425    
1426     ;;; With-Compilation-Unit -- Public
1427     ;;;
1428     ;;;
1429     (defmacro with-compilation-unit (options &body body)
1430     (let ((force nil)
1431     (n-fun (gensym))
1432     (n-abort-p (gensym)))
1433     (when (oddp (length options))
1434     (error "Odd number of key/value pairs: ~S." options))
1435     (do ((opt options (cddr opt)))
1436     ((null opt))
1437     (case (first opt)
1438     (:force
1439     (setq force (second opt)))
1440     (t
1441     (warn "Ignoring unknown option: ~S." (first opt)))))
1442    
1443     `(flet ((,n-fun () ,@body))
1444     (if (or ,force (not *in-compilation-unit*))
1445 ram 1.6 (let ((c::*undefined-warnings* nil)
1446 ram 1.1 (c::*compiler-error-count* 0)
1447     (c::*compiler-warning-count* 0)
1448     (c::*compiler-note-count* 0)
1449     (*in-compilation-unit* t)
1450     (*aborted-compilation-units* 0)
1451     (,n-abort-p t))
1452 ram 1.6 (handler-bind ((c::parse-unknown-type
1453     #'(lambda (c)
1454     (c::note-undefined-reference
1455     (c::parse-unknown-type-specifier c)
1456     :type))))
1457     (unwind-protect
1458     (multiple-value-prog1
1459     (,n-fun)
1460     (setq ,n-abort-p nil))
1461     (c::print-summary ,n-abort-p *aborted-compilation-units*))))
1462 ram 1.1 (let ((,n-abort-p t))
1463     (unwind-protect
1464     (multiple-value-prog1
1465     (,n-fun)
1466     (setq ,n-abort-p nil))
1467     (when ,n-abort-p
1468     (incf *aborted-compilation-units*))))))))

  ViewVC Help
Powered by ViewVC 1.1.5