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