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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5