Libraryless. Click here for Pure Java version (1154L/9K/30K).
1 | !747 |
2 | !awt { |
3 | |
4 | m { |
5 | static new L<Task> tasks; |
6 | static new AIEngine engine; |
7 | |
8 | !include #1001107 // AIEngine |
9 | |
10 | static class Task { |
11 | S text; |
12 | Task forTask; |
13 | S state = "new"; |
14 | Throwable error; |
15 | |
16 | *(S *text) {} |
17 | |
18 | boolean runnable() { return false; } |
19 | void run() {} |
20 | |
21 | boolean ok() { return state.equals("done"); } |
22 | } |
23 | |
24 | static class Show extends Task { |
25 | Find find; |
26 | |
27 | *() { super("show java class"); } |
28 | |
29 | boolean runnable() { return find.result != null; } |
30 | |
31 | void run() { |
32 | //showText("A Java class", find.result); |
33 | JTextArea textArea = new JTextArea(find.result); |
34 | makeFrame("A Java class", new JScrollPane(textArea)); |
35 | engine.remember(engine.addObject(find.result) + " is visualized in " + engine.addObject(textArea)); |
36 | } |
37 | } |
38 | |
39 | static class Find extends Task { |
40 | S result; |
41 | |
42 | *() { super("find a java class"); } |
43 | |
44 | boolean runnable() { return true; } |
45 | |
46 | void run() ctex { |
47 | result = loadSnippet("#1001027"); // DerivedHashMap |
48 | S classID = engine.addObject(result); |
49 | engine.remember("the java class is " + classID); |
50 | } |
51 | } |
52 | |
53 | static class FindText extends Task { |
54 | JTextArea textArea; |
55 | |
56 | *() { super("find text"); } |
57 | |
58 | boolean runnable() { return true; } |
59 | |
60 | void run() { |
61 | textArea = engine.findObjectOfType(JTextArea.class); |
62 | if (textArea == null) |
63 | fail("no text area found"); |
64 | } |
65 | |
66 | S getText() { |
67 | return textArea.getText(); |
68 | } |
69 | |
70 | void updateText(final S text) { |
71 | awt { |
72 | textArea.setText(text); |
73 | } |
74 | } |
75 | } |
76 | |
77 | static class DeleteFirstLine extends Task { |
78 | new FindText find; |
79 | |
80 | *() { |
81 | super("delete first line"); |
82 | tasks.add(find); |
83 | } |
84 | |
85 | boolean runnable() { return find.ok(); } |
86 | |
87 | void run() { |
88 | S text = find.getText(); |
89 | int i = text.indexOf('\n'); |
90 | text = i >= 0 ? text.substring(i+1) : ""; |
91 | find.updateText(text); |
92 | } |
93 | } |
94 | |
95 | static class DeleteDoubleVariables extends Task { |
96 | new FindText find; |
97 | |
98 | *() { |
99 | super("delete double variables"); |
100 | tasks.add(find); |
101 | } |
102 | |
103 | boolean runnable() { return find.ok(); } |
104 | |
105 | void run() { |
106 | S text = find.getText(); |
107 | text = applyTranslator("#1001045", text); |
108 | find.updateText(text); |
109 | } |
110 | } |
111 | |
112 | static class ReplaceTwoDigitNumbersWithNothing extends Task { |
113 | new FindText find; |
114 | |
115 | *() { |
116 | super("replace bla"); |
117 | tasks.add(find); |
118 | } |
119 | |
120 | boolean runnable() { return find.ok(); } |
121 | |
122 | void run() { |
123 | S text = find.getText(); |
124 | text = text.replaceAll("\\d\\d", ""); |
125 | find.updateText(text); |
126 | } |
127 | } |
128 | |
129 | static class CountLines extends Task { |
130 | new FindText find; |
131 | int result; |
132 | |
133 | *() { |
134 | super("count lines"); |
135 | tasks.add(find); |
136 | } |
137 | |
138 | boolean runnable() { return find.ok(); } |
139 | |
140 | void run() { |
141 | S text = find.getText(); |
142 | result = toLines(text).size(); |
143 | } |
144 | } |
145 | |
146 | static S taskResult(Task t) { |
147 | return t.state.equals("new") ? "hm" : |
148 | t.state.equals("done") ? "ok" : |
149 | t.state.equals("error") ? "oops: " + t.error : |
150 | "hmhm? " + t.state; // impossible state |
151 | } |
152 | |
153 | p { |
154 | makeAndroid("This is the Java programming android.", stringfunc { |
155 | fallback(s, answer(s)) |
156 | }); |
157 | } |
158 | |
159 | static S fallback(S s, S answer) { |
160 | if (answer != null) return answer; |
161 | return "?"; |
162 | } |
163 | |
164 | static synchronized S answer(S s) { |
165 | if (match3("give me a java class", s) || match3("show me a java class", s)) { |
166 | new Show show; |
167 | new Find find; |
168 | show.find = find; |
169 | tasks.add(show); |
170 | tasks.add(find); |
171 | doStuff(); |
172 | return taskResult(show); |
173 | } |
174 | |
175 | if (match3("delete first line", s) || match3("delete the first line", s)) { |
176 | new DeleteFirstLine t; |
177 | process(t); |
178 | return taskResult(t); |
179 | } |
180 | |
181 | if (match3("count lines", s)) { |
182 | new CountLines t; |
183 | process(t); |
184 | return t.ok() ? t.result + " line(s)" : taskResult(t); |
185 | } |
186 | |
187 | if (match3("delete double variables", s)) { |
188 | new DeleteDoubleVariables t; |
189 | process(t); |
190 | return taskResult(t); |
191 | } |
192 | |
193 | if (match3("replace two digit numbers with nothing", s)) { |
194 | new ReplaceTwoDigitNumbersWithNothing t; |
195 | process(t); |
196 | return taskResult(t); |
197 | } |
198 | |
199 | return null; |
200 | } |
201 | |
202 | static void process(Task t) { |
203 | tasks.add(t); |
204 | doStuff(); |
205 | } |
206 | |
207 | static void showTasks() { |
208 | for (Task t: tasks) |
209 | print(t.state + ": " + t.text); |
210 | } |
211 | |
212 | static void doStuff() { |
213 | while (doStuff1()) {} |
214 | showTasks(); |
215 | engine.show(); |
216 | } |
217 | |
218 | static boolean doStuff1() { |
219 | boolean didAnything = false; |
220 | for (int i = 0; i < tasks.size(); i++) { |
221 | Task t = tasks.get(i); |
222 | if (!t.state.equals("new")) continue; |
223 | if (t.runnable()) { |
224 | didAnything = true; |
225 | try { |
226 | t.run(); |
227 | t.state = "done"; |
228 | } catch (Throwable e) { |
229 | e.printStackTrace(); |
230 | t.error = e; |
231 | t.state = "error"; |
232 | } |
233 | } |
234 | } |
235 | return didAnything; |
236 | } |
237 | |
238 | // called by match3 |
239 | static L<S> parse(S s) { |
240 | return dropPunctuation(javaTokPlusPeriod(s)); |
241 | } |
242 | |
243 | static S applyTranslator(S translatorID, S text) { |
244 | Class javax = getJavaX(); |
245 | File X = cast call(javax, "TempDirMaker_make"); |
246 | saveTextFile(new File(X, "main.java"), text); |
247 | X = (File) call(javax, "applyTranslator", X, translatorID, "", new ArrayList<File>()); |
248 | return readTextFile(new File(X, "main.java")); |
249 | } |
250 | } |
Began life as a copy of #1001102
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1001105 |
Snippet name: | Java programming android (developing) |
Eternal ID of this version: | #1001105/1 |
Text MD5: | 4100432220e23c005c93fc465c3119a4 |
Transpilation MD5: | a500ad514ac935a51c760472afa04ca3 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-09-23 15:44:51 |
Source code size: | 5785 bytes / 250 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 698 / 736 |
Referenced in: | [show references] |