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

46
LINES

< > BotCompany Repo | #1035554 // tok_swapFunctions

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

Transpiled version (10255L) is out of date.

1  
/*
2  
  swap method o.add(a, b) to {
3  
    ret super + " (added)";
4  
  }
5  
  
6  
  or: swap method o.add(a, b) {
7  
    ret super + " (added)";
8  
  }
9  
  =>
10  
    var add_old = cc.add;
11  
    o.add = (a, b) -> {
12  
      ret o.add_fallback(add_old, a, b) + " (added)";
13  
    };
14  
*/
15  
16  
svoid tok_swapFunctions(LS tok) {
17  
  int i;
18  
  while ((i = jfind(tok, "swap method <id>.<id>(")) >= 0) {
19  
    S obj = tok.get(i+4);
20  
    S methodName = tok.get(i+8);
21  
    int iOpening = i+10;
22  
    int iClosing = indexOf(tok, i, ")");
23  
    S args = joinSubList(tok, iOpening+2, iClosing-1);
24  
    int iOpeningCurly = iClosing+2;
25  
    if (eqGet(tok, iOpeningCurly, "to")) iOpeningCurly += 2;
26  
    assertEquals(get(tok, iOpeningCurly), "{");
27  
    int iClosingCurly = tok_findEndOfBlock(tok, iOpeningCurly)-1;
28  
    
29  
    S add_old = makeVar();
30  
    
31  
    LS tokBody = cloneList(cncSubList(tok, iOpeningCurly+1, iClosingCurly));
32  
    bool needsSuper = jreplace_dyn(tokBody, "super",
33  
      (_tok, nIdx) -> !eqGet(_tok, nIdx+3, "."),
34  
      -> "\*obj*/.\*methodName*/_fallback(\*add_old*/, \*args*/)");
35  
      
36  
    S assignment =
37  
        "\*obj*/.\*methodName*/ = (\*args*/) -> { " + join(tokBody) + "};";
38  
    
39  
    S src = needsSuper ?
40  
      "{ var \*add_old*/ = \*obj*/.\*methodName*/; \n"
41  
        + assignment + "\n}"
42  
      : assignment;
43  
      
44  
    replaceTokens_reTok(tok, i, iClosingCurly+1, src);
45  
  }
46  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035554
Snippet name: tok_swapFunctions
Eternal ID of this version: #1035554/7
Text MD5: 1a9f845618d74cd7562c66d51ae75d08
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-06-17 00:20:56
Source code size: 1379 bytes / 46 lines
Pitched / IR pitched: No / No
Views / Downloads: 75 / 133
Version history: 6 change(s)
Referenced in: [show references]