Libraryless. Click here for Pure Java version (137L/2K/6K).
1 | // Syntax: thread { doSomething(); } |
2 | // Short for: new Thread() { public void run() { doSomething(); }}.start(); |
3 | // similar for daemon {} |
4 | |
5 | !592 1000265 // auto-importer, standard function adder |
6 | |
7 | public class main { |
8 | public static void main(String[] args) throws Exception { |
9 | String s = loadMainJava(); |
10 | |
11 | Pattern regex = Pattern.compile("(thread|daemon)\\s*\\{"); |
12 | |
13 | for (int i = 0; i < 100; i++) { |
14 | if (i == 99) System.out.println("Warning: 100 iterations!"); |
15 | Matcher matcher = regex.matcher(s); |
16 | if (!matcher.find()) |
17 | break; |
18 | |
19 | int start = matcher.start(), end = matcher.end(); |
20 | int endOfBlock = findEndOfBlock(s, end); |
21 | boolean daemon = matcher.group().startsWith("d"); |
22 | |
23 | String var = "_t_" + i; |
24 | String code = "Thread " + var + " = new Thread() {\n" + |
25 | "public void run() {\n" + |
26 | "try {\n" + |
27 | s.substring(end, endOfBlock) + "} catch (Exception _e) {\n" + |
28 | " throw _e instanceof RuntimeException ? (RuntimeException) _e : new RuntimeException(_e); } }\n};\n" + |
29 | (daemon ? var + ".setDaemon(true);\n" : "") + |
30 | var + ".start();"; |
31 | s = s.substring(0, start) + code + s.substring(endOfBlock+1); |
32 | } |
33 | |
34 | saveMainJava(s); |
35 | } |
36 | |
37 | // start is the index AFTER the opening bracket |
38 | // returns index OF closing bracket |
39 | static int findEndOfBlock(String s, int start) { |
40 | int level = 1; |
41 | for (int i = start; i < s.length(); i++) { |
42 | if (s.charAt(i) == '{') ++level; |
43 | else if (s.charAt(i) == '}') --level; |
44 | if (level == 0) |
45 | return i; |
46 | } |
47 | return s.length(); |
48 | } |
49 | } |
Began life as a copy of #673
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: | #721 |
Snippet name: | "thread {" and "daemon {" (JavaX translator, v2) |
Eternal ID of this version: | #721/1 |
Text MD5: | e682691166e3126473c50e01d9f2bb74 |
Transpilation MD5: | d9bc21e24b8d67fc91260897a61a1dff |
Author: | stefan |
Category: | |
Type: | JavaX translator |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-01-22 00:01:22 |
Source code size: | 1663 bytes / 49 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 667 / 16389 |
Referenced in: | [show references] |