Libraryless. Click here for Pure Java version (1523L/10K/32K).
1 | !752 |
2 | |
3 | p { |
4 | L<S> tok = pythonToJavaTok(loadSnippet("#1001999")); |
5 | L<L<S>> lines = pythonToLines(tok); |
6 | new L<S> out; |
7 | out.add("!752\n"); |
8 | new L<S> statements; |
9 | for (int i = 0; i < l(lines); i++) { |
10 | L<S> l = lines.get(i); |
11 | //print("Line: " + quote(join(l))); |
12 | if (l(l) == 1) { |
13 | out.add(join(l)); |
14 | continue; |
15 | } |
16 | new Matches m; |
17 | if (mymatch("def *(*):", l, m)) { |
18 | int j = endOfBlock(lines, i+1, " "); |
19 | out.add("static S " + m.get(0) + "(S " + m.get(1) + ") {"); |
20 | for (int k = i+1; k < j; k++) |
21 | out.add(convertStatement(lines.get(k))); |
22 | out.add("}"); |
23 | i = j-1; |
24 | } else { |
25 | if (mymatchStart("* = ", l, m)) |
26 | statements.add("S " + m.get(0) + ";"); |
27 | statements.add(convertStatement(l)); |
28 | //out.add("// unknown line: " + join(l)); |
29 | } |
30 | } |
31 | S java = fromLines(concatLists( |
32 | out, |
33 | litlist("p {"), |
34 | indent(" ", statements), |
35 | litlist("}") |
36 | )); |
37 | print(java); |
38 | } |
39 | |
40 | static S convertStatement(L<S> l) { |
41 | l.set(l(l)-2, l.get(l(l)-2) + ";"); |
42 | ret join(l); |
43 | } |
44 | |
45 | static boolean mymatch(S thepat, L<S> tok, Matches m) { |
46 | L<S> pat = pythonToJavaTok(thepat); |
47 | if (pat.size() != tok.size()) return false; |
48 | ret mymatchStart(pat, tok, m); |
49 | } |
50 | |
51 | static boolean mymatchStart(S thepat, L<S> tok, Matches m) { |
52 | L<S> pat = pythonToJavaTok(thepat); |
53 | ret mymatchStart(pat, tok, m); |
54 | } |
55 | |
56 | static boolean mymatchStart(L<S> pat, L<S> tok, Matches m) { |
57 | new L<S> result; |
58 | if (tok.size() < pat.size()) return false; |
59 | for (int i = 1; i < pat.size(); i += 2) { |
60 | S p = pat.get(i), t = tok.get(i); |
61 | if (eq(p, "*")) |
62 | result.add(t); |
63 | else if (neq(p, t)) |
64 | return false; |
65 | } |
66 | if (m != null) m.m = result.toArray(new S[result.size()]); |
67 | ret true; |
68 | } |
69 | |
70 | static int endOfBlock(L<L<S>> lines, int i, S indent) { |
71 | while (i < l(lines) && lines.get(i).get(0).startsWith(indent)) |
72 | ++i; |
73 | ret i; |
74 | } |
75 | |
76 | static L<L<S>> pythonToLines(L<S> tok) { |
77 | new L<L<S>> lines; |
78 | for (S line : toLines(join(tok))) |
79 | lines.add(javaTok(line)); |
80 | ret lines; |
81 | } |
82 | |
83 | /* TODO - too complicated |
84 | static L<L<S>> tokToLines(L<S> tok) { |
85 | new L<S> lines; |
86 | int i = 0; // i and j are on whitespace tokens |
87 | while (i < l(tok)-3) { |
88 | int j = i; |
89 | while (j < l(tok)-1 && !tok.get(j).contains("\n")) |
90 | j += 2; |
91 | if (i == j) |
92 | lines.add(litlist("")); |
93 | else |
94 | lines.add(concatLists(litlist(""), tok.subList(i+1, j), litlist(""))); |
95 | i = j; |
96 | } |
97 | lines.add( |
98 | ret lines; |
99 | } |
100 | */ |
Began life as a copy of #1002000
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1002001 |
Snippet name: | Port bot.py to JavaX |
Eternal ID of this version: | #1002001/1 |
Text MD5: | 077e43911f8c2acde5a2735f81a5fe91 |
Transpilation MD5: | 8b0155477b9cc8f3c1d3c5ffb14be5f1 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-12-11 21:19:56 |
Source code size: | 2532 bytes / 100 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 816 / 801 |
Referenced in: | [show references] |