Libraryless. Click here for Pure Java version (2348L/18K/53K).
1 | !636 |
2 | !modern |
3 | |
4 | main { |
5 | static S programID; |
6 | |
7 | psvm { |
8 | if (args.length == 0) { |
9 | print("Hello, I'm The Learner."); |
10 | L<S> log = loadLog(); |
11 | print("Last log entries:"); |
12 | for (int i = Math.max(0, log.size()-2); i < log.size(); i++) |
13 | print(" " + log.get(i)); |
14 | return; |
15 | } |
16 | |
17 | S a = args[0]; |
18 | |
19 | List<S> prog = null, intent = null; |
20 | |
21 | if (a == "mappings") { |
22 | print(structure(allMappings())); |
23 | return; |
24 | } |
25 | |
26 | if (a == "good" || a == "ok") { |
27 | L<S> log = loadLog(); |
28 | /*for (int i = 0; i < log.size(); i++) |
29 | print(i + ": " + log.get(i));*/ |
30 | if (log.size() < 2) confused("Too few log lines."); |
31 | prog = (List) unstructure(log.get(log.size()-1)); |
32 | intent = (List) unstructure(log.get(log.size()-2)); |
33 | print("prog: " + prog); |
34 | print("intent: " + intent); |
35 | if (!prog.get(0).equals("program")) fail("prog: wrong type"); |
36 | if (!intent.get(0).equals("intent")) fail("intent: wrong type"); |
37 | S p = prog.get(1), i = intent.get(1); |
38 | |
39 | if (a != "ok") { |
40 | print("map? (type 'ok')"); |
41 | } else { |
42 | S map = findMapping(i); |
43 | if (map != null) |
44 | print("Remapping from " + map); |
45 | if (p.equals(map)) |
46 | print("Same mapping exists, skipping."); |
47 | else { |
48 | print("Mapping."); |
49 | addMapping(intent.get(1), prog.get(1)); |
50 | map = findMapping(i); |
51 | boolean sane = p.equals(map); |
52 | print("Done. Sanity check " + (sane ? "OK" : "not OK")); |
53 | } |
54 | } |
55 | } else if (isSnippetID(a)) { |
56 | logProgramRun(args); |
57 | Class c = hotwire(a); |
58 | callMain(c, drop(1, args)); |
59 | } else { |
60 | boolean ok = args[args.length-1].equals("ok"); |
61 | if (ok) args = dropLast(args, 1); |
62 | logIntent(args); |
63 | S i = smartJoin(args); |
64 | S map = findMapping(i); |
65 | if (map == null) |
66 | print("No mapping found for intent"); |
67 | else { |
68 | if (ok) { |
69 | print("Invoking program: #" + map); |
70 | S[] _args = map.split(" "); |
71 | Class c = hotwire(_args[0]); |
72 | callMain(c, drop(1, _args)); |
73 | } else { |
74 | print("Suggested program: #" + map); |
75 | print("Type " + quote(i + " ok") + " to invoke."); |
76 | } |
77 | } |
78 | } |
79 | } |
80 | |
81 | static S logFile() { |
82 | return new File(userHome(), ".javax/" + programID + "/learnerlog.txt").getPath(); |
83 | } |
84 | |
85 | static S mapFile() { |
86 | return new File(userHome(), ".javax/" + programID + "/learnermap.txt").getPath(); |
87 | } |
88 | |
89 | static S smartJoin(S[] list) { |
90 | return join(" ", list); |
91 | } |
92 | |
93 | static void logIntent(S[] args) { |
94 | stringLogAppend(structure(new Object[] {"intent", smartJoin(args)})); |
95 | print("Logged intent: " + smartJoin(args)); |
96 | } |
97 | |
98 | static void logProgramRun(S[] args) { |
99 | stringLogAppend(structure(new Object[] {"program", smartJoin(args)})); |
100 | } |
101 | |
102 | static void stringLogAppend(S s) { |
103 | appendToFile(logFile(), "\n" + s + "\n"); |
104 | } |
105 | |
106 | static void appendToFile(String path, String s) ctex { |
107 | new File(path).getParentFile().mkdirs(); |
108 | print("[Logging to " + path + "]"); |
109 | Writer writer = new BufferedWriter(new OutputStreamWriter( |
110 | new FileOutputStream(path, true), "UTF-8")); |
111 | writer.write(s); |
112 | writer.close(); |
113 | } |
114 | |
115 | static L<S> loadLog() ctex { |
116 | return toLinesFullTrim(readTextFile(logFile(), "")); |
117 | } |
118 | |
119 | static class Consumer { |
120 | L<S> list; |
121 | int i; |
122 | |
123 | *(L<S> *list) {} |
124 | |
125 | S get() { return i < list.size() ? list.get(i) : null; } |
126 | void next() { if (i < list.size()) ++i; } |
127 | void next(int n) { i = Math.min(list.size(), i+n); } |
128 | boolean has() { return i < list.size(); } |
129 | boolean is(S s) { return s.equals(get()); } |
130 | boolean isNot(S s) { return !s.equals(get()); } |
131 | } |
132 | |
133 | static O unstructure(S s) { |
134 | L<S> tok = javaTok(s); |
135 | Consumer con = new Consumer(tok); |
136 | con.next(); // skip whitespace |
137 | return unstructure(con); |
138 | } |
139 | |
140 | static O unstructure(Consumer con) { |
141 | S t = con.get(); |
142 | if (t.startsWith("\"")) { |
143 | con.next(2); |
144 | return unquote(t); |
145 | } |
146 | |
147 | if (t.equals("null")) { |
148 | con.next(2); |
149 | return null; |
150 | } |
151 | |
152 | if (t.equals("{")) { |
153 | new L list; |
154 | con.next(2); |
155 | while (con.has() && con.isNot("}")) { |
156 | if (!con.has()) break; |
157 | if (con.is(",")) |
158 | con.next(2); |
159 | O o = unstructure(con); |
160 | list.add(o); |
161 | } |
162 | if (con.is("}")) con.next(2); |
163 | return list; |
164 | } |
165 | |
166 | throw todo(t); |
167 | } |
168 | |
169 | static void addMapping(S intent, S prog) { |
170 | S s = structure(new Object[] {"map", intent, prog}); |
171 | appendToFile(mapFile(), "\n" + s + "\n"); |
172 | } |
173 | |
174 | static Map<S, S> allMappings() ctex { |
175 | L<S> list = toLinesFullTrim(readTextFile(mapFile(), "")); |
176 | new Map<S, S> map; |
177 | for (int i = list.size()-1; i >= 0; i--) { // start from end to get newest mappings |
178 | S s = list.get(i); |
179 | List x = cast unstructure(s); |
180 | if (x.get(0).equals("map") && !map.containsKey(x.get(1))) |
181 | map.put((S) x.get(1), (S) x.get(2)); |
182 | } |
183 | return map; |
184 | } |
185 | |
186 | static S findMapping(S intent) ctex { |
187 | L<S> list = toLinesFullTrim(readTextFile(mapFile(), "")); |
188 | for (int i = list.size()-1; i >= 0; i--) { // start from end to get newest mappings |
189 | S s = list.get(i); |
190 | List x = cast unstructure(s); |
191 | if (x.get(0).equals("map") && x.get(1).equals(intent)) |
192 | return (S) x.get(2); |
193 | } |
194 | return null; |
195 | } |
196 | } |
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: | #745 |
Snippet name: | Learner (developing) |
Eternal ID of this version: | #745/1 |
Text MD5: | 349f74dc7cc207238881967da3a92ba8 |
Transpilation MD5: | 475d67d53e58f6797922788de024346d |
Author: | stefan |
Category: | |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-08-23 19:29:52 |
Source code size: | 5604 bytes / 196 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 673 / 1484 |
Referenced in: | [show references] |