Repository navigation
Expand file tree
/
Copy pathoperations.ts
More file actions
344 lines (308 loc) Β· 8.06 KB
/
Copy pathoperations.ts
File metadata and controls
344 lines (308 loc) Β· 8.06 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
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
import { errorType } from './errors';
import { TypeError, ZeroDivisionError } from './exceptions';
import { Value } from './value';
import { Type } from './value/types';
import { typeOf } from './value/typeof';
import { cast } from './value/cast';
import { falsey } from './value/truthiness';
// TODO: I stumbled on a really nasty circular import bug. I was originally
// importing and re-exporting everything I'm importing here in value's index.
// When I did that, I triggered the circular import bug.
//
// Importing the libraries directly seems to avoid it, and I've moved towards
// using that pattern more generally. But I suspect that with the wrong
// imports in any file, I could trigger the issue once again.
function typePrecedence(type: Type): number {
switch (type) {
case Type.Boolean:
return 0;
case Type.Integer:
return 1;
case Type.Real:
return 2;
case Type.String:
return 3;
default:
return 4;
}
}
export function highestTypePrecedence(a: Type, b: Type): Type {
if (typePrecedence(a) > typePrecedence(b)) {
return a;
}
return b;
}
@errorType('Invalid')
class Invalid extends Error {
constructor() {
super('Invalid');
Object.setPrototypeOf(this, new.target.prototype);
}
}
function invalid(): never {
throw new Invalid();
}
type BinaryOperator = {
name: string;
check?: (
aOriginal: Value,
aType: Type,
aCast: Value,
bOriginal: Value,
bType: Type,
bCast: Value,
tCast: Type,
) => void;
boolean: (a: boolean, b: boolean) => Value;
integer: (a: number, b: number) => Value;
real: (a: number, b: number) => Value;
string: (a: string, b: string) => Value;
};
export type BinaryOperation = (a: Value, b: Value) => Value;
export function binaryOperation(op: BinaryOperator): BinaryOperation {
return function operation(a: Value, b: Value): Value {
const typeA = typeOf(a);
const typeB = typeOf(b);
const castTo = highestTypePrecedence(typeA, typeB);
const castA: any = cast(a, typeA, castTo);
const castB: any = cast(b, typeB, castTo);
let method: string;
try {
switch (castTo) {
case Type.Boolean:
method = 'boolean';
break;
case Type.Integer:
method = 'integer';
break;
case Type.Real:
method = 'real';
break;
case Type.String:
method = 'string';
break;
default:
invalid();
}
if (op.check) {
op.check(a, typeA, castA, b, typeB, castB, castTo);
}
return op[method](castA, castB);
} catch (err) {
if (err instanceof Invalid) {
throw new TypeError(
`Invalid operand for ${op.name} on ${typeA}: ${typeB}`,
b,
typeB,
Type.Invalid,
);
}
throw err;
}
};
}
type UnaryOperator = {
name: string;
boolean: (a: boolean) => Value;
integer: (a: number) => Value;
real: (a: number) => Value;
string: (a: string) => Value;
};
export type UnaryOperation = (a: Value) => Value;
export function unaryOperation(op: UnaryOperator): UnaryOperation {
return function operation(a: Value): Value {
const type = typeOf(a);
try {
switch (type) {
case Type.Boolean:
return op.boolean(a as boolean);
case Type.Integer:
return op.integer(a as number);
case Type.Real:
return op.real(a as number);
case Type.String:
return op.string(a as string);
default:
invalid();
}
} catch (err) {
if (err instanceof Invalid) {
throw new TypeError(
`Invalid operand for ${op.name}: ${type}`,
a,
type,
Type.Invalid,
);
}
throw err;
}
};
}
type ComparisonOperator = {
name: string;
any: (a: Value, aT: Type, b: Value, bT: Type) => boolean;
boolean: (a: boolean, b: boolean) => boolean;
integer: (a: number, b: number) => boolean;
real: (a: number, b: number) => boolean;
string: (a: string, b: string) => boolean;
};
export type ComparisonOperation = (a: Value, b: Value) => boolean;
export function comparisonOperation(
op: ComparisonOperator,
): ComparisonOperation {
return function operation(a: Value, b: Value): boolean {
const typeA = typeOf(a);
const typeB = typeOf(b);
try {
if (typeA != typeB) {
return op.any(a, typeA, b, typeB);
}
switch (typeA) {
case Type.Boolean:
return op.boolean(a as boolean, b as boolean);
case Type.Integer:
return op.integer(a as number, b as number);
case Type.Real:
return op.real(a as number, b as number);
case Type.String:
return op.string(a as string, b as string);
default:
invalid();
}
} catch (err) {
if (err instanceof Invalid) {
throw new TypeError(
`Invalid operand for ${op.name} on ${typeA}: ${typeB}`,
b,
typeB,
Type.Invalid,
);
}
throw err;
}
};
}
//
// Math operators
//
export const add = binaryOperation({
name: '+',
boolean: (a, b) => (a ? (b ? 2 : 1) : b ? 1 : 0),
integer: (a, b) => a + b,
real: (a, b) => a + b,
string: (a, b) => a + b,
});
export const sub = binaryOperation({
name: '-',
boolean: (a, b) => {
if (a) {
return b ? 0 : 1;
} else {
return b ? -1 : 0;
}
},
integer: (a, b) => a - b,
real: (a, b) => a - b,
string: (_a, _b) => invalid(),
});
export const mul = binaryOperation({
name: '*',
boolean: (a, b) => a && b,
integer: (a, b) => a * b,
real: (a, b) => a * b,
string: (_a, _b) => invalid(),
});
export const div = binaryOperation({
name: '/',
check: (aOriginal, aType, _aCast, bOriginal, bType, bCast, _castTo) => {
// Divide by zero check. In this case we don't need to switch on type,
// because JavaScript will do the right thing for any numbers, and strings
// are invalid anyway.
if (!bCast) {
throw new ZeroDivisionError(aOriginal, aType, bOriginal, bType);
}
},
boolean: (a, _b) => (a ? 1 : 0),
integer: (a, b) => a / b,
real: (a, b) => a / b,
string: (_a, _b) => invalid(),
});
export const neg = unaryOperation({
name: '-',
boolean: (a) => (a ? -1 : 0),
integer: (a) => -a,
real: (a) => -a,
string: (_a) => invalid(),
});
//
// Comparison operators
//
export const eq = comparisonOperation({
name: '==',
any: (_a, _aT, _b, _bT) => false,
boolean: (a, b) => a === b,
integer: (a, b) => a === b,
real: (a, b) => a === b,
string: (a, b) => a === b,
});
export const ne = comparisonOperation({
name: '<>',
any: (_a, _aT, _b, _bT) => true,
boolean: (a, b) => a !== b,
integer: (a, b) => a !== b,
real: (a, b) => a !== b,
string: (a, b) => a !== b,
});
export const gt = comparisonOperation({
name: '>',
any: (_a, _aT, _b, _bT) => invalid(),
boolean: (a, b) => a > b,
integer: (a, b) => a > b,
real: (a, b) => a > b,
string: (a, b) => a > b,
});
export const ge = comparisonOperation({
name: '>=',
any: (_a, _aT, _b, _bT) => invalid(),
boolean: (a, b) => a >= b,
integer: (a, b) => a >= b,
real: (a, b) => a >= b,
string: (a, b) => a >= b,
});
export const lt = comparisonOperation({
name: '<',
any: (_a, _aT, _b, _bT) => invalid(),
boolean: (a, b) => a < b,
integer: (a, b) => a < b,
real: (a, b) => a < b,
string: (a, b) => a < b,
});
export const le = comparisonOperation({
name: '<=',
any: (_a, _aT, _b, _bT) => invalid(),
boolean: (a, b) => a <= b,
integer: (a, b) => a <= b,
real: (a, b) => a <= b,
string: (a, b) => a <= b,
});
//
// Logic operations. And and Or are implemented in bytecode using a combination
// of Jump and JumpIfFalse. That leaves Not. Not is equivalent to falsey,
// but is wrapped to provide a better error.
//
export function not(a: Value): boolean {
const type = typeOf(a);
try {
return falsey(a, type);
} catch (err) {
if (err instanceof TypeError) {
throw new TypeError(
`Invalid operand for not: ${type}`,
a,
type,
Type.Invalid,
);
}
throw err;
}
}