@nyariv/sandboxjs 0.8.21 → 0.8.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/npm-publish.yml +2 -2
- package/build/Sandbox.d.ts +5 -4
- package/build/Sandbox.js +3 -3
- package/build/executor.d.ts +46 -24
- package/build/executor.js +627 -624
- package/build/parser.d.ts +321 -52
- package/build/parser.js +217 -157
- package/dist/Sandbox.d.ts +5 -4
- package/dist/Sandbox.js +1 -1
- package/dist/Sandbox.js.map +1 -1
- package/dist/executor.d.ts +46 -24
- package/dist/node/Sandbox.d.ts +5 -4
- package/dist/node/Sandbox.js +844 -784
- package/dist/node/executor.d.ts +46 -24
- package/dist/node/parser.d.ts +321 -52
- package/dist/parser.d.ts +321 -52
- package/package.json +14 -15
- package/rollup.config.mjs +33 -0
- package/dist/Sandbox.min.js +0 -2
- package/dist/Sandbox.min.js.map +0 -1
package/build/parser.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import unraw from "./unraw.js";
|
|
2
|
+
function createLisp(obj) {
|
|
3
|
+
return [obj.op, obj.a, obj.b];
|
|
4
|
+
}
|
|
2
5
|
let lispTypes = new Map();
|
|
3
6
|
export class ParseError extends Error {
|
|
4
7
|
constructor(message, code) {
|
|
@@ -6,40 +9,6 @@ export class ParseError extends Error {
|
|
|
6
9
|
this.code = code;
|
|
7
10
|
}
|
|
8
11
|
}
|
|
9
|
-
export class Lisp {
|
|
10
|
-
constructor(obj) {
|
|
11
|
-
this.op = obj.op;
|
|
12
|
-
this.a = obj.a;
|
|
13
|
-
this.b = obj.b;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
export class If {
|
|
17
|
-
constructor(t, f) {
|
|
18
|
-
this.t = t;
|
|
19
|
-
this.f = f;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
export class KeyVal {
|
|
23
|
-
constructor(key, val) {
|
|
24
|
-
this.key = key;
|
|
25
|
-
this.val = val;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
export class SpreadObject {
|
|
29
|
-
constructor(item) {
|
|
30
|
-
this.item = item;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
export class SpreadArray {
|
|
34
|
-
constructor(item) {
|
|
35
|
-
this.item = item;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
export const lispArrayKey = {};
|
|
39
|
-
export function toLispArray(arr) {
|
|
40
|
-
arr.lisp = lispArrayKey;
|
|
41
|
-
return arr;
|
|
42
|
-
}
|
|
43
12
|
const inlineIfElse = /^:/;
|
|
44
13
|
const elseIf = /^else(?![\w\$])/;
|
|
45
14
|
const ifElse = /^if(?![\w\$])/;
|
|
@@ -162,7 +131,6 @@ export let expectTypes = {
|
|
|
162
131
|
void: /^void(?![\w\$])\s*/,
|
|
163
132
|
await: /^await(?![\w\$])\s*/,
|
|
164
133
|
new: /^new(?![\w\$])\s*/,
|
|
165
|
-
throw: /^throw(?![\w\$])\s*/
|
|
166
134
|
},
|
|
167
135
|
next: [
|
|
168
136
|
'splitter',
|
|
@@ -176,6 +144,7 @@ export let expectTypes = {
|
|
|
176
144
|
types: {
|
|
177
145
|
initialize: /^(var|let|const)\s+([a-zA-Z\$_][a-zA-Z\d\$_]*)\s*(=)?/,
|
|
178
146
|
return: /^return(?![\w\$])/,
|
|
147
|
+
throw: /^throw(?![\w\$])\s*/
|
|
179
148
|
},
|
|
180
149
|
next: [
|
|
181
150
|
'modifier',
|
|
@@ -504,6 +473,16 @@ const closingsCreate = {
|
|
|
504
473
|
'arrayProp': /^\]/,
|
|
505
474
|
'call': /^\)/
|
|
506
475
|
};
|
|
476
|
+
const typesCreate = {
|
|
477
|
+
'createArray': 12 /* LispType.CreateArray */,
|
|
478
|
+
'createObject': 22 /* LispType.CreateObject */,
|
|
479
|
+
'group': 23 /* LispType.Group */,
|
|
480
|
+
'arrayProp': 19 /* LispType.ArrayProp */,
|
|
481
|
+
'call': 5 /* LispType.Call */,
|
|
482
|
+
'prop': 1 /* LispType.Prop */,
|
|
483
|
+
'?prop': 20 /* LispType.PropOptional */,
|
|
484
|
+
'?call': 21 /* LispType.CallOptional */,
|
|
485
|
+
};
|
|
507
486
|
setLispType(['createArray', 'createObject', 'group', 'arrayProp', 'call'], (constants, type, part, res, expect, ctx) => {
|
|
508
487
|
let extract = emptyString;
|
|
509
488
|
let arg = [];
|
|
@@ -516,7 +495,7 @@ setLispType(['createArray', 'createObject', 'group', 'arrayProp', 'call'], (cons
|
|
|
516
495
|
/^,/
|
|
517
496
|
]);
|
|
518
497
|
i += extract.length;
|
|
519
|
-
if (extract.length) {
|
|
498
|
+
if (extract.trim().length) {
|
|
520
499
|
arg.push(extract);
|
|
521
500
|
}
|
|
522
501
|
if (part.char(i) !== ',') {
|
|
@@ -537,13 +516,13 @@ setLispType(['createArray', 'createObject', 'group', 'arrayProp', 'call'], (cons
|
|
|
537
516
|
case 'call':
|
|
538
517
|
case 'createArray':
|
|
539
518
|
// @TODO: support 'empty' values
|
|
540
|
-
l =
|
|
519
|
+
l = arg.map((e) => lispify(constants, e, [...next, 'spreadArray']));
|
|
541
520
|
break;
|
|
542
521
|
case 'createObject':
|
|
543
|
-
l =
|
|
522
|
+
l = arg.map((str) => {
|
|
544
523
|
str = str.trimStart();
|
|
545
524
|
let value;
|
|
546
|
-
let key;
|
|
525
|
+
let key = '';
|
|
547
526
|
funcFound = expectTypes.expFunction.types.function.exec('function ' + str);
|
|
548
527
|
if (funcFound) {
|
|
549
528
|
key = funcFound[2].trimStart();
|
|
@@ -552,56 +531,111 @@ setLispType(['createArray', 'createObject', 'group', 'arrayProp', 'call'], (cons
|
|
|
552
531
|
else {
|
|
553
532
|
let extract = restOfExp(constants, str, [/^:/]);
|
|
554
533
|
key = lispify(constants, extract, [...next, 'spreadObject']);
|
|
555
|
-
if (key
|
|
556
|
-
key = key
|
|
534
|
+
if (key[0] === 1 /* LispType.Prop */) {
|
|
535
|
+
key = key[2];
|
|
557
536
|
}
|
|
558
|
-
if (extract.length === str.length)
|
|
559
|
-
return key;
|
|
560
537
|
value = lispify(constants, str.substring(extract.length + 1));
|
|
561
538
|
}
|
|
562
|
-
return
|
|
563
|
-
op:
|
|
539
|
+
return createLisp({
|
|
540
|
+
op: 6 /* LispType.KeyVal */,
|
|
564
541
|
a: key,
|
|
565
542
|
b: value
|
|
566
543
|
});
|
|
567
|
-
})
|
|
544
|
+
});
|
|
568
545
|
break;
|
|
569
546
|
}
|
|
570
|
-
|
|
571
|
-
ctx.lispTree = lispify(constants, part.substring(i + 1), expectTypes[expect].next,
|
|
572
|
-
op:
|
|
547
|
+
let lisptype = (type === 'arrayProp' ? (res[1] ? 20 /* LispType.PropOptional */ : 1 /* LispType.Prop */) : (type === 'call' ? (res[1] ? 21 /* LispType.CallOptional */ : 5 /* LispType.Call */) : typesCreate[type]));
|
|
548
|
+
ctx.lispTree = lispify(constants, part.substring(i + 1), expectTypes[expect].next, createLisp({
|
|
549
|
+
op: lisptype,
|
|
573
550
|
a: ctx.lispTree,
|
|
574
551
|
b: l,
|
|
575
552
|
}));
|
|
576
553
|
});
|
|
554
|
+
const modifierTypes = {
|
|
555
|
+
'inverse': 64 /* LispType.Inverse */,
|
|
556
|
+
'not': 24 /* LispType.Not */,
|
|
557
|
+
'positive': 59 /* LispType.Positive */,
|
|
558
|
+
'negative': 58 /* LispType.Negative */,
|
|
559
|
+
'typeof': 60 /* LispType.Typeof */,
|
|
560
|
+
'delete': 61 /* LispType.Delete */
|
|
561
|
+
};
|
|
577
562
|
setLispType(['inverse', 'not', 'negative', 'positive', 'typeof', 'delete'], (constants, type, part, res, expect, ctx) => {
|
|
578
563
|
let extract = restOfExp(constants, part.substring(res[0].length), [/^([^\s\.\?\w\$]|\?[^\.])/]);
|
|
579
|
-
ctx.lispTree = lispify(constants, part.substring(extract.length + res[0].length), restOfExp.next,
|
|
580
|
-
op: [
|
|
564
|
+
ctx.lispTree = lispify(constants, part.substring(extract.length + res[0].length), restOfExp.next, createLisp({
|
|
565
|
+
op: modifierTypes[type],
|
|
581
566
|
a: ctx.lispTree,
|
|
582
567
|
b: lispify(constants, extract, expectTypes[expect].next),
|
|
583
568
|
}));
|
|
584
569
|
});
|
|
570
|
+
const incrementTypes = {
|
|
571
|
+
'++$': 25 /* LispType.IncrementBefore */,
|
|
572
|
+
'--$': 27 /* LispType.DecrementBefore */,
|
|
573
|
+
'$++': 26 /* LispType.IncrementAfter */,
|
|
574
|
+
'$--': 28 /* LispType.DecrementAfter */
|
|
575
|
+
};
|
|
585
576
|
setLispType(['incrementerBefore'], (constants, type, part, res, expect, ctx) => {
|
|
586
577
|
let extract = restOfExp(constants, part.substring(2), [/^[^\s\.\w\$]/]);
|
|
587
|
-
ctx.lispTree = lispify(constants, part.substring(extract.length + 2), restOfExp.next,
|
|
588
|
-
op: res[0] + "$",
|
|
578
|
+
ctx.lispTree = lispify(constants, part.substring(extract.length + 2), restOfExp.next, createLisp({
|
|
579
|
+
op: incrementTypes[res[0] + "$"],
|
|
589
580
|
a: lispify(constants, extract, expectTypes[expect].next),
|
|
581
|
+
b: 0 /* LispType.None */
|
|
590
582
|
}));
|
|
591
583
|
});
|
|
592
584
|
setLispType(['incrementerAfter'], (constants, type, part, res, expect, ctx) => {
|
|
593
|
-
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next,
|
|
594
|
-
op: "$" + res[0],
|
|
585
|
+
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next, createLisp({
|
|
586
|
+
op: incrementTypes["$" + res[0]],
|
|
595
587
|
a: ctx.lispTree,
|
|
588
|
+
b: 0 /* LispType.None */
|
|
596
589
|
}));
|
|
597
590
|
});
|
|
591
|
+
const adderTypes = {
|
|
592
|
+
'&&': 29 /* LispType.And */,
|
|
593
|
+
'||': 30 /* LispType.Or */,
|
|
594
|
+
'instanceof': 62 /* LispType.Instanceof */,
|
|
595
|
+
'in': 63 /* LispType.In */,
|
|
596
|
+
'=': 9 /* LispType.Assign */,
|
|
597
|
+
'-=': 65 /* LispType.SubractEquals */,
|
|
598
|
+
'+=': 66 /* LispType.AddEquals */,
|
|
599
|
+
'/=': 67 /* LispType.DivideEquals */,
|
|
600
|
+
'**=': 68 /* LispType.PowerEquals */,
|
|
601
|
+
'*=': 69 /* LispType.MultiplyEquals */,
|
|
602
|
+
'%=': 70 /* LispType.ModulusEquals */,
|
|
603
|
+
'^=': 71 /* LispType.BitNegateEquals */,
|
|
604
|
+
'&=': 72 /* LispType.BitAndEquals */,
|
|
605
|
+
'|=': 73 /* LispType.BitOrEquals */,
|
|
606
|
+
'>>>=': 74 /* LispType.UnsignedShiftRightEquals */,
|
|
607
|
+
'<<=': 76 /* LispType.ShiftLeftEquals */,
|
|
608
|
+
'>>=': 75 /* LispType.ShiftRightEquals */,
|
|
609
|
+
};
|
|
598
610
|
setLispType(['assign', 'assignModify', 'boolOp'], (constants, type, part, res, expect, ctx) => {
|
|
599
|
-
ctx.lispTree =
|
|
600
|
-
op: res[0],
|
|
611
|
+
ctx.lispTree = createLisp({
|
|
612
|
+
op: adderTypes[res[0]],
|
|
601
613
|
a: ctx.lispTree,
|
|
602
614
|
b: lispify(constants, part.substring(res[0].length), expectTypes[expect].next)
|
|
603
615
|
});
|
|
604
616
|
});
|
|
617
|
+
const opTypes = {
|
|
618
|
+
'&': 77 /* LispType.BitAnd */,
|
|
619
|
+
'|': 78 /* LispType.BitOr */,
|
|
620
|
+
'^': 79 /* LispType.BitNegate */,
|
|
621
|
+
'<<': 80 /* LispType.BitShiftLeft */,
|
|
622
|
+
'>>': 81 /* LispType.BitShiftRight */,
|
|
623
|
+
'>>>': 82 /* LispType.BitUnsignedShiftRight */,
|
|
624
|
+
'<=': 54 /* LispType.SmallerEqualThan */,
|
|
625
|
+
'>=': 55 /* LispType.LargerEqualThan */,
|
|
626
|
+
'<': 56 /* LispType.SmallerThan */,
|
|
627
|
+
'>': 57 /* LispType.LargerThan */,
|
|
628
|
+
'!==': 31 /* LispType.StrictNotEqual */,
|
|
629
|
+
'!=': 53 /* LispType.NotEqual */,
|
|
630
|
+
'===': 32 /* LispType.StrictEqual */,
|
|
631
|
+
'==': 52 /* LispType.Equal */,
|
|
632
|
+
'+': 33 /* LispType.Plus */,
|
|
633
|
+
'-': 47 /* LispType.Minus */,
|
|
634
|
+
'/': 48 /* LispType.Divide */,
|
|
635
|
+
'**': 49 /* LispType.Power */,
|
|
636
|
+
'*': 50 /* LispType.Multiply */,
|
|
637
|
+
'%': 51 /* LispType.Modulus */,
|
|
638
|
+
};
|
|
605
639
|
setLispType(['opHigh', 'op', 'comparitor', 'bitwise'], (constants, type, part, res, expect, ctx) => {
|
|
606
640
|
const next = [
|
|
607
641
|
expectTypes.inlineIf.types.inlineIf,
|
|
@@ -619,8 +653,8 @@ setLispType(['opHigh', 'op', 'comparitor', 'bitwise'], (constants, type, part, r
|
|
|
619
653
|
next.push(expectTypes.splitter.types.boolOp);
|
|
620
654
|
}
|
|
621
655
|
let extract = restOfExp(constants, part.substring(res[0].length), next);
|
|
622
|
-
ctx.lispTree = lispify(constants, part.substring(extract.length + res[0].length), restOfExp.next,
|
|
623
|
-
op: res[0],
|
|
656
|
+
ctx.lispTree = lispify(constants, part.substring(extract.length + res[0].length), restOfExp.next, createLisp({
|
|
657
|
+
op: opTypes[res[0]],
|
|
624
658
|
a: ctx.lispTree,
|
|
625
659
|
b: lispify(constants, extract, expectTypes[expect].next),
|
|
626
660
|
}));
|
|
@@ -645,14 +679,17 @@ setLispType(['inlineIf'], (constants, type, part, res, expect, ctx) => {
|
|
|
645
679
|
}
|
|
646
680
|
}
|
|
647
681
|
extract.start = part.start + 1;
|
|
648
|
-
ctx.lispTree =
|
|
649
|
-
op:
|
|
682
|
+
ctx.lispTree = createLisp({
|
|
683
|
+
op: 15 /* LispType.InlineIf */,
|
|
650
684
|
a: ctx.lispTree,
|
|
651
|
-
b:
|
|
685
|
+
b: createLisp({
|
|
686
|
+
op: 16 /* LispType.InlineIfCase */,
|
|
687
|
+
a: lispifyExpr(constants, extract),
|
|
688
|
+
b: lispifyExpr(constants, part.substring(res[0].length + extract.length + 1))
|
|
689
|
+
})
|
|
652
690
|
});
|
|
653
691
|
});
|
|
654
692
|
function extractIfElse(constants, part) {
|
|
655
|
-
var _a;
|
|
656
693
|
let count = 0;
|
|
657
694
|
let found = part.substring(0, 0);
|
|
658
695
|
let foundElse = emptyString;
|
|
@@ -688,7 +725,7 @@ function extractIfElse(constants, part) {
|
|
|
688
725
|
break;
|
|
689
726
|
}
|
|
690
727
|
if (!count) {
|
|
691
|
-
let ie = extractIfElse(constants, part.substring(found.end - part.start + (
|
|
728
|
+
let ie = extractIfElse(constants, part.substring(found.end - part.start + (/^;?\s*else(?![\w\$])/.exec(f)?.[0].length)));
|
|
692
729
|
foundElse = ie.all;
|
|
693
730
|
break;
|
|
694
731
|
}
|
|
@@ -711,10 +748,14 @@ setLispType(['if'], (constants, type, part, res, expect, ctx) => {
|
|
|
711
748
|
trueBlock = trueBlock.slice(1, -1);
|
|
712
749
|
if (elseBlock.char(0) === "{")
|
|
713
750
|
elseBlock = elseBlock.slice(1, -1);
|
|
714
|
-
ctx.lispTree =
|
|
715
|
-
op:
|
|
751
|
+
ctx.lispTree = createLisp({
|
|
752
|
+
op: 13 /* LispType.If */,
|
|
716
753
|
a: lispifyExpr(constants, condition),
|
|
717
|
-
b:
|
|
754
|
+
b: createLisp({
|
|
755
|
+
op: 14 /* LispType.IfCase */,
|
|
756
|
+
a: lispifyBlock(trueBlock, constants),
|
|
757
|
+
b: lispifyBlock(elseBlock, constants)
|
|
758
|
+
})
|
|
718
759
|
});
|
|
719
760
|
});
|
|
720
761
|
setLispType(['switch'], (constants, type, part, res, expect, ctx) => {
|
|
@@ -761,16 +802,16 @@ setLispType(['switch'], (constants, type, part, res, expect, ctx) => {
|
|
|
761
802
|
}
|
|
762
803
|
}
|
|
763
804
|
statement = statement.substring(i);
|
|
764
|
-
cases.push(
|
|
765
|
-
op:
|
|
805
|
+
cases.push(createLisp({
|
|
806
|
+
op: 41 /* LispType.SwitchCase */,
|
|
766
807
|
a: caseFound[1] === "default" ? undefined : lispifyExpr(constants, cond),
|
|
767
|
-
b:
|
|
808
|
+
b: exprs
|
|
768
809
|
}));
|
|
769
810
|
}
|
|
770
|
-
ctx.lispTree =
|
|
771
|
-
op:
|
|
811
|
+
ctx.lispTree = createLisp({
|
|
812
|
+
op: 40 /* LispType.Switch */,
|
|
772
813
|
a: lispifyExpr(constants, test),
|
|
773
|
-
b:
|
|
814
|
+
b: cases
|
|
774
815
|
});
|
|
775
816
|
});
|
|
776
817
|
setLispType(['dot', 'prop'], (constants, type, part, res, expect, ctx) => {
|
|
@@ -790,51 +831,52 @@ setLispType(['dot', 'prop'], (constants, type, part, res, expect, ctx) => {
|
|
|
790
831
|
throw new SyntaxError('Hanging dot');
|
|
791
832
|
}
|
|
792
833
|
}
|
|
793
|
-
ctx.lispTree = lispify(constants, part.substring(index), expectTypes[expect].next,
|
|
794
|
-
op: op,
|
|
834
|
+
ctx.lispTree = lispify(constants, part.substring(index), expectTypes[expect].next, createLisp({
|
|
835
|
+
op: typesCreate[op],
|
|
795
836
|
a: ctx.lispTree,
|
|
796
837
|
b: prop
|
|
797
838
|
}));
|
|
798
839
|
});
|
|
799
840
|
setLispType(['spreadArray', 'spreadObject'], (constants, type, part, res, expect, ctx) => {
|
|
800
|
-
ctx.lispTree =
|
|
801
|
-
op: type
|
|
841
|
+
ctx.lispTree = createLisp({
|
|
842
|
+
op: type === 'spreadArray' ? 18 /* LispType.SpreadArray */ : 17 /* LispType.SpreadObject */,
|
|
843
|
+
a: 0 /* LispType.None */,
|
|
802
844
|
b: lispify(constants, part.substring(res[0].length), expectTypes[expect].next)
|
|
803
845
|
});
|
|
804
846
|
});
|
|
805
847
|
setLispType(['return', 'throw'], (constants, type, part, res, expect, ctx) => {
|
|
806
|
-
ctx.lispTree =
|
|
807
|
-
op: type
|
|
848
|
+
ctx.lispTree = createLisp({
|
|
849
|
+
op: type === 'return' ? 8 /* LispType.Return */ : 46 /* LispType.Throw */,
|
|
850
|
+
a: 0 /* LispType.None */,
|
|
808
851
|
b: lispifyExpr(constants, part.substring(res[0].length))
|
|
809
852
|
});
|
|
810
853
|
});
|
|
811
|
-
const primitives = {
|
|
812
|
-
"true": true,
|
|
813
|
-
"false": false,
|
|
814
|
-
"null": null,
|
|
815
|
-
Infinity,
|
|
816
|
-
NaN,
|
|
817
|
-
"und": undefined
|
|
818
|
-
};
|
|
819
854
|
setLispType(['number', 'boolean', 'null', 'und', 'NaN', 'Infinity'], (constants, type, part, res, expect, ctx) => {
|
|
820
|
-
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next,
|
|
855
|
+
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next, createLisp({
|
|
856
|
+
op: type === "number" ? res[10] ? 83 /* LispType.BigInt */ : 7 /* LispType.Number */ : 35 /* LispType.GlobalSymbol */,
|
|
857
|
+
a: 0 /* LispType.None */,
|
|
858
|
+
b: res[10] ? res[1] : res[0]
|
|
859
|
+
}));
|
|
821
860
|
});
|
|
822
861
|
setLispType(['string', 'literal', 'regex'], (constants, type, part, res, expect, ctx) => {
|
|
823
|
-
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next,
|
|
824
|
-
op: type
|
|
825
|
-
|
|
862
|
+
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next, createLisp({
|
|
863
|
+
op: type === 'string' ? 2 /* LispType.StringIndex */ : type === 'literal' ? 84 /* LispType.LiteralIndex */ : 85 /* LispType.RegexIndex */,
|
|
864
|
+
a: 0 /* LispType.None */,
|
|
865
|
+
b: res[1],
|
|
826
866
|
}));
|
|
827
867
|
});
|
|
828
868
|
setLispType(['initialize'], (constants, type, part, res, expect, ctx) => {
|
|
869
|
+
const lt = res[1] === 'var' ? 34 /* LispType.Var */ : res[1] === 'let' ? 3 /* LispType.Let */ : 4 /* LispType.Const */;
|
|
829
870
|
if (!res[3]) {
|
|
830
|
-
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next,
|
|
831
|
-
op:
|
|
832
|
-
a: res[2]
|
|
871
|
+
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next, createLisp({
|
|
872
|
+
op: lt,
|
|
873
|
+
a: res[2],
|
|
874
|
+
b: 0 /* LispType.None */
|
|
833
875
|
}));
|
|
834
876
|
}
|
|
835
877
|
else {
|
|
836
|
-
ctx.lispTree =
|
|
837
|
-
op:
|
|
878
|
+
ctx.lispTree = createLisp({
|
|
879
|
+
op: lt,
|
|
838
880
|
a: res[2],
|
|
839
881
|
b: lispify(constants, part.substring(res[0].length), expectTypes[expect].next)
|
|
840
882
|
});
|
|
@@ -844,7 +886,7 @@ setLispType(['function', 'inlineFunction', 'arrowFunction', 'arrowFunctionSingle
|
|
|
844
886
|
const isArrow = type !== 'function' && type !== 'inlineFunction';
|
|
845
887
|
const isReturn = isArrow && !res[res.length - 1];
|
|
846
888
|
const argPos = isArrow ? 2 : 3;
|
|
847
|
-
const isAsync =
|
|
889
|
+
const isAsync = res[1] ? 88 /* LispType.True */ : 0 /* LispType.None */;
|
|
848
890
|
const args = res[argPos] ? res[argPos].replace(/\s+/g, "").split(/,/g) : [];
|
|
849
891
|
if (!isArrow) {
|
|
850
892
|
args.unshift((res[2] || "").trimStart());
|
|
@@ -858,23 +900,23 @@ setLispType(['function', 'inlineFunction', 'arrowFunction', 'arrowFunctionSingle
|
|
|
858
900
|
});
|
|
859
901
|
args.unshift(isAsync);
|
|
860
902
|
const f = restOfExp(constants, part.substring(res[0].length), !isReturn ? [/^}/] : [/^[,\)\}\]]/, semiColon]);
|
|
861
|
-
const func = (isReturn ? 'return ' + f : f);
|
|
862
|
-
ctx.lispTree = lispify(constants, part.substring(res[0].length + func.length + 1), expectTypes[expect].next,
|
|
863
|
-
op: isArrow ? '
|
|
864
|
-
a:
|
|
903
|
+
const func = (isReturn ? 'return ' + f : f.toString());
|
|
904
|
+
ctx.lispTree = lispify(constants, part.substring(res[0].length + func.length + 1), expectTypes[expect].next, createLisp({
|
|
905
|
+
op: isArrow ? 11 /* LispType.ArrowFunction */ : type === 'function' ? 37 /* LispType.Function */ : 10 /* LispType.InlineFunction */,
|
|
906
|
+
a: args,
|
|
865
907
|
b: constants.eager ? lispifyFunction(new CodeString(func), constants) : func
|
|
866
908
|
}));
|
|
867
909
|
});
|
|
868
910
|
const iteratorRegex = /^((let|var|const)\s+)?\s*([a-zA-Z\$_][a-zA-Z\d\$_]*)\s+(in|of)(?![\w\$])/;
|
|
869
911
|
setLispType(['for', 'do', 'while'], (constants, type, part, res, expect, ctx) => {
|
|
870
912
|
let i = 0;
|
|
871
|
-
let startStep =
|
|
872
|
-
let startInternal =
|
|
913
|
+
let startStep = 88 /* LispType.True */;
|
|
914
|
+
let startInternal = [];
|
|
873
915
|
let getIterator;
|
|
874
|
-
let beforeStep =
|
|
875
|
-
let checkFirst =
|
|
916
|
+
let beforeStep = 0 /* LispType.None */;
|
|
917
|
+
let checkFirst = 88 /* LispType.True */;
|
|
876
918
|
let condition;
|
|
877
|
-
let step =
|
|
919
|
+
let step = 88 /* LispType.True */;
|
|
878
920
|
let body;
|
|
879
921
|
switch (type) {
|
|
880
922
|
case 'while':
|
|
@@ -900,20 +942,20 @@ setLispType(['for', 'do', 'while'], (constants, type, part, res, expect, ctx) =>
|
|
|
900
942
|
if (args.length === 1 && (iterator = iteratorRegex.exec(args[0].toString()))) {
|
|
901
943
|
if (iterator[4] === 'of') {
|
|
902
944
|
getIterator = lispifyReturnExpr(constants, args[0].substring(iterator[0].length)),
|
|
903
|
-
startInternal =
|
|
945
|
+
startInternal = [
|
|
904
946
|
ofStart2,
|
|
905
947
|
ofStart3
|
|
906
|
-
]
|
|
948
|
+
];
|
|
907
949
|
condition = ofCondition;
|
|
908
950
|
step = ofStep;
|
|
909
951
|
beforeStep = lispify(constants, new CodeString((iterator[1] || 'let ') + iterator[3] + ' = $$next.value'), ['initialize']);
|
|
910
952
|
}
|
|
911
953
|
else {
|
|
912
954
|
getIterator = lispifyReturnExpr(constants, args[0].substring(iterator[0].length)),
|
|
913
|
-
startInternal =
|
|
955
|
+
startInternal = [
|
|
914
956
|
inStart2,
|
|
915
957
|
inStart3
|
|
916
|
-
]
|
|
958
|
+
];
|
|
917
959
|
step = inStep;
|
|
918
960
|
condition = inCondition;
|
|
919
961
|
beforeStep = lispify(constants, new CodeString((iterator[1] || 'let ') + iterator[3] + ' = $$keys[$$keyIndex]'), ['initialize']);
|
|
@@ -932,26 +974,31 @@ setLispType(['for', 'do', 'while'], (constants, type, part, res, expect, ctx) =>
|
|
|
932
974
|
body = body.slice(1, -1);
|
|
933
975
|
break;
|
|
934
976
|
case 'do':
|
|
935
|
-
checkFirst =
|
|
977
|
+
checkFirst = 0 /* LispType.None */;
|
|
936
978
|
const isBlock = !!res[3];
|
|
937
979
|
body = restOfExp(constants, part.substring(res[0].length), isBlock ? [/^\}/] : [semiColon]);
|
|
938
980
|
condition = lispifyReturnExpr(constants, restOfExp(constants, part.substring(part.toString().indexOf("(", res[0].length + body.length) + 1), [], "("));
|
|
939
981
|
break;
|
|
940
982
|
}
|
|
941
|
-
const a =
|
|
942
|
-
ctx.lispTree =
|
|
943
|
-
op:
|
|
983
|
+
const a = [checkFirst, startInternal, getIterator, startStep, step, condition, beforeStep];
|
|
984
|
+
ctx.lispTree = createLisp({
|
|
985
|
+
op: 38 /* LispType.Loop */,
|
|
944
986
|
a,
|
|
945
987
|
b: lispifyBlock(body, constants)
|
|
946
988
|
});
|
|
947
989
|
});
|
|
948
990
|
setLispType(['block'], (constants, type, part, res, expect, ctx) => {
|
|
949
|
-
ctx.lispTree =
|
|
991
|
+
ctx.lispTree = createLisp({
|
|
992
|
+
op: 42 /* LispType.Block */,
|
|
993
|
+
a: lispifyBlock(restOfExp(constants, part.substring(1), [], "{"), constants),
|
|
994
|
+
b: 0 /* LispType.None */
|
|
995
|
+
});
|
|
950
996
|
});
|
|
951
997
|
setLispType(['loopAction'], (constants, type, part, res, expect, ctx) => {
|
|
952
|
-
ctx.lispTree =
|
|
953
|
-
op:
|
|
998
|
+
ctx.lispTree = createLisp({
|
|
999
|
+
op: 86 /* LispType.LoopAction */,
|
|
954
1000
|
a: res[1],
|
|
1001
|
+
b: 0 /* LispType.None */
|
|
955
1002
|
});
|
|
956
1003
|
});
|
|
957
1004
|
const catchReg = /^\s*(catch\s*(\(\s*([a-zA-Z\$_][a-zA-Z\d\$_]*)\s*\))?|finally)\s*\{/;
|
|
@@ -959,7 +1006,7 @@ setLispType(['try'], (constants, type, part, res, expect, ctx) => {
|
|
|
959
1006
|
const body = restOfExp(constants, part.substring(res[0].length), [], "{");
|
|
960
1007
|
let catchRes = catchReg.exec(part.substring(res[0].length + body.length + 1).toString());
|
|
961
1008
|
let finallyBody;
|
|
962
|
-
let exception;
|
|
1009
|
+
let exception = "";
|
|
963
1010
|
let catchBody;
|
|
964
1011
|
let offset = 0;
|
|
965
1012
|
if (catchRes[1].startsWith('catch')) {
|
|
@@ -974,22 +1021,23 @@ setLispType(['try'], (constants, type, part, res, expect, ctx) => {
|
|
|
974
1021
|
else {
|
|
975
1022
|
finallyBody = restOfExp(constants, part.substring(res[0].length + body.length + 1 + catchRes[0].length), [], "{");
|
|
976
1023
|
}
|
|
977
|
-
const b =
|
|
1024
|
+
const b = [
|
|
978
1025
|
exception,
|
|
979
1026
|
lispifyBlock(insertSemicolons(constants, catchBody || emptyString), constants),
|
|
980
1027
|
lispifyBlock(insertSemicolons(constants, finallyBody || emptyString), constants),
|
|
981
|
-
]
|
|
982
|
-
ctx.lispTree =
|
|
983
|
-
op:
|
|
1028
|
+
];
|
|
1029
|
+
ctx.lispTree = createLisp({
|
|
1030
|
+
op: 39 /* LispType.Try */,
|
|
984
1031
|
a: lispifyBlock(insertSemicolons(constants, body), constants),
|
|
985
1032
|
b
|
|
986
1033
|
});
|
|
987
1034
|
});
|
|
988
1035
|
setLispType(['void', 'await'], (constants, type, part, res, expect, ctx) => {
|
|
989
1036
|
const extract = restOfExp(constants, part.substring(res[0].length), [/^([^\s\.\?\w\$]|\?[^\.])/]);
|
|
990
|
-
ctx.lispTree = lispify(constants, part.substring(res[0].length + extract.length), expectTypes[expect].next,
|
|
991
|
-
op: type
|
|
1037
|
+
ctx.lispTree = lispify(constants, part.substring(res[0].length + extract.length), expectTypes[expect].next, createLisp({
|
|
1038
|
+
op: type === 'void' ? 87 /* LispType.Void */ : 44 /* LispType.Await */,
|
|
992
1039
|
a: lispify(constants, extract),
|
|
1040
|
+
b: 0 /* LispType.None */
|
|
993
1041
|
}));
|
|
994
1042
|
});
|
|
995
1043
|
setLispType(['new'], (constants, type, part, res, expect, ctx) => {
|
|
@@ -1007,10 +1055,10 @@ setLispType(['new'], (constants, type, part, res, expect, ctx) => {
|
|
|
1007
1055
|
args.push(found.trim());
|
|
1008
1056
|
}
|
|
1009
1057
|
}
|
|
1010
|
-
ctx.lispTree = lispify(constants, part.substring(i), expectTypes.expEdge.next,
|
|
1011
|
-
op:
|
|
1058
|
+
ctx.lispTree = lispify(constants, part.substring(i), expectTypes.expEdge.next, createLisp({
|
|
1059
|
+
op: 45 /* LispType.New */,
|
|
1012
1060
|
a: lispify(constants, obj, expectTypes.initialize.next),
|
|
1013
|
-
b:
|
|
1061
|
+
b: args.map((arg) => lispify(constants, arg, expectTypes.initialize.next)),
|
|
1014
1062
|
}));
|
|
1015
1063
|
});
|
|
1016
1064
|
const ofStart2 = lispify(undefined, new CodeString('let $$iterator = $$obj[Symbol.iterator]()'), ['initialize']);
|
|
@@ -1027,6 +1075,7 @@ var lastLastPart;
|
|
|
1027
1075
|
var lastLastLastPart;
|
|
1028
1076
|
var lastLastLastLastPart;
|
|
1029
1077
|
function lispify(constants, part, expected, lispTree, topLevel = false) {
|
|
1078
|
+
lispTree = lispTree || [0 /* LispType.None */, 0 /* LispType.None */, 0 /* LispType.None */];
|
|
1030
1079
|
expected = expected || expectTypes.initialize.next;
|
|
1031
1080
|
if (part === undefined)
|
|
1032
1081
|
return lispTree;
|
|
@@ -1102,22 +1151,26 @@ function lispifyExpr(constants, str, expected) {
|
|
|
1102
1151
|
if (expected.includes('initialize')) {
|
|
1103
1152
|
let defined = expectTypes.initialize.types.initialize.exec(subExpressions[0].toString());
|
|
1104
1153
|
if (defined) {
|
|
1105
|
-
return
|
|
1154
|
+
return createLisp({
|
|
1155
|
+
op: 42 /* LispType.Block */,
|
|
1156
|
+
a: subExpressions.map((str, i) => lispify(constants, i ? new CodeString(defined[1] + ' ' + str) : str, ['initialize'], undefined, true)),
|
|
1157
|
+
b: 0 /* LispType.None */
|
|
1158
|
+
});
|
|
1106
1159
|
}
|
|
1107
1160
|
else if (expectTypes.initialize.types.return.exec(subExpressions[0].toString())) {
|
|
1108
1161
|
return lispify(constants, str, expected, undefined, true);
|
|
1109
1162
|
}
|
|
1110
1163
|
}
|
|
1111
|
-
const exprs =
|
|
1112
|
-
return
|
|
1164
|
+
const exprs = subExpressions.map((str, i) => lispify(constants, str, expected, undefined, true));
|
|
1165
|
+
return createLisp({ op: 43 /* LispType.Expression */, a: exprs, b: 0 /* LispType.None */ });
|
|
1113
1166
|
}
|
|
1114
1167
|
export function lispifyReturnExpr(constants, str) {
|
|
1115
|
-
return
|
|
1168
|
+
return createLisp({ op: 8 /* LispType.Return */, a: 0 /* LispType.None */, b: lispifyExpr(constants, str) });
|
|
1116
1169
|
}
|
|
1117
1170
|
export function lispifyBlock(str, constants, expression = false) {
|
|
1118
1171
|
str = insertSemicolons(constants, str);
|
|
1119
1172
|
if (!str.trim().length)
|
|
1120
|
-
return
|
|
1173
|
+
return [];
|
|
1121
1174
|
let parts = [];
|
|
1122
1175
|
let part;
|
|
1123
1176
|
let pos = 0;
|
|
@@ -1146,20 +1199,37 @@ export function lispifyBlock(str, constants, expression = false) {
|
|
|
1146
1199
|
if (skipped) {
|
|
1147
1200
|
parts.push(str.substring(start, pos - (isInserted ? 0 : 1)));
|
|
1148
1201
|
}
|
|
1149
|
-
return
|
|
1202
|
+
return parts.map((str) => str.trimStart()).filter((str) => str.length).map((str, j) => {
|
|
1150
1203
|
return lispifyExpr(constants, str.trimStart(), startingExecpted);
|
|
1151
|
-
})
|
|
1204
|
+
});
|
|
1152
1205
|
}
|
|
1153
1206
|
export function lispifyFunction(str, constants, expression = false) {
|
|
1154
1207
|
if (!str.trim().length)
|
|
1155
|
-
return
|
|
1208
|
+
return [];
|
|
1156
1209
|
const tree = lispifyBlock(str, constants, expression);
|
|
1157
|
-
let hoisted =
|
|
1210
|
+
let hoisted = [];
|
|
1158
1211
|
hoist(tree, hoisted);
|
|
1159
|
-
return
|
|
1212
|
+
return hoisted.concat(tree);
|
|
1213
|
+
}
|
|
1214
|
+
export function isLisp(item) {
|
|
1215
|
+
return Array.isArray(item) && typeof item[0] === 'number' && item[0] !== 0 /* LispType.None */ && item[0] !== 88 /* LispType.True */;
|
|
1160
1216
|
}
|
|
1161
1217
|
function hoist(item, res) {
|
|
1162
|
-
if (
|
|
1218
|
+
if (isLisp(item)) {
|
|
1219
|
+
const [op, a, b] = item;
|
|
1220
|
+
if (op === 39 /* LispType.Try */ || op === 13 /* LispType.If */ || op === 38 /* LispType.Loop */ || op === 40 /* LispType.Switch */) {
|
|
1221
|
+
hoist(a, res);
|
|
1222
|
+
hoist(b, res);
|
|
1223
|
+
}
|
|
1224
|
+
else if (op === 34 /* LispType.Var */) {
|
|
1225
|
+
res.push(createLisp({ op: 34 /* LispType.Var */, a: a, b: 0 /* LispType.None */ }));
|
|
1226
|
+
}
|
|
1227
|
+
else if (op === 37 /* LispType.Function */ && a[1]) {
|
|
1228
|
+
res.push(item);
|
|
1229
|
+
return true;
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
else if (Array.isArray(item)) {
|
|
1163
1233
|
const rep = [];
|
|
1164
1234
|
for (let it of item) {
|
|
1165
1235
|
if (!hoist(it, res)) {
|
|
@@ -1171,19 +1241,6 @@ function hoist(item, res) {
|
|
|
1171
1241
|
item.push(...rep);
|
|
1172
1242
|
}
|
|
1173
1243
|
}
|
|
1174
|
-
else if (item instanceof Lisp) {
|
|
1175
|
-
if (item.op === "try" || item.op === "if" || item.op === "loop" || item.op === "switch") {
|
|
1176
|
-
hoist(item.a, res);
|
|
1177
|
-
hoist(item.b, res);
|
|
1178
|
-
}
|
|
1179
|
-
else if (item.op === "var") {
|
|
1180
|
-
res.push(new Lisp({ op: 'var', a: item.a }));
|
|
1181
|
-
}
|
|
1182
|
-
else if (item.op === "function" && item.a[1]) {
|
|
1183
|
-
res.push(item);
|
|
1184
|
-
return true;
|
|
1185
|
-
}
|
|
1186
|
-
}
|
|
1187
1244
|
return false;
|
|
1188
1245
|
}
|
|
1189
1246
|
const closingsNoInsertion = /^(\})\s*(catch|finally|else|while|instanceof)(?![\w\$])/;
|
|
@@ -1279,7 +1336,7 @@ export function extractConstants(constants, str, currentEnclosure = "") {
|
|
|
1279
1336
|
let regexFound;
|
|
1280
1337
|
let comment = "";
|
|
1281
1338
|
let commentStart = -1;
|
|
1282
|
-
let currJs =
|
|
1339
|
+
let currJs = [];
|
|
1283
1340
|
let char = "";
|
|
1284
1341
|
const strRes = [];
|
|
1285
1342
|
const enclosures = [];
|
|
@@ -1312,11 +1369,13 @@ export function extractConstants(constants, str, currentEnclosure = "") {
|
|
|
1312
1369
|
}
|
|
1313
1370
|
else if (quote === char) {
|
|
1314
1371
|
if (quote === '`') {
|
|
1315
|
-
|
|
1316
|
-
op:
|
|
1372
|
+
const li = createLisp({
|
|
1373
|
+
op: 36 /* LispType.Literal */,
|
|
1317
1374
|
a: unraw(extract.join("")),
|
|
1318
|
-
b:
|
|
1375
|
+
b: [],
|
|
1319
1376
|
});
|
|
1377
|
+
li.tempJsStrings = currJs;
|
|
1378
|
+
constants.literals.push(li);
|
|
1320
1379
|
strRes.push(`\``, constants.literals.length - 1, `\``);
|
|
1321
1380
|
}
|
|
1322
1381
|
else {
|
|
@@ -1332,7 +1391,7 @@ export function extractConstants(constants, str, currentEnclosure = "") {
|
|
|
1332
1391
|
}
|
|
1333
1392
|
else {
|
|
1334
1393
|
if ((char === "'" || char === '"' || char === '`')) {
|
|
1335
|
-
currJs =
|
|
1394
|
+
currJs = [];
|
|
1336
1395
|
quote = char;
|
|
1337
1396
|
}
|
|
1338
1397
|
else if (closings[currentEnclosure] === char && !enclosures.length) {
|
|
@@ -1383,7 +1442,8 @@ export function parse(code, eager = false, expression = false) {
|
|
|
1383
1442
|
const constants = { strings: [], literals: [], regexes: [], eager };
|
|
1384
1443
|
str = extractConstants(constants, str).str;
|
|
1385
1444
|
for (let l of constants.literals) {
|
|
1386
|
-
l
|
|
1445
|
+
l[2] = l.tempJsStrings.map((js) => lispifyExpr(constants, new CodeString(js)));
|
|
1446
|
+
delete l.tempJsStrings;
|
|
1387
1447
|
}
|
|
1388
1448
|
return { tree: lispifyFunction(new CodeString(str), constants, expression), constants };
|
|
1389
1449
|
}
|