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

76
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 (6403L/38K).

1  
svoid tok_swappableFunctions(LS tok) {
2  
  jreplace(tok, "swappable static", "static swappable");
3  
  jreplace(tok, "pswappable <id>", "persistently swappable $2");
4  
  jreplace(tok, "persistent swappable <id>", "persistently swappable $3");
5  
  jreplace(tok, "transient swappable <id>", "swappable $3");
6  
7  
  int i;
8  
  while ((i = jfind_any(tok,
9  
    "swappable <id> <id>(", "swappable <id> <", "swappable <id>.", "swappable <id>[")) >= 0) {
10  
    // left-scan for modifiers
11  
    int iModEnd = i;
12  
    bool persistently = eqGet(tok, i-2, "persistently");
13  
    if (persistently) iModEnd -= 2;
14  
    int iMod = leftScanModifiers(tok, iModEnd);
15  
    S modifiers = joinSubList(tok, iMod, iModEnd);
16  
    
17  
    // scan annotations
18  
    int iAnnot = iMod;
19  
    bool atOverride = subListEquals(tok, iAnnot-4, "@", "", "Override");
20  
    if (atOverride) iAnnot -= 4;
21  
    
22  
    // scan return type, find function name
23  
    int iEndOfType = tok_findEndOfType(tok, i+2);
24  
    S returnType = joinSubList(tok, i+2, iEndOfType-1);
25  
    S name = assertIdentifier(tok.get(iEndOfType));
26  
    int iArgs = iEndOfType+2;
27  
    assertEquals("(", get(tok, iArgs));
28  
    
29  
    try {
30  
      int iCurly = indexOf(tok, iEndOfType, "{");
31  
      int iArgsEnd = lastIndexOf(tok, iCurly, ")");
32  
      assertTrue(iArgsEnd >= 0);
33  
      S throwsClauses = joinSubList(tok, iArgsEnd+1, iCurly-1); // TODO
34  
      LS tokArgs = subList(tok, iArgs+1, iArgsEnd);
35  
      LPairS args = tok_typesAndNamesOfParams(tokArgs);
36  
      S args1 = join(tokArgs);
37  
      S args2 = joinWithComma(pairsB(args));
38  
      bool isVoidMethod = eq(returnType, "void");
39  
      LS types = pairsA(args);
40  
      if (!isVoidMethod) types.add(returnType);
41  
      S typeParams = joinWithComma(map(type -> tok_varArgTypeToArray(tok_toNonPrimitiveTypes(type)), types)); // TODO: modifiers
42  
      S base = name + "_base", fallback = name + "_fallback";
43  
      S mods = modifiers + (persistently || containsJavaToken(modifiers, "static") ? "" : "transient ");
44  
      S mainFunctionModifiers = modifiers;
45  
      S baseFunctionModifiers = modifiers;
46  
      if (atOverride) mainFunctionModifiers = "@Override " + mainFunctionModifiers;
47  
      S src;
48  
      if (isVoidMethod)
49  
        if (empty(args))
50  
          // no args, returning void
51  
          src = mods + "Runnable \*name*/;\n" + 
52  
            "\*mainFunctionModifiers*/\*returnType*/ \*name*/(\*args1*/) { if (\*name*/ != null) \*name*/.run(); else \*base*/(\*args2*/); }\n" +
53  
            "final \*modifiers*/\*returnType*/ \*fallback*/(Runnable _f, \*args1*/) { if (_f != null) _f.run(); else \*base*/(); }\n" +
54  
            "\*baseFunctionModifiers*/\*returnType*/ \*base*/(\*args1*/) {";
55  
        else {
56  
          // args, returning void
57  
          S varType = "IVF\*l(args)*/<\*typeParams*/>"; 
58  
          src = "\*mods*/ \*varType*/ \*name*/;\n" + 
59  
            "\*mainFunctionModifiers*/\*returnType*/ \*name*/(\*args1*/) { if (\*name*/ != null) \*name*/.get(\*args2*/); else \*base*/(\*args2*/); }\n" +
60  
            "final \*modifiers*/\*returnType*/ \*fallback*/(\*varType*/ _f, \*args1*/) { if (_f != null) _f.get(\*args2*/); else \*base*/(\*args2*/); }\n" +
61  
            "\*baseFunctionModifiers*/\*returnType*/ \*base*/(\*args1*/) {";
62  
        }
63  
      else {
64  
        // zero or more args, not returning void
65  
        S varType = "IF\*l(args)*/<\*typeParams*/>";
66  
        src = "\*mods*/ \*varType*/ \*name*/;\n" + 
67  
          "\*mainFunctionModifiers*/\*returnType*/ \*name*/(\*args1*/) { ret \*name*/ != null ? \*name*/.get(\*args2*/) : \*base*/(\*args2*/); }\n" +
68  
            "final \*modifiers*/\*returnType*/ \*fallback*/(\*varType*/ _f, \*args1*/) { ret _f != null ? _f.get(\*args2*/) : \*base*/(\*args2*/); }\n" +
69  
          "\*baseFunctionModifiers*/\*returnType*/ \*base*/(\*args1*/) {";
70  
      }
71  
      replaceTokens_reTok(tok, iAnnot, iCurly+1, src);
72  
    } catch e {
73  
      fail("Was processing function " + name, e);
74  
    }
75  
  }
76  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1026384
Snippet name: tok_swappableFunctions [v4, with complex return types, LIVE]
Eternal ID of this version: #1026384/43
Text MD5: bf9453f567ebdb2508dbd67d33431f56
Transpilation MD5: be0c067582e81d8ef2864e23a2c3e0a2
Author: stefan
Category: javax / linux
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-03 18:20:29
Source code size: 3938 bytes / 76 lines
Pitched / IR pitched: No / No
Views / Downloads: 423 / 699
Version history: 42 change(s)
Referenced in: [show references]