| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
package edu.rice.cs.drjava.model.repl.newjvm; |
| 38 | |
|
| 39 | |
import java.io.StringWriter; |
| 40 | |
import java.io.PrintWriter; |
| 41 | |
import java.io.Serializable; |
| 42 | |
import edu.rice.cs.dynamicjava.interpreter.InterpreterException; |
| 43 | |
import edu.rice.cs.dynamicjava.interpreter.EvaluatorException; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | 0 | public abstract class InterpretResult implements Serializable { |
| 51 | |
public abstract <T> T apply(Visitor<T> v); |
| 52 | |
|
| 53 | |
public static interface Visitor<T> { |
| 54 | |
public T forNoValue(); |
| 55 | |
public T forStringValue(String val); |
| 56 | |
public T forCharValue(Character val); |
| 57 | |
public T forNumberValue(Number val); |
| 58 | |
public T forBooleanValue(Boolean val); |
| 59 | |
public T forObjectValue(String valString, String objTypeStr); |
| 60 | |
public T forException(String message); |
| 61 | |
public T forEvalException(String message, StackTraceElement[] stackTrace); |
| 62 | |
public T forUnexpectedException(Throwable t); |
| 63 | |
public T forBusy(); |
| 64 | |
} |
| 65 | |
|
| 66 | 0 | public static InterpretResult busy() { return BusyResult.INSTANCE; } |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | 0 | private static class BusyResult extends InterpretResult { |
| 71 | 0 | public static final BusyResult INSTANCE = new BusyResult(); |
| 72 | 0 | public <T> T apply(Visitor<T> v) { return v.forBusy(); } |
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | 0 | public static InterpretResult exception(InterpreterException e) { return new ExceptionResult(e); } |
| 77 | |
|
| 78 | |
private static class ExceptionResult extends InterpretResult { |
| 79 | |
private final String _msg; |
| 80 | |
private final StackTraceElement[] _stackTrace; |
| 81 | 0 | public ExceptionResult(InterpreterException e) { |
| 82 | 0 | if (e instanceof EvaluatorException) { |
| 83 | |
|
| 84 | 0 | _msg = e.getMessage(); |
| 85 | 0 | _stackTrace = e.getCause().getStackTrace(); |
| 86 | |
} |
| 87 | |
else { |
| 88 | |
|
| 89 | 0 | StringWriter msg = new StringWriter(); |
| 90 | 0 | e.printUserMessage(new PrintWriter(msg)); |
| 91 | 0 | _msg = msg.toString().trim(); |
| 92 | 0 | _stackTrace = null; |
| 93 | |
} |
| 94 | 0 | } |
| 95 | |
public <T> T apply(Visitor<T> v) { |
| 96 | 0 | if (_stackTrace != null) |
| 97 | 0 | return v.forEvalException(_msg, _stackTrace); |
| 98 | |
else |
| 99 | 0 | return v.forException(_msg); |
| 100 | |
} |
| 101 | |
} |
| 102 | |
|
| 103 | |
|
| 104 | |
public static InterpretResult unexpectedException(Throwable t) { |
| 105 | 0 | return new UnexpectedExceptionResult(t); |
| 106 | |
} |
| 107 | |
|
| 108 | |
private static class UnexpectedExceptionResult extends InterpretResult { |
| 109 | |
private final Throwable _t; |
| 110 | 0 | public UnexpectedExceptionResult(Throwable t) { _t = t; } |
| 111 | 0 | public <T> T apply(Visitor<T> v) { return v.forUnexpectedException(_t); } |
| 112 | |
} |
| 113 | |
|
| 114 | |
|
| 115 | 0 | public static InterpretResult noValue() { return NoValueResult.INSTANCE; } |
| 116 | |
|
| 117 | 0 | private static class NoValueResult extends InterpretResult { |
| 118 | 0 | public static final NoValueResult INSTANCE = new NoValueResult(); |
| 119 | 0 | public <T> T apply(Visitor<T> v) { return v.forNoValue(); } |
| 120 | |
} |
| 121 | |
|
| 122 | |
|
| 123 | 0 | public static InterpretResult stringValue(String s) { return new StringValueResult(s); } |
| 124 | |
|
| 125 | |
private static class StringValueResult extends InterpretResult { |
| 126 | |
private final String _val; |
| 127 | 0 | public StringValueResult(String val) { _val = val; } |
| 128 | 0 | public <T> T apply(Visitor<T> v) { return v.forStringValue(_val); } |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | 0 | public static InterpretResult charValue(Character c) { return new CharValueResult(c); } |
| 133 | |
|
| 134 | |
private static class CharValueResult extends InterpretResult { |
| 135 | |
private final Character _val; |
| 136 | 0 | public CharValueResult(Character val) { _val = val; } |
| 137 | 0 | public <T> T apply(Visitor<T> v) { return v.forCharValue(_val); } |
| 138 | |
} |
| 139 | |
|
| 140 | |
|
| 141 | 0 | public static InterpretResult numberValue(Number n) { return new NumberValueResult(n); } |
| 142 | |
|
| 143 | |
private static class NumberValueResult extends InterpretResult { |
| 144 | |
private final Number _val; |
| 145 | 0 | public NumberValueResult(Number val) { _val = val; } |
| 146 | 0 | public <T> T apply(Visitor<T> v) { return v.forNumberValue(_val); } |
| 147 | |
} |
| 148 | |
|
| 149 | |
|
| 150 | 0 | public static InterpretResult booleanValue(Boolean b) { return new BooleanValueResult(b); } |
| 151 | |
|
| 152 | |
private static class BooleanValueResult extends InterpretResult { |
| 153 | |
private final Boolean _val; |
| 154 | 0 | public BooleanValueResult(Boolean val) { _val = val; } |
| 155 | 0 | public <T> T apply(Visitor<T> v) { return v.forBooleanValue(_val); } |
| 156 | |
} |
| 157 | |
|
| 158 | |
|
| 159 | 0 | public static InterpretResult objectValue(String objS, String objTS) { return new ObjectValueResult(objS, objTS); } |
| 160 | |
|
| 161 | |
private static class ObjectValueResult extends InterpretResult { |
| 162 | |
private final String _objString; |
| 163 | |
private final String _objTypeStr; |
| 164 | 0 | public ObjectValueResult(String objString, String objTypeStr) { |
| 165 | 0 | _objString = objString; |
| 166 | 0 | _objTypeStr = objTypeStr; |
| 167 | 0 | } |
| 168 | 0 | public <T> T apply(Visitor<T> v) { return v.forObjectValue(_objString, _objTypeStr); } |
| 169 | |
} |
| 170 | |
|
| 171 | |
} |