KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > TypeError


1 /*
2  * TypeError.java
3  *
4  * Copyright (C) 2002-2004 Peter Graves
5  * $Id: TypeError.java,v 1.20 2004/05/27 20:31:49 piso Exp $
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
22 package org.armedbear.lisp;
23
24 public class TypeError extends LispError
25 {
26     protected LispObject datum;
27     protected LispObject expectedType;
28     private String JavaDoc typeString;
29
30     public TypeError()
31     {
32         datum = NIL;
33         expectedType = NIL;
34     }
35
36     public TypeError(LispObject datum, LispObject expectedType)
37     {
38         this.datum = datum;
39         this.expectedType = expectedType;
40     }
41
42     public TypeError(LispObject initArgs) throws ConditionThrowable
43     {
44         super(initArgs);
45         LispObject datum = NIL;
46         LispObject expectedType = NIL;
47         LispObject first, second;
48         while (initArgs != NIL) {
49             first = initArgs.car();
50             initArgs = initArgs.cdr();
51             second = initArgs.car();
52             initArgs = initArgs.cdr();
53             if (first == Keyword.DATUM)
54                 datum = second;
55             else if (first == Keyword.EXPECTED_TYPE)
56                 expectedType = second;
57         }
58         this.datum = datum;
59         this.expectedType = expectedType;
60         this.typeString = String.valueOf(expectedType);
61     }
62
63     public TypeError(String JavaDoc message)
64     {
65         super(message);
66         datum = NIL;
67         expectedType = NIL;
68     }
69
70     public TypeError(LispObject datum, String JavaDoc typeString)
71     {
72         this.datum = datum;
73         expectedType = NIL;
74         this.typeString = typeString;
75     }
76
77     public LispObject typeOf()
78     {
79         return Symbol.TYPE_ERROR;
80     }
81
82     public LispClass classOf()
83     {
84         return BuiltInClass.TYPE_ERROR;
85     }
86
87     public LispObject typep(LispObject type) throws ConditionThrowable
88     {
89         if (type == Symbol.TYPE_ERROR)
90             return T;
91         if (type == BuiltInClass.TYPE_ERROR)
92             return T;
93         return super.typep(type);
94     }
95
96     public String JavaDoc getMessage()
97     {
98         // FIXME
99
try {
100             final LispThread thread = LispThread.currentThread();
101             final Environment oldDynEnv = thread.getDynamicEnvironment();
102             thread.bindSpecial(_PRINT_ESCAPE_, T);
103             try {
104                 String JavaDoc s = super.getMessage();
105                 if (s != null)
106                     return s;
107                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
108                 String JavaDoc name = datum != null ? datum.writeToString() : null;
109                 String JavaDoc type = null;
110                 if (typeString != null)
111                     type = typeString;
112                 else if (expectedType != null)
113                     type = expectedType.writeToString();
114                 if (type != null) {
115                     if (name != null) {
116                         sb.append("The value ");
117                         sb.append(name);
118                     } else
119                         sb.append("Value");
120                     sb.append(" is not of type ");
121                     sb.append(type);
122                 } else if (name != null) {
123                     sb.append("Wrong type: ");
124                     sb.append(name);
125                 }
126                 sb.append('.');
127                 return sb.toString();
128             }
129             catch (Throwable JavaDoc t) {
130                 // FIXME
131
Debug.trace(t);
132                 return toString();
133             }
134             finally {
135                 thread.setDynamicEnvironment(oldDynEnv);
136             }
137         }
138         catch (Throwable JavaDoc t) {
139             return toString();
140         }
141     }
142
143     // ### type-error-datum
144
private static final Primitive1 TYPE_ERROR_DATUM =
145         new Primitive1("type-error-datum", "condition")
146     {
147         public LispObject execute(LispObject arg) throws ConditionThrowable
148         {
149             if (arg instanceof TypeError)
150                 return ((TypeError)arg).datum;
151             return signal(new TypeError(arg, Symbol.TYPE_ERROR));
152         }
153     };
154
155     // ### type-error-expected-type
156
private static final Primitive1 TYPE_ERROR_EXPECTED_TYPE =
157         new Primitive1("type-error-expected-type", "condition")
158     {
159         public LispObject execute(LispObject arg) throws ConditionThrowable
160         {
161             if (arg instanceof TypeError)
162                 return ((TypeError)arg).expectedType;
163             return signal(new TypeError(arg, Symbol.TYPE_ERROR));
164         }
165     };
166 }
167
Popular Tags