1 3 5 22 23 package edu.neu.ccs.jmk; 24 25 import java.util.Hashtable ; 26 27 32 final class GlobalTable 33 { 34 final Hashtable ht = new Hashtable (); 35 36 39 Global get(String identifier) { 40 Global var = (Global)ht.get(identifier); 41 if (var == null) { 42 var = new Global(identifier); 43 ht.put(identifier, var); 44 } 45 return var; 46 } 47 48 51 GlobalTable() { 52 put(new PatSubst()); 53 put(new Subst()); 54 put(new Cat()); 55 put(new Glob()); 56 put(new Dirs()); 57 put(new GetProp()); 58 put(new Join()); 59 put(new Equal()); 60 put(new First()); 61 put(new Rest()); 62 put(new Load()); 63 } 64 65 private void put(Function fun) { 66 String name = fun.getName(); 67 Global var = get(name); 68 var.setValue(fun); 69 } 70 71 private static boolean isOneString(Value v) { 72 if (StringList.isStringList(v)) { 73 StringList sl = (StringList)v; 74 return sl != null && sl.getRest() == null; 75 } 76 else 77 return false; 78 } 79 80 83 84 private static class PatSubst 85 implements Function 86 { 87 private final static int nargs = 3; 88 89 public String getName() { 90 return "patsubst"; 91 } 92 93 public Value invoke(Value[] args, StringList list) 94 throws Exception 95 { 96 if (args.length == nargs) { 97 if (isOneString(args[0])) { 98 StringList sl0 = (StringList)args[0]; 99 if (isOneString(args[1])) { 100 StringList sl1 = (StringList)args[1]; 101 if (StringList.isStringList(args[2])) { 102 StringList sl2 = (StringList)args[2]; 103 return StringUtils.patsubst(sl0.getString(), 104 sl1.getString(), 105 sl2, list); 106 } 107 else { 108 String msg = getName() 109 + ": the third argument is not a string list"; 110 throw new StringListCastException(msg); 111 } 112 } 113 else { 114 String msg = getName() 115 + ": the second argument is not a string"; 116 throw new StringListCastException(msg); 117 } 118 } 119 else { 120 String msg = getName() 121 + ": the first argument is not a string"; 122 throw new StringListCastException(msg); 123 } 124 } 125 else { 126 String msg = "Arg count error: " + getName() + " expecting " + nargs + 127 " but got " + args.length + " arguments"; 128 throw new WrongArgCountException(msg); 129 } 130 } 131 } 132 133 private static class Subst 134 implements Function 135 { 136 private final static int nargs = 3; 137 138 public String getName() { 139 return "subst"; 140 } 141 142 public Value invoke(Value[] args, StringList list) 143 throws Exception 144 { 145 if (args.length == nargs) { 146 if (isOneString(args[0])) { 147 StringList sl0 = (StringList)args[0]; 148 if (isOneString(args[1])) { 149 StringList sl1 = (StringList)args[1]; 150 if (StringList.isStringList(args[2])) { 151 StringList sl2 = (StringList)args[2]; 152 return StringUtils.subst(sl0.getString(), 153 sl1.getString(), 154 sl2, list); 155 } 156 else { 157 String msg = getName() 158 + ": the third argument is not a string list"; 159 throw new StringListCastException(msg); 160 } 161 } 162 else { 163 String msg = getName() 164 + ": the second argument is not a string"; 165 throw new StringListCastException(msg); 166 } 167 } 168 else { 169 String msg = getName() 170 + ": the first argument is not a string"; 171 throw new StringListCastException(msg); 172 } 173 } 174 else { 175 String msg = "Arg count error: " + getName() + " expecting " + nargs + 176 " but got " + args.length + " arguments"; 177 throw new WrongArgCountException(msg); 178 } 179 } 180 } 181 182 private static class Cat 183 implements Function 184 { 185 private final static int nargs = 1; 186 187 public String getName() { 188 return "cat"; 189 } 190 191 public Value invoke(Value[] args, StringList list) 192 throws Exception 193 { 194 if (args.length == nargs) { 195 if (StringList.isStringList(args[0])) { 196 return StringUtils.cat((StringList)args[0], list); 197 } 198 else { 199 String msg = getName() 200 + ": the argument is not a string list"; 201 throw new StringListCastException(msg); 202 } 203 } 204 else { 205 String msg = "Arg count error: " + getName() + " expecting " + nargs + 206 " but got " + args.length + " arguments"; 207 throw new WrongArgCountException(msg); 208 } 209 } 210 } 211 212 private static class Glob 213 implements Function 214 { 215 private final static int nargs = 1; 216 217 public String getName() { 218 return "glob"; 219 } 220 221 public Value invoke(Value[] args, StringList list) 222 throws Exception 223 { 224 if (args.length == nargs) { 225 if (StringList.isStringList(args[0])) { 226 return StringUtils.glob((StringList)args[0], list); 227 } 228 else { 229 String msg = getName() 230 + ": the argument is not a string list"; 231 throw new StringListCastException(msg); 232 } 233 } 234 else { 235 String msg = "Arg count error: " + getName() + " expecting " + nargs + 236 " but got " + args.length + " arguments"; 237 throw new WrongArgCountException(msg); 238 } 239 } 240 } 241 242 private static class Dirs 243 implements Function 244 { 245 private final static int nargs = 1; 246 247 public String getName() { 248 return "dirs"; 249 } 250 251 public Value invoke(Value[] args, StringList list) 252 throws Exception 253 { 254 if (args.length == nargs) { 255 if (StringList.isStringList(args[0])) { 256 return StringUtils.dirs((StringList)args[0], list); 257 } 258 else { 259 String msg = getName() 260 + ": the argument is not a string list"; 261 throw new StringListCastException(msg); 262 } 263 } 264 else { 265 String msg = "Arg count error: " + getName() + " expecting " + nargs + 266 " but got " + args.length + " arguments"; 267 throw new WrongArgCountException(msg); 268 } 269 } 270 } 271 272 private static class GetProp 273 implements Function 274 { 275 private final static int nargs = 1; 276 277 public String getName() { 278 return "getprop"; 279 } 280 281 public Value invoke(Value[] args, StringList list) 282 throws Exception 283 { 284 if (args.length == nargs) { 285 if (StringList.isStringList(args[0])) { 286 return StringUtils.getprop((StringList)args[0], list); 287 } 288 else { 289 String msg = getName() 290 + ": the argument is not a string list"; 291 throw new StringListCastException(msg); 292 } 293 } 294 else { 295 String msg = "Arg count error: " + getName() + " expecting " + nargs + 296 " but got " + args.length + " arguments"; 297 throw new WrongArgCountException(msg); 298 } 299 } 300 } 301 302 private static class Join 303 implements Function 304 { 305 private final static int nargs = 2; 306 307 public String getName() { 308 return "join"; 309 } 310 311 public Value invoke(Value[] args, StringList list) 312 throws Exception 313 { 314 if (args.length == nargs) { 315 if (StringList.isStringList(args[0])) { 316 if (StringList.isStringList(args[1])) { 317 return StringUtils.join((StringList)args[0], 318 (StringList)args[1], 319 list); 320 } 321 else { 322 String msg = getName() 323 + ": the second argument is not a string list"; 324 throw new StringListCastException(msg); 325 } 326 } 327 else { 328 String msg = getName() 329 + ": the first argument is not a string list"; 330 throw new StringListCastException(msg); 331 } 332 } 333 else { 334 String msg = "Arg count error: " + getName() + " expecting " + nargs + 335 " but got " + args.length + " arguments"; 336 throw new WrongArgCountException(msg); 337 } 338 } 339 } 340 341 private static class Equal 342 implements Function 343 { 344 private final static int nargs = 2; 345 346 public String getName() { 347 return "equal"; 348 } 349 350 public Value invoke(Value[] args, StringList list) 351 throws Exception 352 { 353 if (args.length == nargs) { 354 if (StringList.isStringList(args[0])) { 355 if (StringList.isStringList(args[1])) { 356 return StringUtils.equal((StringList)args[0], 357 (StringList)args[1], 358 list); 359 } 360 else { 361 String msg = getName() 362 + ": the second argument is not a string list"; 363 throw new StringListCastException(msg); 364 } 365 } 366 else { 367 String msg = getName() 368 + ": the first argument is not a string list"; 369 throw new StringListCastException(msg); 370 } 371 } 372 else { 373 String msg = "Arg count error: " + getName() + " expecting " + nargs + 374 " but got " + args.length + " arguments"; 375 throw new WrongArgCountException(msg); 376 } 377 } 378 } 379 380 private static class First 381 implements Function 382 { 383 private final static int nargs = 1; 384 385 public String getName() { 386 return "first"; 387 } 388 389 public Value invoke(Value[] args, StringList list) 390 throws Exception 391 { 392 if (args.length == nargs) { 393 if (StringList.isStringList(args[0])) { 394 StringList sl = (StringList)args[0]; 395 if (sl == null) 396 return list; 397 else 398 return new StringList(sl.getString(), list); 399 } 400 else { 401 String msg = getName() 402 + ": the argument is not a string list"; 403 throw new StringListCastException(msg); 404 } 405 } 406 else { 407 String msg = "Arg count error: " + getName() + " expecting " + nargs + 408 " but got " + args.length + " arguments"; 409 throw new WrongArgCountException(msg); 410 } 411 } 412 } 413 414 private static class Rest 415 implements Function 416 { 417 private final static int nargs = 1; 418 419 public String getName() { 420 return "rest"; 421 } 422 423 public Value invoke(Value[] args, StringList list) 424 throws Exception 425 { 426 if (args.length == nargs) { 427 if (StringList.isStringList(args[0])) { 428 StringList sl = (StringList)args[0]; 429 if (sl == null) 430 return list; 431 else 432 return StringList.append(sl.getRest(), list); 433 } 434 else { 435 String msg = getName() 436 + ": the argument is not a string list"; 437 throw new StringListCastException(msg); 438 } 439 } 440 else { 441 String msg = "Arg count error: " + getName() + " expecting " + nargs + 442 " but got " + args.length + " arguments"; 443 throw new WrongArgCountException(msg); 444 } 445 } 446 } 447 448 private static class Load 449 implements Function 450 { 451 private final static int nargs = 1; 452 453 public String getName() { 454 return "load"; 455 } 456 457 public Value invoke(Value[] args, StringList list) 458 throws Exception 459 { 460 if (args.length == nargs) { 461 if (list == null) { 462 if (isOneString(args[0])) { 463 String clsName = ((StringList)args[0]).getString(); 464 Class cls = Class.forName(clsName); 465 return (Function)cls.newInstance(); 466 } 467 else { 468 String msg = getName() 469 + ": the argument is not a string"; 470 throw new StringListCastException(msg); 471 } 472 } 473 else { 474 String msg = getName() 475 + ": loaded value is not a string"; 476 throw new StringListCastException(msg); 477 } 478 } 479 else { 480 String msg = "Arg count error: " + getName() + " expecting " + nargs + 481 " but got " + args.length + " arguments"; 482 throw new WrongArgCountException(msg); 483 } 484 } 485 } 486 } 487 | Popular Tags |