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).

sbool tok_doubleFor_v2_debug;

static void tok_doubleFor_v2(L<S> tok) {
  for (int i : reversed(indexesOf(tok, "for"))) {
    if (neq(get(tok, i+2), "(")) continue;
    int argsFrom = i+4;
    // move loop label to proper place
    if (eqGet(tok, i-2, ":") && isIdentifier(get(tok, i-4))) i -= 4;
    int iColon = indexOfAny(tok, argsFrom, ":", ")");
    if (neq(get(tok, iColon), ":")) continue;
    if (tok_doubleFor_v2_debug) print("have colon");
    tok_typesAndNamesOfParams_keepModifiers.set(true);
    L<PairS> args;
    try {
      args = tok_typesAndNamesOfParams(subList(tok, argsFrom-1, iColon-1));
    } catch {
      print("tok_doubleFor_v2: Skipping parsing complicated for statement (probably not a double for)");
      continue;
    }
    
    if (l(args) != 2) continue; // simple for or a three-argument for (out of this world!)
    
    // S a, b => S a, S b
    if (eq(second(args).a, "?")) second(args).a = first(args).a;
    
    if (tok_doubleFor_v2_debug) print("have 2 args: " + sfu(args));
    int iClosing = tok_findEndOfForExpression(tok, iColon+2);
    if (iClosing < 0) continue;
    if (tok_doubleFor_v2_debug) print("have closing");
    S exp = trimJoinSubList(tok, iColon+2, iClosing-1);
    if (tok_doubleFor_v2_debug) print("Expression: " + exp);
    int iCurly = iClosing+2;
    tok_statementToBlock(tok, iCurly);
    int iCurlyEnd = tok_findEndOfStatement(tok, iCurly)-1;
    if (iCurlyEnd < 0) continue;
    if (tok_doubleFor_v2_debug) print("have curly end");
    
    // complicated expression? evaluate to variable first
    
    if (!isIdentifier(exp)) {
      S expVar = makeVar();
      S mapType = tok_toNonPrimitiveTypes("Map<" + first(args).a + ", " + second(args).a + ">");
      tokPrepend(tok, i, "{ final " + mapType + " " + expVar + " = " + exp + "; ");
      tokAppend(tok, iCurlyEnd, "}");
      replaceTokens(tok, iColon+2, iClosing-1, expVar);
      exp = expVar;
    }
    
    tokAppend(tok, iColon, " keys(");
    tokPrepend(tok, iClosing, ")");
    
    replaceTokens(tok, argsFrom, iColon-1, joinPairWithSpace(first(args)));
    tokAppend(tok, iCurly, " " + joinPairWithSpace(second(args)) + " = " + exp + ".get(" + first(args).b + "); ");
    reTok(tok, i, iCurlyEnd+1);
  }
}

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: 575 / 631
Version history: 23 change(s)
Referenced in: [show references]