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

47
LINES

< > BotCompany Repo | #1010313 // tok_unpair - "S a, S b = unpair ...;" construct

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5458L/31K).

static void tok_unpair(L<S> tok) {
  if (!tok.contains("unpair")) ret;
  
  int i;
  
  while ((i = jfind(tok, "var <id>, <id> = unpair")) >= 0) {
    int iUnpair = indexOf(tok, "unpair", i);
    int j = findEndOfStatement(tok, iUnpair);
    S var1 = tok.get(i+2);
    S var2 = tok.get(i+6);
    S v = makeVar();
    replaceTokens(tok, i, iUnpair+1, "var " + v + " ="); 
    tok.set(j-1, "; var " + var1 + " = " + v + ".a; var " + var2 + " = " + v + ".b;");
    reTok(tok, i, j);
  }
  
  jreplace(tok, "<id> <id>, <id> = unpair", "$1 $2, $1 $4 = unpair");
  
  while ((i = jfind(tok, "<id> <id>, <id> <id> = unpair")) >= 0) {
    int idx = indexOf(tok, "unpair", i);
    int j = findEndOfStatement(tok, idx);
    S type1 = tok.get(i), var1 = tok.get(i+2);
    S type2 = tok.get(i+6), var2 = tok.get(i+8);
    S v = makeVar();
    //printVars(+type1, +var1, +type2, +var2);
    tok.set(i+4, ";");
    tok.set(idx-2, ";");
    tok.set(idx, "Pair<" + tok_toNonPrimitiveTypes(type1) + "," + tok_toNonPrimitiveTypes(type2) + "> " + v + "=");
    tok.set(j-1, "; " + var1 + " = " + v + ".a; " + var2 + " = " + v + ".b;");
    reTok(tok, i, j);
  }
  
  while ((i = jfind(tok, "<id> <id>, <id> < <id>,<id> > <id> = unpair")) >= 0 || (i = jfind(tok, "<id> <id>, <id><<id>> <id> = unpair")) >= 0) {
    print("unpair");
    int idx = indexOf(tok, "unpair", i);
    int j = findEndOfStatement(tok, idx);
    S type1 = tok.get(i), var1 = tok.get(i+2);
    S type2 = joinSubList(tok, i+5, idx-5), var2 = tok.get(idx-4);
    S v = makeVar();
    tok.set(i+4, ";");
    tok.set(idx-2, ";");
    tok.set(idx-1, "");
    tok.set(idx, "Pair<" + type1 + "," + type2 + "> " + v + "=");
    tok.set(j-1, "; " + var1 + " = " + v + ".a; " + var2 + " = " + v + ".b;");
    reTok(tok, i, j);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1010313
Snippet name: tok_unpair - "S a, S b = unpair ...;" construct
Eternal ID of this version: #1010313/17
Text MD5: c0d54daf48a8727b802a4b8da7663d91
Transpilation MD5: bf8f9b78a22109096b83e051ccae74c6
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-03 01:04:28
Source code size: 1820 bytes / 47 lines
Pitched / IR pitched: No / No
Views / Downloads: 552 / 679
Version history: 16 change(s)
Referenced in: [show references]