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

60
LINES

< > BotCompany Repo | #697 // "*" constructor syntax (translator, v2)

JavaX translator [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (1775L/11K/26K).

1  
!636
2  
!quicktranslator
3  
!class JavaTok
4  
5  
// expand shortened constructor syntax:
6  
//   *(...) { ... }
7  
// to
8  
//   MyClass(...) { ... }
9  
10  
List<String> tok = JavaTok.split(s);
11  
mainLoop: for (int i = 3; i+6 < tok.size(); i += 2) {
12  
  String t = tok.get(i), l = tok.get(i-2);
13  
  if (!t.equals("*"))
14  
    continue;
15  
  if (!tok.get(i+2).equals("("))
16  
    continue;
17  
  if (!(l.equals("}") || l.equals(";") || l.equals("{"))) // is this correct...??
18  
    continue;
19  
    
20  
  // ok, it seems like a constructor declaration.
21  
  // Now find class name by going backwards.
22  
  
23  
  int j = i, level = 1;
24  
  while (j > 0 && level > 0) {
25  
    t = tok.get(j);
26  
    if (t.equals("}")) ++level;
27  
    if (t.equals("{")) --level;
28  
    j -= 2;
29  
  }
30  
  
31  
  while (j > 0) {
32  
    t = tok.get(j);
33  
    if (t.equals("class")) {
34  
      String className = tok.get(j+2);
35  
      tok.set(i, className); // exchange constructor name!
36  
      
37  
      // now for the parameters.
38  
      // Syntax: *(Learner *learner) {
39  
      // We will surely add type inference here in time... :)
40  
      
41  
      j = i+2;
42  
      while (!tok.get(j).equals("{"))
43  
        j += 2;
44  
      int block = j+1;
45  
      for (int k = i+2; k < block-1; k += 2)
46  
        if (tok.get(k).equals("*")) {
47  
          tok.remove(k);
48  
          tok.remove(k);
49  
          block -= 2;
50  
          String name = tok.get(k);
51  
          tok.addAll(block, Arrays.asList(new String[] {
52  
            "\n  ", "this", "", ".", "", name, " ", "=", " ", name, "", ";" }));
53  
        }
54  
      
55  
      continue mainLoop;
56  
    }
57  
    j -= 2;
58  
  }
59  
}
60  
s = JavaTok.join(tok);

Author comment

Began life as a copy of #696

download  show line numbers  debug dex  old transpilations   

Travelled to 22 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, dhtvkmknsjym, gwrvuhgaqvyk, ishqpsrjomds, jlatgrcjtklg, jtubtzbbkimh, kmhbujppghqa, liwcxgsjrgqn, lpdgvwnxivlt, mqqgnosmbjvj, mrjhfnjfopze, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #697
Snippet name: "*" constructor syntax (translator, v2)
Eternal ID of this version: #697/1
Text MD5: 9c824be59837e8a84844e653f54865c5
Transpilation MD5: 7df7c057d6e981b39c915474aa9c32a5
Author: stefan
Category: javax
Type: JavaX translator
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-01-21 17:27:54
Source code size: 1586 bytes / 60 lines
Pitched / IR pitched: No / No
Views / Downloads: 730 / 20022
Referenced in: [show references]