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

62
LINES

< > BotCompany Repo | #1026384 - tok_swappableFunctions [v4, with complex return types, LIVE]

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

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

svoid tok_swappableFunctions(LS tok) {
  jreplace(tok, "swappable static", "static swappable");
  jreplace(tok, "pswappable <id>", "persistently swappable $2");
  jreplace(tok, "persistent swappable <id>", "persistently swappable $3");
  jreplace(tok, "transient swappable <id>", "swappable $3");

  int i;
  while ((i = jfind_any(tok,
    "swappable <id> <id>(", "swappable <id> <", "swappable <id>.", "swappable <id>[")) >= 0) {
    // left-scan for modifiers
    int iModEnd = i;
    bool persistently = eqGet(tok, i-2, "persistently");
    if (persistently) iModEnd -= 2;
    int iMod = leftScanModifiers(tok, iModEnd);
    S modifiers = joinSubList(tok, iMod, iModEnd);
    
    // scan return type, find function name
    int iEndOfType = tok_findEndOfType(tok, i+2);
    S returnType = joinSubList(tok, i+2, iEndOfType-1);
    S name = assertIdentifier(tok.get(iEndOfType));
    int iArgs = iEndOfType+2;
    assertEquals("(", get(tok, iArgs));
    
    int iCurly = indexOf(tok, iEndOfType, "{");
    assertEquals(")", get(tok, iCurly-2));
    LS tokArgs = subList(tok, iArgs+1, iCurly-2);
    LPairS args = tok_typesAndNamesOfParams(tokArgs);
    S args1 = join(tokArgs);
    S args2 = joinWithComma(pairsB(args));
    bool isVoidMethod = eq(returnType, "void");
    LS types = pairsA(args);
    if (!isVoidMethod) types.add(returnType);
    S typeParams = joinWithComma(map(type -> tok_varArgTypeToArray(tok_toNonPrimitiveTypes(type)), types)); // TODO: modifiers
    S base = name + "_base", fallback = name + "_fallback";
    S mods = modifiers + (persistently || containsJavaToken(modifiers, "static") ? "" : "transient ");
    S src;
    if (isVoidMethod)
      if (empty(args))
        // no args, returning void
        src = mods + "Runnable \*name*/;\n" + 
          "\*modifiers*/\*returnType*/ \*name*/(\*args1*/) { if (\*name*/ != null) \*name*/.run(); else \*base*/(\*args2*/); }\n" +
          "final \*modifiers*/\*returnType*/ \*fallback*/(Runnable _f, \*args1*/) { if (_f != null) _f.run(); else \*base*/(); }\n" +
          "\*modifiers*/\*returnType*/ \*base*/(\*args1*/) {";
      else {
        // args, returning void
        S varType = "IVF\*l(args)*/<\*typeParams*/>"; 
        src = "\*mods*/ \*varType*/ \*name*/;\n" + 
          "\*modifiers*/\*returnType*/ \*name*/(\*args1*/) { if (\*name*/ != null) \*name*/.get(\*args2*/); else \*base*/(\*args2*/); }\n" +
          "final \*modifiers*/\*returnType*/ \*fallback*/(\*varType*/ _f, \*args1*/) { if (_f != null) _f.get(\*args2*/); else \*base*/(\*args2*/); }\n" +
          "\*modifiers*/\*returnType*/ \*base*/(\*args1*/) {";
      }
    else {
      // zero or more args, not returning void
      S varType = "IF\*l(args)*/<\*typeParams*/>";
      src = "\*mods*/ \*varType*/ \*name*/;\n" + 
        "\*modifiers*/\*returnType*/ \*name*/(\*args1*/) { ret \*name*/ != null ? \*name*/.get(\*args2*/) : \*base*/(\*args2*/); }\n" +
          "final \*modifiers*/\*returnType*/ \*fallback*/(\*varType*/ _f, \*args1*/) { ret _f != null ? _f.get(\*args2*/) : \*base*/(\*args2*/); }\n" +
        "\*modifiers*/\*returnType*/ \*base*/(\*args1*/) {";
    }
    replaceTokens_reTok(tok, iMod, iCurly+1, src);
  }
}

download  show line numbers  debug dex   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, xrpafgyirdlv

No comments. add comment

Snippet ID: #1026384
Snippet name: tok_swappableFunctions [v4, with complex return types, LIVE]
Eternal ID of this version: #1026384/37
Text MD5: 3ba414b64320b8a8233a0fa256f3bae3
Transpilation MD5: 6a0f9cc98e7dd1517dd03e8c196f0804
Author: stefan
Category: javax / linux
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-10-27 09:49:42
Source code size: 3254 bytes / 62 lines
Pitched / IR pitched: No / No
Views / Downloads: 171 / 367
Version history: 36 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1026384 & http://1026384.tinybrain.de