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

49
LINES

< > BotCompany Repo | #1023412 // tok_doubleFor_v2 OLD

JavaX fragment (include)

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<Pair<S>> args = tok_typesAndNamesOfParams(subList(tok, argsFrom-1, iColon-1));
14  
    if (l(args) != 2) continue; // simple for or a three-argument for (out of this world!)
15  
    
16  
    // S a, b => S a, S b
17  
    if (eq(second(args).a, "?")) second(args).a = first(args).a;
18  
    
19  
    if (tok_doubleFor_v2_debug) print("have 2 args: " + sfu(args));
20  
    int iClosing = tok_findEndOfForExpression(tok, iColon+2);
21  
    if (iClosing < 0) continue;
22  
    if (tok_doubleFor_v2_debug) print("have closing");
23  
    S exp = trimJoinSubList(tok, iColon+2, iClosing-1);
24  
    if (tok_doubleFor_v2_debug) print("Expression: " + exp);
25  
    int iCurly = iClosing+2;
26  
    if (neq(get(tok, iCurly), "{")) fail("please use { with double for");
27  
    int iCurlyEnd = findEndOfStatement(tok, iCurly)-1;
28  
    if (iCurlyEnd < 0) continue;
29  
    if (tok_doubleFor_v2_debug) print("have curly end");
30  
    
31  
    // complicated expression? evaluate to variable first
32  
    
33  
    if (!isIdentifier(exp)) {
34  
      S expVar = makeVar();
35  
      S mapType = tok_toNonPrimitiveTypes("Map<" + first(args).a + ", " + second(args).a + ">");
36  
      tokPrepend(tok, i, "{ final " + mapType + " " + expVar + " = " + exp + "; ");
37  
      tokAppend(tok, iCurlyEnd, "}");
38  
      replaceTokens(tok, iColon+2, iClosing-1, expVar);
39  
      exp = expVar;
40  
    }
41  
    
42  
    tokAppend(tok, iColon, " keys(");
43  
    tokPrepend(tok, iClosing, ")");
44  
    
45  
    replaceTokens(tok, argsFrom, iColon-1, joinPairWithSpace(first(args)));
46  
    tokAppend(tok, iCurly, " " + joinPairWithSpace(second(args)) + " =" + exp + ".get(" + first(args).b + "); ");
47  
    reTok(tok, i, iCurlyEnd+1);
48  
  }
49  
}

Author comment

Began life as a copy of #1013206

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1023412
Snippet name: tok_doubleFor_v2 OLD
Eternal ID of this version: #1023412/1
Text MD5: 3e3bffc9b7c63f08d99f5f8c5d02eaf2
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-06-17 23:55:18
Source code size: 2150 bytes / 49 lines
Pitched / IR pitched: No / No
Views / Downloads: 229 / 272
Referenced in: [show references]