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

103
LINES

< > BotCompany Repo | #1001155 // Compiler Bot with outdated caching (still used for building x*.jar)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (7004L/49K/157K).

1  
!7
2  
3  
m {
4  
  // md5 to class dir
5  
  // class dirs are in {user.home}/.javax, so they might get deleted
6  
  // after a day. we check for dir existence when using the cache.
7  
  static new Map<S, File> cache;
8  
  
9  
  p {
10  
    readLocally("cache");
11  
    makeAndroid("Compiler bot!");
12  
  }
13  
  
14  
  static synchronized S answer(S s) {
15  
    //print("Compile bot processing: " + shorten(s, 200));
16  
    //print(shorten(structure(parse3(s)), 200));
17  
    //print(structure(parse3("Please compile this Java text: *")));
18  
19  
    new Matches m;
20  
    if (match3("Please compile this JavaX snippet: *", s, m)) {
21  
      S snippetID = unquote(m.m[0]);
22  
      S transpiledSrc = getServerTranspiled(snippetID);
23  
      int i = transpiledSrc.indexOf('\n');
24  
      String libs = transpiledSrc.substring(0, Math.max(0, i));
25  
      transpiledSrc = transpiledSrc.substring(i+1);
26  
      return compile(transpiledSrc);
27  
    }
28  
    
29  
    if (match3("Please compile this Java text: *", s, m)) {
30  
      String src = unquote(m.m[0]);
31  
      return compile(src);
32  
    }
33  
34  
    if (match3("Please compile this Java text: * for java version *", s, m)) {
35  
      String src = unquote(m.m[0]);
36  
      String target = unquote(m.m[1]);
37  
      return compile(src, target);
38  
    }
39  
40  
    return null;
41  
  }
42  
  
43  
  static synchronized S compile(S src) {
44  
    return compile(src, null);
45  
  }
46  
  
47  
  static synchronized S compile(S src, S javaTarget) {
48  
    S md5 = md5(src);
49  
    File classesDir = cache.get(md5);
50  
    if (classesDir == null || !classesDir.isDirectory()) {
51  
      Class j = getJavaX();
52  
      setOpt(j, "javaTarget", javaTarget);
53  
      //setOpt(j, "verbose", true);
54  
      File srcDir = cast call(j, "TempDirMaker_make");
55  
      L<S> tok = javaTok(src);
56  
      
57  
      S className = null;
58  
      if (tok.contains("package"))
59  
        splitJavaFiles(tok, srcDir);
60  
      else {
61  
        className = getNameOfPublicClass(tok);
62  
        if (className == null) className = getNameOfAnyClass(tok);
63  
        S fileName = className + ".java";
64  
        saveTextFile(new File(srcDir, fileName), src);
65  
      }
66  
      classesDir = (File) call(j, "TempDirMaker_make");
67  
      new L<File> libraries;
68  
      try {
69  
        print("Compiler said: " + call(j, "compileJava", srcDir, libraries, classesDir));
70  
        
71  
        // sanity test
72  
        if (!tok.contains("package"))
73  
          if (!new File(classesDir, className + ".class").exists())
74  
            fail("No class generated (" + className + ")");
75  
      } catch (Exception e) {
76  
        e.printStackTrace();
77  
        return "Compile Error. " + getOpt(j, "javaCompilerOutput");
78  
      }
79  
      
80  
      cache.put(md5, classesDir); // cache on success only
81  
      saveLocally("cache");
82  
    } else
83  
      print("Getting classes from cache.");
84  
    
85  
    return "ok, " + quote(classesDir.getAbsolutePath());
86  
  }
87  
  
88  
  static S getNameOfPublicClass(L<S> tok) {
89  
    for (L<S> c : allClasses(tok))
90  
      if (modifiersContain(c, "public"))
91  
        return getClassDeclarationName(c);
92  
    return null;
93  
  }
94  
  
95  
  static L<S> modifiers = litlist("static", "abstract", "public", "private", "protected", "final", "native", "volatile", "synchronized", "transient");
96  
97  
  static boolean modifiersContain(L<S> tok, S modifier) {
98  
    for (int i = 1; i < tok.size() && modifiers.contains(tok.get(i)); i += 2)
99  
      if (tok.get(i).equals(modifier))
100  
        return true;
101  
    return false;
102  
  }
103  
}

Author comment

Began life as a copy of #1001150

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001155
Snippet name: Compiler Bot with outdated caching (still used for building x*.jar)
Eternal ID of this version: #1001155/5
Text MD5: c88f278f4c5d54efbf45c4bd4da385c4
Transpilation MD5: ce3c6e539e3d142bf13bef39f9c2313f
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-06-28 23:01:42
Source code size: 3404 bytes / 103 lines
Pitched / IR pitched: No / No
Views / Downloads: 798 / 5139
Version history: 4 change(s)
Referenced in: [show references]