1 | // class for linking Java methods to NL |
2 | static class Native {
|
3 | Lisp snl; |
4 | SNLMatches m; |
5 | SNLMatches trans; |
6 | boolean printExceptions; |
7 | int exceptions; |
8 | L<S> ins, strings; |
9 | |
10 | *() {} // good to have
|
11 | |
12 | *(Lisp *snl) {}
|
13 | |
14 | // assume it's a Lisp structure from another bot - |
15 | // restructure to import it |
16 | /* *(O o) {
|
17 | snl = (Lisp) restructure(o); |
18 | }*/ |
19 | |
20 | *(S s) { snl = snlToTree(s); }
|
21 | |
22 | boolean match(S pat) {
|
23 | m = new SNLMatches; |
24 | trans = new SNLMatches; |
25 | Lisp tree = snlToTree_cached(pat); |
26 | ins = new L; |
27 | strings = new L; |
28 | tree = findInVariables(tree, ins, strings); |
29 | //print("tree now: " + tree);
|
30 | if (!snlMatch2(tree, snl, trans)) |
31 | ret false; |
32 | for (S in : ins) |
33 | if (get(in) == null) |
34 | ret false; |
35 | for (S string : strings) |
36 | if (get(string) == null || !get(string).isEmpty()) |
37 | ret false; |
38 | ret true; |
39 | } |
40 | |
41 | Lisp findInVariables(Lisp tree, L<S> ins, L<S> strings) {
|
42 | if (tree == null) ret tree; |
43 | |
44 | if (tree.isEmpty()) {
|
45 | if (tree.head.startsWith("in S ")) {
|
46 | S var = dropPrefix("in S ", tree.head);
|
47 | if (startsWithUpperCase(var)) {
|
48 | if (!strings.contains(var)) |
49 | strings.add(var); |
50 | ret lisp(var); |
51 | } |
52 | } |
53 | |
54 | if (tree.head.startsWith("in ")) {
|
55 | S var = dropPrefix("in ", tree.head);
|
56 | if (startsWithUpperCase(var)) {
|
57 | if (!ins.contains(var)) |
58 | ins.add(var); |
59 | ret lisp(var); |
60 | } |
61 | } |
62 | |
63 | ret tree; |
64 | } else {
|
65 | // recurse |
66 | Lisp lisp = new Lisp(tree.head); |
67 | for (Lisp child : tree) |
68 | lisp.add(findInVariables(child, ins, strings)); |
69 | ret lisp; |
70 | } |
71 | } |
72 | |
73 | Lisp get(S var) {
|
74 | Lisp val = trans.get(var); |
75 | if (!isVar(val)) ret val; |
76 | ret getOuter(val.raw()); |
77 | } |
78 | |
79 | // if only one in var |
80 | Lisp get() {
|
81 | ret get(anyInVar()); |
82 | } |
83 | |
84 | // ditto |
85 | S str() {
|
86 | ret str(anyInVar()); |
87 | } |
88 | |
89 | S anyInVar() {
|
90 | if (!empty(ins)) ret first(ins); |
91 | ret first(strings); |
92 | } |
93 | |
94 | boolean isVar(Lisp l) {
|
95 | ret l != null && l.isEmpty() && startsWithUpperCase(l.raw()); |
96 | } |
97 | |
98 | // called from outside native |
99 | Lisp getOuter(S var) {
|
100 | ret m.get(var); |
101 | } |
102 | |
103 | // called from inside native |
104 | S str(S var) {
|
105 | Lisp val = get(var); |
106 | if (val == null) fail("variable " + quote(var) + " not set");
|
107 | if (!val.isEmpty()) fail("variable " + quote(var) + " not a string: " + struct(val));
|
108 | ret unquote(val.raw()); |
109 | } |
110 | |
111 | S strOuter(S var) {
|
112 | Lisp val = getOuter(var); |
113 | if (val == null) fail("variable " + quote(var) + " not set");
|
114 | if (!val.isEmpty()) fail("variable " + quote(var) + " not a string: " + struct(val));
|
115 | ret unquote(val.raw()); |
116 | } |
117 | |
118 | // may throw an exception if variable problem (already set to different value) |
119 | void set(S var, O val) {
|
120 | if (val instanceof List) val = structure(val); // TODO |
121 | Lisp lisp = val instanceof Lisp ? (Lisp) val : lisp(quote(main.str(val))); |
122 | Lisp outer = trans.get(var); |
123 | if (isVar(outer)) {
|
124 | if (!m.put(trans.raw(var), lisp)) |
125 | fail(); |
126 | } else |
127 | if (!eq(lisp, outer)) {
|
128 | //print("neq " + lisp + " != " + outer);
|
129 | fail(); |
130 | } |
131 | } |
132 | |
133 | boolean callExternal(O bot) {
|
134 | try {
|
135 | O copy = newObject(main.getClass(bot, "main$Native"), quickExport(snl, bot)); |
136 | //O copy = quickExport(this, bot); |
137 | if (!isTrue(callOpt(bot, "yo", copy))) |
138 | ret false; |
139 | |
140 | m = (SNLMatches) quickImport(main.get(copy, "m")); |
141 | ret true; |
142 | } catch (Exception e) {
|
143 | handle(e); |
144 | ret false; |
145 | } |
146 | } |
147 | |
148 | boolean callExternal(Method yo) {
|
149 | try {
|
150 | Class c = yo.getDeclaringClass(); |
151 | //print("Declaring class: " + c + " (" + identityHashCode(c) + ")");
|
152 | O copy = newObject(main.getClass(c, "main$Native"), quickExport(snl, c)); |
153 | if (!isTrue(yo.invoke(null, copy))) |
154 | ret false; |
155 | |
156 | m = (SNLMatches) quickImport(main.get(copy, "m")); |
157 | ret true; |
158 | } catch (Exception e) {
|
159 | handle(e); |
160 | ret false; |
161 | } |
162 | } |
163 | |
164 | void handle(Throwable t) {
|
165 | if (printExceptions) |
166 | printStackTrace(t); |
167 | ++exceptions; |
168 | } |
169 | } |
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1002774 |
| Snippet name: | class Native (include) |
| Eternal ID of this version: | #1002774/4 |
| Text MD5: | f5482417f092686a9e48129c662587da |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2018-05-02 09:08:32 |
| Source code size: | 4310 bytes / 169 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 1064 / 2226 |
| Version history: | 3 change(s) |
| Referenced in: | [show references] |