1 | /* |
---|
2 | * TypeError.java |
---|
3 | * |
---|
4 | * Copyright (C) 2002-2005 Peter Graves |
---|
5 | * $Id: TypeError.java 15519 2021-09-19 08:54:57Z mevenson $ |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | * |
---|
21 | * As a special exception, the copyright holders of this library give you |
---|
22 | * permission to link this library with independent modules to produce an |
---|
23 | * executable, regardless of the license terms of these independent |
---|
24 | * modules, and to copy and distribute the resulting executable under |
---|
25 | * terms of your choice, provided that you also meet, for each linked |
---|
26 | * independent module, the terms and conditions of the license of that |
---|
27 | * module. An independent module is a module which is not derived from |
---|
28 | * or based on this library. If you modify this library, you may extend |
---|
29 | * this exception to your version of the library, but you are not |
---|
30 | * obligated to do so. If you do not wish to do so, delete this |
---|
31 | * exception statement from your version. |
---|
32 | */ |
---|
33 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | import static org.armedbear.lisp.Lisp.*; |
---|
37 | |
---|
38 | public class TypeError extends LispError |
---|
39 | { |
---|
40 | public TypeError() |
---|
41 | { |
---|
42 | super(StandardClass.TYPE_ERROR); |
---|
43 | } |
---|
44 | |
---|
45 | protected TypeError(LispClass cls) |
---|
46 | { |
---|
47 | super(cls); |
---|
48 | } |
---|
49 | |
---|
50 | public TypeError(LispObject datum, LispObject expectedType) |
---|
51 | |
---|
52 | { |
---|
53 | super(StandardClass.TYPE_ERROR); |
---|
54 | setDatum(datum); |
---|
55 | setExpectedType(expectedType); |
---|
56 | } |
---|
57 | |
---|
58 | public TypeError(LispObject initArgs) |
---|
59 | { |
---|
60 | super(StandardClass.TYPE_ERROR); |
---|
61 | initialize(initArgs); |
---|
62 | } |
---|
63 | |
---|
64 | @Override |
---|
65 | protected void initialize(LispObject initArgs) |
---|
66 | { |
---|
67 | super.initialize(initArgs); |
---|
68 | LispObject datum = null; |
---|
69 | LispObject expectedType = null; |
---|
70 | LispObject first, second; |
---|
71 | while (initArgs != NIL) { |
---|
72 | first = initArgs.car(); |
---|
73 | initArgs = initArgs.cdr(); |
---|
74 | second = initArgs.car(); |
---|
75 | initArgs = initArgs.cdr(); |
---|
76 | if (first == Keyword.DATUM) { |
---|
77 | if (datum == null) |
---|
78 | datum = second; |
---|
79 | } else if (first == Keyword.EXPECTED_TYPE) { |
---|
80 | if (expectedType == null) |
---|
81 | expectedType = second; |
---|
82 | } |
---|
83 | } |
---|
84 | if (datum != null) |
---|
85 | setDatum(datum); |
---|
86 | if (expectedType != null) |
---|
87 | setExpectedType(expectedType); |
---|
88 | } |
---|
89 | |
---|
90 | public TypeError(String message) |
---|
91 | { |
---|
92 | super(StandardClass.TYPE_ERROR); |
---|
93 | setFormatControl(message); |
---|
94 | setDatum(NIL); |
---|
95 | setExpectedType(NIL); |
---|
96 | } |
---|
97 | |
---|
98 | public TypeError(String message, LispObject datum, LispObject expectedType) |
---|
99 | |
---|
100 | { |
---|
101 | super(StandardClass.TYPE_ERROR); |
---|
102 | setFormatControl(message); |
---|
103 | setDatum(datum); |
---|
104 | setExpectedType(expectedType); |
---|
105 | } |
---|
106 | |
---|
107 | @Override |
---|
108 | public LispObject typeOf() |
---|
109 | { |
---|
110 | return Symbol.TYPE_ERROR; |
---|
111 | } |
---|
112 | |
---|
113 | @Override |
---|
114 | public LispObject classOf() |
---|
115 | { |
---|
116 | return StandardClass.TYPE_ERROR; |
---|
117 | } |
---|
118 | |
---|
119 | @Override |
---|
120 | public LispObject typep(LispObject type) |
---|
121 | { |
---|
122 | if (type == Symbol.TYPE_ERROR) |
---|
123 | return T; |
---|
124 | if (type == StandardClass.TYPE_ERROR) |
---|
125 | return T; |
---|
126 | return super.typep(type); |
---|
127 | } |
---|
128 | |
---|
129 | @Override |
---|
130 | public String getMessage() |
---|
131 | { |
---|
132 | final LispThread thread = LispThread.currentThread(); |
---|
133 | final SpecialBindingsMark mark = thread.markSpecialBindings(); |
---|
134 | thread.bindSpecial(Symbol.PRINT_ESCAPE, T); |
---|
135 | try { |
---|
136 | String s = super.getMessage(); |
---|
137 | if (s != null) |
---|
138 | return s; |
---|
139 | final LispObject datum = getDatum(); |
---|
140 | final LispObject expectedType = getExpectedType(); |
---|
141 | StringBuilder sb = new StringBuilder(); |
---|
142 | String name = datum != null ? datum.princToString() : null; |
---|
143 | String type = null; |
---|
144 | if (expectedType != null) |
---|
145 | type = expectedType.princToString(); |
---|
146 | if (type != null) { |
---|
147 | if (name != null) { |
---|
148 | sb.append("The value "); |
---|
149 | sb.append(name); |
---|
150 | } else |
---|
151 | sb.append("Value"); |
---|
152 | sb.append(" is not of type "); |
---|
153 | sb.append(type); |
---|
154 | } else if (name != null) { |
---|
155 | sb.append("Wrong type: "); |
---|
156 | sb.append(name); |
---|
157 | } |
---|
158 | sb.append('.'); |
---|
159 | return sb.toString(); |
---|
160 | } |
---|
161 | finally { |
---|
162 | thread.resetSpecialBindings(mark); |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | public final LispObject getDatum() |
---|
167 | { |
---|
168 | return getInstanceSlotValue(Symbol.DATUM); |
---|
169 | } |
---|
170 | |
---|
171 | private final void setDatum(LispObject datum) |
---|
172 | { |
---|
173 | setInstanceSlotValue(Symbol.DATUM, datum); |
---|
174 | } |
---|
175 | |
---|
176 | public final LispObject getExpectedType() |
---|
177 | { |
---|
178 | return getInstanceSlotValue(Symbol.EXPECTED_TYPE); |
---|
179 | } |
---|
180 | |
---|
181 | private final void setExpectedType(LispObject expectedType) |
---|
182 | |
---|
183 | { |
---|
184 | setInstanceSlotValue(Symbol.EXPECTED_TYPE, expectedType); |
---|
185 | } |
---|
186 | |
---|
187 | // ### type-error-datum |
---|
188 | private static final Primitive TYPE_ERROR_DATUM = |
---|
189 | new Primitive(Symbol.TYPE_ERROR_DATUM, "condition") |
---|
190 | { |
---|
191 | @Override |
---|
192 | public LispObject execute(LispObject arg) |
---|
193 | { |
---|
194 | if (arg.typep(Symbol.TYPE_ERROR) == NIL) { |
---|
195 | return type_error(arg, Symbol.TYPE_ERROR); |
---|
196 | } |
---|
197 | |
---|
198 | final StandardObject obj = (StandardObject) arg; |
---|
199 | return obj.getInstanceSlotValue(Symbol.DATUM); |
---|
200 | } |
---|
201 | }; |
---|
202 | |
---|
203 | // ### type-error-expected-type |
---|
204 | private static final Primitive TYPE_ERROR_EXPECTED_TYPE = |
---|
205 | new Primitive(Symbol.TYPE_ERROR_EXPECTED_TYPE, "condition") |
---|
206 | { |
---|
207 | @Override |
---|
208 | public LispObject execute(LispObject arg) |
---|
209 | { |
---|
210 | if (arg.typep(Symbol.TYPE_ERROR) == NIL) { |
---|
211 | return type_error(arg, Symbol.TYPE_ERROR); |
---|
212 | } |
---|
213 | |
---|
214 | final StandardObject obj = (StandardObject) arg; |
---|
215 | return obj.getInstanceSlotValue(Symbol.EXPECTED_TYPE); |
---|
216 | } |
---|
217 | }; |
---|
218 | } |
---|