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.

!752

p {
  L<S> tok = javaTok(loadSnippet("#1002095"));
  
  replaceToken(tok, "push_back", "add");
  
  jreplace(tok, "int main()", "p");
  
  // throw away stuff
  jreplace(tok, "using namespace std;", "");
  jreplace(tok, "#include <*>", "");
  
  // list, vector
  jreplace(tok, "list<", "new LinkedList<");
  jreplace(tok, "vector<", "new ArrayList<");
  jreplace(tok, "deque<*> *;", "LinkedList<$3> $4 = cppLinkedList("); // TODO
  
  jreplace(tok, "*.resize(", "resizeCollection($1, 0,");
  
  jreplace(tok, "copy(*.begin(), *.end(), *.begin())", "cppCopy($3, $15)");
  
  // cout
  jreplace(tok, "cout << * << endl", "print($4)");
  
  print();
  print("!752\n\n" + join(tok));
}

static void jreplace(L<S> tok, S in, S out) {
  L<S> tokin = javaTok(in);
  for (int n = 0; n < 10000; n++) {
    int i = findCodeTokens(tok, toStringArray(codeTokensOnly(tokin)));
    if (i < 0) {
      replaceCollection(tok, javaTok(tok));
      ret;
    }
    L<S> subList = tok.subList(i-1, i+l(tokin));
    S expansion = expandRefs(out, subList);
    clearAllTokens(tok.subList(i, i+l(tokin)-2));
    tok.set(i, expansion);
  }
  throw fail("woot? 10000!");
}

// "$1" is first code token, "$2" second code token etc.
static S expandRefs(S s, L<S> tokref) {
  L<S> tok = javaTok(s);
  for (int i = 1; i < l(tok)-2; i += 2) {
    if (tok.get(i).startsWith("$") && isInteger(tok.get(i).substring(1))) {
      S x = tokref.get(-1+parseInt(tok.get(i).substring(1))*2);
      tok.set(i, x);
    }
  }
  ret join(tok);
}

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: 678 / 725
Referenced in: [show references]