-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.d
454 lines (426 loc) · 10.6 KB
/
match.d
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
module xtk.match;
private import xtk.meta;
struct Ignore{};
enum _ = Ignore();
struct Ellipsis{}
enum __dollar = Ellipsis();
// debug = Print1; //compiletime print1
// debug = Print2; // compiletime print2
// debug = Print3; //runtime print
debug(Print3) import std.stdio;
/**
* Save pattern matching information
* Supports array elements and std.typecons.Tuple fields
*/
struct Match(bool ellipse, Elem...)
{
public:
template Repeat(T, size_t n)
{
static if (n == 0)
alias TypeTuple!() Repeat;
else
alias TypeTuple!(T, Repeat!(T, n-1)) Repeat;
}
template FieldTypeTupleOf(A)
{
static if (isArray!A)
alias Repeat!(typeof(A.init[0]), Elem.length) FieldTypeTupleOf;
else static if (isTuple!A)
alias typeof(A.field) FieldTypeTupleOf;
}
template isMatchParameter(int i, P, Q)
{
static if (is(P == Ignore))
{
debug(Print2) pragma(msg, "[", i, "] ignore, P=", P, ", Q=", Q);
enum isMatchParameter = true;
}
else static if (is(P U : U*) && is(Q : U))
{
debug(Print2) pragma(msg, "[", i, "] capture, P=", P, ", Q=", Q);
enum isMatchParameter = true; // capture
}
else static if (is(P V == Match!(f, W), bool f, W...))
{
debug(Print2) pragma(msg, "[", i, "] pattern, P=", P, ", Q=", Q);
//enum isMatchParameter = true; // pattern
enum isMatchParameter = V.isMatchParameterWith!Q; // pattern
}
else static if (is(P == void*) && is(Q == class))
{
debug(Print2) pragma(msg, "[", i, "] value(void*), P=", P, ", Q=", Q);
enum isMatchParameter = true; // value(void*)
}
else static if (is(P X) && is(Q : X))
{
debug(Print2) pragma(msg, "[", i, "] value, P=", P, ", Q=", Q);
enum isMatchParameter = true; // value
}
else
{
debug(Print2) pragma(msg, "[", i, "] failed : (", P, ", ", Q, ")");
enum isMatchParameter = false;
}
}
template isMatchParameterWith(Q...)
{
static if (Q.length==1 && __traits(compiles, Q[0].opMatch))
{
debug(Print2) pragma(msg, typeof(this), ".isMatchParameterWith : cls Q=", Q);
enum isMatchParameterWith = true; //todo?
}
else static if (Q.length==1 && isArray!(Q[0]))
{
debug(Print2) pragma(msg, typeof(this), ".isMatchParameterWith : arr Q=", Q);
enum isMatchParameterWith =
isMatchParameterWith!(FieldTypeTupleOf!(Q[0]));
}
else static if (Q.length==1 && isTuple!(Q[0]))
{
debug(Print2) pragma(msg, typeof(this), ".isMatchParameterWith : tup Q=", Q);
enum isMatchParameterWith =
isMatchParameterWith!(FieldTypeTupleOf!(Q[0]));
}
else
{
debug(Print2) pragma(msg, typeof(this), ".isMatchParameterWith : ... Q =", Q);
static if (ellipse)
{
static if (Elem.length <= Q.length)
enum isMatchParameterWith =
allSatisfy!(isMatchParameter,
staticZip!(Pack!(staticIota!(staticLength!Elem)), Pack!Elem, Pack!(Q[0 .. Elem.length])));
else
enum isMatchParameterWith = false;
}
else
{
static if (Elem.length == Q.length)
enum isMatchParameterWith =
allSatisfy!(isMatchParameter,
staticZip!(Pack!(staticIota!(staticLength!Elem)), Pack!Elem, Pack!Q));
else
enum isMatchParameterWith = false;
}
}
}
/// match operator(like an arrow operator)
bool opOpAssign(string op, Ag)(auto ref Ag ag) if (op=="<<")
{
//debug(Print3) writefln("%s <<= %s", typeid(typeof(this)), typeid(typeof(ag)));
static if (__traits(compiles, ag.opMatch))
{
debug(Print1) pragma(msg, typeof(this), " <<= class ", Ag);
return ag.opMatch(this);
}
else static if (isArray!Ag)
{
debug(Print1) pragma(msg, typeof(this), " <<= ", Ag, "");
static if (ellipse)
{
static if(isMatchParameterWith!Ag)
{
return (Elem.length <= ag.length ? check(ag[0 .. Elem.length]) : false);
}
else
return false;
}
else
{
static if(isMatchParameterWith!Ag)
{
return (Elem.length == ag.length ? check(ag) : false);
}
else
return false;
}
}
else static if (isTuple!Ag)
{
debug(Print1) pragma(msg, typeof(this), " <<= ", Ag, "");
static if (ellipse)
{
// static if (Elem.length <= ag.length &&
/// isMatchParameterWith!(typeof(ag.slice!(0, Elem.length)().field)))
// isMatchParameterWith!Ag)
static if (isMatchParameterWith!Ag)
{
return check(ag.slice!(0, Elem.length));
}
else
{
return false;
}
}
else
{
// static if (Elem.length == ag.length && isMatchParameterWith!Ag)
static if (isMatchParameterWith!Ag)
{
return check(ag);
}
else
{
return false;
}
}
}
else
return false;
}
private:
static if (Elem.length > 0) // elem is ()
Elem refs;
///
this(ref Elem elem)
{
foreach (i, e; elem)
{
static if (is(typeof(elem[i]) == Ignore))
{}
else static if (isPointer!(typeof(elem[i])))
refs[i] = elem[i];
else
refs[i] = elem[i];
}
}
bool check(Ag)(Ag ag)
in{ assert(Elem.length == ag.length); }
body{
// debug(Print1) pragma(msg, "Match.check : Elem = ", Elem, ", Ag = ", Ag);
// debug(Print3) writefln("Match.check : ag = %s", ag);
static if (Elem.length > 0)
{
bool result = true;
foreach (i, e; refs)
{
static if (is(Elem[i] == Ignore))
{
debug(Print3) writefln("[%s] ignore %s <<= %s:%s", i, typeof(refs[i]).stringof, ag[i], typeof(ag[i]).stringof);
}
else static if (is(Elem[i] U : U*) && is(typeof(ag[i]) : U))
{
debug(Print3) writefln("[%s] capture %s <<= %s:%s", i, typeof(refs[i]).stringof, ag[i], typeof(ag[i]).stringof);
*refs[i] = ag[i];
}
else static if (is(Elem[i] V == Match!(f, W), bool f, W...))
{
debug(Print3) writefln("[%s] pattern %s <<= %s:%s", i, typeof(refs[i]).stringof, ag[i], typeof(ag[i]).stringof);
result = refs[i] <<= ag[i];
}
else static if (is(Elem[i] == void*) && is(typeof(ag[i]) == class))
{
debug(Print3) writefln("[%s] value(void*) %s <<= %s", i, refs[i], ag[i]);
if (refs[i] == null)
result = ag[i] is null;
else
result = refs[i] == &ag[i];
}
else static if (is(Elem[i] X) && is(typeof(ag[i]) : X))
{
debug(Print3) writefln("[%s] value, %s:%s <<= %s:%s", i, refs[i], typeof(refs[i]).stringof, ag[i], typeof(ag[i]).stringof);
result = (refs[i] == ag[i]);
}
else
{
static if (Elem.length == Ag.length)
debug(Print3) writefln("[%s] fail %s <<= %s", i, typeid(Elem[i]), ag[i]);
result = false;
}
if (!result) break;
}
debug(Print3) writefln(" check %s", result);
return result;
}
else
return true;
}
}
// Pattern literal
struct p
{
// array/tuple literal pattern
static auto opSlice()
{
return Match!(false)();
}
static auto opIndex(Elem...)(Elem elem)
{
static if (is(Elem[$-1] == Ellipsis))
return Match!(true, Elem[0..$-1])(elem[0..$-1]);
else
return Match!(false, Elem)(elem);
}
/+ // tuple literal pattern
// Sadly p(..., $) is invalid D expression, so we can't use this.
static auto opCall(Elem...)(Elem elem)
{
static if (is(Elem[$-1] == Ellipsis))
return Match!(true, Elem[0..$-1])(elem[0..$-1]);
else
return Match!(false, Elem)(elem);
}+/
}
debug(match)
unittest
{
scope(success) std.stdio.writefln("unittest succeeded @ %s:%s", __FILE__, __LINE__);
int x, y;
double a, b;
assert(p[] <<= []);
assert(p[_] <<= [1]);
assert(p[1] <<= [1]);
assert(p[1, $] <<= [1]);
assert(p[1, 2] <<= [1, 2]);
assert(p[&x, &y, $] <<= [1, 2, 3]);
assert(x == 1 && y == 2);
assert(p[_, &x, _, &y, $] <<= [1, 2, 3, 4, 5, 6]);
assert(x == 2 && y == 4);
assert(p[_, &a, _, &b, $] <<= [1, 2, 3, 4, 5, 6]);
assert(a == 2.0 && b == 4.0);
assert(p[p[1, &x], p[&y, 4]] <<= [[1,2], [3,4]]);
assert(x == 2 && y == 3);
assert(!(p[] <<= [1]));
assert(!(p[1, $] <<= [0, 0]));
assert(!(p[1, 2] <<= [1, 3]));
assert(!(p[1, &x, &y] <<= [1, 2, 3, 4]));
assert(!(p[&x, 0, &y, $] <<= [1, 2, 3]));
assert(!(p[_, _] <<= [1]));
assert(!(p[_, 0] <<= [1, 2]));
assert(p[] <<= tuple());
assert(p[_] <<= tuple(1));
assert(p[1] <<= tuple(1));
assert(p[1, $] <<= tuple(1));
assert(p[1, 2] <<= tuple(1, 2));
assert(p[&x, &y, $] <<= tuple(1, 2, 3));
assert(x == 1 && y == 2);
assert(p[_, &x, _, &y, $] <<= tuple(1, 2, 3, 4, 5, 6));
assert(x == 2 && y == 4);
assert(p[_, &a, _, &b, $] <<= tuple(1, 2, 3, 4, 5, 6));
assert(a == 2.0 && b == 4.0);
assert(p[p[1, &x], p[&y, 4]] <<= tuple(tuple(1,2), tuple(3,4)));
assert(x == 2 && y == 3);
assert(!(p[] <<= tuple(1)));
assert(!(p[1, $] <<= tuple(0, 0)));
assert(!(p[1, 2] <<= tuple(1, 3)));
assert(!(p[1, &x, &y] <<= tuple(1, 2, 3, 4)));
assert(!(p[&x, 0, &y, $] <<= tuple(1, 2, 3)));
assert(!(p[_, _] <<= tuple(1)));
assert(!(p[_, 0] <<= tuple(1, 2)));
class Exp
{
int tag;
this(int n){ tag = n; }
bool opMatch(Match)(ref Match m)
{
if (tag == 1)
return m <<= tuple(0, "");
else
return m <<= tuple("", 0);
}
}
auto exp1 = new Exp(1);
auto exp2 = new Exp(2);
assert(p[0, ""] <<= exp1);
assert(p["", 0] <<= exp2);
assert(!(p[1, ""] <<= exp1));
assert(!(p["", 1] <<= exp2));
assert(!(p["", ""] <<= exp1));
assert(!(p["", ""] <<= exp2));
}
/**
*
*/
auto match(T, U...)(T x, U matches)
in
{
static assert(U.length % 2 == 0);
}
body
{
foreach (I,m; matches)
{
static if (I%2 == 0)
{
static if (is(typeof(matches[I]) == typeof(_)))
{
return matches[I+1]();
}
else
{
if (matches[I] <<= x)
{
return matches[I+1]();
}
}
}
}
static if (!is(typeof(return) == void))
{
return typeof(return).init;
}
}
debug(match)
unittest
{
scope(success) std.stdio.writefln("unittest succeeded @ %s:%s", __FILE__, __LINE__);
// statement version
int d;
match(tuple(1, 3.14),
p[&d, 3.14], { assert(d == 1); },
_, { assert(0); }
);
match(tuple(1, "hello"),
p[&d, "bad"], { assert(0); },
_, { /*otherwise*/; }
);
// static assert(!__traits(compiles, ()
// {
// match(tuple(1, "hello"),
// p[&d, 3.14], { },
// _, { }
// );
// }));
// expression version
assert(
match(tuple(1, "hello"),
p[&d, "hello"], { return 1; },
p[&d, "bad"], { return 2; }
)
== 1);
assert(
match(tuple(1, "hello"),
p[&d, "bad1"], { return 1; },
p[&d, "bad2"], { return 2; },
_, { return 3; }
)
== 3);
assert(
match(tuple(1, "hello"),
p[&d, "bad1"], { return 1; },
p[&d, "bad2"], { return 2; }
)
== int.init);
}
template PointerTypeOf(T)
{
alias T* PointerTypeOf;
}
struct tie
{
static auto opIndex(Elem...)(ref Elem captures)
{
staticMap!(PointerTypeOf, typeof(captures)) pointers;
foreach (i, c; captures)
pointers[i] = &captures[i];
return p[pointers];
}
}
debug(match)
unittest
{
scope(success) std.stdio.writefln("unittest succeeded @ %s:%s", __FILE__, __LINE__);
int x, y;
tie[x, y] <<= [10, 20];
}