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).

1  
svoid tok_questionDot(LS tok) {
2  
  jreplace_dyn(tok, "->?.", -> {
3  
    S var = makeVar();
4  
    ret var + " -> " + var + "?.";
5  
  });
6  
  
7  
  jreplace(tok, "if (<id> ?. <id)", "if ($3 != null && $3.$6)");
8  
  
9  
  int i;
10  
  while ((i = jfind(tok, "<id> ?.")) >= 0) {
11  
    S var = tok.get(i);
12  
    bool isExpression = eqGetOneOf(tok, i-2, "ret", "return", ",", "=", "(", "?", "+", ">"); // too few
13  
    if (!isExpression) {
14  
      int j = tok_findEndOfStatement(tok, i)-1;
15  
      tokAppend_reTok(tok, j, " }");
16  
      tokReplace_reTok(tok, i, i+6, "{ if (\*var*/ != null) \*var*/.");
17  
    } else {
18  
      S expr = "\*var*/ == null ? null : \*var*/.";
19  
      bool needBrackets = eqGet(tok, i-2, "+");
20  
      if (needBrackets) {
21  
        expr = "(" + expr;
22  
        int j = tok_endOfExpression(tok, i)-1;
23  
        tokPrepend_reTok(tok, j, ")");
24  
      }
25  
      tokReplace_reTok(tok, i, i+6, expr);
26  
    }
27  
  }
28  
    
29  
  jreplace_dyn(tok, "<id>()?.",
30  
    func(LS tok, int i) -> S {
31  
      int iQ = indexOf(tok, i, "?");
32  
      S expr = joinSubList(tok, i, iQ-1);
33  
      bool isExpression = eqGetOneOf(tok, i-2, "ret", "return", ",", "=", "(", "?"); // too few
34  
      S var = makeVar();
35  
      if (!isExpression) {
36  
        int j = tok_findEndOfStatement(tok, i)-1;
37  
        tokAppend_reTok(tok, j, " }");
38  
        ret "{ var \*var*/ = \*expr*/; if (\*var*/ != null) \*var*/.";
39  
      } else {  // experimental
40  
        int j = tok_findEndOfExpression(tok, i)-1;
41  
        tokAppend_reTok(tok, j, ")");
42  
        ret "rCallF(\*expr*/, \*var*/ -> \*var*/ == null ?: \*var*/.";
43  
      }
44  
    });
45  
}

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: 355 / 551
Version history: 27 change(s)
Referenced in: [show references]