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

102
LINES

< > BotCompany Repo | #1002105 // Cheesebase Bot (knows stuff about the "cheesebase" project)

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

Libraryless. Click here for Pure Java version (2093L/14K/47K).

1  
!752
2  
3  
static class Thing {
4  
  S name, type, file;
5  
}
6  
7  
static S masterURL = "https://github.com/mcheese/cheesebase/archive/master.zip";
8  
static File zipFile;
9  
10  
static new MultiMap<S, Thing> map;
11  
static new MultiMap<S, S> definitions;
12  
13  
p {
14  
  zipFile = getProgramFile("master.zip");
15  
  if (!zipFile.exists())
16  
    loadMaster();
17  
  analyze();
18  
  makeBot("Cheesebase Bot.");
19  
}
20  
21  
static void loadMaster() ctex {
22  
  saveBinaryFile(zipFile, loadBinaryPage(masterURL));
23  
  print("master.zip loaded from github.");
24  
}
25  
26  
static void analyze() {
27  
  findStuff("namespace", "class", "using", "template", "struct", "union", "auto");
28  
}
29  
30  
synchronized answer {
31  
  if (match("what is *", s, m)) {
32  
    S name = m.unq(0);
33  
    L<Thing> things = lookupPossiblyIgnoreCase(map, name);
34  
    L<S> defs = lookupPossiblyIgnoreCase(definitions, name);
35  
    if (!things.isEmpty() || !empty(defs)) {
36  
      L<S> types = list(getSortedSet(things, "type"));
37  
      L<S> files = list(getSortedSet(things, "file"));
38  
39  
      types = replace(types, "using", "alias");
40  
      types = replace(types, "auto", "variable");
41  
      
42  
      S theTypes = isEmpty(types) ? "unknown something" : join("/", types);
43  
      S a = (empty(things) ? name : things.get(0).name) + " is " + a(theTypes) + " in cheesebase. It is defined ";
44  
      if (!empty(defs))
45  
        a += "as " + join("/", quoteAll(defs)) + " ";
46  
      if (!empty(files)) {
47  
        a += "in ";
48  
        if (l(files) == 1)
49  
          a += "file " + quote(files.get(0));
50  
        else if (l(files) == 2)
51  
          a += "files " + quote(files.get(0)) + " and " + quote(files  .get(1));
52  
        else
53  
          a += quote(files.get(0)) + " and " + n(l(files)-1, " other file");
54  
      }
55  
      ret a + ".";
56  
    }
57  
  }
58  
  if (match("all things", s))
59  
    ret structure(map);
60  
  if (match("cheesebase reload", s)) {
61  
    loadMaster();
62  
    analyze();
63  
    ret "OK, reloaded from GitHub.";
64  
  }
65  
}
66  
67  
static void findStuff(S... keywords) {
68  
  map.clear();
69  
  definitions.clear();
70  
  
71  
  for (S file : listZip(zipFile)) {
72  
    if (endsWithOneOf(file, ".h", ".cc")) pcall {
73  
      S text = loadTextFileFromZip(zipFile, file);
74  
      
75  
      print("Scanning " + file);
76  
      L<S> tok = javaTok(text);
77  
      for (S keyword : keywords) {
78  
        int i = 0;
79  
        while ((i = findCodeTokens(tok, i+1, keyword, "<id>")) >= 0) {
80  
          new Thing t;
81  
          t.name = tok.get(i+2);
82  
          t.type = keyword;
83  
          t.file = dropPrefix("cheesebase-master/" , file);
84  
          map.put(t.name, t);
85  
        }
86  
      }
87  
      
88  
      definitions.putAll(findInitializers(text));
89  
    }
90  
  }
91  
}
92  
93  
static L replace(L in, O a, O b) {
94  
  new L l;
95  
  for (O x: in)
96  
    l.add(eq(x, a) ? b : x);
97  
  ret l;
98  
}
99  
100  
static S a(S s) {
101  
  ret ("aeiou".contains(substr(s,0, 1)) ? "an " : "a ")+s;
102  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002105
Snippet name: Cheesebase Bot (knows stuff about the "cheesebase" project)
Eternal ID of this version: #1002105/1
Text MD5: 495e8489f0f731cefaeb6ba6f5863120
Transpilation MD5: 54399904d550a7bbfdd60992170b0df4
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-12-20 22:53:25
Source code size: 2824 bytes / 102 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 761 / 1739
Referenced in: [show references]