Libraryless. Click here for Pure Java version (286L/2K/9K).
1 | !747 |
2 | |
3 | m {
|
4 | // variable assignments |
5 | static class Ass {
|
6 | new Map<S, O> map; |
7 | |
8 | void assign(S var, O value) {
|
9 | O v = map.get(var); |
10 | if (v == null) |
11 | map.put(var, value); |
12 | else if (!v.equals(value)) |
13 | fail("mismatch");
|
14 | } |
15 | } |
16 | |
17 | static class Pred {
|
18 | } |
19 | |
20 | static class Assign extends Pred {
|
21 | S var; |
22 | O value; |
23 | |
24 | *(S *var, O *value) {}
|
25 | } |
26 | |
27 | static class And extends Pred {
|
28 | new List<Pred> parts; |
29 | |
30 | *(Pred... parts) {
|
31 | this.parts = new ArrayList<Pred>(Arrays.asList(parts)); |
32 | } |
33 | } |
34 | |
35 | static class Or extends Pred {
|
36 | new List<Pred> parts; |
37 | |
38 | *(Pred... parts) {
|
39 | this.parts = new ArrayList<Pred>(Arrays.asList(parts)); |
40 | } |
41 | } |
42 | |
43 | static void eval(Pred p, Ass ass) {
|
44 | if (p instanceof Assign) {
|
45 | Assign a = cast p; |
46 | ass.assign(a.var, a.value); |
47 | } else if (p instanceof And) {
|
48 | And a = cast p; |
49 | for (Pred part : a.parts) |
50 | eval(part, ass); |
51 | } else |
52 | fail(structure(p)); |
53 | } |
54 | |
55 | static void shouldFail(Pred p) {
|
56 | try {
|
57 | eval(p, new Ass); |
58 | fail("did not fail");
|
59 | } catch (RuntimeException e) {
|
60 | print("OK (expected fail): " + structure(p));
|
61 | } |
62 | } |
63 | |
64 | static <X> List<X> singleton(X x) {
|
65 | new List<X> list; |
66 | list.add(x); |
67 | return list; |
68 | } |
69 | |
70 | static abstract class Iter {
|
71 | abstract Ass next(); |
72 | } |
73 | |
74 | static class Singleton extends Iter {
|
75 | Ass value; |
76 | |
77 | *(Ass *value) {}
|
78 | |
79 | Ass next() {
|
80 | if (value != null) {
|
81 | Ass v = value; value = null; return v; |
82 | } |
83 | return null; |
84 | } |
85 | } |
86 | |
87 | static class OrIterator extends Iter {
|
88 | Or o; |
89 | int i; |
90 | |
91 | *(Or *o) {}
|
92 | |
93 | public Ass next() {
|
94 | if (i >= o.parts.size()) return null; |
95 | new Ass ass; |
96 | eval(o.parts.get(i++), ass); |
97 | return ass; |
98 | } |
99 | } |
100 | |
101 | static Iter multiEval(Pred p) {
|
102 | if (p instanceof Or) {
|
103 | Or o = cast p; |
104 | return new OrIterator(o); |
105 | } else {
|
106 | new Ass ass; |
107 | eval(p, ass); |
108 | return new Singleton(ass); |
109 | } |
110 | } |
111 | |
112 | p {
|
113 | new Ass ass; |
114 | eval(new Assign("x", new Integer(1)), ass);
|
115 | print(structure(ass)); |
116 | |
117 | ass = new Ass; |
118 | eval(new And( |
119 | new Assign("x", new Integer(1)),
|
120 | new Assign("y", new Integer(2))), ass);
|
121 | print(structure(ass)); |
122 | |
123 | shouldFail(new And( |
124 | new Assign("x", new Integer(1)),
|
125 | new Assign("x", new Integer(2))));
|
126 | |
127 | // test multiEval |
128 | |
129 | L<Ass> list = all(multiEval(new Assign("a", new Integer(5))));
|
130 | assertEquals(1, list.size()); |
131 | print(structure(list)); |
132 | |
133 | list = all(multiEval(new Or( |
134 | new Assign("a", new Integer(5)),
|
135 | new Assign("a", new Integer(7)))));
|
136 | assertEquals(2, list.size()); |
137 | print(structure(list)); |
138 | } |
139 | |
140 | static L<Ass> all(Iter it) {
|
141 | new L<Ass> l; |
142 | while (true) {
|
143 | Ass ass = it.next(); |
144 | if (ass != null) |
145 | l.add(ass); |
146 | else |
147 | break; |
148 | } |
149 | return l; |
150 | } |
151 | |
152 | static void assertEquals(O x, O y) {
|
153 | if (!(x == null ? y == null : x.equals(y))) |
154 | fail(structure(x) + " != " + structure(y)); |
155 | } |
156 | } |
Note the liberal use of the moniker "ass" for "assignments".
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1000772 |
| Snippet name: | Prolog spike |
| Eternal ID of this version: | #1000772/1 |
| Text MD5: | 213582425da6b60ef181e1d1a40fe32f |
| Transpilation MD5: | f00a46c11777799598801651451e8635 |
| Author: | stefan |
| Category: | |
| Type: | JavaX source code |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2015-09-09 16:57:10 |
| Source code size: | 3238 bytes / 156 lines |
| Pitched / IR pitched: | No / Yes |
| Views / Downloads: | 872 / 968 |
| Referenced in: | [show references] |