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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5.1.1 - (hide annotations) (vendor branch)
Sat Mar 24 15:45:13 1990 UTC (24 years ago) by wlott
Changes since 1.5: +17 -4 lines
Added defsetf for row-major-aref

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

  ViewVC Help
Powered by ViewVC 1.1.5