Libraryless. Click here for Pure Java version (523L/5K/14K).
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 | answerQuestionsOnPortAndConsole(stringfunc { |
11 | fallback(s, answer(s)) |
12 | }); |
13 | } |
14 | |
15 | static class Matches { |
16 | S[] m; |
17 | } |
18 | |
19 | static S fallback(S s, S answer) { |
20 | if (answer != null) return answer; |
21 | return "?"; |
22 | } |
23 | |
24 | static S answer(S s) { |
25 | if (match("on * say *", s)) { |
26 | rules.add(0, s); // add rule on top to override others |
27 | return "ok"; |
28 | } |
29 | |
30 | new Matches matches; |
31 | for (S rule : rules) { |
32 | if (match("on * say *", rule, matches)) { |
33 | S in = matches.m[0], out = matches.m[1]; |
34 | if (match(unquote(in), s)) |
35 | return unquote(out); |
36 | } |
37 | } |
38 | return null; |
39 | } |
40 | |
41 | static boolean match(S pat, S s) { |
42 | return match(pat, s, null); |
43 | } |
44 | |
45 | static boolean match(S pat, S s, Matches matches) { |
46 | L<S> tokpat = parse(pat), toks = parse(s); |
47 | S[] m = match2(tokpat, toks); |
48 | //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m)); |
49 | if (m == null) |
50 | return false; |
51 | else { |
52 | if (matches != null) matches.m = m; |
53 | return true; |
54 | } |
55 | } |
56 | |
57 | static L<S> parse(S s) { |
58 | return dropPunctuation(javaTokPlusPeriod(s)); |
59 | } |
60 | } |
Began life as a copy of #1001092
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: | #1001097 |
Snippet name: | Learning android |
Eternal ID of this version: | #1001097/1 |
Text MD5: | c97d5fea534f8c8054369b28ed5d4a4c |
Transpilation MD5: | f970aecec57cf69dc7090b5e34cdd9e4 |
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:14:39 |
Source code size: | 1316 bytes / 60 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 688 / 690 |
Referenced in: | [show references] |