Libraryless. Click here for Pure Java version (980L/7K/23K).
1 | !747 |
2 | !multi-line strings |
3 | |
4 | m {
|
5 | static L<S> rules = litlist( |
6 | [[ on "hello", say "hello you too" ]] |
7 | ); |
8 | |
9 | p {
|
10 | readLocally("rules");
|
11 | answerQuestionsOnPortAndConsole(stringfunc {
|
12 | fallback(s, answer(s)) |
13 | }); |
14 | } |
15 | |
16 | static class Matches {
|
17 | S[] m; |
18 | } |
19 | |
20 | static S fallback(S s, S answer) {
|
21 | if (answer != null) return answer; |
22 | return "?"; |
23 | } |
24 | |
25 | static synchronized S answer(S s) {
|
26 | if (match("on * say *", s)) {
|
27 | rules.add(0, s); // add rule on top to override others |
28 | saveLocally("rules");
|
29 | return "ok"; |
30 | } |
31 | |
32 | new Matches matches; |
33 | for (S rule : rules) {
|
34 | if (match("on * say *", rule, matches)) {
|
35 | S in = matches.m[0], out = matches.m[1]; |
36 | if (match(unquote(in), s)) |
37 | return unquote(out); |
38 | } |
39 | } |
40 | return null; |
41 | } |
42 | |
43 | static boolean match(S pat, S s) {
|
44 | return match(pat, s, null); |
45 | } |
46 | |
47 | static boolean match(S pat, S s, Matches matches) {
|
48 | L<S> tokpat = parse(pat), toks = parse(s); |
49 | S[] m = match2(tokpat, toks); |
50 | //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m)); |
51 | if (m == null) |
52 | return false; |
53 | else {
|
54 | if (matches != null) matches.m = m; |
55 | return true; |
56 | } |
57 | } |
58 | |
59 | static L<S> parse(S s) {
|
60 | return dropPunctuation(javaTokPlusPeriod(s)); |
61 | } |
62 | } |
Began life as a copy of #1001097
download show line numbers debug dex old transpilations
Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xinetxnxrdbb
No comments. add comment
| Snippet ID: | #1001100 |
| Snippet name: | Learning android with persistence |
| Eternal ID of this version: | #1001100/1 |
| Text MD5: | ac2bf8fb449c4daf461478065f610bda |
| Transpilation MD5: | a4ffd7a356704a9ca6ae9d2751227ec4 |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX source code |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2015-09-20 23:57:57 |
| Source code size: | 1385 bytes / 62 lines |
| Pitched / IR pitched: | No / Yes |
| Views / Downloads: | 1054 / 1153 |
| Referenced in: | [show references] |