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

  ViewVC Help
Powered by ViewVC 1.1.5