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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
package ij.macro;
import ij.IJ;
import java.util.ArrayList;
public class ExtensionDescriptor {
public String name;
public int[] argTypes;
public MacroExtension handler;
public ExtensionDescriptor(String theName, int[] theArgTypes, MacroExtension theHandler) {
this.name = theName;
this.argTypes = theArgTypes;
this.handler = theHandler;
}
public static ExtensionDescriptor newDescriptor(String theName, MacroExtension theHandler, int[] types) {
int[] argTypes = new int[types.length];
for (int i=0; i < types.length; ++i) {
argTypes[i] = types[i];
}
return new ExtensionDescriptor(theName, argTypes, theHandler);
}
public static ExtensionDescriptor newDescriptor(String theName, MacroExtension theHandler) {
return newDescriptor(theName, theHandler, new int[0]);
}
public static ExtensionDescriptor newDescriptor(String theName, MacroExtension theHandler, int type) {
return newDescriptor(theName, theHandler, new int[] {type});
}
public static ExtensionDescriptor newDescriptor(String theName, MacroExtension theHandler, int t1, int t2) {
return newDescriptor(theName, theHandler, new int[] {t1, t2});
}
public static ExtensionDescriptor newDescriptor(String theName, MacroExtension theHandler, int t1, int t2, int t3) {
return newDescriptor(theName, theHandler, new int[] {t1, t2, t3});
}
public static ExtensionDescriptor newDescriptor(String theName, MacroExtension theHandler, int t1, int t2, int t3, int t4) {
return newDescriptor(theName, theHandler, new int[] {t1, t2, t3, t4});
}
public static ExtensionDescriptor newDescriptor(String theName, MacroExtension theHandler, Integer[] types) {
int[] argTypes = new int[types.length];
for (int i=0; i < types.length; ++i) {
argTypes[i] = types[i].intValue();
}
return new ExtensionDescriptor(theName, argTypes, theHandler);
}
public static boolean isOptionalArg(int argType) {
return (argType & MacroExtension.ARG_OPTIONAL) == MacroExtension.ARG_OPTIONAL;
}
public static boolean isOutputArg(int argType) {
return (argType & MacroExtension.ARG_OUTPUT) == MacroExtension.ARG_OUTPUT;
}
public static int getRawType(int argType) {
return argType & ~(MacroExtension.ARG_OUTPUT|MacroExtension.ARG_OPTIONAL);
}
public boolean checkArguments(Object[] args) {
for (int i=0; i < argTypes.length; ++i) {
boolean optional = isOptionalArg(argTypes[i]);
boolean output = isOutputArg(argTypes[i]);
int rawType = getRawType(argTypes[i]);
if (args.length < i)
return optional ? true : false;
switch(rawType) {
case MacroExtension.ARG_STRING:
if (output) {
if (! (args[i] instanceof String[])) return false;
} else {
if (! (args[i] instanceof String)) return false;
}
case MacroExtension.ARG_NUMBER:
if (output) {
if (! (args[i] instanceof Double[])) return false;
} else {
if (!(args[i] instanceof Double)) return false;
}
case MacroExtension.ARG_ARRAY:
if (!(args[i] instanceof Object[])) return false;
}
}
return true;
}
public static String getTypeName(int argType) {
switch(getRawType(argType)) {
case MacroExtension.ARG_STRING:
return "string";
case MacroExtension.ARG_NUMBER:
return "number";
case MacroExtension.ARG_ARRAY:
return "array";
default:
return "unknown";
}
}
private static String getVariableTypename(int type) {
switch (type) {
case Variable.STRING:
return "string";
case Variable.NUMBER:
return "number";
case Variable.ARRAY:
return "array";
default:
return "unknown";
}
}
// TODO: this doesn't account for "loops" in the arrays, which will result in an infinite loop
private static Object[] convertArray(Variable[] array) {
Object[] oArray = new Object[ array.length ];
for(int i=0; i < array.length; ++i) {
switch(array[i].getType()) {
case Variable.STRING:
oArray[i] = array[i].getString();
break;
case Variable.VALUE:
oArray[i] = new Double( array[i].getValue() );
break;
case Variable.ARRAY:
oArray[i] = convertArray(array[i].getArray());
break;
default:
oArray[i] = null;
}
}
return oArray;
}
Variable[] parseArgumentList(Functions func) {
Interpreter interp = func.interp;
Variable[] vArray = new Variable[argTypes.length];
int i=0;
do {
if (i >= argTypes.length) {
interp.error("too many arguments (expected "+argTypes.length+")");
return null;
}
boolean output = isOutputArg(argTypes[i]);
Variable v;
if (output) {
v = func.getVariable();
} else {
v = new Variable();
switch (getRawType(argTypes[i])) {
case MacroExtension.ARG_STRING:
v.setString(func.getString());
break;
case MacroExtension.ARG_NUMBER:
v.setValue(interp.getExpression());
break;
case MacroExtension.ARG_ARRAY:
v.setArray(func.getArray());
break;
}
}
vArray[i] = v;
++i;
interp.getToken();
} while (interp.token == ',');
if (interp.token!=')')
interp.error("')' expected");
if (i < argTypes.length && !isOptionalArg(argTypes[i])) {
interp.error("too few arguments, expected "+argTypes.length+" but found "+i);
}
return vArray;
}
public static Object convertVariable(Interpreter interp, int rawType, Variable var) {
int type = getRawType(rawType);
boolean output = isOutputArg(rawType);
if (var == null)
return null;
switch (type) {
case MacroExtension.ARG_STRING:
if (!output && var.getType()!=Variable.STRING) {
interp.error("Expected string, but variable type is "+getVariableTypename(var.getType()));
return null;
}
if (output) {
String s = var.getString();
if (s==null) s = "";
return new String[] { s };
} else {
return var.getString();
}
case MacroExtension.ARG_NUMBER:
if (var.getType() != Variable.VALUE) {
interp.error("Expected number, but variable type is "+getVariableTypename(var.getType()));
return null;
}
if (output) {
return new Double[] { new Double(var.getValue()) };
} else {
return new Double( var.getValue() );
}
case MacroExtension.ARG_ARRAY:
if (var.getType() != Variable.ARRAY) {
interp.error("Expected array, but variable type is "+getVariableTypename(var.getType()));
return null;
}
return convertArray(var.getArray());
default:
interp.error("Unknown descriptor type "+type+" ("+rawType+")");
return null;
}
}
public static void convertOutputType(Variable variable, Object object) {
if (object instanceof String[]) {
String[] str = (String[]) object;
variable.setString(str[0]);
} else if (object instanceof Double[]) {
Double[] dbl = (Double[]) object;
variable.setValue(dbl[0].doubleValue());
} else if (object instanceof Object[]) {
Object[] arr = (Object[]) object;
Variable[] vArr = new Variable[arr.length];
for (int i=0; i < arr.length; ++i) {
vArr[i] = new Variable();
convertOutputType(vArr[i], arr[i]);
}
variable.setArray(vArr);
}
}
public String dispatch(Functions func) {
Interpreter interp = func.interp;
if (argTypes.length==0) {
interp.getParens();
return handler.handleExtension(name, null);
}
interp.getLeftParen();
Variable[] vArgs = null;
int next = interp.nextToken();
if (next != ')') {
vArgs = parseArgumentList(func);
}
//for (int i=0; i < vArgs.length; ++i) {
// Variable v = vArgs[i];
// System.err.println("variable is "+(v!= null?v.toString():"(null)"));
//}
Object[] args = new Object[ argTypes.length ];
// check variable types...
for (int i=0; i < argTypes.length; ++i) {
if (i >= vArgs.length) {
if (!ExtensionDescriptor.isOptionalArg(argTypes[i])) {
interp.error("expected argument "+(i+1)+" of type "+ExtensionDescriptor.getTypeName(argTypes[i]));
return null;
} else {
break;
}
}
args[i] = ExtensionDescriptor.convertVariable(interp, argTypes[i], vArgs[i]);
}
String retVal = handler.handleExtension(name, args);
for (int i=0; i < argTypes.length; ++i) {
if (i >= vArgs.length) break;
if (ExtensionDescriptor.isOutputArg(argTypes[i])) {
ExtensionDescriptor.convertOutputType(vArgs[i], args[i]);
}
}
return retVal;
}
}
|