-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltins.js
657 lines (618 loc) · 19.7 KB
/
builtins.js
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
import _ from "lodash";
import { setError } from "./general.js";
import {
ADTValue,
IntLit,
Int32L,
Int64L,
Int128L,
Int256L,
UintLit,
Uint32L,
Uint64L,
Uint128L,
Uint256L,
StringLit,
Bystr,
BystrX,
Map,
BNumLit,
ScillaLiterals,
literalType,
} from "./literals.js";
import { Error } from "./syntax.js";
import {
Int128,
Int256,
Int32,
Int64,
Uint128,
Uint256,
Uint32,
Uint64,
} from "./types.js";
function reverseString(str) {
const stringArray = str.split("");
const reverseArray = stringArray.reverse();
const joinArray = reverseArray.join("");
return joinArray;
}
export default class Builtins {
constructor() {
this.builtinDict = {
eq: this.eq,
add: this.add,
sub: this.sub,
mul: this.mul,
div: this.div,
rem: this.rem,
lt: this.lt,
pow: this.pow,
isqrt: this.isqrt,
to_nat: this.to_nat,
concat: this.concat,
to_int32: this.to_int32,
to_int64: this.to_int64,
to_int128: this.to_int128,
to_int256: this.to_int256,
to_uint32: this.to_uint32,
to_uint64: this.to_uint64,
to_uint128: this.to_uint128,
to_uint256: this.to_uint256,
concat: this.concat,
substring: this.substring,
to_string: this.to_string,
strlen: this.strlen,
strrev: this.strrev,
to_ascii: undefined,
put: this.put,
get: this.get,
contains: this.contains,
remove: this.remove,
to_list: undefined,
size: this.size,
blt: undefined,
badd: undefined,
bsub: undefined,
to_bystr: undefined,
};
}
printError(funcname, msg) {
console.log("[ERROR]" + funcname + ": " + msg);
}
getJSValueFromLiteral(lit) {
return lit instanceof StringLit
? lit.s
: lit instanceof IntLit
? lit.i
: lit instanceof UintLit
? lit.i
: lit instanceof Bystr
? lit.s
: lit instanceof BystrX
? lit.s
: lit instanceof BNumLit
? lit.i
: undefined;
}
parseBuiltinIdentifier(id) {
return this.builtinDict[id];
}
eq = (x) => (y) => {
if (x instanceof IntLit && y instanceof IntLit) {
return x.i === y.i
? new ADTValue("True", [], [])
: new ADTValue("False", [], []);
} else if (x instanceof UintLit && y instanceof UintLit) {
return x.i === y.i
? new ADTValue("True", [], [])
: new ADTValue("False", [], []);
} else if (x instanceof StringLit && y instanceof StringLit) {
return x.s === y.s
? new ADTValue("True", [], [])
: new ADTValue("False", [], []);
} else {
setError(new Error(`Error: builtin eq of ${x} and ${y}`));
return;
}
};
add = (x) => (y) => {
if (x instanceof IntLit && y instanceof IntLit) {
if (x instanceof Int256L || y instanceof Int256L) {
return new Int256L(x.i + y.i);
} else if (x instanceof Int128L || y instanceof Int128L) {
return new Int128L(x.i + y.i);
} else if (x instanceof Int64L || y instanceof Int64L) {
return new Int64L(x.i + y.i);
} else if (x instanceof Int32L || y instanceof Int32L) {
return new Int32L(x.i + y.i);
}
} else if (x instanceof UintLit && y instanceof UintLit) {
if (x instanceof Uint256L || y instanceof Uint256L) {
return new Uint256L(x.i + y.i);
} else if (x instanceof Uint128L || y instanceof Uint128L) {
return new Uint128L(x.i + y.i);
} else if (x instanceof Uint64L || y instanceof Uint64L) {
return new Uint64L(x.i + y.i);
} else if (x instanceof Uint32L || y instanceof Uint32L) {
return new Uint32L(x.i + y.i);
}
} else {
setError(new Error(`Error: builtin add of ${x} and ${y}`));
return;
}
};
sub = (x) => (y) => {
if (x instanceof IntLit && y instanceof IntLit) {
if (x instanceof Int256L || y instanceof Int256L) {
return new Int256L(x.i - y.i);
} else if (x instanceof Int128L || y instanceof Int128L) {
return new Int128L(x.i - y.i);
} else if (x instanceof Int64L || y instanceof Int64L) {
return new Int64L(x.i - y.i);
} else if (x instanceof Int32L || y instanceof Int32L) {
return new Int32L(x.i - y.i);
}
} else if (x instanceof UintLit && y instanceof UintLit) {
if (x instanceof Uint256L || y instanceof Uint256L) {
return new Uint256L(x.i - y.i);
} else if (x instanceof Uint128L || y instanceof Uint128L) {
return new Uint128L(x.i - y.i);
} else if (x instanceof Uint64L || y instanceof Uint64L) {
return new Uint64L(x.i - y.i);
} else if (x instanceof Uint32L || y instanceof Uint32L) {
return new Uint32L(x.i - y.i);
}
} else {
setError(new Error(`Error: builtin sub of ${x} and ${y}`));
return;
}
};
mul = (x) => (y) => {
if (x instanceof IntLit && y instanceof IntLit) {
if (x instanceof Int256L || y instanceof Int256L) {
return new Int256L(x.i * y.i);
} else if (x instanceof Int128L || y instanceof Int128L) {
return new Int128L(x.i * y.i);
} else if (x instanceof Int64L || y instanceof Int64L) {
return new Int64L(x.i * y.i);
} else if (x instanceof Int32L || y instanceof Int32L) {
return new Int32L(x.i * y.i);
}
} else if (x instanceof UintLit && y instanceof UintLit) {
if (x instanceof Uint256L || y instanceof Uint256L) {
return new Uint256L(x.i * y.i);
} else if (x instanceof Uint128L || y instanceof Uint128L) {
return new Uint128L(x.i * y.i);
} else if (x instanceof Uint64L || y instanceof Uint64L) {
return new Uint64L(x.i * y.i);
} else if (x instanceof Uint32L || y instanceof Uint32L) {
return new Uint32L(x.i * y.i);
}
} else {
setError(new Error(`Error: builtin mul of ${x} and ${y}`));
return;
}
};
div = (x) => (y) => {
if (x instanceof IntLit && y instanceof IntLit) {
if (y.i === 0) {
setError(
new Error(
`Error: builtin div of ${x} and ${y}, divisor cannot be zero`
)
);
return;
}
if (x instanceof Int256L || y instanceof Int256L) {
return new Int256L(Math.floor(x.i / y.i));
} else if (x instanceof Int128L || y instanceof Int128L) {
return new Int128L(Math.floor(x.i / y.i));
} else if (x instanceof Int64L || y instanceof Int64L) {
return new Int64L(Math.floor(x.i / y.i));
} else if (x instanceof Int32L || y instanceof Int32L) {
return new Int32L(Math.floor(x.i / y.i));
}
} else if (x instanceof UintLit && y instanceof UintLit) {
if (x instanceof Uint256L || y instanceof Uint256L) {
return new Uint256L(Math.floor(x.i / y.i));
} else if (x instanceof Uint128L || y instanceof Uint128L) {
return new Uint128L(Math.floor(x.i / y.i));
} else if (x instanceof Uint64L || y instanceof Uint64L) {
return new Uint64L(Math.floor(x.i / y.i));
} else if (x instanceof Uint32L || y instanceof Uint32L) {
return new Uint32L(Math.floor(x.i / y.i));
}
} else {
setError(new Error(`Error: builtin div of ${x} and ${y}`));
return;
}
};
rem = (x) => (y) => {
if (x instanceof IntLit && y instanceof IntLit) {
if (x instanceof Int256L || y instanceof Int256L) {
return new Int256L(x.i % y.i);
} else if (x instanceof Int128L || y instanceof Int128L) {
return new Int128L(x.i % y.i);
} else if (x instanceof Int64L || y instanceof Int64L) {
return new Int64L(x.i % y.i);
} else if (x instanceof Int32L || y instanceof Int32L) {
return new Int32L(x.i % y.i);
}
} else if (x instanceof UintLit && y instanceof UintLit) {
if (x instanceof Uint256L || y instanceof Uint256L) {
return new Uint256L(x.i % y.i);
} else if (x instanceof Uint128L || y instanceof Uint128L) {
return new Uint128L(x.i % y.i);
} else if (x instanceof Uint64L || y instanceof Uint64L) {
return new Uint64L(x.i % y.i);
} else if (x instanceof Uint32L || y instanceof Uint32L) {
return new Uint32L(x.i % y.i);
}
} else {
setError(new Error(`Error: builtin rem of ${x} and ${y}`));
return;
}
};
lt = (x) => (y) => {
if (x instanceof IntLit && y instanceof IntLit) {
return x.i < y.i
? new ADTValue("True", [], [])
: new ADTValue("False", [], []);
} else if (x instanceof UintLit && y instanceof UintLit) {
return x.i < y.i
? new ADTValue("True", [], [])
: new ADTValue("False", [], []);
} else if (x instanceof StringLit && y instanceof StringLit) {
return x.s < y.s
? new ADTValue("True", [], [])
: new ADTValue("False", [], []);
} else {
setError(new Error(`Error: builtin lt of ${x} and ${y}`));
return;
}
};
pow = (x) => (y) => {
if (x instanceof IntLit && y instanceof IntLit) {
if (x instanceof Int256L) {
return new Int256L(Math.pow(x.i, y.i));
} else if (x instanceof Int128L) {
return new Int128L(Math.pow(x.i, y.i));
} else if (x instanceof Int64L) {
return new Int64L(Math.pow(x.i, y.i));
} else if (x instanceof Int32L) {
return new Int32L(Math.pow(x.i, y.i));
}
} else if (x instanceof UintLit && y instanceof UintLit) {
if (x instanceof Uint256L) {
return new Uint256L(Math.pow(x.i, y.i));
} else if (x instanceof Uint128L) {
return new Uint128L(Math.pow(x.i, y.i));
} else if (x instanceof Uint64L) {
return new Uint64L(Math.pow(x.i, y.i));
} else if (x instanceof Uint32L) {
return new Uint32L(Math.pow(x.i, y.i));
}
} else {
setError(new Error(`Error: builtin pow of ${x} and ${y}`));
return;
}
};
isqrt = (x) => {
if (x instanceof IntLit) {
if (x instanceof Int256L) {
return new Int256L(Math.floor(Math.sqrt(x.i)));
} else if (x instanceof Int128L) {
return new Int128L(Math.floor(Math.sqrt(x.i)));
} else if (x instanceof Int64L) {
return new Int64L(Math.floor(Math.sqrt(x.i)));
} else if (x instanceof Int32L) {
return new Int32L(Math.floor(Math.sqrt(x.i)));
}
} else if (x instanceof UintLit) {
if (x instanceof Uint256L) {
return new Uint256L(Math.floor(Math.sqrt(x.i)));
} else if (x instanceof Uint128L) {
return new Uint128L(Math.floor(Math.sqrt(x.i)));
} else if (x instanceof Uint64L) {
return new Uint64L(Math.floor(Math.sqrt(x.i)));
} else if (x instanceof Uint32L) {
return new Uint32L(Math.floor(Math.sqrt(x.i)));
}
} else {
setError(new Error(`Error: builtin isqrt of ${x}`));
return;
}
};
to_nat = (x) => {
if (x instanceof Uint32L) {
let res = new ADTValue("Zero", [], []);
for (let i = 0; i < x.i; i++) {
res = new ADTValue("Succ", [], [res]);
}
return res;
} else {
setError(new Error(`Error: builtin to_nat of ${x}`));
}
};
to_int32 = (x) => {
if (x instanceof IntLit) {
return new ADTValue("Some", [new Int32()], [new Int32L(x.i)]);
} else if (x instanceof UintLit) {
return new ADTValue("Some", [new Int32()], [new Int32L(x.i)]);
} else if (x instanceof StringLit) {
return Number(x.s) !== NaN
? new ADTValue("Some", [new Int32()], [new Int32L(Number(x.s))])
: new ADTValue("None", [new Int32()], []);
}
};
to_int64 = (x) => {
if (x instanceof IntLit) {
return new ADTValue("Some", [new Int64()], [new Int64L(x.i)]);
} else if (x instanceof UintLit) {
return new ADTValue("Some", [new Int64()], [new Int64L(x.i)]);
} else if (x instanceof StringLit) {
return Number(x.s) !== NaN
? new ADTValue("Some", [new Int64()], [new Int64L(Number(x.s))])
: new ADTValue("None", [new Int64()], []);
}
};
to_int128 = (x) => {
if (x instanceof IntLit) {
return new ADTValue("Some", [new Int128()], [new Int128L(x.i)]);
} else if (x instanceof UintLit) {
return new ADTValue("Some", [new Int128()], [new Int128L(x.i)]);
} else if (x instanceof StringLit) {
return Number(x.s) !== NaN
? new ADTValue("Some", [new Int128()], [new Int128L(Number(x.s))])
: new ADTValue("None", [new Int128()], []);
}
};
to_int256 = (x) => {
if (x instanceof IntLit) {
return new ADTValue("Some", [new Int256()], [new Int256L(x.i)]);
} else if (x instanceof UintLit) {
return new ADTValue("Some", [new Int256()], [new Int256L(x.i)]);
} else if (x instanceof StringLit) {
return Number(x.s) !== NaN
? new ADTValue("Some", [new Int256()], [new Int256L(Number(x.s))])
: new ADTValue("None", [new Int256()], []);
}
};
to_uint32 = (x) => {
if (x instanceof IntLit) {
return x.i >= 0
? new ADTValue("Some", [new Uint32()], [new Uint32L(x.i)])
: new ADTValue("None", [new Uint32()], []);
} else if (x instanceof UintLit) {
return new ADTValue("Some", [new Uint32()], [new Uint32L(x.i)]);
} else if (x instanceof StringLit) {
return Number(x.s) !== NaN
? Number(x.s) >= 0
? new ADTValue("Some", [new Uint32()], [new Uint32L(Number(x.s))])
: new ADTValue("None", [new Uint32()], [])
: new ADTValue("None", [new Uint32()], []);
}
};
to_uint64 = (x) => {
if (x instanceof IntLit) {
return x.i >= 0
? new ADTValue("Some", [new Uint64()], [new Uint64L(x.i)])
: new ADTValue("None", [new Uint64()], []);
} else if (x instanceof UintLit) {
return new ADTValue("Some", [new Uint64()], [new Uint64L(x.i)]);
} else if (x instanceof StringLit) {
return Number(x.s) !== NaN
? Number(x.s) >= 0
? new ADTValue("Some", [new Uint64()], [new Uint64L(Number(x.s))])
: new ADTValue("None", [new Uint64()], [])
: new ADTValue("None", [new Uint64()], []);
}
};
to_uint128 = (x) => {
if (x instanceof IntLit) {
return x.i >= 0
? new ADTValue("Some", [new Uint128()], [new Uint128L(x.i)])
: new ADTValue("None", [new Uint128()], []);
} else if (x instanceof UintLit) {
return new ADTValue("Some", [new Uint128()], [new Uint128L(x.i)]);
} else if (x instanceof StringLit) {
return Number(x.s) !== NaN
? Number(x.s) >= 0
? new ADTValue("Some", [new Uint128()], [new Uint128L(Number(x.s))])
: new ADTValue("None", [new Uint128()], [])
: new ADTValue("None", [new Uint128()], []);
}
};
to_uint256 = (x) => {
if (x instanceof IntLit) {
return x.i >= 0
? new ADTValue("Some", [new Uint256()], [new Uint256L(x.i)])
: new ADTValue("None", [new Uint256()], []);
} else if (x instanceof UintLit) {
return new ADTValue("Some", [new Uint256()], [new Uint256L(x.i)]);
} else if (x instanceof StringLit) {
return Number(x.s) !== NaN
? Number(x.s) >= 0
? new ADTValue("Some", [new Uint256()], [new Uint256L(Number(x.s))])
: new ADTValue("None", [new Uint256()], [])
: new ADTValue("None", [new Uint256()], []);
}
};
concat = (s1) => (s2) => {
if (s1 instanceof StringLit && s2 instanceof StringLit) {
return new StringLit(s1.s + s2.s);
} else {
setError(new Error(`Error: builtin concat of ${s1} and ${s2}`));
return;
}
};
substring = (s) => (idx) => (len) => {
if (s instanceof StringLit) {
if (idx instanceof Uint32L) {
if (len instanceof Uint32L) {
try {
const substr = s.substring(idx, idx + len);
return new StringLit(substr);
} catch (error) {
setError(new Error(`Error: builtin substring ${error}`));
return;
}
} else {
setError(
new Error(
`Error: builtin substring len ${len} needs to be of type Uint32`
)
);
return;
}
} else {
setError(
new Error(
`Error: builtin substring idx ${idx} needs to be of type Uint32`
)
);
return;
}
} else {
setError(
new Error(`Error: builtin substring s ${s} needs to be of type String`)
);
return;
}
};
to_string = (x) => {
if (x instanceof IntLit || x instanceof UintLit) {
return new StringLit(String(x.i));
} else if (x instanceof Bystr || x instanceof BystrX) {
return new StringLit(String(x.s));
} else {
setError(
new Error(`Error: builtin to_string X ${x} needs to be of type IntLit or UintLit or ByStr
or ByStrX`)
);
return;
}
};
strlen = (s) => {
if (s instanceof StringLit) {
return new Uint32L(s.s.length);
} else if (s instanceof Bystr) {
return new Uint32L(s.width());
} else {
setError(
new Error(
`Error: builtin strlen s ${s} needs to be of type String or ByStr`
)
);
return;
}
};
strrev = (s) => {
if (s instanceof StringLit) {
return new StringLit(reverseString(s.s));
} else if (s instanceof Bystr || s instanceof BystrX) {
return new StringLit(reverseString(s.s));
} else {
setError(
new Error(
`Error: builtin strrev s ${s} needs to be of type String or ByStr or ByStrX`
)
);
return;
}
};
to_ascii = (h) => {
console.log(`to_ascii TODO`);
};
put = (m) => (k) => (v) => {
if (m instanceof Map) {
const mapKey = this.getJSValueFromLiteral(k);
const newMap = _.cloneDeep(m);
newMap.update(mapKey, v);
return newMap;
} else {
setError(new Error(`Error: builtin put m ${m} needs to be of type Map`));
return;
}
};
get = (m) => (k) => {
if (m instanceof Map) {
const mapKey = this.getJSValueFromLiteral(k);
if (_.has(m.kv, mapKey)) {
return new ADTValue(
"Some",
[literalType(m.kv[mapKey])],
[m.kv[mapKey]]
);
} else {
return new ADTValue("None", [], []);
}
} else {
setError(new Error(`Error: builtin get m ${m} needs to be of type Map`));
return;
}
};
contains = (m) => (k) => {
if (m instanceof Map) {
const mapKey = this.getJSValueFromLiteral(k);
return _.has(m.kv, mapKey)
? new ADTValue("True", [], [])
: new ADTValue("False", [], []);
} else {
setError(
new Error(`Error: builtin contains m ${m} needs to be of type Map`)
);
return;
}
};
remove = (m) => (k) => {
if (m instanceof Map) {
const mapKey = this.getJSValueFromLiteral(k);
const newMap = _.cloneDeep(m);
if (_.has(m.kv, mapKey)) {
delete newMap.kv[mapKey];
}
return newMap;
} else {
setError(
new Error(`Error: builtin remove m ${m} needs to be of type Map`)
);
return;
}
};
to_list = (m) => {
if (m instanceof Map) {
const pairTyp1 = m.mtyp.t1;
const pairTyp2 = m.mtyp.t2;
const keys = Object.keys(m.kv);
// const res = keys.reduce(
// (prev, curr) =>
// new ADTValue("Pair", [pairTyp1, pairTyp2], [m.kv[curr], prev]),
// new ADTValue("Nil", [pairTyp1, pairTyp2], [])
// );
// return res;
}
console.log("MAP to_list TODO");
};
size = (m) => {
if (m instanceof Map) {
return Object.keys(m).length;
} else {
setError(new Error(`Error: builtin size m ${m} needs to be of type Map`));
return;
}
};
blt = (b1) => (b2) => {
console.log("BNum blt todo");
};
badd = (b1) => (i1) => {
console.log("BNum badd todo");
};
bsub = (b1) => (b2) => {
console.log("BNum bsub todo");
};
to_bystr = (h) => {
console.log("to_bystr todo");
};
}