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

Contents of /src/code/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5