1 | ;;; case.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2003-2005 Peter Graves |
---|
4 | ;;; $Id: case.lisp 11391 2008-11-15 22:38:34Z vvoutilainen $ |
---|
5 | ;;; |
---|
6 | ;;; This program is free software; you can redistribute it and/or |
---|
7 | ;;; modify it under the terms of the GNU General Public License |
---|
8 | ;;; as published by the Free Software Foundation; either version 2 |
---|
9 | ;;; of the License, or (at your option) any later version. |
---|
10 | ;;; |
---|
11 | ;;; This program is distributed in the hope that it will be useful, |
---|
12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | ;;; GNU General Public License for more details. |
---|
15 | ;;; |
---|
16 | ;;; You should have received a copy of the GNU General Public License |
---|
17 | ;;; along with this program; if not, write to the Free Software |
---|
18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | ;;; |
---|
20 | ;;; As a special exception, the copyright holders of this library give you |
---|
21 | ;;; permission to link this library with independent modules to produce an |
---|
22 | ;;; executable, regardless of the license terms of these independent |
---|
23 | ;;; modules, and to copy and distribute the resulting executable under |
---|
24 | ;;; terms of your choice, provided that you also meet, for each linked |
---|
25 | ;;; independent module, the terms and conditions of the license of that |
---|
26 | ;;; module. An independent module is a module which is not derived from |
---|
27 | ;;; or based on this library. If you modify this library, you may extend |
---|
28 | ;;; this exception to your version of the library, but you are not |
---|
29 | ;;; obligated to do so. If you do not wish to do so, delete this |
---|
30 | ;;; exception statement from your version. |
---|
31 | |
---|
32 | ;;; Adapted from SBCL. |
---|
33 | |
---|
34 | (in-package #:system) |
---|
35 | |
---|
36 | ;;; Is X a (possibly-improper) list of at least N elements? |
---|
37 | (defun list-of-length-at-least-p (x n) |
---|
38 | (or (zerop n) ; since anything can be considered an improper list of length 0 |
---|
39 | (and (consp x) |
---|
40 | (list-of-length-at-least-p (cdr x) (1- n))))) |
---|
41 | |
---|
42 | (defun case-body-error (name keyform keyform-value expected-type keys) |
---|
43 | (declare (ignore name keys)) |
---|
44 | (restart-case |
---|
45 | (error 'type-error |
---|
46 | :datum keyform-value |
---|
47 | :expected-type expected-type) |
---|
48 | (store-value (value) |
---|
49 | :report (lambda (stream) |
---|
50 | (format stream "Supply a new value for ~S." keyform)) |
---|
51 | :interactive read-evaluated-form |
---|
52 | value))) |
---|
53 | |
---|
54 | ;;; CASE-BODY-AUX provides the expansion once CASE-BODY has groveled |
---|
55 | ;;; all the cases. Note: it is not necessary that the resulting code |
---|
56 | ;;; signal case-failure conditions, but that's what KMP's prototype |
---|
57 | ;;; code did. We call CASE-BODY-ERROR, because of how closures are |
---|
58 | ;;; compiled. RESTART-CASE has forms with closures that the compiler |
---|
59 | ;;; causes to be generated at the top of any function using the case |
---|
60 | ;;; macros, regardless of whether they are needed. |
---|
61 | ;;; |
---|
62 | ;;; The CASE-BODY-ERROR function is defined later, when the |
---|
63 | ;;; RESTART-CASE macro has been defined. |
---|
64 | (defun case-body-aux (name keyform keyform-value clauses keys |
---|
65 | errorp proceedp expected-type) |
---|
66 | (if proceedp |
---|
67 | (let ((block (gensym)) |
---|
68 | (again (gensym))) |
---|
69 | `(let ((,keyform-value ,keyform)) |
---|
70 | (block ,block |
---|
71 | (tagbody |
---|
72 | ,again |
---|
73 | (return-from |
---|
74 | ,block |
---|
75 | (cond ,@(nreverse clauses) |
---|
76 | (t |
---|
77 | (setf ,keyform-value |
---|
78 | (setf ,keyform |
---|
79 | (case-body-error |
---|
80 | ',name ',keyform ,keyform-value |
---|
81 | ',expected-type ',keys))) |
---|
82 | (go ,again)))))))) |
---|
83 | `(let ((,keyform-value ,keyform)) |
---|
84 | (cond |
---|
85 | ,@(nreverse clauses) |
---|
86 | ,@(if errorp |
---|
87 | ;; `((t (error 'case-failure |
---|
88 | ;; :name ',name |
---|
89 | ;; :datum ,keyform-value |
---|
90 | ;; :expected-type ',expected-type |
---|
91 | ;; :possibilities ',keys)))))))) |
---|
92 | `((t (error 'type-error |
---|
93 | :datum ,keyform-value |
---|
94 | :expected-type ',expected-type)))))))) |
---|
95 | |
---|
96 | ;;; CASE-BODY returns code for all the standard "case" macros. NAME is |
---|
97 | ;;; the macro name, and KEYFORM is the thing to case on. MULTI-P |
---|
98 | ;;; indicates whether a branch may fire off a list of keys; otherwise, |
---|
99 | ;;; a key that is a list is interpreted in some way as a single key. |
---|
100 | ;;; When MULTI-P, TEST is applied to the value of KEYFORM and each key |
---|
101 | ;;; for a given branch; otherwise, TEST is applied to the value of |
---|
102 | ;;; KEYFORM and the entire first element, instead of each part, of the |
---|
103 | ;;; case branch. When ERRORP, no T or OTHERWISE branch is permitted, |
---|
104 | ;;; and an ERROR form is generated. When PROCEEDP, it is an error to |
---|
105 | ;;; omit ERRORP, and the ERROR form generated is executed within a |
---|
106 | ;;; RESTART-CASE allowing KEYFORM to be set and retested. |
---|
107 | (defun case-body (name keyform cases multi-p test errorp proceedp needcasesp) |
---|
108 | (unless (or cases (not needcasesp)) |
---|
109 | (warn "no clauses in ~S" name)) |
---|
110 | (let ((keyform-value (gensym)) |
---|
111 | (clauses ()) |
---|
112 | (keys ())) |
---|
113 | (do* ((cases cases (cdr cases)) |
---|
114 | (case (car cases) (car cases))) |
---|
115 | ((null cases) nil) |
---|
116 | (unless (list-of-length-at-least-p case 1) |
---|
117 | (error "~S -- bad clause in ~S" case name)) |
---|
118 | (destructuring-bind (keyoid &rest forms) case |
---|
119 | (cond ((and (memq keyoid '(t otherwise)) |
---|
120 | (null (cdr cases))) |
---|
121 | (if errorp |
---|
122 | (progn |
---|
123 | (style-warn "~@<Treating bare ~A in ~A as introducing a ~ |
---|
124 | normal-clause, not an otherwise-clause~@:>" |
---|
125 | keyoid name) |
---|
126 | (push keyoid keys) |
---|
127 | (push `((,test ,keyform-value ',keyoid) nil ,@forms) |
---|
128 | clauses)) |
---|
129 | (push `(t nil ,@forms) clauses))) |
---|
130 | ((and multi-p (listp keyoid)) |
---|
131 | (setf keys (append keyoid keys)) |
---|
132 | (push `((or ,@(mapcar (lambda (key) |
---|
133 | `(,test ,keyform-value ',key)) |
---|
134 | keyoid)) |
---|
135 | nil |
---|
136 | ,@forms) |
---|
137 | clauses)) |
---|
138 | (t |
---|
139 | (push keyoid keys) |
---|
140 | (push `((,test ,keyform-value ',keyoid) |
---|
141 | nil |
---|
142 | ,@forms) |
---|
143 | clauses))))) |
---|
144 | (case-body-aux name keyform keyform-value clauses keys errorp proceedp |
---|
145 | `(,(if multi-p 'member 'or) ,@keys)))) |
---|
146 | |
---|
147 | (defmacro case (keyform &body cases) |
---|
148 | "CASE Keyform {({(Key*) | Key} Form*)}* |
---|
149 | Evaluates the Forms in the first clause with a Key EQL to the value of |
---|
150 | Keyform. If a singleton key is T then the clause is a default clause." |
---|
151 | (case-body 'case keyform cases t 'eql nil nil nil)) |
---|
152 | |
---|
153 | (defmacro ccase (keyform &body cases) |
---|
154 | "CCASE Keyform {({(Key*) | Key} Form*)}* |
---|
155 | Evaluates the Forms in the first clause with a Key EQL to the value of |
---|
156 | Keyform. If none of the keys matches then a correctable error is |
---|
157 | signalled." |
---|
158 | (case-body 'ccase keyform cases t 'eql t t t)) |
---|
159 | |
---|
160 | (defmacro ecase (keyform &body cases) |
---|
161 | "ECASE Keyform {({(Key*) | Key} Form*)}* |
---|
162 | Evaluates the Forms in the first clause with a Key EQL to the value of |
---|
163 | Keyform. If none of the keys matches then an error is signalled." |
---|
164 | (case-body 'ecase keyform cases t 'eql t nil t)) |
---|
165 | |
---|
166 | (defmacro typecase (keyform &body cases) |
---|
167 | "TYPECASE Keyform {(Type Form*)}* |
---|
168 | Evaluates the Forms in the first clause for which TYPEP of Keyform and Type |
---|
169 | is true." |
---|
170 | (case-body 'typecase keyform cases nil 'typep nil nil nil)) |
---|
171 | |
---|
172 | (defmacro ctypecase (keyform &body cases) |
---|
173 | "CTYPECASE Keyform {(Type Form*)}* |
---|
174 | Evaluates the Forms in the first clause for which TYPEP of Keyform and Type |
---|
175 | is true. If no form is satisfied then a correctable error is signalled." |
---|
176 | (case-body 'ctypecase keyform cases nil 'typep t t t)) |
---|
177 | |
---|
178 | (defmacro etypecase (keyform &body cases) |
---|
179 | "ETYPECASE Keyform {(Type Form*)}* |
---|
180 | Evaluates the Forms in the first clause for which TYPEP of Keyform and Type |
---|
181 | is true. If no form is satisfied then an error is signalled." |
---|
182 | (case-body 'etypecase keyform cases nil 'typep t nil t)) |
---|