Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

56
LINES

< > BotCompany Repo | #1002096 // C++ to JavaX Translator (developing)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Transpiled version (1451L) is out of date.

1  
!752
2  
3  
p {
4  
  L<S> tok = javaTok(loadSnippet("#1002095"));
5  
  
6  
  replaceToken(tok, "push_back", "add");
7  
  
8  
  jreplace(tok, "int main()", "p");
9  
  
10  
  // throw away stuff
11  
  jreplace(tok, "using namespace std;", "");
12  
  jreplace(tok, "#include <*>", "");
13  
  
14  
  // list, vector
15  
  jreplace(tok, "list<", "new LinkedList<");
16  
  jreplace(tok, "vector<", "new ArrayList<");
17  
  jreplace(tok, "deque<*> *;", "LinkedList<$3> $4 = cppLinkedList("); // TODO
18  
  
19  
  jreplace(tok, "*.resize(", "resizeCollection($1, 0,");
20  
  
21  
  jreplace(tok, "copy(*.begin(), *.end(), *.begin())", "cppCopy($3, $15)");
22  
  
23  
  // cout
24  
  jreplace(tok, "cout << * << endl", "print($4)");
25  
  
26  
  print();
27  
  print("!752\n\n" + join(tok));
28  
}
29  
30  
static void jreplace(L<S> tok, S in, S out) {
31  
  L<S> tokin = javaTok(in);
32  
  for (int n = 0; n < 10000; n++) {
33  
    int i = findCodeTokens(tok, toStringArray(codeTokensOnly(tokin)));
34  
    if (i < 0) {
35  
      replaceCollection(tok, javaTok(tok));
36  
      ret;
37  
    }
38  
    L<S> subList = tok.subList(i-1, i+l(tokin));
39  
    S expansion = expandRefs(out, subList);
40  
    clearAllTokens(tok.subList(i, i+l(tokin)-2));
41  
    tok.set(i, expansion);
42  
  }
43  
  throw fail("woot? 10000!");
44  
}
45  
46  
// "$1" is first code token, "$2" second code token etc.
47  
static S expandRefs(S s, L<S> tokref) {
48  
  L<S> tok = javaTok(s);
49  
  for (int i = 1; i < l(tok)-2; i += 2) {
50  
    if (tok.get(i).startsWith("$") && isInteger(tok.get(i).substring(1))) {
51  
      S x = tokref.get(-1+parseInt(tok.get(i).substring(1))*2);
52  
      tok.set(i, x);
53  
    }
54  
  }
55  
  ret join(tok);
56  
}

Author comment

Began life as a copy of #1002056

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: #1002096
Snippet name: C++ to JavaX Translator (developing)
Eternal ID of this version: #1002096/1
Text MD5: 4e2647ea867d059aac2415fdd87637f6
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-12-18 17:14:22
Source code size: 1566 bytes / 56 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 679 / 727
Referenced in: [show references]