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

1  
// Syntax: thread { doSomething(); }
2  
// Short for: new Thread() { public void run() { doSomething(); }}
3  
4  
!592 1000265 // auto-importer, standard function adder 
5  
6  
public class main {
7  
  public static void main(String[] args) throws Exception {
8  
    String s = loadMainJava();
9  
    
10  
    Pattern regex = Pattern.compile("thread\\s*\\{");
11  
    
12  
    for (int i = 0; i < 100; i++) {
13  
      Matcher matcher = regex.matcher(s);
14  
      if (!matcher.find())
15  
        break;
16  
17  
      int start = matcher.start(), end = matcher.end();
18  
      int endOfBlock = findEndOfBlock(s, end);
19  
      
20  
      String listener = "new Thread() {\n" +
21  
        "public void run() {\n" +
22  
        s.substring(end, endOfBlock) + "\n}";
23  
      s = s.substring(0, start) + listener + s.substring(endOfBlock);
24  
    }
25  
    
26  
    saveMainJava(s);
27  
  }
28  
  
29  
  // start is the index AFTER the opening bracket
30  
  // returns index OF closing bracket
31  
  static int findEndOfBlock(String s, int start) {
32  
    int level = 1;
33  
    for (int i = start; i < s.length(); i++) {
34  
      if (s.charAt(i) == '{') ++level;
35  
      else if (s.charAt(i) == '}') --level;
36  
      if (level == 0)
37  
        return i;
38  
    }
39  
    return s.length();
40  
  }
41  
}

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: 740 / 877
Referenced in: [show references]