@cuxt/sandboxjs 0.1.1 → 0.1.4
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/LICENSE +21 -21
- package/README.md +198 -185
- package/{build → dist/cjs}/Sandbox.d.ts +15 -6
- package/dist/cjs/Sandbox.js +126 -0
- package/dist/{SandboxExec.d.ts → cjs/SandboxExec.d.ts} +8 -17
- package/dist/cjs/SandboxExec.js +227 -0
- package/{build/eval.d.ts → dist/cjs/eval/index.d.ts} +10 -2
- package/dist/cjs/eval/index.js +233 -0
- package/dist/cjs/executor/executorUtils.d.ts +161 -0
- package/dist/cjs/executor/executorUtils.js +930 -0
- package/dist/cjs/executor/index.d.ts +1 -0
- package/dist/cjs/executor/index.js +2 -0
- package/dist/cjs/executor/ops/assignment.d.ts +1 -0
- package/dist/cjs/executor/ops/assignment.js +88 -0
- package/dist/cjs/executor/ops/call.d.ts +1 -0
- package/dist/cjs/executor/ops/call.js +160 -0
- package/dist/cjs/executor/ops/comparison.d.ts +1 -0
- package/dist/cjs/executor/ops/comparison.js +36 -0
- package/dist/cjs/executor/ops/control.d.ts +1 -0
- package/dist/cjs/executor/ops/control.js +203 -0
- package/dist/cjs/executor/ops/functions.d.ts +1 -0
- package/dist/cjs/executor/ops/functions.js +55 -0
- package/dist/cjs/executor/ops/index.d.ts +0 -0
- package/dist/cjs/executor/ops/index.js +11 -0
- package/dist/cjs/executor/ops/literals.d.ts +1 -0
- package/dist/cjs/executor/ops/literals.js +45 -0
- package/dist/cjs/executor/ops/misc.d.ts +1 -0
- package/dist/cjs/executor/ops/misc.js +25 -0
- package/dist/cjs/executor/ops/object.d.ts +1 -0
- package/dist/cjs/executor/ops/object.js +51 -0
- package/dist/cjs/executor/ops/prop.d.ts +1 -0
- package/dist/cjs/executor/ops/prop.js +70 -0
- package/dist/cjs/executor/ops/unary.d.ts +1 -0
- package/dist/cjs/executor/ops/unary.js +48 -0
- package/dist/cjs/executor/ops/variables.d.ts +1 -0
- package/dist/cjs/executor/ops/variables.js +16 -0
- package/dist/cjs/executor/opsRegistry.d.ts +33 -0
- package/dist/cjs/executor/opsRegistry.js +8 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/parser/index.d.ts +3 -0
- package/dist/cjs/parser/index.js +1 -0
- package/dist/{parser.d.ts → cjs/parser/lisp.d.ts} +29 -42
- package/dist/cjs/parser/lispTypes/conditionals.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/conditionals.js +135 -0
- package/dist/cjs/parser/lispTypes/control.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/control.js +204 -0
- package/dist/cjs/parser/lispTypes/declarations.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/declarations.js +99 -0
- package/dist/cjs/parser/lispTypes/index.d.ts +3 -0
- package/dist/cjs/parser/lispTypes/index.js +17 -0
- package/dist/cjs/parser/lispTypes/operators.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/operators.js +252 -0
- package/dist/cjs/parser/lispTypes/shared.d.ts +38 -0
- package/dist/cjs/parser/lispTypes/structures.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/structures.js +188 -0
- package/dist/cjs/parser/lispTypes/values.d.ts +2 -0
- package/dist/cjs/parser/lispTypes/values.js +89 -0
- package/dist/cjs/parser/parserUtils.d.ts +34 -0
- package/dist/cjs/parser/parserUtils.js +968 -0
- package/dist/cjs/utils/CodeString.d.ts +16 -0
- package/dist/cjs/utils/CodeString.js +64 -0
- package/dist/cjs/utils/ExecContext.d.ts +34 -0
- package/dist/cjs/utils/ExecContext.js +171 -0
- package/dist/cjs/utils/Prop.d.ts +16 -0
- package/dist/cjs/utils/Prop.js +81 -0
- package/dist/cjs/utils/Scope.d.ts +47 -0
- package/dist/cjs/utils/Scope.js +127 -0
- package/dist/cjs/utils/errors.d.ts +10 -0
- package/dist/cjs/utils/errors.js +12 -0
- package/dist/cjs/utils/functionReplacements.d.ts +11 -0
- package/dist/cjs/utils/functionReplacements.js +362 -0
- package/dist/cjs/utils/index.d.ts +7 -0
- package/dist/cjs/utils/index.js +7 -0
- package/dist/cjs/utils/types.d.ts +221 -0
- package/dist/cjs/utils/types.js +164 -0
- package/dist/cjs/utils/unraw.js +145 -0
- package/dist/{node → esm}/Sandbox.d.ts +15 -6
- package/dist/esm/Sandbox.js +115 -0
- package/dist/esm/Sandbox.js.map +1 -0
- package/{build → dist/esm}/SandboxExec.d.ts +8 -17
- package/dist/esm/SandboxExec.js +224 -0
- package/dist/esm/SandboxExec.js.map +1 -0
- package/dist/{eval.d.ts → esm/eval/index.d.ts} +10 -2
- package/dist/esm/eval/index.js +235 -0
- package/dist/esm/eval/index.js.map +1 -0
- package/dist/esm/executor/executorUtils.d.ts +161 -0
- package/dist/esm/executor/executorUtils.js +898 -0
- package/dist/esm/executor/executorUtils.js.map +1 -0
- package/dist/esm/executor/index.d.ts +1 -0
- package/dist/esm/executor/index.js +2 -0
- package/dist/esm/executor/ops/assignment.d.ts +1 -0
- package/dist/esm/executor/ops/assignment.js +90 -0
- package/dist/esm/executor/ops/assignment.js.map +1 -0
- package/dist/esm/executor/ops/call.d.ts +1 -0
- package/dist/esm/executor/ops/call.js +162 -0
- package/dist/esm/executor/ops/call.js.map +1 -0
- package/dist/esm/executor/ops/comparison.d.ts +1 -0
- package/dist/esm/executor/ops/comparison.js +38 -0
- package/dist/esm/executor/ops/comparison.js.map +1 -0
- package/dist/esm/executor/ops/control.d.ts +1 -0
- package/dist/esm/executor/ops/control.js +205 -0
- package/dist/esm/executor/ops/control.js.map +1 -0
- package/dist/esm/executor/ops/functions.d.ts +1 -0
- package/dist/esm/executor/ops/functions.js +57 -0
- package/dist/esm/executor/ops/functions.js.map +1 -0
- package/dist/esm/executor/ops/index.d.ts +0 -0
- package/dist/esm/executor/ops/index.js +11 -0
- package/dist/esm/executor/ops/literals.d.ts +1 -0
- package/dist/esm/executor/ops/literals.js +47 -0
- package/dist/esm/executor/ops/literals.js.map +1 -0
- package/dist/esm/executor/ops/misc.d.ts +1 -0
- package/dist/esm/executor/ops/misc.js +27 -0
- package/dist/esm/executor/ops/misc.js.map +1 -0
- package/dist/esm/executor/ops/object.d.ts +1 -0
- package/dist/esm/executor/ops/object.js +53 -0
- package/dist/esm/executor/ops/object.js.map +1 -0
- package/dist/esm/executor/ops/prop.d.ts +1 -0
- package/dist/esm/executor/ops/prop.js +72 -0
- package/dist/esm/executor/ops/prop.js.map +1 -0
- package/dist/esm/executor/ops/unary.d.ts +1 -0
- package/dist/esm/executor/ops/unary.js +50 -0
- package/dist/esm/executor/ops/unary.js.map +1 -0
- package/dist/esm/executor/ops/variables.d.ts +1 -0
- package/dist/esm/executor/ops/variables.js +18 -0
- package/dist/esm/executor/ops/variables.js.map +1 -0
- package/dist/esm/executor/opsRegistry.d.ts +33 -0
- package/dist/esm/executor/opsRegistry.js +9 -0
- package/dist/esm/executor/opsRegistry.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/parser/index.d.ts +3 -0
- package/dist/esm/parser/index.js +1 -0
- package/{build/parser.d.ts → dist/esm/parser/lisp.d.ts} +29 -42
- package/dist/esm/parser/lispTypes/conditionals.d.ts +2 -0
- package/dist/esm/parser/lispTypes/conditionals.js +137 -0
- package/dist/esm/parser/lispTypes/conditionals.js.map +1 -0
- package/dist/esm/parser/lispTypes/control.d.ts +2 -0
- package/dist/esm/parser/lispTypes/control.js +206 -0
- package/dist/esm/parser/lispTypes/control.js.map +1 -0
- package/dist/esm/parser/lispTypes/declarations.d.ts +2 -0
- package/dist/esm/parser/lispTypes/declarations.js +101 -0
- package/dist/esm/parser/lispTypes/declarations.js.map +1 -0
- package/dist/esm/parser/lispTypes/index.d.ts +3 -0
- package/dist/esm/parser/lispTypes/index.js +19 -0
- package/dist/esm/parser/lispTypes/index.js.map +1 -0
- package/dist/esm/parser/lispTypes/operators.d.ts +2 -0
- package/dist/esm/parser/lispTypes/operators.js +254 -0
- package/dist/esm/parser/lispTypes/operators.js.map +1 -0
- package/dist/esm/parser/lispTypes/shared.d.ts +38 -0
- package/dist/esm/parser/lispTypes/structures.d.ts +2 -0
- package/dist/esm/parser/lispTypes/structures.js +190 -0
- package/dist/esm/parser/lispTypes/structures.js.map +1 -0
- package/dist/esm/parser/lispTypes/values.d.ts +2 -0
- package/dist/esm/parser/lispTypes/values.js +91 -0
- package/dist/esm/parser/lispTypes/values.js.map +1 -0
- package/dist/esm/parser/parserUtils.d.ts +34 -0
- package/dist/esm/parser/parserUtils.js +959 -0
- package/dist/esm/parser/parserUtils.js.map +1 -0
- package/dist/esm/utils/CodeString.d.ts +16 -0
- package/dist/esm/utils/CodeString.js +66 -0
- package/dist/esm/utils/CodeString.js.map +1 -0
- package/dist/esm/utils/ExecContext.d.ts +34 -0
- package/dist/esm/utils/ExecContext.js +168 -0
- package/dist/esm/utils/ExecContext.js.map +1 -0
- package/dist/esm/utils/Prop.d.ts +16 -0
- package/dist/esm/utils/Prop.js +80 -0
- package/dist/esm/utils/Prop.js.map +1 -0
- package/dist/esm/utils/Scope.d.ts +47 -0
- package/dist/esm/utils/Scope.js +122 -0
- package/dist/esm/utils/Scope.js.map +1 -0
- package/dist/esm/utils/errors.d.ts +10 -0
- package/dist/esm/utils/errors.js +10 -0
- package/dist/esm/utils/errors.js.map +1 -0
- package/dist/esm/utils/functionReplacements.d.ts +11 -0
- package/dist/esm/utils/functionReplacements.js +361 -0
- package/dist/esm/utils/functionReplacements.js.map +1 -0
- package/dist/esm/utils/index.d.ts +7 -0
- package/dist/esm/utils/index.js +7 -0
- package/dist/esm/utils/types.d.ts +221 -0
- package/dist/esm/utils/types.js +160 -0
- package/dist/esm/utils/types.js.map +1 -0
- package/dist/esm/utils/unraw.js +147 -0
- package/dist/esm/utils/unraw.js.map +1 -0
- package/dist/umd/Sandbox.min.js +2 -0
- package/dist/umd/Sandbox.min.js.map +1 -0
- package/dist/umd/SandboxExec.min.js +2 -0
- package/dist/umd/SandboxExec.min.js.map +1 -0
- package/package.json +17 -16
- package/build/Sandbox.js +0 -62
- package/build/SandboxExec.js +0 -214
- package/build/eval.js +0 -205
- package/build/executor.d.ts +0 -124
- package/build/executor.js +0 -1554
- package/build/parser.js +0 -1527
- package/build/unraw.js +0 -168
- package/build/utils.d.ts +0 -264
- package/build/utils.js +0 -362
- package/dist/Sandbox.d.ts +0 -25
- package/dist/Sandbox.js +0 -270
- package/dist/Sandbox.js.map +0 -1
- package/dist/Sandbox.min.js +0 -2
- package/dist/Sandbox.min.js.map +0 -1
- package/dist/SandboxExec.js +0 -218
- package/dist/SandboxExec.js.map +0 -1
- package/dist/SandboxExec.min.js +0 -2
- package/dist/SandboxExec.min.js.map +0 -1
- package/dist/executor.d.ts +0 -124
- package/dist/executor.js +0 -1558
- package/dist/executor.js.map +0 -1
- package/dist/node/Sandbox.js +0 -277
- package/dist/node/SandboxExec.d.ts +0 -66
- package/dist/node/SandboxExec.js +0 -225
- package/dist/node/eval.d.ts +0 -27
- package/dist/node/executor.d.ts +0 -124
- package/dist/node/executor.js +0 -1575
- package/dist/node/parser.d.ts +0 -154
- package/dist/node/parser.js +0 -1704
- package/dist/node/utils.d.ts +0 -264
- package/dist/node/utils.js +0 -385
- package/dist/parser.js +0 -1690
- package/dist/parser.js.map +0 -1
- package/dist/unraw.d.ts +0 -11
- package/dist/utils.d.ts +0 -264
- package/dist/utils.js +0 -365
- package/dist/utils.js.map +0 -1
- /package/{build → dist/cjs/utils}/unraw.d.ts +0 -0
- /package/dist/{node → esm/utils}/unraw.d.ts +0 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
const require_CodeString = require("../../utils/CodeString.js");
|
|
2
|
+
const require_types = require("../../utils/types.js");
|
|
3
|
+
require("../../utils/index.js");
|
|
4
|
+
//#region src/parser/lispTypes/control.ts
|
|
5
|
+
function registerControlLispTypes({ ParseError, createLisp, emptyString, expandDestructure, extractStatementLabels, insertSemicolons, lispify, lispifyBlock, lispifyExpr, lispifyReturnExpr, restOfExp, semiColon, setLispType, startingExecpted, wrapLabeledStatement }) {
|
|
6
|
+
const iteratorRegex = /^((let|var|const|internal)\s+)?\s*([a-zA-Z$_][a-zA-Z\d$_]*)\s+(in|of)(?![\w$])/;
|
|
7
|
+
const iteratorDestructureRegex = /^((let|var|const|internal)\s+)\s*([[{])/;
|
|
8
|
+
const ofStart2 = lispify({ maxDepth: 10 }, new require_CodeString.CodeString("let $$iterator = $$obj[Symbol.iterator]()"), ["initialize"]);
|
|
9
|
+
const ofStart3 = lispify({ maxDepth: 10 }, new require_CodeString.CodeString("let $$next = $$iterator.next()"), ["initialize"]);
|
|
10
|
+
const ofCondition = lispify({ maxDepth: 10 }, new require_CodeString.CodeString("return !$$next.done"), ["initialize"]);
|
|
11
|
+
const ofStep = lispify({ maxDepth: 10 }, new require_CodeString.CodeString("$$next = $$iterator.next()"));
|
|
12
|
+
const asyncOfStart2 = lispify({ maxDepth: 10 }, new require_CodeString.CodeString("let $$iterator = $$obj"), ["initialize"]);
|
|
13
|
+
const asyncOfStart3 = lispify({ maxDepth: 10 }, new require_CodeString.CodeString("let $$next = $$iterator.next()"), ["initialize"]);
|
|
14
|
+
const asyncOfStep = lispify({ maxDepth: 10 }, new require_CodeString.CodeString("$$next = $$iterator.next()"));
|
|
15
|
+
const inStart2 = lispify({ maxDepth: 10 }, new require_CodeString.CodeString("let $$keys = Object.keys($$obj)"), ["initialize"]);
|
|
16
|
+
const inStart3 = lispify({ maxDepth: 10 }, new require_CodeString.CodeString("let $$keyIndex = 0"), ["initialize"]);
|
|
17
|
+
const inStep = lispify({ maxDepth: 10 }, new require_CodeString.CodeString("$$keyIndex++"));
|
|
18
|
+
const inCondition = lispify({ maxDepth: 10 }, new require_CodeString.CodeString("return $$keyIndex < $$keys.length"), ["initialize"]);
|
|
19
|
+
setLispType([
|
|
20
|
+
"for",
|
|
21
|
+
"do",
|
|
22
|
+
"while"
|
|
23
|
+
], (ctx) => {
|
|
24
|
+
const { constants, type, part, res } = ctx;
|
|
25
|
+
const labels = extractStatementLabels(res[1]);
|
|
26
|
+
let i = 0;
|
|
27
|
+
let startStep = require_types.LispType.True;
|
|
28
|
+
let startInternal = [];
|
|
29
|
+
let getIterator = require_types.LispType.None;
|
|
30
|
+
let beforeStep = require_types.LispType.None;
|
|
31
|
+
let checkFirst = require_types.LispType.True;
|
|
32
|
+
let condition;
|
|
33
|
+
let step = require_types.LispType.True;
|
|
34
|
+
let body;
|
|
35
|
+
const isForAwait = type === "for" && res[2] ? require_types.LispType.True : require_types.LispType.None;
|
|
36
|
+
switch (type) {
|
|
37
|
+
case "while": {
|
|
38
|
+
i = part.toString().indexOf("(") + 1;
|
|
39
|
+
const extract = restOfExp(constants, part.substring(i), [], "(");
|
|
40
|
+
if (!extract.trimStart().length) throw new SyntaxError("Unexpected end of expression");
|
|
41
|
+
condition = lispifyReturnExpr(constants, extract);
|
|
42
|
+
body = restOfExp(constants, part.substring(i + extract.length + 1)).trim();
|
|
43
|
+
if (body.char(0) === "{") body = body.slice(1, -1);
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
case "for": {
|
|
47
|
+
i = part.toString().indexOf("(") + 1;
|
|
48
|
+
const args = [];
|
|
49
|
+
let extract2 = emptyString;
|
|
50
|
+
for (let k = 0; k < 3; k++) {
|
|
51
|
+
extract2 = restOfExp(constants, part.substring(i), [/^[;)]/]);
|
|
52
|
+
args.push(extract2.trim());
|
|
53
|
+
i += extract2.length + 1;
|
|
54
|
+
if (part.char(i - 1) === ")") break;
|
|
55
|
+
}
|
|
56
|
+
let iterator;
|
|
57
|
+
let iteratorDestructure;
|
|
58
|
+
if (args.length === 1 && (iterator = iteratorRegex.exec(args[0].toString()))) {
|
|
59
|
+
const iterableExpr = args[0].substring(iterator[0].length);
|
|
60
|
+
if (!iterableExpr.trimStart().length) throw new SyntaxError("Unexpected end of expression");
|
|
61
|
+
if (iterator[4] === "of") {
|
|
62
|
+
getIterator = lispifyReturnExpr(constants, iterableExpr);
|
|
63
|
+
startInternal = isForAwait ? [asyncOfStart2, asyncOfStart3] : [ofStart2, ofStart3];
|
|
64
|
+
condition = ofCondition;
|
|
65
|
+
step = isForAwait ? asyncOfStep : ofStep;
|
|
66
|
+
beforeStep = lispify(constants, new require_CodeString.CodeString((iterator[1] || "let ") + iterator[3] + " = $$next.value"), ["initialize"]);
|
|
67
|
+
} else {
|
|
68
|
+
if (isForAwait) throw new SyntaxError("Unexpected token 'in'");
|
|
69
|
+
getIterator = lispifyReturnExpr(constants, iterableExpr);
|
|
70
|
+
startInternal = [inStart2, inStart3];
|
|
71
|
+
step = inStep;
|
|
72
|
+
condition = inCondition;
|
|
73
|
+
beforeStep = lispify(constants, new require_CodeString.CodeString((iterator[1] || "let ") + iterator[3] + " = $$keys[$$keyIndex]"), ["initialize"]);
|
|
74
|
+
}
|
|
75
|
+
} else if (args.length === 1 && (iteratorDestructure = iteratorDestructureRegex.exec(args[0].toString()))) {
|
|
76
|
+
const keyword = iteratorDestructure[1].trim();
|
|
77
|
+
const openBracket = iteratorDestructure[3];
|
|
78
|
+
const closeBracket = openBracket === "[" ? "]" : "}";
|
|
79
|
+
const keywordPrefixLen = iteratorDestructure[0].length - 1;
|
|
80
|
+
const patternContent = restOfExp(constants, args[0].substring(keywordPrefixLen + 1), [], openBracket);
|
|
81
|
+
const patternStr = openBracket + patternContent.toString() + closeBracket;
|
|
82
|
+
const afterClose = args[0].substring(keywordPrefixLen + 1 + patternContent.length + 1).trimStart();
|
|
83
|
+
const inOfMatch = /^(in|of)(?![\w$])\s*/.exec(afterClose.toString());
|
|
84
|
+
if (!inOfMatch) throw new SyntaxError("Invalid for loop definition");
|
|
85
|
+
const inOf = inOfMatch[1];
|
|
86
|
+
const iterExpr = afterClose.substring(inOfMatch[0].length);
|
|
87
|
+
if (!iterExpr.trimStart().length) throw new SyntaxError("Unexpected end of expression");
|
|
88
|
+
const tempVarName = "$$_fv";
|
|
89
|
+
if (inOf === "of") {
|
|
90
|
+
getIterator = lispifyReturnExpr(constants, iterExpr);
|
|
91
|
+
startInternal = isForAwait ? [asyncOfStart2, asyncOfStart3] : [ofStart2, ofStart3];
|
|
92
|
+
condition = ofCondition;
|
|
93
|
+
step = isForAwait ? asyncOfStep : ofStep;
|
|
94
|
+
const stmts = lispifyBlock(new require_CodeString.CodeString(expandDestructure(keyword, patternStr, tempVarName)), constants, false, ctx);
|
|
95
|
+
beforeStep = createLisp({
|
|
96
|
+
op: require_types.LispType.InternalBlock,
|
|
97
|
+
a: [lispify(constants, new require_CodeString.CodeString(`${keyword} ${tempVarName} = $$next.value`), ["initialize"]), ...stmts],
|
|
98
|
+
b: require_types.LispType.None
|
|
99
|
+
});
|
|
100
|
+
} else {
|
|
101
|
+
getIterator = lispifyReturnExpr(constants, iterExpr);
|
|
102
|
+
startInternal = [inStart2, inStart3];
|
|
103
|
+
step = inStep;
|
|
104
|
+
condition = inCondition;
|
|
105
|
+
const stmts = lispifyBlock(new require_CodeString.CodeString(expandDestructure(keyword, patternStr, tempVarName)), constants, false, ctx);
|
|
106
|
+
beforeStep = createLisp({
|
|
107
|
+
op: require_types.LispType.InternalBlock,
|
|
108
|
+
a: [lispify(constants, new require_CodeString.CodeString(`${keyword} ${tempVarName} = $$keys[$$keyIndex]`), ["initialize"]), ...stmts],
|
|
109
|
+
b: require_types.LispType.None
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
} else if (args.length === 3) {
|
|
113
|
+
const [startArg, conditionArg, stepArg] = args;
|
|
114
|
+
if (startArg.length) startStep = lispifyExpr(constants, startArg, startingExecpted, ctx);
|
|
115
|
+
condition = conditionArg.length ? lispifyReturnExpr(constants, conditionArg) : require_types.LispType.True;
|
|
116
|
+
if (stepArg.length) step = lispifyExpr(constants, stepArg, void 0, ctx);
|
|
117
|
+
} else throw new SyntaxError("Invalid for loop definition");
|
|
118
|
+
body = restOfExp(constants, part.substring(i)).trim();
|
|
119
|
+
if (body.char(0) === "{") body = body.slice(1, -1);
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
case "do": {
|
|
123
|
+
checkFirst = require_types.LispType.None;
|
|
124
|
+
const isBlock = !!res[2];
|
|
125
|
+
body = restOfExp(constants, part.substring(res[0].length), isBlock ? [/^\}/] : [semiColon]);
|
|
126
|
+
condition = lispifyReturnExpr(constants, restOfExp(constants, part.substring(part.toString().indexOf("(", res[0].length + body.length) + 1), [], "("));
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const a = [
|
|
131
|
+
checkFirst,
|
|
132
|
+
startInternal,
|
|
133
|
+
getIterator,
|
|
134
|
+
startStep,
|
|
135
|
+
step,
|
|
136
|
+
condition,
|
|
137
|
+
beforeStep,
|
|
138
|
+
isForAwait,
|
|
139
|
+
labels
|
|
140
|
+
];
|
|
141
|
+
ctx.lispTree = createLisp({
|
|
142
|
+
op: require_types.LispType.Loop,
|
|
143
|
+
a,
|
|
144
|
+
b: lispifyBlock(body, constants, false, ctx)
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
setLispType(["block"], (ctx) => {
|
|
148
|
+
const { constants, part, res } = ctx;
|
|
149
|
+
ctx.lispTree = wrapLabeledStatement(extractStatementLabels(res[1]), createLisp({
|
|
150
|
+
op: require_types.LispType.Block,
|
|
151
|
+
a: lispifyBlock(restOfExp(constants, part.substring(res[0].length), [], "{"), constants, false, ctx),
|
|
152
|
+
b: require_types.LispType.None
|
|
153
|
+
}));
|
|
154
|
+
});
|
|
155
|
+
setLispType(["loopAction"], (ctx) => {
|
|
156
|
+
const { part, res } = ctx;
|
|
157
|
+
const remaining = part.substring(res[0].length).trimStart();
|
|
158
|
+
if (remaining.length && !res[2]) throw new SyntaxError(`Unexpected token '${remaining.char(0)}'`);
|
|
159
|
+
ctx.lispTree = createLisp({
|
|
160
|
+
op: require_types.LispType.LoopAction,
|
|
161
|
+
a: res[1],
|
|
162
|
+
b: res[2] || require_types.LispType.None
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
const catchReg = /^\s*(catch\s*(\(\s*([a-zA-Z$_][a-zA-Z\d$_]*)\s*\))?|finally)\s*\{/;
|
|
166
|
+
const catchEmptyBindingReg = /^\s*catch\s*\(\s*\)/;
|
|
167
|
+
const catchReservedBindingReg = /^\s*catch\s*\(\s*(break|case|catch|continue|debugger|default|delete|do|else|finally|for|function|if|in|instanceof|new|return|switch|this|throw|try|typeof|var|void|while|with|class|const|enum|export|extends|implements|import|interface|let|package|private|protected|public|static|super|yield|await)\s*\)/;
|
|
168
|
+
const finallyKeywordReg = /^\s*finally\b/;
|
|
169
|
+
setLispType(["try"], (ctx) => {
|
|
170
|
+
const { constants, part, res } = ctx;
|
|
171
|
+
const body = restOfExp(constants, part.substring(res[0].length), [], "{");
|
|
172
|
+
const afterBody = part.substring(res[0].length + body.length + 1).toString();
|
|
173
|
+
if (catchEmptyBindingReg.test(afterBody)) throw new ParseError("Unexpected token ')'", part.toString());
|
|
174
|
+
const reservedMatch = catchReservedBindingReg.exec(afterBody);
|
|
175
|
+
if (reservedMatch) throw new ParseError(`Unexpected token '${reservedMatch[1]}'`, part.toString());
|
|
176
|
+
const finallyMatch = finallyKeywordReg.exec(afterBody);
|
|
177
|
+
if (finallyMatch && !/^\s*\{/.test(afterBody.substring(finallyMatch[0].length))) throw new ParseError("Unexpected token", part.toString());
|
|
178
|
+
let catchRes = catchReg.exec(afterBody);
|
|
179
|
+
let finallyBody;
|
|
180
|
+
let exception = "";
|
|
181
|
+
let catchBody;
|
|
182
|
+
let offset = 0;
|
|
183
|
+
if (!catchRes) throw new ParseError("Missing catch or finally after try", part.toString());
|
|
184
|
+
if (catchRes[1].startsWith("catch")) {
|
|
185
|
+
catchRes = catchReg.exec(part.substring(res[0].length + body.length + 1).toString());
|
|
186
|
+
exception = catchRes[3] || "";
|
|
187
|
+
catchBody = restOfExp(constants, part.substring(res[0].length + body.length + 1 + catchRes[0].length), [], "{");
|
|
188
|
+
offset = res[0].length + body.length + 1 + catchRes[0].length + catchBody.length + 1;
|
|
189
|
+
if ((catchRes = catchReg.exec(part.substring(offset).toString())) && catchRes[1].startsWith("finally")) finallyBody = restOfExp(constants, part.substring(offset + catchRes[0].length), [], "{");
|
|
190
|
+
} else finallyBody = restOfExp(constants, part.substring(res[0].length + body.length + 1 + catchRes[0].length), [], "{");
|
|
191
|
+
const b = [
|
|
192
|
+
exception,
|
|
193
|
+
lispifyBlock(insertSemicolons(constants, catchBody || emptyString), constants, false, ctx),
|
|
194
|
+
lispifyBlock(insertSemicolons(constants, finallyBody || emptyString), constants, false, ctx)
|
|
195
|
+
];
|
|
196
|
+
ctx.lispTree = wrapLabeledStatement(extractStatementLabels(res[1]), createLisp({
|
|
197
|
+
op: require_types.LispType.Try,
|
|
198
|
+
a: lispifyBlock(insertSemicolons(constants, body), constants, false, ctx),
|
|
199
|
+
b
|
|
200
|
+
}));
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
//#endregion
|
|
204
|
+
exports.registerControlLispTypes = registerControlLispTypes;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { RegisterLispTypesDeps } from './shared';
|
|
2
|
+
export declare function registerDeclarationLispTypes({ createLisp, expectTypes, expandDestructure, expandFunctionParamDestructure, lispify, lispifyBlock, lispifyFunction, restOfExp, semiColon, setLispType, splitByCommasDestructure, }: RegisterLispTypesDeps): void;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
const require_CodeString = require("../../utils/CodeString.js");
|
|
2
|
+
const require_types = require("../../utils/types.js");
|
|
3
|
+
require("../../utils/index.js");
|
|
4
|
+
//#region src/parser/lispTypes/declarations.ts
|
|
5
|
+
function registerDeclarationLispTypes({ createLisp, expectTypes, expandDestructure, expandFunctionParamDestructure, lispify, lispifyBlock, lispifyFunction, restOfExp, semiColon, setLispType, splitByCommasDestructure }) {
|
|
6
|
+
setLispType(["initializeDestructure"], (ctx) => {
|
|
7
|
+
const { constants, part, res } = ctx;
|
|
8
|
+
const keyword = res[1];
|
|
9
|
+
const openBracket = res[2];
|
|
10
|
+
const closeBracket = openBracket === "[" ? "]" : "}";
|
|
11
|
+
const patternContent = restOfExp(constants, part.substring(res[0].length), [], openBracket);
|
|
12
|
+
const patternStr = openBracket + patternContent.toString() + closeBracket;
|
|
13
|
+
const afterClose = part.substring(res[0].length + patternContent.length + 1).trimStart();
|
|
14
|
+
if (!afterClose.length || afterClose.char(0) !== "=") throw new SyntaxError("Destructuring declaration requires an initializer");
|
|
15
|
+
const stmts = lispifyBlock(new require_CodeString.CodeString(expandDestructure(keyword, patternStr, restOfExp(constants, afterClose.substring(1).trimStart(), [semiColon]).toString())), constants, false, ctx);
|
|
16
|
+
ctx.lispTree = createLisp({
|
|
17
|
+
op: require_types.LispType.InternalBlock,
|
|
18
|
+
a: stmts,
|
|
19
|
+
b: require_types.LispType.None
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
setLispType(["initialize"], (ctx) => {
|
|
23
|
+
const { constants, part, res, expect } = ctx;
|
|
24
|
+
const lt = res[1] === "var" ? require_types.LispType.Var : res[1] === "let" ? require_types.LispType.Let : res[1] === "const" ? require_types.LispType.Const : require_types.LispType.Internal;
|
|
25
|
+
if (!res[3]) ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next, createLisp({
|
|
26
|
+
op: lt,
|
|
27
|
+
a: res[2],
|
|
28
|
+
b: require_types.LispType.None
|
|
29
|
+
}), false, ctx);
|
|
30
|
+
else {
|
|
31
|
+
const initExpr = part.substring(res[0].length);
|
|
32
|
+
if (!initExpr.trimStart().length) throw new SyntaxError("Unexpected end of expression");
|
|
33
|
+
ctx.lispTree = createLisp({
|
|
34
|
+
op: lt,
|
|
35
|
+
a: res[2],
|
|
36
|
+
b: lispify(constants, initExpr, expectTypes[expect].next, void 0, false, ctx)
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
setLispType([
|
|
41
|
+
"function",
|
|
42
|
+
"inlineFunction",
|
|
43
|
+
"arrowFunction",
|
|
44
|
+
"arrowFunctionSingle"
|
|
45
|
+
], (ctx) => {
|
|
46
|
+
const { constants, type, part, res, expect, generatorDepth, asyncDepth } = ctx;
|
|
47
|
+
const isArrow = type !== "function" && type !== "inlineFunction";
|
|
48
|
+
const isReturn = isArrow && !res[res.length - 1];
|
|
49
|
+
const isGenerator = !isArrow && res[2] && res[2].trimStart().startsWith("*") ? require_types.LispType.True : require_types.LispType.None;
|
|
50
|
+
const isAsync = res[1] ? require_types.LispType.True : require_types.LispType.None;
|
|
51
|
+
let rawArgStr;
|
|
52
|
+
let bodyOffset;
|
|
53
|
+
if (type === "function" || type === "inlineFunction") {
|
|
54
|
+
const argsCode = restOfExp(constants, part.substring(res[0].length), [], "(");
|
|
55
|
+
rawArgStr = argsCode.toString().trim();
|
|
56
|
+
bodyOffset = res[0].length + argsCode.length + 1;
|
|
57
|
+
const afterParen = part.substring(bodyOffset).trimStart();
|
|
58
|
+
bodyOffset = part.length - afterParen.length + 1;
|
|
59
|
+
} else {
|
|
60
|
+
rawArgStr = res[2] ?? "";
|
|
61
|
+
bodyOffset = 0;
|
|
62
|
+
}
|
|
63
|
+
const args = [...rawArgStr ? splitByCommasDestructure(rawArgStr).map((a) => a.trim()).filter(Boolean) : []];
|
|
64
|
+
if (!isArrow) args.unshift((res[3] || "").trimStart());
|
|
65
|
+
let ended = false;
|
|
66
|
+
args.forEach((arg) => {
|
|
67
|
+
if (ended) throw new SyntaxError("Rest parameter must be last formal parameter");
|
|
68
|
+
if (arg.startsWith("...")) ended = true;
|
|
69
|
+
});
|
|
70
|
+
const f = restOfExp(constants, isArrow ? part.substring(res[0].length) : part.substring(bodyOffset), !isReturn ? [/^}/] : [/^[,)}\]]/, semiColon]);
|
|
71
|
+
let funcBody = isReturn ? "return " + f : f.toString();
|
|
72
|
+
const expanded = expandFunctionParamDestructure(isArrow ? args : args.slice(1), funcBody);
|
|
73
|
+
const finalArgs = isArrow ? expanded.args : [args[0], ...expanded.args];
|
|
74
|
+
funcBody = expanded.body;
|
|
75
|
+
finalArgs.forEach((arg) => {
|
|
76
|
+
if (require_types.reservedWords.has(arg.replace(/^\.\.\./, ""))) throw new SyntaxError(`Unexpected token '${arg}'`);
|
|
77
|
+
});
|
|
78
|
+
const afterFunc = isArrow ? res[0].length + f.length + 1 : bodyOffset + f.length + 1;
|
|
79
|
+
ctx.lispTree = lispify(constants, part.substring(afterFunc), expectTypes[expect].next, createLisp({
|
|
80
|
+
op: isArrow ? require_types.LispType.ArrowFunction : type === "function" ? require_types.LispType.Function : require_types.LispType.InlineFunction,
|
|
81
|
+
a: isArrow ? [isAsync, ...finalArgs] : [
|
|
82
|
+
isAsync,
|
|
83
|
+
isGenerator,
|
|
84
|
+
...finalArgs
|
|
85
|
+
],
|
|
86
|
+
b: constants.eager ? lispifyFunction(new require_CodeString.CodeString(funcBody), constants, false, isArrow ? {
|
|
87
|
+
generatorDepth: 0,
|
|
88
|
+
asyncDepth: 0,
|
|
89
|
+
lispDepth: 0
|
|
90
|
+
} : {
|
|
91
|
+
generatorDepth: isGenerator === require_types.LispType.True ? generatorDepth + 1 : 0,
|
|
92
|
+
asyncDepth: isAsync === require_types.LispType.True ? asyncDepth + 1 : 0,
|
|
93
|
+
lispDepth: 0
|
|
94
|
+
}) : funcBody
|
|
95
|
+
}), false, ctx);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
//#endregion
|
|
99
|
+
exports.registerDeclarationLispTypes = registerDeclarationLispTypes;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const require_conditionals = require("./conditionals.js");
|
|
2
|
+
const require_control = require("./control.js");
|
|
3
|
+
const require_declarations = require("./declarations.js");
|
|
4
|
+
const require_operators = require("./operators.js");
|
|
5
|
+
const require_structures = require("./structures.js");
|
|
6
|
+
const require_values = require("./values.js");
|
|
7
|
+
//#region src/parser/lispTypes/index.ts
|
|
8
|
+
function registerLispTypes(deps) {
|
|
9
|
+
require_structures.registerStructureLispTypes(deps);
|
|
10
|
+
require_operators.registerOperatorLispTypes(deps);
|
|
11
|
+
require_conditionals.registerConditionalLispTypes(deps);
|
|
12
|
+
require_values.registerValueLispTypes(deps);
|
|
13
|
+
require_declarations.registerDeclarationLispTypes(deps);
|
|
14
|
+
require_control.registerControlLispTypes(deps);
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
exports.registerLispTypes = registerLispTypes;
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
const require_CodeString = require("../../utils/CodeString.js");
|
|
2
|
+
const require_types = require("../../utils/types.js");
|
|
3
|
+
const require_ExecContext = require("../../utils/ExecContext.js");
|
|
4
|
+
require("../../utils/index.js");
|
|
5
|
+
//#region src/parser/lispTypes/operators.ts
|
|
6
|
+
function registerOperatorLispTypes({ createLisp, expandDestructure, expectTypes, getDestructurePatternSource, lispifyBlock, restOfExp, lispify, lispifyExpr, setLispType }) {
|
|
7
|
+
const inlineIfElse = /^:/;
|
|
8
|
+
const modifierTypes = {
|
|
9
|
+
inverse: require_types.LispType.Inverse,
|
|
10
|
+
not: require_types.LispType.Not,
|
|
11
|
+
positive: require_types.LispType.Positive,
|
|
12
|
+
negative: require_types.LispType.Negative,
|
|
13
|
+
typeof: require_types.LispType.Typeof,
|
|
14
|
+
delete: require_types.LispType.Delete
|
|
15
|
+
};
|
|
16
|
+
setLispType([
|
|
17
|
+
"inverse",
|
|
18
|
+
"not",
|
|
19
|
+
"negative",
|
|
20
|
+
"positive",
|
|
21
|
+
"typeof",
|
|
22
|
+
"delete"
|
|
23
|
+
], (ctx) => {
|
|
24
|
+
const { constants, type, part, res, expect } = ctx;
|
|
25
|
+
const extract = restOfExp(constants, part.substring(res[0].length), [/^([^\s.?\w$]|\?[^.])/]);
|
|
26
|
+
if (part.substring(extract.length + res[0].length).trim().toString().startsWith("**")) throw new SyntaxError("Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence");
|
|
27
|
+
ctx.lispTree = lispify(constants, part.substring(extract.length + res[0].length), restOfExp.next, createLisp({
|
|
28
|
+
op: modifierTypes[type],
|
|
29
|
+
a: ctx.lispTree,
|
|
30
|
+
b: lispify(constants, extract, expectTypes[expect].next)
|
|
31
|
+
}), false, ctx);
|
|
32
|
+
});
|
|
33
|
+
setLispType(["taggedTemplate"], (ctx) => {
|
|
34
|
+
const { constants, part, res, expect } = ctx;
|
|
35
|
+
const literalIndex = res[1];
|
|
36
|
+
const [, templateStr, jsExprs] = constants.literals[parseInt(literalIndex)];
|
|
37
|
+
const stringParts = [];
|
|
38
|
+
const expressions = [];
|
|
39
|
+
let currentStr = "";
|
|
40
|
+
let i = 0;
|
|
41
|
+
while (i < templateStr.length) if (templateStr.substring(i, i + 2) === "${") {
|
|
42
|
+
let j = i + 2;
|
|
43
|
+
let exprIndex = "";
|
|
44
|
+
let isValidPlaceholder = false;
|
|
45
|
+
while (j < templateStr.length && templateStr[j] !== "}") {
|
|
46
|
+
exprIndex += templateStr[j];
|
|
47
|
+
j++;
|
|
48
|
+
}
|
|
49
|
+
if (j < templateStr.length && templateStr[j] === "}" && /^\d+$/.test(exprIndex)) isValidPlaceholder = true;
|
|
50
|
+
if (isValidPlaceholder) {
|
|
51
|
+
stringParts.push(currentStr);
|
|
52
|
+
currentStr = "";
|
|
53
|
+
expressions.push(jsExprs[parseInt(exprIndex)]);
|
|
54
|
+
i = j + 1;
|
|
55
|
+
} else {
|
|
56
|
+
currentStr += templateStr[i];
|
|
57
|
+
i++;
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
currentStr += templateStr[i];
|
|
61
|
+
i++;
|
|
62
|
+
}
|
|
63
|
+
stringParts.push(currentStr);
|
|
64
|
+
const stringsArray = stringParts.map((str) => createLisp({
|
|
65
|
+
op: require_types.LispType.StringIndex,
|
|
66
|
+
a: require_types.LispType.None,
|
|
67
|
+
b: String(constants.strings.push(str) - 1)
|
|
68
|
+
}));
|
|
69
|
+
const stringsArrayLisp = createLisp({
|
|
70
|
+
op: require_types.LispType.CreateArray,
|
|
71
|
+
a: createLisp({
|
|
72
|
+
op: require_types.LispType.None,
|
|
73
|
+
a: require_types.LispType.None,
|
|
74
|
+
b: require_types.LispType.None
|
|
75
|
+
}),
|
|
76
|
+
b: stringsArray
|
|
77
|
+
});
|
|
78
|
+
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next, createLisp({
|
|
79
|
+
op: require_types.LispType.Call,
|
|
80
|
+
a: ctx.lispTree,
|
|
81
|
+
b: [stringsArrayLisp, ...expressions]
|
|
82
|
+
}), false, ctx);
|
|
83
|
+
});
|
|
84
|
+
const incrementTypes = {
|
|
85
|
+
"++$": require_types.LispType.IncrementBefore,
|
|
86
|
+
"--$": require_types.LispType.DecrementBefore,
|
|
87
|
+
"$++": require_types.LispType.IncrementAfter,
|
|
88
|
+
"$--": require_types.LispType.DecrementAfter
|
|
89
|
+
};
|
|
90
|
+
setLispType(["incrementerBefore"], (ctx) => {
|
|
91
|
+
const { constants, part, res } = ctx;
|
|
92
|
+
const extract = restOfExp(constants, part.substring(2), [/^[^\s.\w$]/]);
|
|
93
|
+
ctx.lispTree = lispify(constants, part.substring(extract.length + 2), restOfExp.next, createLisp({
|
|
94
|
+
op: incrementTypes[res[0] + "$"],
|
|
95
|
+
a: lispify(constants, extract, expectTypes.incrementerBefore.next),
|
|
96
|
+
b: require_types.LispType.None
|
|
97
|
+
}), false, ctx);
|
|
98
|
+
});
|
|
99
|
+
setLispType(["incrementerAfter"], (ctx) => {
|
|
100
|
+
const { constants, part, res, expect } = ctx;
|
|
101
|
+
if (ctx.lispTree[0] === require_types.LispType.Number || ctx.lispTree[0] === require_types.LispType.BigInt || ctx.lispTree[0] === require_types.LispType.GlobalSymbol || ctx.lispTree[0] === require_types.LispType.StringIndex || ctx.lispTree[0] === require_types.LispType.LiteralIndex || ctx.lispTree[0] === require_types.LispType.RegexIndex) throw new SyntaxError("Invalid left-hand side expression in postfix operation");
|
|
102
|
+
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next, createLisp({
|
|
103
|
+
op: incrementTypes["$" + res[0]],
|
|
104
|
+
a: ctx.lispTree,
|
|
105
|
+
b: require_types.LispType.None
|
|
106
|
+
}), false, ctx);
|
|
107
|
+
});
|
|
108
|
+
const adderTypes = {
|
|
109
|
+
"&&": require_types.LispType.And,
|
|
110
|
+
"||": require_types.LispType.Or,
|
|
111
|
+
"??": require_types.LispType.NullishCoalescing,
|
|
112
|
+
instanceof: require_types.LispType.Instanceof,
|
|
113
|
+
in: require_types.LispType.In,
|
|
114
|
+
"=": require_types.LispType.Assign,
|
|
115
|
+
"-=": require_types.LispType.SubractEquals,
|
|
116
|
+
"+=": require_types.LispType.AddEquals,
|
|
117
|
+
"/=": require_types.LispType.DivideEquals,
|
|
118
|
+
"**=": require_types.LispType.PowerEquals,
|
|
119
|
+
"*=": require_types.LispType.MultiplyEquals,
|
|
120
|
+
"%=": require_types.LispType.ModulusEquals,
|
|
121
|
+
"^=": require_types.LispType.BitNegateEquals,
|
|
122
|
+
"&=": require_types.LispType.BitAndEquals,
|
|
123
|
+
"|=": require_types.LispType.BitOrEquals,
|
|
124
|
+
">>>=": require_types.LispType.UnsignedShiftRightEquals,
|
|
125
|
+
"<<=": require_types.LispType.ShiftLeftEquals,
|
|
126
|
+
">>=": require_types.LispType.ShiftRightEquals,
|
|
127
|
+
"&&=": require_types.LispType.AndEquals,
|
|
128
|
+
"||=": require_types.LispType.OrEquals,
|
|
129
|
+
"??=": require_types.LispType.NullishCoalescingEquals
|
|
130
|
+
};
|
|
131
|
+
setLispType([
|
|
132
|
+
"assign",
|
|
133
|
+
"assignModify",
|
|
134
|
+
"nullishCoalescing"
|
|
135
|
+
], (ctx) => {
|
|
136
|
+
const { constants, type, part, res } = ctx;
|
|
137
|
+
if (type !== "nullishCoalescing" && require_ExecContext.isLisp(ctx.lispTree) && (ctx.lispTree[0] === require_types.LispType.PropOptional || ctx.lispTree[0] === require_types.LispType.CallOptional)) throw new SyntaxError("Invalid left-hand side in assignment");
|
|
138
|
+
if (res[0] === "=") {
|
|
139
|
+
const patternStr = getDestructurePatternSource(ctx.lispTree);
|
|
140
|
+
if (patternStr) {
|
|
141
|
+
const rhsStr = part.substring(res[0].length).toString();
|
|
142
|
+
const tempName = `$$_da${Math.random().toString(36).slice(2)}`;
|
|
143
|
+
const expandedCode = `internal ${tempName} = (${rhsStr}); ${expandDestructure("", patternStr, tempName)}; ${tempName}`;
|
|
144
|
+
ctx.lispTree = createLisp({
|
|
145
|
+
op: require_types.LispType.InternalBlock,
|
|
146
|
+
a: lispifyBlock(new require_CodeString.CodeString(expandedCode), constants, false, ctx),
|
|
147
|
+
b: require_types.LispType.None
|
|
148
|
+
});
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
ctx.lispTree = createLisp({
|
|
153
|
+
op: adderTypes[res[0]],
|
|
154
|
+
a: ctx.lispTree,
|
|
155
|
+
b: lispify(constants, part.substring(res[0].length), expectTypes.assignment.next, void 0, false, {
|
|
156
|
+
...ctx,
|
|
157
|
+
lispDepth: ctx.lispDepth + 1
|
|
158
|
+
})
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
const opTypes = {
|
|
162
|
+
"&": require_types.LispType.BitAnd,
|
|
163
|
+
"|": require_types.LispType.BitOr,
|
|
164
|
+
"^": require_types.LispType.BitNegate,
|
|
165
|
+
"<<": require_types.LispType.BitShiftLeft,
|
|
166
|
+
">>": require_types.LispType.BitShiftRight,
|
|
167
|
+
">>>": require_types.LispType.BitUnsignedShiftRight,
|
|
168
|
+
"<=": require_types.LispType.SmallerEqualThan,
|
|
169
|
+
">=": require_types.LispType.LargerEqualThan,
|
|
170
|
+
"<": require_types.LispType.SmallerThan,
|
|
171
|
+
">": require_types.LispType.LargerThan,
|
|
172
|
+
"!==": require_types.LispType.StrictNotEqual,
|
|
173
|
+
"!=": require_types.LispType.NotEqual,
|
|
174
|
+
"===": require_types.LispType.StrictEqual,
|
|
175
|
+
"==": require_types.LispType.Equal,
|
|
176
|
+
"+": require_types.LispType.Plus,
|
|
177
|
+
"-": require_types.LispType.Minus,
|
|
178
|
+
"/": require_types.LispType.Divide,
|
|
179
|
+
"**": require_types.LispType.Power,
|
|
180
|
+
"*": require_types.LispType.Multiply,
|
|
181
|
+
"%": require_types.LispType.Modulus,
|
|
182
|
+
"&&": require_types.LispType.And,
|
|
183
|
+
"||": require_types.LispType.Or,
|
|
184
|
+
instanceof: require_types.LispType.Instanceof,
|
|
185
|
+
in: require_types.LispType.In
|
|
186
|
+
};
|
|
187
|
+
setLispType([
|
|
188
|
+
"power",
|
|
189
|
+
"opHigh",
|
|
190
|
+
"op",
|
|
191
|
+
"comparitor",
|
|
192
|
+
"bitwiseShift",
|
|
193
|
+
"bitwiseAnd",
|
|
194
|
+
"bitwiseXor",
|
|
195
|
+
"bitwiseOr",
|
|
196
|
+
"boolOpAnd",
|
|
197
|
+
"boolOpOr"
|
|
198
|
+
], (ctx) => {
|
|
199
|
+
const { constants, type, part, res } = ctx;
|
|
200
|
+
const next = [expectTypes.inlineIf.types.inlineIf, inlineIfElse];
|
|
201
|
+
switch (type) {
|
|
202
|
+
case "power": break;
|
|
203
|
+
case "opHigh": next.push(expectTypes.splitter.types.opHigh);
|
|
204
|
+
case "op": next.push(expectTypes.splitter.types.op);
|
|
205
|
+
case "comparitor": next.push(expectTypes.splitter.types.comparitor);
|
|
206
|
+
case "bitwiseShift": next.push(expectTypes.splitter.types.bitwiseShift);
|
|
207
|
+
case "bitwiseAnd": next.push(expectTypes.splitter.types.bitwiseAnd);
|
|
208
|
+
case "bitwiseXor": next.push(expectTypes.splitter.types.bitwiseXor);
|
|
209
|
+
case "bitwiseOr": next.push(expectTypes.splitter.types.bitwiseOr);
|
|
210
|
+
case "boolOpAnd": next.push(expectTypes.splitter.types.boolOpAnd);
|
|
211
|
+
case "boolOpOr": next.push(expectTypes.splitter.types.boolOpOr);
|
|
212
|
+
}
|
|
213
|
+
const extract = restOfExp(constants, part.substring(res[0].length), next);
|
|
214
|
+
ctx.lispTree = lispify(constants, part.substring(extract.length + res[0].length), restOfExp.next, createLisp({
|
|
215
|
+
op: opTypes[res[0]],
|
|
216
|
+
a: ctx.lispTree,
|
|
217
|
+
b: lispify(constants, extract, expectTypes.splitter.next)
|
|
218
|
+
}), false, ctx);
|
|
219
|
+
});
|
|
220
|
+
setLispType(["inlineIf"], (ctx) => {
|
|
221
|
+
const { constants, part, res } = ctx;
|
|
222
|
+
let found = false;
|
|
223
|
+
const extract = part.substring(0, 0);
|
|
224
|
+
let quoteCount = 1;
|
|
225
|
+
while (!found && extract.length < part.length) {
|
|
226
|
+
extract.end = restOfExp(constants, part.substring(extract.length + 1), [expectTypes.inlineIf.types.inlineIf, inlineIfElse]).end;
|
|
227
|
+
if (part.char(extract.length) === "?") quoteCount++;
|
|
228
|
+
else quoteCount--;
|
|
229
|
+
if (!quoteCount) found = true;
|
|
230
|
+
}
|
|
231
|
+
extract.start = part.start + 1;
|
|
232
|
+
const falseExpr = part.substring(res[0].length + extract.length + 1);
|
|
233
|
+
if (!falseExpr.trimStart().length) throw new SyntaxError("Unexpected end of expression");
|
|
234
|
+
ctx.lispTree = createLisp({
|
|
235
|
+
op: require_types.LispType.InlineIf,
|
|
236
|
+
a: ctx.lispTree,
|
|
237
|
+
b: createLisp({
|
|
238
|
+
op: require_types.LispType.InlineIfCase,
|
|
239
|
+
a: lispifyExpr(constants, extract, void 0, {
|
|
240
|
+
...ctx,
|
|
241
|
+
lispDepth: ctx.lispDepth + 1
|
|
242
|
+
}),
|
|
243
|
+
b: lispifyExpr(constants, falseExpr, void 0, {
|
|
244
|
+
...ctx,
|
|
245
|
+
lispDepth: ctx.lispDepth + 1
|
|
246
|
+
})
|
|
247
|
+
})
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
//#endregion
|
|
252
|
+
exports.registerOperatorLispTypes = registerOperatorLispTypes;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { CodeString } from '../../utils';
|
|
2
|
+
import { ExtractLispA, ExtractLispB, ExtractLispOp, IConstants, Lisp, LispCallback, LispDepthCtx, LispItem, None, Return } from '../lisp';
|
|
3
|
+
export interface RegisterLispTypesDeps {
|
|
4
|
+
NullLisp: None;
|
|
5
|
+
ParseError: new (message: string, code: string) => Error;
|
|
6
|
+
createLisp: <L extends Lisp>(obj: {
|
|
7
|
+
op: ExtractLispOp<L>;
|
|
8
|
+
a: ExtractLispA<L>;
|
|
9
|
+
b: ExtractLispB<L>;
|
|
10
|
+
}) => L;
|
|
11
|
+
emptyString: CodeString;
|
|
12
|
+
expectTypes: Record<string, {
|
|
13
|
+
types: Record<string, RegExp>;
|
|
14
|
+
next: string[];
|
|
15
|
+
}>;
|
|
16
|
+
expandDestructure: (keyword: string, patternStr: string, rhsStr: string) => string;
|
|
17
|
+
expandFunctionParamDestructure: (args: string[], funcBody: string) => {
|
|
18
|
+
args: string[];
|
|
19
|
+
body: string;
|
|
20
|
+
};
|
|
21
|
+
extractStatementLabels: (prefix?: string) => string[];
|
|
22
|
+
findPatternEndIdx: (s: string) => number;
|
|
23
|
+
getDestructurePatternSource: (tree: LispItem) => string | null;
|
|
24
|
+
insertSemicolons: (constants: IConstants, str: CodeString) => CodeString;
|
|
25
|
+
lispify: (constants: IConstants, part: CodeString, expected?: readonly string[], lispTree?: Lisp, topLevel?: boolean, depthCtx?: LispDepthCtx) => Lisp;
|
|
26
|
+
lispifyBlock: (str: CodeString, constants: IConstants, expression?: boolean, depthCtx?: LispDepthCtx) => Lisp[];
|
|
27
|
+
lispifyExpr: (constants: IConstants, str: CodeString, expected?: readonly string[], depthCtx?: LispDepthCtx) => Lisp;
|
|
28
|
+
lispifyFunction: (str: CodeString, constants: IConstants, expression?: boolean, depthCtx?: LispDepthCtx) => Lisp[];
|
|
29
|
+
lispifyReturnExpr: (constants: IConstants, str: CodeString) => Return;
|
|
30
|
+
restOfExp: ((constants: IConstants, part: CodeString, tests?: RegExp[], quote?: string, firstOpening?: string, closingsTests?: RegExp[], details?: any, depth?: number) => CodeString) & {
|
|
31
|
+
next: string[];
|
|
32
|
+
};
|
|
33
|
+
semiColon: RegExp;
|
|
34
|
+
setLispType: <T extends readonly string[]>(types: T, fn: LispCallback<T[number]>) => void;
|
|
35
|
+
splitByCommasDestructure: (s: string) => string[];
|
|
36
|
+
startingExecpted: readonly string[];
|
|
37
|
+
wrapLabeledStatement: <T extends Lisp>(labels: string[], statement: T) => Lisp;
|
|
38
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { RegisterLispTypesDeps } from './shared';
|
|
2
|
+
export declare function registerStructureLispTypes({ NullLisp, createLisp, emptyString, expectTypes, expandDestructure, findPatternEndIdx, lispify, lispifyBlock, lispifyExpr, restOfExp, setLispType, }: RegisterLispTypesDeps): void;
|