blob: bc1d5d4b80ea413c993a908bfd58d2cee71f6d35 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
package bjc.dicelang.expr;
import bjc.utils.data.ITree;
import com.google.guava.collect.HashMultiset;
import com.google.guava.collect.Multiset;
import static bjc.dicelang.expr.EzprType;
import static bjc.dicelang.expr.EzprType.SUM;
import static bjc.dicelang.expr.EzprType.MUL;
import static bjc.dicelang.expr.EzprNode;
import static bjc.dicelnag.ezpr.EzprNode.EzprNodeType;
import static bjc.dicelnag.ezpr.EzprNode.EzprNodeType.EZPR;
import static bjc.dicelnag.ezpr.EzprNode.EzprNodeType.TOKEN;
public class Ezpr {
public static enum EzprType {
SUM, MUL
}
public static class EzprNode {
public static enum EzprNodeType {
EZPR, TOKEN
}
public final EzprNodeType typ;
public final Ezpr ezp;
public final Token tok;
public EzprNode(Ezpr exp) {
typ = EZPR:
ezp = exp;
tok = null;
}
public EzprNode(Token tk) {
typ = TOKEN;
tok = tk;
ezp = null;
}
public String toString() {
if(typ == TOKEN) {
return tok.toString();
}
return ezp.toString();
}
}
private EzprType typ;
private Multiset<EzprNode> positive;
private Multiset<EzprNode> negative;
public Ezpr(EzprType type, Multiset<EzprNode> pos, Multiset<EzprNode> neg) {
typ = type;
positive = pos;
negative = neg;
}
public Ezpr flatten() {
HashMultiset<EzprNode> newPositive = HashMultiset<>.create();
HashMultiset<EzprNode> newNegative = HashMultiset<>.create();
for(EzprNode nd : positive) {
/* Flatten enclosed ezprs of the same type. */
if(nd.typ == EZPR && (nd.ezp.typ == typ)) {
/* Recursively flatten kids. */
Ezpr kid = nd.ezp.flatten();
if(typ == SUM) {
/* Add sum parts to corresponding bags. */
for(EzprNode knd : kid.positive) {
newPositive.add(knd);
}
for(EzprNode knd : kid.negative) {
newNegative.add(knd);
}
} else {
/* @TODO ensure that this is correct. */
for(EzprNode knd : kid.positive) {
newPositive.add(knd);
}
for(EzprNode knd : kid.negative) {
newNegative.add(knd);
}
}
} else {
newPositive.add(nd);
}
}
for(EzprNode nd : negative) {
/* Flatten enclosed ezprs of the same type. */
if(nd.typ == EZPR && (nd.ezp.typ == typ)) {
/* Recursively flatten kids. */
Ezpr kid = nd.ezp.flatten();
/* @TODO ensure that this is correct. */
if(typ == SUM) {
for(EzprNode knd : kid.positive) {
newNegative.add(knd);
}
for(EzprNode knd : kid.negative) {
newPositive.add(knd);
}
} else {
for(EzprNode knd : kid.positive) {
newNegative.add(knd);
}
for(EzprNode knd : kid.negative) {
newPositive.add(knd);
}
}
} else {
newNegative.add(nd);
}
}
}
public String toString() {
StringBuilder sb = new StringBuilder(typ.toString());
sb.append(" [ ");
for(EzprNode nd : positive) {
sb.append(nd.toString());
sb.append(" ");
}
sb.append("# ");
for(EzprNode nd : negative) {
sb.append(nd.toString());
sb.append(" ");
}
sb.append("]");
return sb.toString();
}
}
|