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

43
LINES

< > BotCompany Repo | #1000805 // "awt {" (JavaX translator)

JavaX translator [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (131L/2K/5K).

// Syntax: awt { doSomethingInAwtThread(); }

!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("(awt)\\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);
      boolean daemon = matcher.group().startsWith("d");
      
      String code = "SwingUtilities.invokeLater(new Runnable() {\n" +
        "public void run() {\n" +
        "try {\n" +
        s.substring(end, endOfBlock) + "} catch (Exception _e) {\n" +
          "  throw _e instanceof RuntimeException ? (RuntimeException) _e : new RuntimeException(_e); } }\n});";
      s = s.substring(0, start) + code + s.substring(endOfBlock+1);
    }
    
    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 #721

download  show line numbers  debug dex  old transpilations   

Travelled to 18 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, dhtvkmknsjym, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000805
Snippet name: "awt {" (JavaX translator)
Eternal ID of this version: #1000805/1
Text MD5: f4eb02052da300952c1a7052261b9b5d
Transpilation MD5: 9004a9598c3a2e5f528a2280f9b495c3
Author: stefan
Category:
Type: JavaX translator
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-01-29 18:38:35
Source code size: 1382 bytes / 43 lines
Pitched / IR pitched: No / No
Views / Downloads: 849 / 4816
Referenced in: #636 - Resolve symbolic translator invocations
#1000795 - swing-top - List running programs (works)
#1000818 - Console
#1000916 - slte function
#1000985 - swing-top - List running programs & bots (old)
#1001188 - "pcall {" (JavaX translator, NOT the one used)
#1001464 - OS Sensor
#1001468 - OS Sensor 2: OS Sensor with Memory
#1001492 - Self-moving OS Sensor
#1001652 - Remote Bots Up?
#1001655 - Central Server Up?
#1001669 - File size watcher (big text)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)