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

63
LINES

< > BotCompany Repo | #1023533 // tok_expandStarConstructors

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

Transpiled version (5225L) is out of date.

1  
static L<S> tok_expandStarConstructors(L<S> tok) {
2  
  int n = tok.size();
3  
  mainLoop: for (int i = 3; i < n-6; i += 2) {
4  
    S t = tok.get(i);
5  
    if (!t.equals("*"))
6  
      continue;
7  
    S l = tok.get(i-2);
8  
    if (!tok.get(i+2).equals("("))
9  
      continue;
10  
      
11  
    if (!eqOneOf(l, "}", "public", "private", "protected", ";", "{", "endif", "endifdef", "endifndef", ">", "")
12  
      && neq(get(tok, i-4), "ifclass")){
13  
      print("Skipping star constructor: " + quote(l));
14  
      continue;
15  
    }
16  
      
17  
    // ok, it seems like a constructor declaration.
18  
    // Now find class name by going backwards.
19  
    
20  
    int j = i, level = 1;
21  
    while (j > 0 && level > 0) {
22  
      t = tok.get(j);
23  
      if (t.equals("}")) ++level;
24  
      if (t.equals("{")) --level;
25  
      j -= 2;
26  
    }
27  
    
28  
    // search for class name
29  
    while (j > 0) {
30  
      t = tok.get(j);
31  
      S className;
32  
      if (eqOneOf(t, "class", "record", "srecord", "noeq", "enum")
33  
        && isIdentifier(className = tok.get(j+2))) {
34  
        tok.set(i, className); // exchange constructor name!
35  
        
36  
        // now for the parameters.
37  
        // Syntax: *(Learner *learner) {
38  
        // We will surely add type inference here in time... :)
39  
        
40  
        j = i+2;
41  
        while (!tok.get(j).equals("{")) j += 2;
42  
        
43  
        // skip calling super/this constructor
44  
        if (eqGetOneOf(tok, j+2, "super", "this")) j = tok_findEndOfStatement(tok, j+2)-1;
45  
        
46  
        // find * arguments (copy to fields of same name)
47  
        int block = j+1;
48  
        for (int k = i+2; k < block-1; k += 2)
49  
          if (tok.get(k).equals("*")) {
50  
            removeSubList(tok, k, k+2);
51  
            block -= 2;
52  
            S name = tok.get(k);
53  
            tok.addAll(block, Arrays.asList(new String[] {
54  
              "\n  ", "this", "", ".", "", name, " ", "=", " ", name, "", ";" }));
55  
          }
56  
        
57  
        continue mainLoop;
58  
      }
59  
      j -= 2;
60  
    }
61  
  }
62  
  ret tok;
63  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1023533
Snippet name: tok_expandStarConstructors
Eternal ID of this version: #1023533/11
Text MD5: 10ce34f5c0412510fbf692edfae61110
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-14 17:39:15
Source code size: 1992 bytes / 63 lines
Pitched / IR pitched: No / No
Views / Downloads: 267 / 396
Version history: 10 change(s)
Referenced in: [show references]