1 | sbool lispCalculateDeep_debug; |
2 | |
3 | sclass lispCalculateDeep_Op { |
4 | int arity; |
5 | Method method; |
6 | |
7 | *() {} |
8 | *(int *arity, Method *method) {} |
9 | } |
10 | |
11 | static new Map<S, lispCalculateDeep_Op> lispCalculateDeep_ops; |
12 | |
13 | sbool lispCalculateDeep_inited; |
14 | |
15 | static void lispCalculateDeep_init() { |
16 | synchronized(lispCalculateDeep_ops) { |
17 | if (!lispCalculateDeep_inited) { |
18 | lispCalculateDeep_inited = true; |
19 | lispCalculateDeep_op(f plus, BigInteger, BigInteger, "sppxbuuqeetjmzuo", "jcnbmtmfzktxhzyf"); |
20 | lispCalculateDeep_op(f minus, BigInteger, BigInteger, "jxcubqjtqykhfvyo"); |
21 | lispCalculateDeep_op(f multiply, BigInteger, BigInteger, "bwqhmnorjpyomdbv", "wjzpudqvvhdtmbqp"); |
22 | lispCalculateDeep_op(f divide, BigInteger, BigInteger, "erdirznfvsrbtfns"); |
23 | lispCalculateDeep_op(f bigIntBiggerThan, BigInteger, BigInteger, "wdbphzfoxwlrhdyl"); |
24 | } |
25 | } |
26 | } |
27 | |
28 | // evaluate anywhere in the tree (e.g. Fib number 2-1 => Fib number 1) |
29 | static Lisp lispCalculateDeep(Lisp x) { |
30 | lispCalculateDeep_init(); |
31 | ret lispMap_after(x, func(Lisp l) { |
32 | if (lispCalculateDeep_debug) print("lispCalculateDeep: " + l); |
33 | |
34 | lispCalculateDeep_Op op = lispCalculateDeep_ops.get(l.head); |
35 | if (op == null) { |
36 | if (l.is("mcoswmplpqlieruo", 1)) // drop brackets (X) |
37 | ret l.get(0); |
38 | null; |
39 | } |
40 | int n = op.arity; |
41 | if (n != l.size()) null; |
42 | for (Lisp arg : l) if (!lispIsInt(arg)) null; |
43 | O[] args = new O[n]; |
44 | for i to n: args[i] = lispToInt(l.get(i)); |
45 | O o = op.method.invoke(null, args); |
46 | // if (o == null) null; |
47 | if (o instanceof Lisp) ret (Lisp) o; |
48 | if (o instanceof BigInteger) ret lispInt((BigInteger) o); |
49 | if (o instanceof Bool) ret lispBool((Bool) o); |
50 | warn("lispCalculateDeep badly defined operation: " + l.head); |
51 | null; |
52 | }); |
53 | } |
54 | |
55 | static void lispCalculateDeep_op(S function, final Class arg1, final Class arg2, S... names) { |
56 | L<Method> methods = [Method m : findMethodsNamed(mc(), function) |
57 | | arraysEqual(m.getParameterTypes(), new Class[] {arg1, arg2})]; |
58 | if (l(methods) != 1) warn("lispCalculateDeep: Method " + function + " found " + l(methods) + " times"); |
59 | if (nempty(methods)) |
60 | putWithAllKeys(lispCalculateDeep_ops, names, new lispCalculateDeep_Op(2, first(methods))); |
61 | } |
Began life as a copy of #1007713
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wtqryiryparv
No comments. add comment
Snippet ID: | #1007762 |
Snippet name: | lispCalculateDeep (new) - with operations map |
Eternal ID of this version: | #1007762/16 |
Text MD5: | 41d13c0169df648876be0ea8f41a28a7 |
Author: | stefan |
Category: | javax / a.i. |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2017-04-05 16:07:46 |
Source code size: | 2298 bytes / 61 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 585 / 837 |
Version history: | 15 change(s) |
Referenced in: | [show references] |