1 | // Logic thread |
2 | |
3 | static interface IEngine {
|
4 | public boolean yo(Native n); |
5 | public void memorize(Lisp statement); |
6 | public void forget(Lisp statement); |
7 | } |
8 | |
9 | static class LThread {
|
10 | IEngine engine; |
11 | new L<Lisp> statements; |
12 | new SNLMatches m; // current variable assignments |
13 | L<Lisp> ops; |
14 | int opIdx; |
15 | new L<Lisp> output; |
16 | L<S> log; |
17 | |
18 | *(Lisp code) {
|
19 | this(null, code); |
20 | } |
21 | |
22 | *(IEngine *engine, Lisp code) {
|
23 | ops = snlSplitOps(code); |
24 | } |
25 | |
26 | boolean done() {
|
27 | ret opIdx >= l(ops); |
28 | } |
29 | |
30 | // returns false if failed |
31 | boolean step() {
|
32 | Lisp code = ops.get(opIdx++); |
33 | boolean ok = stepImpl(code); |
34 | if (log != null) |
35 | log.add((ok ? "[OK] ": "[FAIL] ") + " step " + snlFromTree(code) + " with " + structure(m)); |
36 | ret ok; |
37 | } |
38 | |
39 | void say(Lisp b) {
|
40 | output.add(snlApply(b, m.map)); |
41 | } |
42 | |
43 | boolean stepImpl(Lisp code) {
|
44 | if (code.isic("if *", "and *", "assume *")) {
|
45 | Lisp a = code.get(0); |
46 | Lisp b = snlApply(a, m.map); |
47 | |
48 | if (query(b)) |
49 | ret true; |
50 | |
51 | // Not matched, fail |
52 | |
53 | if (code.isic("assume *"))
|
54 | say(snlToTree("assumption < failed < : < [" + snlFromTree(b) + "]"));
|
55 | |
56 | ret false; |
57 | } |
58 | |
59 | if (code.isic("say *", "then *")) {
|
60 | Lisp b = code.get(0); |
61 | //print ("m: " + structure (m) + ", b: " + structure (b));
|
62 | output.add(snlApply(b, m.map)); |
63 | ret true; |
64 | } |
65 | |
66 | if (code.isic("memorize *")) {
|
67 | if (engine != null) {
|
68 | Lisp b = snlApply(code.get(0), m.map); |
69 | engine.memorize(b); |
70 | } |
71 | ret true; |
72 | } |
73 | |
74 | if (code.isic("forget *")) {
|
75 | if (engine != null) {
|
76 | Lisp b = snlApply(code.get(0), m.map); |
77 | engine.forget(b); |
78 | } |
79 | ret true; |
80 | } |
81 | |
82 | // unknown code |
83 | say(snlToTree("unknown < instruction < : < [" + snlFromTree(code) + "]"));
|
84 | ret false; |
85 | } |
86 | |
87 | // true if succeeded, false if failed |
88 | boolean run() {
|
89 | while (!done()) |
90 | if (!step()) {
|
91 | print("fail");
|
92 | ret false; |
93 | } |
94 | ret true; |
95 | } |
96 | |
97 | boolean query(Lisp b) {
|
98 | // try all statements in context |
99 | |
100 | for (Lisp s : statements) {
|
101 | /* if (snlMatch2(b, s, m)) */ |
102 | |
103 | if (snlMatch2(s, b, new SNLMatches)) |
104 | ret true; |
105 | } |
106 | |
107 | // try the low-level engine |
108 | |
109 | try {
|
110 | if (engine != null) {
|
111 | Native n = new Native(b); |
112 | if (engine.yo(n)) |
113 | ret m.addAll(n.m); |
114 | } |
115 | } catch (Exception e) {
|
116 | |
117 | if (log != null) {
|
118 | log.add("ERROR: " + getStackTrace(e));
|
119 | print(e); |
120 | } |
121 | } |
122 | |
123 | ret false; |
124 | } |
125 | } |
Began life as a copy of #1002750
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1002752 |
| Snippet name: | LThread with SNLMatches and Engine link (CURRENT) |
| Eternal ID of this version: | #1002752/1 |
| Text MD5: | 5e1a9b7b58a78b2d6c697a217c844a04 |
| Author: | stefan |
| Category: | eleu / nl |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2016-02-26 05:32:34 |
| Source code size: | 2707 bytes / 125 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 876 / 1430 |
| Referenced in: | [show references] |