-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconsteval.d
More file actions
258 lines (244 loc) · 7.53 KB
/
consteval.d
File metadata and controls
258 lines (244 loc) · 7.53 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Written in the D programming language
// License: http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0
module ast.consteval;
import ast.lexer: Location, Tok, TokenType;
import ast.expression;
import ast.type;
import util;
import util.maybe;
private Expression make(ℤ v){
return LiteralExp.makeInteger(v);
}
private Expression make(int v){
return make(ℤ(v));
}
private Expression make(TokenType subop)(Location loc, Expression sub1) {
import ast.semantic_: minusType;
assert(isNumericTy(sub1.type));
Expression subty;
static if(subop == Tok!"-") {
subty = minusType(sub1.type);
} else {
static assert(0);
}
assert(subty);
assert(isNumericTy(subty));
auto r = new UnaryExp!subop(sub1);
r.loc = loc;
r.type = subty;
r.setSemCompleted();
return r.eval();
}
private Expression make(TokenType subop)(Location loc, Expression sub1, Expression sub2) {
import ast.semantic_: arithmeticType, subtractionType, nSubType, cmpType;
assert(isNumericTy(sub1.type));
assert(isNumericTy(sub2.type));
Expression subty;
static if(subop == Tok!"+") {
subty = arithmeticType!false(sub1.type, sub2.type);
} else static if(subop == Tok!"-") {
subty = subtractionType(sub1.type, sub2.type);
} else static if(subop == Tok!"sub") {
subty = nSubType(sub1.type, sub2.type);
} else static if(subop == Tok!"·") {
subty = arithmeticType!true(sub1.type, sub2.type);
} else static if(subop == Tok!"=" || subop == Tok!"≠") {
subty = cmpType!true(sub1.type, sub2.type);
} else {
static assert(0);
}
assert(subty);
assert(isNumericTy(subty));
auto r = new BinaryExp!subop(sub1, sub2);
r.loc = loc;
r.type = subty;
r.setSemCompleted();
return r.eval();
}
Expression evalNumericBinop(TokenType op: Tok!"+")(Location loc, Expression ne1, Maybe!ℤ v1, Expression ne2, Maybe!ℤ v2) {
if(v1 && v2) return make(v1.get() + v2.get());
if(v1 && v1.get() == 0) return ne2;
if(v2 && v2.get() == 0) return ne1;
if(v1 && !v2) return make!(Tok!"+")(loc, ne2, ne1);
if(v2 && v2.get() < 0) {
return make!(Tok!"-")(loc, ne1, make(-v2.get()));
}
static foreach(sub1;[Tok!"-",Tok!"sub"]){
if(auto se1 = cast(BinaryExp!sub1)ne1){
static foreach(sub2;[Tok!"-",Tok!"sub"]){
if(auto se2 = cast(BinaryExp!sub2)ne2){
auto nb0 = make!(Tok!"+")(loc, se1.e1, se2.e1);
auto nb1 = make!sub1(loc, nb0, se1.e2);
auto nb2 = make!sub2(loc, nb1, se2.e2);
return nb2;
}
}
auto v12 = se1.e2.asIntegerConstant();
if(v12 && v2) {
return make!sub1(loc, se1.e1, make(v12.get() - v2.get()));
}
if(ne2.isDeterministic() && se1.e2 == ne2) return se1.e1;
}
}
if(v2) {
if(auto ae1 = cast(BinaryExp!(Tok!"+"))ne1){
auto v12 = ae1.e2.asIntegerConstant();
if(v12) {
return make!op(loc, ae1.e1, make(v12.get() + v2.get()));
}
}
}
if(auto ae2 = cast(BinaryExp!(Tok!"+"))ne2){
return make!(Tok!"+")(loc, make!(Tok!"+")(loc, ne1, ae2.e1), ae2.e2);
}
if(ne1.isDeterministic()) {
if(ne1 == ne2){
return make!(Tok!"·")(loc, make(2), ne2);
}
static foreach(sub2;[Tok!"-",Tok!"sub"]){
if(auto se2 = cast(BinaryExp!sub2)ne2){
if(se2.e2 == ne1) return se2.e1;
}
}
}
return evalNumericSum!op(loc, ne1, ne2);
}
Expression evalNumericBinop(TokenType op)(Location loc, Expression ne1, Maybe!ℤ v1, Expression ne2, Maybe!ℤ v2) if(op == Tok!"-" || op == Tok!"sub") {
if(v1 && v2) return make(v1.get() - v2.get());
if(ne1.isDeterministic() && ne1 == ne2) return make(0);
if(v2){
if(v2.get() == 0) return ne1;
if(v2.get() < 0){
return make!(Tok!"+")(loc, ne1, make(-v2.get()));
}
}
if(v1 && v1.get() == 0) return make!(Tok!"-")(loc, ne2);
static if(op == Tok!"-")
if(auto se2 = cast(UnaryExp!(Tok!"-")) ne2) {
return make!(Tok!"+")(loc, ne1, se2.e);
}
if(ne2.isDeterministic()) {
if(auto ae1 = cast(BinaryExp!(Tok!"+"))ne1){
if(ae1.e1 == ne2) return ae1.e2;
if(ae1.e2 == ne2) return ae1.e1;
auto v12 = ae1.e2.asIntegerConstant();
if(v12 && v2){
return make!op(loc, ae1.e1, make(v2.get() - v12.get()));
}
if(auto ae2 = cast(BinaryExp!(Tok!"+"))ne2){
if(ae1.e1 == ae2.e1)
return make!op(loc, ae1.e2, ae2.e2);
}
}
static foreach(sub;[Tok!"-",Tok!"sub"]){
if(auto se1 = cast(BinaryExp!sub)ne1){
if(se1.e1 == ne2) return make!(Tok!"-")(loc, se1.e2);
// TODO: if(se1.e2 == -ne2)
}
}
}
if(ne1.isDeterministic()) {
if(auto ae2 = cast(BinaryExp!(Tok!"+"))ne2){
if(ae2.e1 == ne1) return make!(Tok!"-")(loc, ae2.e2);
if(ae2.e2 == ne1) return make!(Tok!"-")(loc, ae2.e1);
}
static foreach(sub;[Tok!"-",Tok!"sub"]){
if(auto se2 = cast(BinaryExp!sub)ne2){
if(se2.e1 == ne1) return se2.e2.eval();
// TODO: if(se2.e2==-ne1
}
}
}
static foreach(sub1;[Tok!"-",Tok!"sub"]){
if(auto se1 = cast(BinaryExp!sub1)ne1){
if(auto ae = cast(BinaryExp!(Tok!"+"))se1.e1){
if(ne2.isDeterministic()){
if(ae.e1 == ne2) return make!sub1(loc, ae.e2, se1.e2);
if(ae.e2 == ne2) return make!sub1(loc, ae.e1, se1.e2);
}
}
}
}
static foreach(sub2;[Tok!"-",Tok!"sub"]){
if(auto se2 = cast(BinaryExp!sub2)ne2){
return make!(Tok!"-")(loc, make!(Tok!"+")(loc, ne1, se2.e2), se2.e1);
}
}
return evalNumericSum!op(loc, ne1, ne2);
}
Expression evalNumericBinop(TokenType op: Tok!"·")(Location loc, Expression ne1, Maybe!ℤ v1, Expression ne2, Maybe!ℤ v2) {
if(v1 && v2) return make(v1.get() * v2.get());
if(v1 && v1.get() == 0) return ne1;
if(v2 && v2.get() == 0) return ne2;
if(v1 && v1.get() == 1) return ne2;
if(v2 && v2.get() == 1) return ne1;
if(cast(LiteralExp)ne2 && !cast(LiteralExp)ne1) {
return make!op(loc, ne2, ne1);
}
if(v1){
static foreach(sop;[Tok!"+",Tok!"-",Tok!"sub"]){
if(auto se2 = cast(BinaryExp!sop)ne2){
if(auto v = se2.e2.asIntegerConstant()){
return make!sop(loc,
make!(Tok!"·")(loc, ne1, se2.e1),
make(v1.get() * v.get()));
}
}
}
}
return null;
}
Expression evalNumericBinop(TokenType op: Tok!"^")(Location loc, Expression ne1, Maybe!ℤ v1, Expression ne2, Maybe!ℤ v2) {
if(v2 && v2.get() == 0) return make(1);
if(v2 && v2.get() == 1) return ne1;
if(v1 && v1.get() == 1) return make(1);
if(v1 && v2 && v2.get() >= 0) {
return make(pow(v1.get(), v2.get()));
}
return null;
}
Expression evalNumericSum(TokenType op)(Location loc, Expression ne1, Expression ne2) if(op == Tok!"+" || op == Tok!"-" || op == Tok!"sub") {
if(auto me1=cast(BinaryExp!(Tok!"·"))ne1){
if(auto le1=cast(LiteralExp)me1.e1){
if(ne2.isDeterministic() && me1.e2==ne2){
auto a = make!op(loc, le1, make(1));
return make!(Tok!"·")(loc, a, me1.e2);
}
if(auto me2=cast(BinaryExp!(Tok!"·"))ne2){
if(auto le2=cast(LiteralExp)me2.e1){
if(me1.e2.isDeterministic() && me1.e2==me2.e2){
auto a = make!op(loc, le1, le2);
return make!(Tok!"·")(loc, a, me1.e2);
}
}
}
}
}
if(auto me2=cast(BinaryExp!(Tok!"·"))ne2){
if(auto le2=cast(LiteralExp)me2.e1){
if(ne1.isDeterministic() && me2.e2==ne1){
auto a = make!op(loc, make(1), le2);
return make!(Tok!"·")(loc, a, ne1);
}
}
}
return null;
}
Expression evalNumericBinop(TokenType op)(Location loc, Expression ne1, Maybe!ℤ v1, Expression ne2, Maybe!ℤ v2) if(op == Tok!"=" || op == Tok!"≠") {
if(v1 && v2) {
return make((v1.get() == v2.get()) ^ (op != Tok!"="));
}
if(ne1.isDeterministic() && ne1 == ne2) {
return make(op == Tok!"=");
}
if(cast(LiteralExp)ne1 && !cast(LiteralExp)ne2) {
return make!op(loc, ne2, ne1);
}
if(!v2) {
import ast.semantic_: subtractionType;
if(subtractionType(ne1.type, ne2.type)) {
return make!op(loc, make!(Tok!"-")(loc, ne1, ne2), make(0));
}
}
return null;
}