1 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 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 message) 64 { 65 super(message); 66 datum = NIL; 67 expectedType = NIL; 68 } 69 70 public TypeError(LispObject datum, String 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 getMessage() 97 { 98 try { 100 final LispThread thread = LispThread.currentThread(); 101 final Environment oldDynEnv = thread.getDynamicEnvironment(); 102 thread.bindSpecial(_PRINT_ESCAPE_, T); 103 try { 104 String s = super.getMessage(); 105 if (s != null) 106 return s; 107 StringBuffer sb = new StringBuffer (); 108 String name = datum != null ? datum.writeToString() : null; 109 String 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 t) { 130 Debug.trace(t); 132 return toString(); 133 } 134 finally { 135 thread.setDynamicEnvironment(oldDynEnv); 136 } 137 } 138 catch (Throwable t) { 139 return toString(); 140 } 141 } 142 143 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 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 |