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

!636
!quicktranslator
!class JavaTok

// expand shortened constructor syntax:
//   *(...) { ... }
// to
//   MyClass(...) { ... }

List<String> tok = JavaTok.split(s);
mainLoop: for (int i = 3; i+6 < tok.size(); i += 2) {
  String t = tok.get(i), l = tok.get(i-2);
  if (!t.equals("*"))
    continue;
  if (!tok.get(i+2).equals("("))
    continue;
  if (!(l.equals("}") || l.equals(";") || l.equals("{"))) // is this correct...??
    continue;
    
  // ok, it seems like a constructor declaration.
  // Now find class name by going backwards.
  
  int j = i, level = 1;
  while (j > 0 && level > 0) {
    t = tok.get(j);
    if (t.equals("}")) ++level;
    if (t.equals("{")) --level;
    j -= 2;
  }
  
  while (j > 0) {
    t = tok.get(j);
    if (t.equals("class")) {
      String className = tok.get(j+2);
      tok.set(i, className); // exchange constructor name!
      
      // now for the parameters.
      // Syntax: *(Learner *learner) {
      // We will surely add type inference here in time... :)
      
      j = i+2;
      while (!tok.get(j).equals("{"))
        j += 2;
      int block = j+1;
      for (int k = i+2; k < block-1; k += 2)
        if (tok.get(k).equals("*")) {
          tok.remove(k);
          tok.remove(k);
          block -= 2;
          String name = tok.get(k);
          tok.addAll(block, Arrays.asList(new String[] {
            "\n  ", "this", "", ".", "", name, " ", "=", " ", name, "", ";" }));
        }
      
      continue mainLoop;
    }
    j -= 2;
  }
}
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: 793 / 20110
Referenced in: [show references]