!752 p { L tok = pythonToJavaTok(loadSnippet("#1001999")); L> lines = pythonToLines(tok); new L out; out.add("!752\n"); new L statements; for (int i = 0; i < l(lines); i++) { L l = lines.get(i); //print("Line: " + quote(join(l))); if (l(l) == 1) { out.add(join(l)); continue; } new Matches m; if (mymatch("def *(*):", l, m)) { int j = endOfBlock(lines, i+1, " "); out.add("static S " + m.get(0) + "(S " + m.get(1) + ") {"); for (int k = i+1; k < j; k++) out.add(convertStatement(lines.get(k))); out.add("}"); i = j-1; } else { if (mymatchStart("* = ", l, m)) statements.add("S " + m.get(0) + ";"); statements.add(convertStatement(l)); //out.add("// unknown line: " + join(l)); } } S java = fromLines(concatLists( out, litlist("p {"), indent(" ", statements), litlist("}") )); print(java); } static S convertStatement(L l) { l.set(l(l)-2, l.get(l(l)-2) + ";"); ret join(l); } static boolean mymatch(S thepat, L tok, Matches m) { L pat = pythonToJavaTok(thepat); if (pat.size() != tok.size()) return false; ret mymatchStart(pat, tok, m); } static boolean mymatchStart(S thepat, L tok, Matches m) { L pat = pythonToJavaTok(thepat); ret mymatchStart(pat, tok, m); } static boolean mymatchStart(L pat, L tok, Matches m) { new L result; if (tok.size() < pat.size()) return false; for (int i = 1; i < pat.size(); i += 2) { S p = pat.get(i), t = tok.get(i); if (eq(p, "*")) result.add(t); else if (neq(p, t)) return false; } if (m != null) m.m = result.toArray(new S[result.size()]); ret true; } static int endOfBlock(L> lines, int i, S indent) { while (i < l(lines) && lines.get(i).get(0).startsWith(indent)) ++i; ret i; } static L> pythonToLines(L tok) { new L> lines; for (S line : toLines(join(tok))) lines.add(javaTok(line)); ret lines; } /* TODO - too complicated static L> tokToLines(L tok) { new L lines; int i = 0; // i and j are on whitespace tokens while (i < l(tok)-3) { int j = i; while (j < l(tok)-1 && !tok.get(j).contains("\n")) j += 2; if (i == j) lines.add(litlist("")); else lines.add(concatLists(litlist(""), tok.subList(i+1, j), litlist(""))); i = j; } lines.add( ret lines; } */