| 1   | !636
 | 
| 2   | //!* constructors
 | 
| 3   | //!class JavaTok
 | 
| 4   | !L<S>
 | 
| 5   | !quicknew
 | 
| 6   | !standard functions
 | 
| 7   | //!multi-line strings
 | 
| 8   | !699 // improved multi-line strings
 | 
| 9   | !700 // "no exceptions" short hand
 | 
| 10   | 
 | 
| 11   | !include #1000353 // class JavaTok
 | 
| 12   | 
 | 
| 13   | main {
 | 
| 14   |   static Object in;
 | 
| 15   |   static new (Hash)Map<String, Function> functions;
 | 
| 16   |   
 | 
| 17   |   interface Function {
 | 
| 18   |     public Object process(Object[] args);
 | 
| 19   |   }
 | 
| 20   |   
 | 
| 21   |   static class Call {
 | 
| 22   |     String function;
 | 
| 23   |     String[] args;
 | 
| 24   |     
 | 
| 25   |     Object eval() {
 | 
| 26   |       return functions.get(function).process(args);
 | 
| 27   |     }
 | 
| 28   |     
 | 
| 29   |     public String toString() {
 | 
| 30   |       new StringBuilder buf;
 | 
| 31   |       for (int i = 0; i < args.length; i++) {
 | 
| 32   |         if (i != 0) buf.append(", ");
 | 
| 33   |         buf.append(quote(args[i]));
 | 
| 34   |       }
 | 
| 35   |       return function + "(" + buf + ")";
 | 
| 36   |     }
 | 
| 37   |   }
 | 
| 38   |   
 | 
| 39   |   psvm {
 | 
| 40   |     in = [[contains("<all iois>", "<latest ioi>")]];
 | 
| 41   |     parseAsJavaExpression();
 | 
| 42   |     
 | 
| 43   |     replaceStringConstant("<all iois>", "#681");
 | 
| 44   |     replaceStringConstant("<latest ioi>", "#1000384");
 | 
| 45   |     loadSnippetID1();
 | 
| 46   |     
 | 
| 47   |     // replace contains(x, y) with contains(toLines(x), y)
 | 
| 48   |     // use standard function "contains(List, Object)"
 | 
| 49   |     
 | 
| 50   |     functions.put("contains", new Function() {
 | 
| 51   |       public Object process(Object[] args) {
 | 
| 52   |         return toLinesTrim((String) args[0]).contains(args[1]);
 | 
| 53   |       }
 | 
| 54   |     });
 | 
| 55   |     
 | 
| 56   |     System.out.println("Result: " + ((Call) in).eval());
 | 
| 57   |   }
 | 
| 58   |   
 | 
| 59   |   static void parseAsJavaExpression() {
 | 
| 60   |     L<S> tok = JavaTok.split((String) in);
 | 
| 61   |     new Call c;
 | 
| 62   |     c.function = tok.get(1);
 | 
| 63   |     c.args = new String[] { unquote(tok.get(5)), unquote(tok.get(9)) };
 | 
| 64   |     in = c;
 | 
| 65   |   }
 | 
| 66   |   
 | 
| 67   |   static void replaceStringConstant(String before, String now) {
 | 
| 68   |     Call c = (Call) clone(in);
 | 
| 69   |     for (int i = 0; i < c.args.length; i++)
 | 
| 70   |       if (c.args[i].equals(before))
 | 
| 71   |         c.args[i] = now;
 | 
| 72   |     in = c;
 | 
| 73   |   }
 | 
| 74   |   
 | 
| 75   |   static void loadSnippetID1() tex {
 | 
| 76   |     Call c = (Call) clone(in);
 | 
| 77   |     for (int i = 0; i < c.args.length; i++)
 | 
| 78   |       if (c.args[i].startsWith("#") && isSnippetID(c.args[i])) {
 | 
| 79   |         c.args[i] = loadSnippet(c.args[i]);
 | 
| 80   |         break;
 | 
| 81   |       }
 | 
| 82   |     in = c;
 | 
| 83   |   }
 | 
| 84   |   
 | 
| 85   |   static Object clone(Object o) no exceptions {
 | 
| 86   |     if (o == null)
 | 
| 87   |       return o;
 | 
| 88   |     
 | 
| 89   |     if (o instanceof List) {
 | 
| 90   |       List l = new ArrayList();
 | 
| 91   |       for (Object x : (List) o)
 | 
| 92   |         l.add(clone(x));
 | 
| 93   |       return l;
 | 
| 94   |     }
 | 
| 95   |     
 | 
| 96   |     if (o instanceof String || o instanceof Number || o instanceof Boolean)
 | 
| 97   |       return o;
 | 
| 98   |       
 | 
| 99   |     if (o instanceof Object[]) {
 | 
| 100   |       Object[] l = (Object[]) o;
 | 
| 101   |       Object[] l2 = l.clone();
 | 
| 102   |       for (int i = 0; i < l.length; i++)
 | 
| 103   |         l2[i] = clone(l[i]);
 | 
| 104   |       return l2;
 | 
| 105   |     }
 | 
| 106   |     
 | 
| 107   |     // TODO: go to superclasses too
 | 
| 108   |     Field[] fields = o.getClass().getDeclaredFields();
 | 
| 109   |     Object clone = o.getClass().newInstance();
 | 
| 110   |     for (Field field : fields) {
 | 
| 111   |       if ((field.getModifiers() & Modifier.STATIC) != 0)
 | 
| 112   |         continue;
 | 
| 113   |       Object value;
 | 
| 114   |       value = field.get(o);
 | 
| 115   |       field.set(clone, clone(value));
 | 
| 116   |     }
 | 
| 117   |     return clone;
 | 
| 118   |   }
 | 
| 119   |   
 | 
| 120   | } |