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

56
LINES

< > BotCompany Repo | #1013206 // tok_doubleFor_v2

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

Libraryless. Click here for Pure Java version (4705L/30K).

1  
sbool tok_doubleFor_v2_debug;
2  
3  
static void tok_doubleFor_v2(L<S> tok) {
4  
  for (int i : reversed(indexesOf(tok, "for"))) {
5  
    if (neq(get(tok, i+2), "(")) continue;
6  
    int argsFrom = i+4;
7  
    // move loop label to proper place
8  
    if (eqGet(tok, i-2, ":") && isIdentifier(get(tok, i-4))) i -= 4;
9  
    int iColon = indexOfAny(tok, argsFrom, ":", ")");
10  
    if (neq(get(tok, iColon), ":")) continue;
11  
    if (tok_doubleFor_v2_debug) print("have colon");
12  
    tok_typesAndNamesOfParams_keepModifiers.set(true);
13  
    L<PairS> args;
14  
    try {
15  
      args = tok_typesAndNamesOfParams(subList(tok, argsFrom-1, iColon-1));
16  
    } catch {
17  
      print("tok_doubleFor_v2: Skipping parsing complicated for statement (probably not a double for)");
18  
      continue;
19  
    }
20  
    
21  
    if (l(args) != 2) continue; // simple for or a three-argument for (out of this world!)
22  
    
23  
    // S a, b => S a, S b
24  
    if (eq(second(args).a, "?")) second(args).a = first(args).a;
25  
    
26  
    if (tok_doubleFor_v2_debug) print("have 2 args: " + sfu(args));
27  
    int iClosing = tok_findEndOfForExpression(tok, iColon+2);
28  
    if (iClosing < 0) continue;
29  
    if (tok_doubleFor_v2_debug) print("have closing");
30  
    S exp = trimJoinSubList(tok, iColon+2, iClosing-1);
31  
    if (tok_doubleFor_v2_debug) print("Expression: " + exp);
32  
    int iCurly = iClosing+2;
33  
    tok_statementToBlock(tok, iCurly);
34  
    int iCurlyEnd = tok_findEndOfStatement(tok, iCurly)-1;
35  
    if (iCurlyEnd < 0) continue;
36  
    if (tok_doubleFor_v2_debug) print("have curly end");
37  
    
38  
    // complicated expression? evaluate to variable first
39  
    
40  
    if (!isIdentifier(exp)) {
41  
      S expVar = makeVar();
42  
      S mapType = tok_toNonPrimitiveTypes("Map<" + first(args).a + ", " + second(args).a + ">");
43  
      tokPrepend(tok, i, "{ final " + mapType + " " + expVar + " = " + exp + "; ");
44  
      tokAppend(tok, iCurlyEnd, "}");
45  
      replaceTokens(tok, iColon+2, iClosing-1, expVar);
46  
      exp = expVar;
47  
    }
48  
    
49  
    tokAppend(tok, iColon, " keys(");
50  
    tokPrepend(tok, iClosing, ")");
51  
    
52  
    replaceTokens(tok, argsFrom, iColon-1, joinPairWithSpace(first(args)));
53  
    tokAppend(tok, iCurly, " " + joinPairWithSpace(second(args)) + " = " + exp + ".get(" + first(args).b + "); ");
54  
    reTok(tok, i, iCurlyEnd+1);
55  
  }
56  
}

Author comment

Began life as a copy of #1013205

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1013206
Snippet name: tok_doubleFor_v2
Eternal ID of this version: #1013206/24
Text MD5: 079cd04383cdd2d4f79be3bfaa2b2cdf
Transpilation MD5: 0b4f3f024e57433f8ac56f07ac445fa0
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-11-11 21:14:48
Source code size: 2293 bytes / 56 lines
Pitched / IR pitched: No / No
Views / Downloads: 500 / 542
Version history: 23 change(s)
Referenced in: [show references]