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.

static L<S> tok_expandStarConstructors(L<S> tok) {
  int n = tok.size();
  mainLoop: for (int i = 3; i < n-6; i += 2) {
    S t = tok.get(i);
    if (!t.equals("*"))
      continue;
    S l = tok.get(i-2);
    if (!tok.get(i+2).equals("("))
      continue;
      
    if (!eqOneOf(l, "}", "public", "private", "protected", ";", "{", "endif", "endifdef", "endifndef", ">", "")
      && neq(get(tok, i-4), "ifclass")){
      print("Skipping star constructor: " + quote(l));
      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;
    }
    
    // search for class name
    while (j > 0) {
      t = tok.get(j);
      S className;
      if (eqOneOf(t, "class", "record", "srecord", "noeq", "enum")
        && isIdentifier(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;
        
        // skip calling super/this constructor
        if (eqGetOneOf(tok, j+2, "super", "this")) j = tok_findEndOfStatement(tok, j+2)-1;
        
        // find * arguments (copy to fields of same name)
        int block = j+1;
        for (int k = i+2; k < block-1; k += 2)
          if (tok.get(k).equals("*")) {
            removeSubList(tok, k, k+2);
            block -= 2;
            S name = tok.get(k);
            tok.addAll(block, Arrays.asList(new String[] {
              "\n  ", "this", "", ".", "", name, " ", "=", " ", name, "", ";" }));
          }
        
        continue mainLoop;
      }
      j -= 2;
    }
  }
  ret tok;
}

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: 260 / 386
Version history: 10 change(s)
Referenced in: [show references]