001: /*
002: * TypeError.java
003: *
004: * Copyright (C) 2002-2004 Peter Graves
005: * $Id: TypeError.java,v 1.20 2004/05/27 20:31:49 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.lisp;
023:
024: public class TypeError extends LispError {
025: protected LispObject datum;
026: protected LispObject expectedType;
027: private String typeString;
028:
029: public TypeError() {
030: datum = NIL;
031: expectedType = NIL;
032: }
033:
034: public TypeError(LispObject datum, LispObject expectedType) {
035: this .datum = datum;
036: this .expectedType = expectedType;
037: }
038:
039: public TypeError(LispObject initArgs) throws ConditionThrowable {
040: super (initArgs);
041: LispObject datum = NIL;
042: LispObject expectedType = NIL;
043: LispObject first, second;
044: while (initArgs != NIL) {
045: first = initArgs.car();
046: initArgs = initArgs.cdr();
047: second = initArgs.car();
048: initArgs = initArgs.cdr();
049: if (first == Keyword.DATUM)
050: datum = second;
051: else if (first == Keyword.EXPECTED_TYPE)
052: expectedType = second;
053: }
054: this .datum = datum;
055: this .expectedType = expectedType;
056: this .typeString = String.valueOf(expectedType);
057: }
058:
059: public TypeError(String message) {
060: super (message);
061: datum = NIL;
062: expectedType = NIL;
063: }
064:
065: public TypeError(LispObject datum, String typeString) {
066: this .datum = datum;
067: expectedType = NIL;
068: this .typeString = typeString;
069: }
070:
071: public LispObject typeOf() {
072: return Symbol.TYPE_ERROR;
073: }
074:
075: public LispClass classOf() {
076: return BuiltInClass.TYPE_ERROR;
077: }
078:
079: public LispObject typep(LispObject type) throws ConditionThrowable {
080: if (type == Symbol.TYPE_ERROR)
081: return T;
082: if (type == BuiltInClass.TYPE_ERROR)
083: return T;
084: return super .typep(type);
085: }
086:
087: public String getMessage() {
088: // FIXME
089: try {
090: final LispThread thread = LispThread.currentThread();
091: final Environment oldDynEnv = thread
092: .getDynamicEnvironment();
093: thread.bindSpecial(_PRINT_ESCAPE_, T);
094: try {
095: String s = super .getMessage();
096: if (s != null)
097: return s;
098: StringBuffer sb = new StringBuffer();
099: String name = datum != null ? datum.writeToString()
100: : null;
101: String type = null;
102: if (typeString != null)
103: type = typeString;
104: else if (expectedType != null)
105: type = expectedType.writeToString();
106: if (type != null) {
107: if (name != null) {
108: sb.append("The value ");
109: sb.append(name);
110: } else
111: sb.append("Value");
112: sb.append(" is not of type ");
113: sb.append(type);
114: } else if (name != null) {
115: sb.append("Wrong type: ");
116: sb.append(name);
117: }
118: sb.append('.');
119: return sb.toString();
120: } catch (Throwable t) {
121: // FIXME
122: Debug.trace(t);
123: return toString();
124: } finally {
125: thread.setDynamicEnvironment(oldDynEnv);
126: }
127: } catch (Throwable t) {
128: return toString();
129: }
130: }
131:
132: // ### type-error-datum
133: private static final Primitive1 TYPE_ERROR_DATUM = new Primitive1(
134: "type-error-datum", "condition") {
135: public LispObject execute(LispObject arg)
136: throws ConditionThrowable {
137: if (arg instanceof TypeError)
138: return ((TypeError) arg).datum;
139: return signal(new TypeError(arg, Symbol.TYPE_ERROR));
140: }
141: };
142:
143: // ### type-error-expected-type
144: private static final Primitive1 TYPE_ERROR_EXPECTED_TYPE = new Primitive1(
145: "type-error-expected-type", "condition") {
146: public LispObject execute(LispObject arg)
147: throws ConditionThrowable {
148: if (arg instanceof TypeError)
149: return ((TypeError) arg).expectedType;
150: return signal(new TypeError(arg, Symbol.TYPE_ERROR));
151: }
152: };
153: }
|