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

45
LINES

< > BotCompany Repo | #1024937 // tok_questionDot - uses rCallF in the transpilation

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

Libraryless. Click here for Pure Java version (4923L/29K).

svoid tok_questionDot(LS tok) {
  jreplace_dyn(tok, "->?.", -> {
    S var = makeVar();
    ret var + " -> " + var + "?.";
  });
  
  jreplace(tok, "if (<id> ?. <id)", "if ($3 != null && $3.$6)");
  
  int i;
  while ((i = jfind(tok, "<id> ?.")) >= 0) {
    S var = tok.get(i);
    bool isExpression = eqGetOneOf(tok, i-2, "ret", "return", ",", "=", "(", "?", "+", ">"); // too few
    if (!isExpression) {
      int j = tok_findEndOfStatement(tok, i)-1;
      tokAppend_reTok(tok, j, " }");
      tokReplace_reTok(tok, i, i+6, "{ if (\*var*/ != null) \*var*/.");
    } else {
      S expr = "\*var*/ == null ? null : \*var*/.";
      bool needBrackets = eqGet(tok, i-2, "+");
      if (needBrackets) {
        expr = "(" + expr;
        int j = tok_endOfExpression(tok, i)-1;
        tokPrepend_reTok(tok, j, ")");
      }
      tokReplace_reTok(tok, i, i+6, expr);
    }
  }
    
  jreplace_dyn(tok, "<id>()?.",
    func(LS tok, int i) -> S {
      int iQ = indexOf(tok, i, "?");
      S expr = joinSubList(tok, i, iQ-1);
      bool isExpression = eqGetOneOf(tok, i-2, "ret", "return", ",", "=", "(", "?"); // too few
      S var = makeVar();
      if (!isExpression) {
        int j = tok_findEndOfStatement(tok, i)-1;
        tokAppend_reTok(tok, j, " }");
        ret "{ var \*var*/ = \*expr*/; if (\*var*/ != null) \*var*/.";
      } else {  // experimental
        int j = tok_findEndOfExpression(tok, i)-1;
        tokAppend_reTok(tok, j, ")");
        ret "rCallF(\*expr*/, \*var*/ -> \*var*/ == null ?: \*var*/.";
      }
    });
}

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: #1024937
Snippet name: tok_questionDot - uses rCallF in the transpilation
Eternal ID of this version: #1024937/28
Text MD5: 753af11b05aa42fd0e673242d48edcfa
Transpilation MD5: 89d2cf0b6c1e498528e77cf3e3322b56
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-18 05:06:45
Source code size: 1585 bytes / 45 lines
Pitched / IR pitched: No / No
Views / Downloads: 351 / 547
Version history: 27 change(s)
Referenced in: [show references]