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.

/*
  swap method o.add(a, b) to {
    ret super + " (added)";
  }
  
  or: swap method o.add(a, b) {
    ret super + " (added)";
  }
  =>
    var add_old = cc.add;
    o.add = (a, b) -> {
      ret o.add_fallback(add_old, a, b) + " (added)";
    };
*/

svoid tok_swapFunctions(LS tok) {
  int i;
  while ((i = jfind(tok, "swap method <id>.<id>(")) >= 0) {
    S obj = tok.get(i+4);
    S methodName = tok.get(i+8);
    int iOpening = i+10;
    int iClosing = indexOf(tok, i, ")");
    S args = joinSubList(tok, iOpening+2, iClosing-1);
    int iOpeningCurly = iClosing+2;
    if (eqGet(tok, iOpeningCurly, "to")) iOpeningCurly += 2;
    assertEquals(get(tok, iOpeningCurly), "{");
    int iClosingCurly = tok_findEndOfBlock(tok, iOpeningCurly)-1;
    
    S add_old = makeVar();
    
    LS tokBody = cloneList(cncSubList(tok, iOpeningCurly+1, iClosingCurly));
    bool needsSuper = jreplace_dyn(tokBody, "super",
      (_tok, nIdx) -> !eqGet(_tok, nIdx+3, "."),
      -> "\*obj*/.\*methodName*/_fallback(\*add_old*/, \*args*/)");
      
    S assignment =
        "\*obj*/.\*methodName*/ = (\*args*/) -> { " + join(tokBody) + "};";
    
    S src = needsSuper ?
      "{ var \*add_old*/ = \*obj*/.\*methodName*/; \n"
        + assignment + "\n}"
      : assignment;
      
    replaceTokens_reTok(tok, i, iClosingCurly+1, src);
  }
}

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: 21 / 65
Version history: 6 change(s)
Referenced in: [show references]