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

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

Initial checkin.

File size: 6.8 KB
Line 
1;;; case.lisp
2;;;
3;;; Copyright (C) 2003 Peter Graves
4;;; $Id: case.lisp,v 1.1 2003-12-06 15:42:05 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   (declare (ignorable ,keyform-value)) ; e.g. (CASE KEY (T))
83   (cond
84    ,@(nreverse clauses)
85    ,@(if errorp
86;;    `((t (error 'case-failure
87;;          :name ',name
88;;          :datum ,keyform-value
89;;          :expected-type ',expected-type
90;;          :possibilities ',keys))))))))
91    `((t (error 'type-error
92          :datum ,keyform-value
93          :expected-type ',expected-type))))))))
94
95;;; CASE-BODY returns code for all the standard "case" macros. NAME is
96;;; the macro name, and KEYFORM is the thing to case on. MULTI-P
97;;; indicates whether a branch may fire off a list of keys; otherwise,
98;;; a key that is a list is interpreted in some way as a single key.
99;;; When MULTI-P, TEST is applied to the value of KEYFORM and each key
100;;; for a given branch; otherwise, TEST is applied to the value of
101;;; KEYFORM and the entire first element, instead of each part, of the
102;;; case branch. When ERRORP, no T or OTHERWISE branch is permitted,
103;;; and an ERROR form is generated. When PROCEEDP, it is an error to
104;;; omit ERRORP, and the ERROR form generated is executed within a
105;;; RESTART-CASE allowing KEYFORM to be set and retested.
106(defun case-body (name keyform cases multi-p test errorp proceedp needcasesp)
107  (unless (or cases (not needcasesp))
108    (warn "no clauses in ~S" name))
109  (let ((keyform-value (gensym))
110  (clauses ())
111  (keys ()))
112    (do* ((cases cases (cdr cases))
113    (case (car cases) (car cases)))
114   ((null cases) nil)
115      (unless (list-of-length-at-least-p case 1)
116  (error "~S -- bad clause in ~S" case name))
117      (destructuring-bind (keyoid &rest forms) case
118  (cond ((and (memq keyoid '(t otherwise))
119        (null (cdr cases)))
120         (if errorp
121       (progn
122         (style-warn "~@<Treating bare ~A in ~A as introducing a ~
123                                  normal-clause, not an otherwise-clause~@:>"
124         keyoid name)
125         (push keyoid keys)
126         (push `((,test ,keyform-value ',keyoid) nil ,@forms)
127         clauses))
128       (push `(t nil ,@forms) clauses)))
129        ((and multi-p (listp keyoid))
130         (setf keys (append keyoid keys))
131         (push `((or ,@(mapcar (lambda (key)
132               `(,test ,keyform-value ',key))
133             keyoid))
134           nil
135           ,@forms)
136         clauses))
137        (t
138         (push keyoid keys)
139         (push `((,test ,keyform-value ',keyoid)
140           nil
141           ,@forms)
142         clauses)))))
143    (case-body-aux name keyform keyform-value clauses keys errorp proceedp
144       `(,(if multi-p 'member 'or) ,@keys))))
145
146(defmacro case (keyform &body cases)
147  "CASE Keyform {({(Key*) | Key} Form*)}*
148  Evaluates the Forms in the first clause with a Key EQL to the value of
149  Keyform. If a singleton key is T then the clause is a default clause."
150  (case-body 'case keyform cases t 'eql nil nil nil))
151
152(defmacro ccase (keyform &body cases)
153  "CCASE Keyform {({(Key*) | Key} Form*)}*
154  Evaluates the Forms in the first clause with a Key EQL to the value of
155  Keyform. If none of the keys matches then a correctable error is
156  signalled."
157  (case-body 'ccase keyform cases t 'eql t t t))
158
159(defmacro ecase (keyform &body cases)
160  "ECASE Keyform {({(Key*) | Key} Form*)}*
161  Evaluates the Forms in the first clause with a Key EQL to the value of
162  Keyform. If none of the keys matches then an error is signalled."
163  (case-body 'ecase keyform cases t 'eql t nil t))
164
165(defmacro typecase (keyform &body cases)
166  "TYPECASE Keyform {(Type Form*)}*
167  Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
168  is true."
169  (case-body 'typecase keyform cases nil 'typep nil nil nil))
170
171(defmacro ctypecase (keyform &body cases)
172  "CTYPECASE Keyform {(Type Form*)}*
173  Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
174  is true. If no form is satisfied then a correctable error is signalled."
175  (case-body 'ctypecase keyform cases nil 'typep t t t))
176
177(defmacro etypecase (keyform &body cases)
178  "ETYPECASE Keyform {(Type Form*)}*
179  Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
180  is true. If no form is satisfied then an error is signalled."
181  (case-body 'etypecase keyform cases nil 'typep t nil t))
Note: See TracBrowser for help on using the repository browser.