-
Notifications
You must be signed in to change notification settings - Fork 2
/
operator-overloading.ts
207 lines (172 loc) · 4.53 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
import "allocator/arena";
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;
}
}
// 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 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);
}
}
var ais1 = new TesterInlineStatic(1, 2);
var ais2 = new TesterInlineStatic(2, 3);
var ais = ais1 + ais2;
assert(ais.x == 3 && ais.y == 5);
// 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);
}
}
var aii1 = new TesterInlineInstance(1, 2);
var aii2 = new TesterInlineInstance(2, 3);
var aii = aii1 + aii2;
assert(aii.x == 3 && aii.y == 5);