source: trunk/j/src/org/armedbear/lisp/case.lisp @ 6487

Last change on this file since 6487 was 6487, checked in by piso, 18 years ago

Removed IGNORABLE declaration from expanded form.

File size: 6.8 KB
Line 
1;;; case.lisp
2;;;
3;;; Copyright (C) 2003 Peter Graves
4;;; $Id: case.lisp,v 1.2 2004-04-14 14:33:49 piso Exp $
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;;; Adapted from SBCL.
21
22(in-package "SYSTEM")
23
24;;; Is X a (possibly-improper) list of at least N elements?
25(defun list-of-length-at-least-p (x n)
26  (or (zerop n) ; since anything can be considered an improper list of length 0
27      (and (consp x)
28     (list-of-length-at-least-p (cdr x) (1- n)))))
29
30;;; FIXME
31(defun style-warn (&rest args)
32  nil)
33
34(defun case-body-error (name keyform keyform-value expected-type keys)
35  (restart-case
36   #+sbcl
37   (error 'case-failure
38          :name name
39          :datum keyform-value
40          :expected-type expected-type
41          :possibilities keys)
42   #+armedbear
43   (error 'type-error
44          :datum keyform-value
45          :expected-type expected-type)
46   (store-value (value)
47                :report (lambda (stream)
48                          (format stream "Supply a new value for ~S." keyform))
49                :interactive read-evaluated-form
50                value)))
51
52;;; CASE-BODY-AUX provides the expansion once CASE-BODY has groveled
53;;; all the cases. Note: it is not necessary that the resulting code
54;;; signal case-failure conditions, but that's what KMP's prototype
55;;; code did. We call CASE-BODY-ERROR, because of how closures are
56;;; compiled. RESTART-CASE has forms with closures that the compiler
57;;; causes to be generated at the top of any function using the case
58;;; macros, regardless of whether they are needed.
59;;;
60;;; The CASE-BODY-ERROR function is defined later, when the
61;;; RESTART-CASE macro has been defined.
62(defun case-body-aux (name keyform keyform-value clauses keys
63                           errorp proceedp expected-type)
64  (if proceedp
65      (let ((block (gensym))
66      (again (gensym)))
67  `(let ((,keyform-value ,keyform))
68     (block ,block
69       (tagbody
70        ,again
71        (return-from
72         ,block
73         (cond ,@(nreverse clauses)
74         (t
75          (setf ,keyform-value
76          (setf ,keyform
77          (case-body-error
78           ',name ',keyform ,keyform-value
79           ',expected-type ',keys)))
80          (go ,again))))))))
81      `(let ((,keyform-value ,keyform))
82   (cond
83    ,@(nreverse clauses)
84    ,@(if errorp
85;;    `((t (error 'case-failure
86;;          :name ',name
87;;          :datum ,keyform-value
88;;          :expected-type ',expected-type
89;;          :possibilities ',keys))))))))
90    `((t (error 'type-error
91          :datum ,keyform-value
92          :expected-type ',expected-type))))))))
93
94;;; CASE-BODY returns code for all the standard "case" macros. NAME is
95;;; the macro name, and KEYFORM is the thing to case on. MULTI-P
96;;; indicates whether a branch may fire off a list of keys; otherwise,
97;;; a key that is a list is interpreted in some way as a single key.
98;;; When MULTI-P, TEST is applied to the value of KEYFORM and each key
99;;; for a given branch; otherwise, TEST is applied to the value of
100;;; KEYFORM and the entire first element, instead of each part, of the
101;;; case branch. When ERRORP, no T or OTHERWISE branch is permitted,
102;;; and an ERROR form is generated. When PROCEEDP, it is an error to
103;;; omit ERRORP, and the ERROR form generated is executed within a
104;;; RESTART-CASE allowing KEYFORM to be set and retested.
105(defun case-body (name keyform cases multi-p test errorp proceedp needcasesp)
106  (unless (or cases (not needcasesp))
107    (warn "no clauses in ~S" name))
108  (let ((keyform-value (gensym))
109  (clauses ())
110  (keys ()))
111    (do* ((cases cases (cdr cases))
112    (case (car cases) (car cases)))
113   ((null cases) nil)
114      (unless (list-of-length-at-least-p case 1)
115  (error "~S -- bad clause in ~S" case name))
116      (destructuring-bind (keyoid &rest forms) case
117  (cond ((and (memq keyoid '(t otherwise))
118        (null (cdr cases)))
119         (if errorp
120       (progn
121         (style-warn "~@<Treating bare ~A in ~A as introducing a ~
122                                  normal-clause, not an otherwise-clause~@:>"
123         keyoid name)
124         (push keyoid keys)
125         (push `((,test ,keyform-value ',keyoid) nil ,@forms)
126         clauses))
127       (push `(t nil ,@forms) clauses)))
128        ((and multi-p (listp keyoid))
129         (setf keys (append keyoid keys))
130         (push `((or ,@(mapcar (lambda (key)
131               `(,test ,keyform-value ',key))
132             keyoid))
133           nil
134           ,@forms)
135         clauses))
136        (t
137         (push keyoid keys)
138         (push `((,test ,keyform-value ',keyoid)
139           nil
140           ,@forms)
141         clauses)))))
142    (case-body-aux name keyform keyform-value clauses keys errorp proceedp
143       `(,(if multi-p 'member 'or) ,@keys))))
144
145(defmacro case (keyform &body cases)
146  "CASE Keyform {({(Key*) | Key} Form*)}*
147  Evaluates the Forms in the first clause with a Key EQL to the value of
148  Keyform. If a singleton key is T then the clause is a default clause."
149  (case-body 'case keyform cases t 'eql nil nil nil))
150
151(defmacro ccase (keyform &body cases)
152  "CCASE Keyform {({(Key*) | Key} Form*)}*
153  Evaluates the Forms in the first clause with a Key EQL to the value of
154  Keyform. If none of the keys matches then a correctable error is
155  signalled."
156  (case-body 'ccase keyform cases t 'eql t t t))
157
158(defmacro ecase (keyform &body cases)
159  "ECASE Keyform {({(Key*) | Key} Form*)}*
160  Evaluates the Forms in the first clause with a Key EQL to the value of
161  Keyform. If none of the keys matches then an error is signalled."
162  (case-body 'ecase keyform cases t 'eql t nil t))
163
164(defmacro typecase (keyform &body cases)
165  "TYPECASE Keyform {(Type Form*)}*
166  Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
167  is true."
168  (case-body 'typecase keyform cases nil 'typep nil nil nil))
169
170(defmacro ctypecase (keyform &body cases)
171  "CTYPECASE Keyform {(Type Form*)}*
172  Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
173  is true. If no form is satisfied then a correctable error is signalled."
174  (case-body 'ctypecase keyform cases nil 'typep t t t))
175
176(defmacro etypecase (keyform &body cases)
177  "ETYPECASE Keyform {(Type Form*)}*
178  Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
179  is true. If no form is satisfied then an error is signalled."
180  (case-body 'etypecase keyform cases nil 'typep t nil t))
Note: See TracBrowser for help on using the repository browser.