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

41
LINES

< > BotCompany Repo | #673 // "thread {" (JavaX translator)

JavaX source code - run with: x30.jar

// Syntax: thread { doSomething(); }
// Short for: new Thread() { public void run() { doSomething(); }}

!592 1000265 // auto-importer, standard function adder 

public class main {
  public static void main(String[] args) throws Exception {
    String s = loadMainJava();
    
    Pattern regex = Pattern.compile("thread\\s*\\{");
    
    for (int i = 0; i < 100; i++) {
      Matcher matcher = regex.matcher(s);
      if (!matcher.find())
        break;

      int start = matcher.start(), end = matcher.end();
      int endOfBlock = findEndOfBlock(s, end);
      
      String listener = "new Thread() {\n" +
        "public void run() {\n" +
        s.substring(end, endOfBlock) + "\n}";
      s = s.substring(0, start) + listener + s.substring(endOfBlock);
    }
    
    saveMainJava(s);
  }
  
  // start is the index AFTER the opening bracket
  // returns index OF closing bracket
  static int findEndOfBlock(String s, int start) {
    int level = 1;
    for (int i = start; i < s.length(); i++) {
      if (s.charAt(i) == '{') ++level;
      else if (s.charAt(i) == '}') --level;
      if (level == 0)
        return i;
    }
    return s.length();
  }
}

Author comment

Began life as a copy of #614

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, qbtsjoyahagl, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #673
Snippet name: "thread {" (JavaX translator)
Eternal ID of this version: #673/1
Text MD5: bb74f7f6ff5e8d629f6682b1f5f80f79
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-07-16 16:45:59
Source code size: 1204 bytes / 41 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 739 / 875
Referenced in: [show references]