-
-
Notifications
You must be signed in to change notification settings - Fork 667
/
Copy pathoperator-overloading.ts
370 lines (303 loc) · 7.84 KB
/
operator-overloading.ts
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
class Tester {
constructor(public x: i32, public y: i32) {
}
@operator("+")
static add(a: Tester, b: Tester): Tester {
return new Tester(a.x + b.x, a.y + b.y);
}
@operator("-")
static sub(a: Tester, b: Tester): Tester {
return new Tester(a.x - b.x, a.y - b.y);
}
@operator("*")
static mul(a: Tester, b: Tester): Tester {
return new Tester(a.x * b.x, a.y * b.y);
}
@operator("/")
static div(a: Tester, b: Tester): Tester {
return new Tester(a.x / b.x, a.y / b.y);
}
@operator("%")
static mod(a: Tester, b: Tester): Tester {
return new Tester(a.x % b.x, a.y % b.y);
}
@operator("**")
static pow(a: Tester, b: Tester): Tester {
return new Tester(<i32>(a.x ** b.x), <i32>(a.y ** b.y));
}
@operator("|")
static or(a: Tester, b: Tester): Tester {
return new Tester(a.x | b.x, a.y | b.y);
}
@operator("&")
static and(a: Tester, b: Tester): Tester {
return new Tester(a.x & b.x, a.y & b.y);
}
@operator("^")
static xor(a: Tester, b: Tester): Tester {
return new Tester(a.x ^ b.x, a.y ^ b.y);
}
@operator("==")
static equals(a: Tester, b: Tester): bool {
return a.x == b.x && a.y == b.y;
}
@operator("!=")
static notEquals(a: Tester, b: Tester): bool {
return a.x != b.x && a.y != b.y;
}
@operator(">")
static greater(a: Tester, b: Tester): bool {
return a.x > b.x && a.y > b.y;
}
@operator(">=")
static greaterEquals(a: Tester, b: Tester): bool {
return a.x >= b.x && a.y >= b.y;
}
@operator("<")
static less(a: Tester, b: Tester): bool {
return a.x < b.x && a.y < b.y;
}
@operator("<=")
static lessEquals(a: Tester, b: Tester): bool {
return a.x <= b.x && a.y <= b.y;
}
@operator(">>")
static shr(value: Tester, shift: i32): Tester {
return new Tester(value.x >> shift, value.y >> shift);
}
@operator(">>>")
static shu(value: Tester, shift: i32): Tester {
return new Tester(value.x >>> shift, value.y >>> shift);
}
@operator("<<")
static shl(value: Tester, shift: i32): Tester {
return new Tester(value.x << shift, value.y << shift);
}
// unary opterators
@operator.prefix("~")
static not(value: Tester): Tester {
return new Tester(~value.x, ~value.y);
}
@operator.prefix("!")
static excl(value: Tester): bool {
return !value.x && !value.y;
}
@operator.prefix("+")
static pos(value: Tester): Tester {
return new Tester(+value.x, +value.y);
}
@operator.prefix("-")
static neg(value: Tester): Tester {
return new Tester(-value.x, -value.y);
}
@operator.prefix("++")
inc(): this {
++this.x;
++this.y;
return this;
}
@operator.prefix("--")
dec(): this {
--this.x;
--this.y;
return this;
}
@operator.postfix("++")
postInc(): Tester {
return new Tester(this.x + 1, this.y + 1);
}
@operator.postfix("--")
postDec(): Tester {
return new Tester(this.x - 1, this.y - 1);
}
}
// check additional
var a1 = new Tester(1, 2);
var a2 = new Tester(2, 3);
var a = a1 + a2;
assert(a.x == 3 && a.y == 5);
// check subtraction
var s1 = new Tester(2, 3);
var s2 = new Tester(2,-3);
var s = s1 - s2;
assert(s.x == 0 && s.y == 6);
// check multiplication
var m1 = new Tester(2, 5);
var m2 = new Tester(3, 2);
var m = m1 * m2;
assert(m.x == 6 && m.y == 10);
// check division
var d1 = new Tester(6, 50);
var d2 = new Tester(3, 10);
var d = d1 / d2;
assert(d.x == 2 && d.y == 5);
// check remainder
var f1 = new Tester(10, 10);
var f2 = new Tester(6, 10);
var f = f1 % f2;
assert(f.x == 4 && f.y == 0);
// check power
var p1 = new Tester(2, 3);
var p2 = new Tester(4, 5);
var p = p1 ** p2;
assert(p.x == 16 && p.y == 243);
// check bitwise and
var n1 = new Tester(0xFF, 0x0F);
var n2 = new Tester(0x0F, 0xFF);
var n = n1 & n2;
assert(n.x == 0xF && n.y == 0xF);
// check bitwise or
var o1 = new Tester(0x0F0F, 0xFF);
var o2 = new Tester(0xF0F0, 0x00);
var o = o1 | o2;
assert(o.x == 0xFFFF && o.y == 0xFF);
// check bitwise xor
var x1 = new Tester(0x00FF, 0xFF);
var x2 = new Tester(0xFF00, 0x00);
var x = x1 ^ x2;
assert(x.x == 0xFFFF && x.y == 0xFF);
// check truthfully equal
var eq1 = new Tester(1, -2);
var eq2 = new Tester(1, -2);
var eq = eq1 == eq2;
assert(eq == true);
// check falsely equal
var eq3 = new Tester(1, 0);
var eq4 = new Tester(0, 1);
var eqf = eq3 == eq4;
assert(eqf == false);
// check falsely non-equal
eq = eq1 != eq2;
assert(eq == false);
// check truthfully non-equal
eqf = eq3 != eq4;
assert(eqf == true);
// check greater
var gt1 = new Tester(2, i32.MAX_VALUE);
var gt2 = new Tester(1, 0);
var gt = gt1 > gt2;
assert(gt == true);
// check greater or equal
var gte1 = new Tester(2, 2);
var gte2 = new Tester(2, 2);
var gte = gte1 >= gte2;
assert(gte == true);
// check less
var le1 = new Tester(5,-1);
var le2 = new Tester(6, 6);
var le = le1 < le2;
assert(le == true);
// check less or equal
var leq1 = new Tester(4, 3);
var leq2 = new Tester(4, 3);
var leq = leq1 <= leq2;
assert(leq == true);
// check right shift
var shr = new Tester(8, 16);
var sres = shr >> 3;
assert(sres.x == 1 && sres.y == 2);
// check right shift
var shu = new Tester(-8, -16);
var ures = shu >>> 3;
assert(ures.x == 536870911 && ures.y == 536870910);
// check left shift
var shl = new Tester(1, 2);
sres = shl << 3;
assert(sres.x == 8 && sres.y == 16);
// check unary positive
var pos = new Tester(1, -2);
var pres = +pos;
assert(pres.x == pos.x && pres.y == pos.y);
// check unary negative
var neg = new Tester(-1, -2);
var nres = -neg;
assert(nres.x == -neg.x && nres.y == -neg.y);
// check unary not "~"
var not = new Tester(0xFF, 0x10);
var res = ~not;
assert(res.x == ~not.x && res.y == ~not.y);
// check unary exclamation "!"
var excl = new Tester(0, 0);
var bres = !excl;
assert(bres == (!excl.x && !excl.y));
assert(bres == true);
//
var incdec = new Tester(0, 1);
++incdec;
assert(incdec.x == 1 && incdec.y == 2);
--incdec;
assert(incdec.x == 0 && incdec.y == 1);
incdec = new Tester(0, 1);
var tmp = incdec++;
assert(tmp.x == 0 && tmp.y == 1);
assert(incdec.x == 1 && incdec.y == 2);
tmp = incdec--;
assert(tmp.x == 1 && tmp.y == 2);
assert(incdec.x == 0 && incdec.y == 1);
// check inlined static
class TesterInlineStatic {
constructor(public x: i32, public y: i32) {
}
@inline @operator("+")
static add(a: TesterInlineStatic, b: TesterInlineStatic): TesterInlineStatic {
return new TesterInlineStatic(a.x + b.x, a.y + b.y);
}
@inline @operator.postfix("++")
static postInc(a: TesterInlineStatic): TesterInlineStatic {
return new TesterInlineStatic(a.x + 1, a.y + 1);
}
}
var ais1 = new TesterInlineStatic(1, 2);
ais1 = ais1++; // 2, 3 (static skips re-assign)
var ais2 = new TesterInlineStatic(2, 3);
var ais = ais1 + ais2;
assert(ais.x == 4 && ais.y == 6);
// check inlined instance
class TesterInlineInstance {
constructor(public x: i32, public y: i32) {
}
@inline @operator("+")
add(b: TesterInlineInstance): TesterInlineInstance {
return new TesterInlineInstance(this.x + b.x, this.y + b.y);
}
@inline @operator.postfix("++")
postInc(): TesterInlineInstance {
return new TesterInlineInstance(this.x + 1, this.y + 1);
}
}
var aii1 = new TesterInlineInstance(1, 2);
aii1++; // 2, 3
var aii2 = new TesterInlineInstance(2, 3);
var aii = aii1 + aii2;
assert(aii.x == 4 && aii.y == 6);
// test custom element access overloading with string keys
class TesterElementAccess {
[key: string]: number;
constructor(
public x: i32,
public y: i32
) {}
@operator("[]")
__get(key: string): i32 {
return key == "x"
? this.x
: this.y;
}
@operator("[]=")
__set(key: string, value: i32): void {
key == "x"
? this.x = value
: this.y = value;
}
}
var tea = new TesterElementAccess(1, 2);
tea["x"] = -1;
tea["y"] = -2;
assert(tea.x == -1);
assert(tea["x"] == -1);
assert(tea.y == -2);
assert(tea["y"] == -2);
tea["x"]++;
--tea["y"];
assert(tea["x"] == 0);
assert(tea["y"] == -3);