1 | !636
|
2 | !quickmain
|
3 | !auto-import
|
4 | !standard functions
|
5 | !quicknew
|
6 | !1000346 // use "case" as a variable name
|
7 | !688 // buf.isEmpty
|
8 | !class JavaTok
|
9 | |
10 | import java.lang.reflect.*;
|
11 | |
12 | !include #2000470 // class _x16
|
13 | |
14 | interface Function {
|
15 | public Object process(Object in);
|
16 | public void toJava_process(Code code);
|
17 | }
|
18 | |
19 | interface ReversibleFunction extends Function {
|
20 | public Object unprocess(Object in);
|
21 | public void toJava_unprocess(Code code);
|
22 | }
|
23 | |
24 | // generic learner (works on objects)
|
25 | interface Learner {
|
26 | public void processInOut(Object in, Object out);
|
27 | public Object processIn(Object in);
|
28 | public void toJava(Code code);
|
29 | }
|
30 | |
31 | abstract class Base {
|
32 | void printVars() {
|
33 | new StringBuilder buf;
|
34 | Field[] fields = getClass().getDeclaredFields();
|
35 | for (Field field : fields) {
|
36 | if ((field.getModifiers() & Modifier.STATIC) != 0)
|
37 | continue;
|
38 | Object value;
|
39 | try {
|
40 | value = field.get(this);
|
41 | } catch (Exception e) {
|
42 | value = "?";
|
43 | }
|
44 |
|
45 | if (!buf.isEmpty()) buf.append(", ");
|
46 | buf.append(field.getName() + "=" + value);
|
47 | }
|
48 | System.out.println(buf.toString());
|
49 | }
|
50 | }
|
51 | |
52 | abstract class LearnerImpl extends Base implements Learner {
|
53 | }
|
54 | |
55 | class Code {
|
56 | StringBuilder buf = new StringBuilder();
|
57 | String var = "in";
|
58 | String indent = "";
|
59 | List<String> translators = new ArrayList<String>();
|
60 |
|
61 | Code() {
|
62 | translators.add("!636");
|
63 | }
|
64 |
|
65 | void line(String line) {
|
66 | buf.append(indent).append(line).append('\n');
|
67 | }
|
68 |
|
69 | void indent() {
|
70 | indent += " ";
|
71 | }
|
72 |
|
73 | void unindent() {
|
74 | indent = indent.substring(0, indent.length()-2);
|
75 | }
|
76 |
|
77 | void translators(String... ids) {
|
78 | for (String id : ids)
|
79 | if (! translators.contains(id)) // space is needed otherwise translator 636 is fooled :)
|
80 | translators.add(id);
|
81 | }
|
82 |
|
83 | String getTranslators() {
|
84 | return main.fromLines(translators);
|
85 | }
|
86 | |
87 | String s() {
|
88 | return "((String) " + var + ")";
|
89 | }
|
90 | }
|
91 | |
92 | main {
|
93 | static List<Case> cases = new ArrayList<Case>();
|
94 | static boolean testJava = true;
|
95 |
|
96 | psvm {
|
97 | String snippetID = null;
|
98 |
|
99 | for (int i = 0; i < args.length; i++) {
|
100 | String arg = args[i];
|
101 | if (arg.equals("debug"))
|
102 | debugOn(args[++i]);
|
103 | if (arg.equals("-notestjava"))
|
104 | testJava = false;
|
105 | else if (arg.equals("-testjava"))
|
106 | testJava = true;
|
107 | else if (isSnippetID(arg))
|
108 | snippetID = arg;
|
109 | else
|
110 | System.err.println("Unknown argument: " + arg + ", ignoring");
|
111 | }
|
112 |
|
113 | parse(snippetID);
|
114 |
|
115 | int solved = 0, n = cases.size(), goodJava = 0;
|
116 | for (int i = 0; i < n; i++) {
|
117 | try {
|
118 | calculate(cases.get(i));
|
119 | } catch (Throwable e) {
|
120 | e.printStackTrace();
|
121 | }
|
122 | if (cases.get(i).winner != null)
|
123 | ++solved;
|
124 | if (cases.get(i).goodJava)
|
125 | ++goodJava;
|
126 | System.out.println((i+1) + " case(s) processed, " + solved + " solved.");
|
127 | if (testJava && goodJava < solved)
|
128 | System.out.println((solved-goodJava) + " case(s) with BAD JAVA.");
|
129 | }
|
130 | |
131 | print ""
|
132 | print "----"
|
133 | |
134 | boolean allSolved = solved == n;
|
135 | if (solved != 0) {
|
136 | System.out.println();
|
137 | System.out.print("Solved: ");
|
138 | for (Case case : cases)
|
139 | if (case.winner != null)
|
140 | System.out.print(case.id + " ");
|
141 | System.out.println();
|
142 | }
|
143 | if (testJava && solved > goodJava) {
|
144 | System.out.println();
|
145 | System.out.print("Bad Java: ");
|
146 | for (Case case : cases)
|
147 | if (case.winner != null && !case.goodJava)
|
148 | System.out.print(case.id + " ");
|
149 | System.out.println();
|
150 | }
|
151 | if (!allSolved) {
|
152 | System.out.println();
|
153 | System.out.print("Unsolved: ");
|
154 | for (Case case : cases)
|
155 | if (case.winner == null)
|
156 | System.out.print(case.id + " ");
|
157 | System.out.println();
|
158 | }
|
159 | System.out.println();
|
160 | if (allSolved && testJava && goodJava < solved)
|
161 | System.out.println("All solved (" + solved + "), but some BAD JAVA.");
|
162 | else {
|
163 | System.out.println(allSolved ? "ALL SOLVED (" + solved + ")" : "Solved " + solved + " out of " + n + ".");
|
164 | if (testJava)
|
165 | if (goodJava == solved)
|
166 | System.out.println("All Java code OK" + (allSolved ? "" : " (for solved cases)") + ".");
|
167 | else
|
168 | System.out.println("Some bad Java.");
|
169 | else
|
170 | System.out.println("Java not tested.");
|
171 | }
|
172 | print ""
|
173 | }
|
174 |
|
175 | static class Case {
|
176 | String id;
|
177 | List<String[]> fullExamples = new ArrayList<String[]>();
|
178 | List<String> halfExamples = new ArrayList<String>();
|
179 | List<String[]> examples1, examples2;
|
180 | Learner winner;
|
181 | boolean goodJava;
|
182 | }
|
183 | |
184 | static void parse(String arg) tex {
|
185 | Case case = new Case();
|
186 | String text;
|
187 | if (arg != null) {
|
188 | case.id = arg;
|
189 | text = loadSnippet(arg);
|
190 | } else {
|
191 | case.id = "input.txt";
|
192 | text = loadTextFile("input/input.txt", null);
|
193 | if (text == null) {
|
194 | //case.id = "#2000455"; // example input
|
195 | case.id = "#681"; // a collection of "all cases"!
|
196 | text = loadSnippet(case.id);
|
197 | }
|
198 | }
|
199 |
|
200 | // it's a collection of cases!
|
201 | if (text.trim().startsWith("#")) {
|
202 | for (String line : toLines(text))
|
203 | parse(line);
|
204 | return;
|
205 | }
|
206 |
|
207 | // it's a "Continue:" task - transform to I/O format
|
208 | if (text.trim().startsWith("Continue:")) {
|
209 | List<String> lines = toLines(text);
|
210 | new StringBuilder buf;
|
211 | for (int i = 1; i < lines.size(); i++) {
|
212 | buf.append("In: " + quote("" + i) + "\n");
|
213 | buf.append("Out: " + quote(lines.get(i)) + "\n");
|
214 | }
|
215 | int numAsking = 3;
|
216 | for (int i = lines.size(); i < lines.size()+numAsking; i++)
|
217 | buf.append("In: " + quote("" + i) + "\n");
|
218 | text = buf.toString();
|
219 | }
|
220 |
|
221 | System.out.println(text);
|
222 | String in = null, out = null;
|
223 |
|
224 | for (String line : toLines(text)) {
|
225 | if (line.toUpperCase().startsWith("I")) { // "In: " or "I: "
|
226 | if (in != null)
|
227 | case.halfExamples.add(in);
|
228 | in = unquote(line.substring(line.indexOf(':')+1).trim());
|
229 | out = null;
|
230 | } else if (line.toUpperCase().startsWith("O")) { // "Out: " or "O: "
|
231 | out = unquote(line.substring(line.indexOf(':')+1).trim());
|
232 | System.out.println(quote(in) + " => " + quote(out));
|
233 | case.fullExamples.add(new String[] {in, out});
|
234 | in = out = null;
|
235 | }
|
236 | }
|
237 |
|
238 | if (in != null)
|
239 | case.halfExamples.add(in);
|
240 | cases.add(case);
|
241 | }
|
242 |
|
243 | static void calculate(Case case) tex {
|
244 | if (case.fullExamples.size() < 2)
|
245 | throw new RuntimeException("Too few examples (" + case.fullExamples.size() + ")");
|
246 | int splitPoint = case.fullExamples.size()-1;
|
247 | System.out.println("Full examples: " + case.fullExamples.size() + ", splitPoint: " + splitPoint);
|
248 | case.examples1 = case.fullExamples.subList(0, splitPoint);
|
249 | case.examples2 = case.fullExamples.subList(splitPoint, case.fullExamples.size());
|
250 |
|
251 | Learner learner = findOKLearner(case);
|
252 | if (learner == null)
|
253 | print "\nProblem not solved"
|
254 | else {
|
255 | print "\nSolved!"
|
256 | case.winner = learner;
|
257 | Code code = new Code();
|
258 | learner.toJava(code);
|
259 |
|
260 | if (testJava)
|
261 | testJava(case, code); // prints "GOOD JAVA" or "BAD JAVA"
|
262 | else
|
263 | print "Java:"
|
264 |
|
265 | System.out.println(indent(" ", code.getTranslators()));
|
266 | System.out.println(indent(" ", code.buf.toString()));
|
267 |
|
268 | for (String in : case.halfExamples) {
|
269 | Object out = learner.processIn(in);
|
270 | System.out.println(quote(in) + " =>! " + quote((String) out));
|
271 | }
|
272 | }
|
273 | }
|
274 |
|
275 | static Learner findOKLearner(Case case) {
|
276 | for (Learner learner : makeLearners()) try {
|
277 | if (learnerOK(learner, case))
|
278 | return learner;
|
279 | } catch (Throwable e) {
|
280 | e.printStackTrace();
|
281 | }
|
282 | return null;
|
283 | }
|
284 |
|
285 | static boolean learnerOK(Learner learner, Case case) {
|
286 | String[] _e = null;
|
287 | try {
|
288 | for (String[] e : case.examples1) {
|
289 | _e = e;
|
290 | learner.processInOut(e[0], e[1]);
|
291 | }
|
292 |
|
293 | // full validation against all examples
|
294 | for (String[] e : case.fullExamples) {
|
295 | _e = e;
|
296 | String out = (String) learner.processIn(e[0]);
|
297 | if (!e[1].equals(out)) {
|
298 | System.out.println("[fail] " + learner + " on " + quote(e[0]) + " - got: " + quote(out) + " rather than: " + quote(e[1]));
|
299 | return false;
|
300 | }
|
301 | }
|
302 | return true; // all test examples passed
|
303 | } catch (Throwable e) {
|
304 | System.out.println("[fail] " + learner + " on " + (_e == null ? "?" : quote(_e[0])) + " - " + e);
|
305 | silentException(e);
|
306 | return false;
|
307 | }
|
308 | }
|
309 |
|
310 | static void silentException(Throwable e) {
|
311 | }
|
312 |
|
313 | /* XXX - important part */
|
314 | static Iterable<Learner> makeLearners() {
|
315 | List<Learner> list = new ArrayList<Learner>();
|
316 | //list.add(new LId()); // subsumed by trivial case of PrefixSuffix
|
317 | list.add(new LPrefixSuffix());
|
318 | list.add(new LSplitInput(new LOutPattern()));
|
319 | list.add(new LInputPattern());
|
320 | list.add(new LFixed());
|
321 | list.add(new LBoth(new RFJavaTok(), new LEach(new LFixedFunction(new EscapeCase()))));
|
322 | list.add(new LCharShift());
|
323 | list.add(new LOutSuffix(new LFirst(new LCharShift())));
|
324 | return list;
|
325 | }
|
326 |
|
327 | public static String unquote(String s) {
|
328 | if (s.startsWith("\"") && s.endsWith("\"") && s.length() > 1)
|
329 | return s.substring(1, s.length()-1).replace("\\\"", "\"").replace("\\\\", "\\"); // SHOULD work...
|
330 | else
|
331 | return s; // Return SOMETHING
|
332 | }
|
333 |
|
334 | public static String quote(String s) {
|
335 | if (s == null) return "null";
|
336 | return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
|
337 | }
|
338 |
|
339 | static String indent(String indent, String s) {
|
340 | return indent + s.replace("\n", "\n" + indent);
|
341 | }
|
342 |
|
343 | static void debugOn(String name) {
|
344 | try {
|
345 | Class c = Class.forName("main$" + name);
|
346 | Field field;
|
347 | while (true)
|
348 | try {
|
349 | field = c.getDeclaredField("debug");
|
350 | break;
|
351 | } catch (NoSuchFieldException e) {
|
352 | c = c.getSuperclass();
|
353 | }
|
354 | field.setBoolean(null, true);
|
355 | } catch (Exception e) {
|
356 | e.printStackTrace();
|
357 | System.err.println("Cannot debug class " + name);
|
358 | }
|
359 | }
|
360 |
|
361 | // splits the input at some point, takes only one part
|
362 | static class LSplitInput implements Learner {
|
363 | int splitIdx = 1; // split after first character
|
364 | Learner baseLearner;
|
365 |
|
366 | LSplitInput(Learner baseLearner) {
|
367 | this.baseLearner = baseLearner;
|
368 | }
|
369 |
|
370 | public void processInOut(Object _in, Object _out) {
|
371 | String in = (String) _in, out = (String) _out;
|
372 | in = in.substring(splitIdx);
|
373 | baseLearner.processInOut(in, out);
|
374 | }
|
375 |
|
376 | public Object processIn(Object _in) {
|
377 | String in = (String) _in;
|
378 | in = in.substring(splitIdx);
|
379 | return baseLearner.processIn(in);
|
380 | }
|
381 |
|
382 | public void toJava(Code code) {
|
383 | code.line(code.var + " = ((String) " + code.var + ").substring(" + splitIdx + ");");
|
384 | baseLearner.toJava(code);
|
385 | }
|
386 | }
|
387 |
|
388 | // removes common suffix from out, delegates to base learner
|
389 | static class LOutSuffix implements Learner {
|
390 | String suffixOut = "";
|
391 | Learner baseLearner;
|
392 |
|
393 | LOutSuffix(Learner baseLearner) {
|
394 | this.baseLearner = baseLearner;
|
395 | }
|
396 |
|
397 | public void processInOut(Object _in, Object _out) {
|
398 | String in = (String) _in, out = (String) _out;
|
399 | if (out.endsWith("!"))
|
400 | suffixOut = "!";
|
401 | if (out.endsWith(suffixOut))
|
402 | out = out.substring(0, out.length()-suffixOut.length());
|
403 |
|
404 | baseLearner.processInOut(in, out);
|
405 | }
|
406 |
|
407 | public Object processIn(Object _in) {
|
408 | String in = (String) _in;
|
409 | return baseLearner.processIn(in) + suffixOut;
|
410 | }
|
411 |
|
412 | public void toJava(Code code) {
|
413 | baseLearner.toJava(code);
|
414 | if (suffixOut.length() != 0)
|
415 | code.line(code.var + " = " + code.s() + "+" + quote(suffixOut) + ";");
|
416 | }
|
417 | }
|
418 |
|
419 | // if input appears in output in fixed pattern
|
420 | static class LOutPattern implements Learner {
|
421 | String pattern = "%!%";
|
422 | |
423 | public void processInOut(Object _in, Object _out) {
|
424 | String in = (String) _in, out = (String) _out;
|
425 | pattern = out.replace(in, "%!%");
|
426 | }
|
427 |
|
428 | public String processIn(Object _in) {
|
429 | String in = (String) _in;
|
430 | return pattern.replace("%!%", in);
|
431 | }
|
432 |
|
433 | public void toJava(Code code) {
|
434 | code.line(code.var + " = " + quote(pattern) + ".replace(" + quote("%!%") + ", (String) " + code.var + ");");
|
435 | }
|
436 | }
|
437 |
|
438 | // learns to exchange common prefixes and suffixes
|
439 | static class LPrefixSuffix extends LearnerImpl {
|
440 | static boolean debug;
|
441 | String prefixIn, suffixIn, prefixOut, suffixOut;
|
442 |
|
443 | public void processInOut(Object _in, Object _out) {
|
444 | String in = (String) _in, out = (String) _out;
|
445 | updateIn(in);
|
446 | prefixOut = prefixOut == null ? out : commonPrefix(prefixOut, out);
|
447 | suffixOut = suffixOut == null ? out : commonSuffix(suffixOut, out);
|
448 | if (debug)
|
449 | printState("processInOut(" + quote(in) + ", " + quote(out) + ")");
|
450 | }
|
451 |
|
452 | void updateIn(String in) {
|
453 | prefixIn = prefixIn == null ? in : commonPrefix(prefixIn, in);
|
454 | suffixIn = suffixIn == null ? in : commonSuffix(suffixIn, in);
|
455 | if (debug)
|
456 | printState("updateIn(" + quote(in) + ")");
|
457 | }
|
458 | |
459 | public String processIn(Object _in) {
|
460 | String in = (String) _in;
|
461 | //System.out.println("[before last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut));
|
462 | //System.out.println("[last info] " + quote(in));
|
463 |
|
464 | // use latest information
|
465 | String p = prefixIn, s = suffixIn;
|
466 | updateIn(in);
|
467 | prefixOut = prefixOut.substring(0, prefixOut.length()-(p.length()-prefixIn.length()));
|
468 | suffixOut = suffixOut.substring(s.length()-suffixIn.length());
|
469 |
|
470 | //System.out.println("[after last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut));
|
471 | String core = in.substring(prefixIn.length(), in.length()-suffixIn.length());
|
472 | return prefixOut + core + suffixOut;
|
473 | }
|
474 |
|
475 | public void toJava(Code code) {
|
476 | code.line(code.var + " = ((String) " + code.var + ").substring(" + prefixIn.length() + ", " + code.s() + ".length()-" + suffixIn.length() + ");");
|
477 | code.line(code.var + " = " + quote(prefixOut) + " + " + code.var + " + " + quote(suffixOut) + ";");
|
478 | }
|
479 |
|
480 | void printState(String text) {
|
481 | System.out.println(text);
|
482 | printVars();
|
483 | }
|
484 | }
|
485 |
|
486 | // for "find" tasks (e.g. "abcde" to "[[abc]]de")
|
487 | static class LInputPattern implements Learner {
|
488 | String regexp = "";
|
489 |
|
490 | public void processInOut(Object _in, Object _out) {
|
491 | String in = (String) _in, out = (String) _out;
|
492 | int i = out.indexOf("[["), j = out.indexOf("]]", i+1);
|
493 | if (j < 0) return;
|
494 | String s = out.substring(i+2, j);
|
495 | regexp = s.replaceAll("\\d+", Matcher.quoteReplacement("\\d+"));
|
496 | System.out.println("regexp: " + regexp);
|
497 | }
|
498 |
|
499 | public String processIn(Object _in) {
|
500 | String in = (String) _in;
|
501 | if (regexp.length() == 0)
|
502 | return in;
|
503 | else
|
504 | return in.replaceAll("(" + regexp + ")", "[[$1]]");
|
505 | }
|
506 |
|
507 | public void toJava(Code code) {
|
508 | code.line(code.var + " = ((String) " + code.var + ").replaceAll(" + quote("(" + regexp + ")") + ", \"[[$1]]\");");
|
509 | }
|
510 | }
|
511 |
|
512 | static class LFixed extends LearnerImpl {
|
513 | static boolean debug;
|
514 | Object value;
|
515 |
|
516 | public void processInOut(Object in, Object out) {
|
517 | value = out;
|
518 | if (debug)
|
519 | printVars();
|
520 | }
|
521 |
|
522 | public Object processIn(Object in) {
|
523 | return value;
|
524 | }
|
525 |
|
526 | public void toJava(Code code) {
|
527 | code.line(code.var + " = " + quote((String) value) + ";");
|
528 | }
|
529 | }
|
530 |
|
531 | static void fail(String msg) {
|
532 | throw new RuntimeException(msg);
|
533 | }
|
534 |
|
535 | static void assertSameSize(List a, List b) {
|
536 | if (a.size() != b.size())
|
537 | fail("wrong list sizes");
|
538 | }
|
539 | |
540 | // process lists in parallel
|
541 | // (in and out must be a list of same length)
|
542 | static class LEach extends LearnerImpl {
|
543 | static boolean debug;
|
544 | Learner base;
|
545 |
|
546 | LEach(Learner base) {
|
547 | this.base = base;
|
548 | }
|
549 |
|
550 | public void processInOut(Object _in, Object _out) {
|
551 | List in = (List) _in, out = (List) _out;
|
552 | assertSameSize(in, out);
|
553 | for (int i = 0; i < in.size(); i++)
|
554 | base.processInOut(in.get(i), out.get(i));
|
555 | if (debug)
|
556 | printVars();
|
557 | }
|
558 |
|
559 | public Object processIn(Object _in) {
|
560 | List in = (List) _in;
|
561 | List out = new ArrayList();
|
562 | for (Object x : in)
|
563 | out.add(base.processIn(x));
|
564 | return out;
|
565 | }
|
566 |
|
567 | public void toJava(Code code) {
|
568 | code.line("List out = new ArrayList();");
|
569 | code.line("for (Object x : (List) in) {");
|
570 | code.indent();
|
571 | code.line("in = x;");
|
572 | base.toJava(code);
|
573 | code.line("out.add(in);");
|
574 | code.unindent();
|
575 | code.line("}");
|
576 | code.line("in = out;");
|
577 | }
|
578 | }
|
579 |
|
580 | static class LBoth extends LearnerImpl {
|
581 | ReversibleFunction f;
|
582 | Learner base;
|
583 |
|
584 | LBoth(ReversibleFunction f, Learner base) {
|
585 | this.f = f;
|
586 | this.base = base;
|
587 | }
|
588 |
|
589 | public void processInOut(Object in, Object out) {
|
590 | in = f.process(in);
|
591 | out = f.process(out);
|
592 | base.processInOut(in, out);
|
593 | }
|
594 |
|
595 | public Object processIn(Object in) {
|
596 | in = f.process(in);
|
597 | in = base.processIn(in);
|
598 | in = f.unprocess(in);
|
599 | return in;
|
600 | }
|
601 |
|
602 | public void toJava(Code code) {
|
603 | f.toJava_process(code);
|
604 | base.toJava(code);
|
605 | f.toJava_unprocess(code);
|
606 | }
|
607 | }
|
608 |
|
609 | static class RFJavaTok implements ReversibleFunction {
|
610 | public Object process(Object in) {
|
611 | return JavaTok.split((String) in);
|
612 | }
|
613 |
|
614 | public Object unprocess(Object in) {
|
615 | return JavaTok.join((List) in);
|
616 | }
|
617 |
|
618 | public void toJava_process(Code code) {
|
619 | code.translators("!636", "!class JavaTok");
|
620 | code.line(code.var + " = JavaTok.split((String) " + code.var + ");");
|
621 | }
|
622 |
|
623 | public void toJava_unprocess(Code code) {
|
624 | code.translators("!636", "!class JavaTok");
|
625 | code.line(code.var + " = JavaTok.join((List) " + code.var + ");");
|
626 | }
|
627 | }
|
628 |
|
629 | static class LFixedFunction extends LearnerImpl {
|
630 | Function f;
|
631 |
|
632 | LFixedFunction(Function f) {
|
633 | this.f = f;
|
634 | }
|
635 |
|
636 | public void processInOut(Object in, Object out) {
|
637 | }
|
638 |
|
639 | public Object processIn(Object in) {
|
640 | return f.process(in);
|
641 | }
|
642 |
|
643 | public void toJava(Code code) {
|
644 | f.toJava_process(code);
|
645 | }
|
646 | }
|
647 |
|
648 | static class EscapeCase implements Function {
|
649 | static boolean debug;
|
650 |
|
651 | public Object process(Object _in) {
|
652 | if (debug)
|
653 | System.out.println("EscapeCase: " + _in);
|
654 | String in = (String) _in;
|
655 | return in.equals("case") ? "_case" : in;
|
656 | }
|
657 |
|
658 | public void toJava_process(Code code) {
|
659 | code.line("if (\"case\".equals(" + code.var + ")) " + code.var + " = " + quote("_case") + ";");
|
660 | }
|
661 | }
|
662 |
|
663 | static class LCharShift extends LearnerImpl {
|
664 | int shift;
|
665 |
|
666 | public void processInOut(Object _in, Object _out) {
|
667 | String in = (String) _in, out = (String) _out;
|
668 | shift = (int) out.charAt(0) - (int) in.charAt(0);
|
669 | }
|
670 |
|
671 | public Object processIn(Object _in) {
|
672 | String in = (String) _in;
|
673 | char[] c = new char[in.length()];
|
674 | for (int i = 0; i < c.length; i++)
|
675 | c[i] = (char) ((int) in.charAt(i) + shift);
|
676 | return new String(c);
|
677 | }
|
678 |
|
679 | public void toJava(Code code) {
|
680 | code.line("char[] c = new char[((String) " + code.var + ").length()];");
|
681 | code.line("for (int i = 0; i < c.length; i++)");
|
682 | code.line(" c[i] = (char) ((int) ((String) " + code.var + ").charAt(i)" + (shift < 0 ? "" + shift : "+" + shift) + ");");
|
683 | code.line(code.var + " = new String(c);");
|
684 | }
|
685 | }
|
686 |
|
687 | // applies base learner to first char of string
|
688 | // (or first element of list, TODO)
|
689 | static class LFirst implements Learner {
|
690 | Learner baseLearner;
|
691 |
|
692 | LFirst(Learner baseLearner) {
|
693 | this.baseLearner = baseLearner;
|
694 | }
|
695 |
|
696 | public void processInOut(Object _in, Object _out) {
|
697 | String in = (String) _in, out = (String) _out;
|
698 | if (in.length() == 0)
|
699 | return;
|
700 | String firstIn = in.substring(0, 1), firstOut = out.substring(0, 1);
|
701 | baseLearner.processInOut(firstIn, firstOut);
|
702 | }
|
703 |
|
704 | public Object processIn(Object _in) {
|
705 | String in = (String) _in;
|
706 | if (in.length() == 0)
|
707 | return in;
|
708 | String firstIn = in.substring(0, 1);
|
709 | return baseLearner.processIn(firstIn) + in.substring(1);
|
710 | }
|
711 |
|
712 | public void toJava(Code code) {
|
713 | code.line("if (" + code.s() + ".length() != 0) {");
|
714 | code.indent();
|
715 | code.line("String rest = " + code.s() + ".substring(1);");
|
716 | code.line(code.var + " = " + code.s() + ".substring(0, 1);");
|
717 | baseLearner.toJava(code);
|
718 | code.line(code.var + " = " + code.s() + "+rest;");
|
719 | code.unindent();
|
720 | code.line("}");
|
721 | }
|
722 | }
|
723 |
|
724 | static Method findMainMethod(Class<?> theClass) {
|
725 | for (Method method : theClass.getMethods())
|
726 | if (method.getName().equals("main") && method.getParameterTypes().length == 1)
|
727 | return method;
|
728 | throw new RuntimeException("Method 'main' with 1 parameter not found in " + theClass.getName());
|
729 | }
|
730 |
|
731 | static Method compileJava(Code code) {
|
732 | try {
|
733 | String java = code.buf.toString();
|
734 | String prelude = /*"import java.util.*;\n\n" +*/
|
735 | "public class main { public static Object main(Object in) throws Exception {\n";
|
736 | String postlude = "\nreturn in;\n}}";
|
737 | String src = code.getTranslators() + "\n" + prelude + java + postlude;
|
738 | File srcDir = _x16.TempDirMaker_make();
|
739 | File classesDir = _x16.TempDirMaker_make();
|
740 | _x16.saveTextFile(new File(srcDir, "main.java").getPath(), src);
|
741 | List<File> libraries = new ArrayList<File>();
|
742 | File transpiledDir = _x16.topLevelTranslate(srcDir, libraries);
|
743 | String javacOutput = _x16.compileJava(transpiledDir, libraries, classesDir);
|
744 | System.out.println(javacOutput);
|
745 | URL[] urls = {classesDir.toURI().toURL()};
|
746 |
|
747 | // make class loader
|
748 | URLClassLoader classLoader = new URLClassLoader(urls);
|
749 |
|
750 | // load main class
|
751 | Class<?> mainClass = classLoader.loadClass("main");
|
752 |
|
753 | return findMainMethod(mainClass);
|
754 | } catch (Exception e) {
|
755 | throw new RuntimeException(e);
|
756 | }
|
757 | }
|
758 |
|
759 | static void testJava(Case case, Code code) {
|
760 | try {
|
761 | Method m = compileJava(code);
|
762 |
|
763 | for (String[] e : case.fullExamples) {
|
764 | String out = (String) m.invoke(null, e[0]);
|
765 |
|
766 | if (!e[1].equals(out)) {
|
767 | throw new RuntimeException("[fail] Java code on " + quote(e[0]) + " - got: " + quote(out) + " rather than: " + quote(e[1]));
|
768 | }
|
769 | }
|
770 |
|
771 | System.out.println("\nGOOD JAVA.");
|
772 | case.goodJava = true;
|
773 | } catch (Throwable e) {
|
774 | e.printStackTrace();
|
775 | System.out.println("\nBAD JAVA.");
|
776 | }
|
777 | }
|
778 | } |