@mojir/lits 2.1.2 → 2.1.3
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/dist/cli/cli.js +174 -37
- package/dist/cli/src/builtin/interface.d.ts +1 -0
- package/dist/cli/src/constants/constants.d.ts +1 -1
- package/dist/cli/src/evaluator/ContextStack.d.ts +3 -2
- package/dist/cli/src/evaluator/functionExecutors.d.ts +1 -1
- package/dist/cli/src/parser/types.d.ts +6 -1
- package/dist/cli/src/tokenizer/token.d.ts +2 -2
- package/dist/index.esm.js +173 -36
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +173 -36
- package/dist/index.js.map +1 -1
- package/dist/lits.iife.js +173 -36
- package/dist/lits.iife.js.map +1 -1
- package/dist/src/builtin/interface.d.ts +1 -0
- package/dist/src/constants/constants.d.ts +1 -1
- package/dist/src/evaluator/ContextStack.d.ts +3 -2
- package/dist/src/evaluator/functionExecutors.d.ts +1 -1
- package/dist/src/parser/types.d.ts +6 -1
- package/dist/src/tokenizer/token.d.ts +2 -2
- package/dist/testFramework.esm.js +173 -36
- package/dist/testFramework.esm.js.map +1 -1
- package/dist/testFramework.js +173 -36
- package/dist/testFramework.js.map +1 -1
- package/package.json +1 -1
package/dist/cli/cli.js
CHANGED
|
@@ -92,7 +92,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
92
92
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
93
93
|
};
|
|
94
94
|
|
|
95
|
-
var version = "2.1.
|
|
95
|
+
var version = "2.1.3";
|
|
96
96
|
|
|
97
97
|
function getCodeMarker(sourceCodeInfo) {
|
|
98
98
|
if (!sourceCodeInfo.position || !sourceCodeInfo.code)
|
|
@@ -103,9 +103,15 @@ function getCodeMarker(sourceCodeInfo) {
|
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
function getLitsErrorMessage(message, sourceCodeInfo) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
106
|
+
if (!sourceCodeInfo) {
|
|
107
|
+
return message;
|
|
108
|
+
}
|
|
109
|
+
var location = "".concat(sourceCodeInfo.position.line, ":").concat(sourceCodeInfo.position.column);
|
|
110
|
+
var filePathLine = sourceCodeInfo.filePath
|
|
111
|
+
? "\n".concat(sourceCodeInfo.filePath, ":").concat(location)
|
|
112
|
+
: "\nLocation ".concat(location);
|
|
113
|
+
var codeLine = "\n".concat(sourceCodeInfo.code);
|
|
114
|
+
var codeMarker = "\n".concat(getCodeMarker(sourceCodeInfo));
|
|
109
115
|
return "".concat(message).concat(filePathLine).concat(codeLine).concat(codeMarker);
|
|
110
116
|
}
|
|
111
117
|
var RecurSignal = /** @class */ (function (_super) {
|
|
@@ -240,6 +246,7 @@ var functionTypes = [
|
|
|
240
246
|
'SomePred',
|
|
241
247
|
'Fnull',
|
|
242
248
|
'Builtin',
|
|
249
|
+
'SpecialBuiltin',
|
|
243
250
|
'NativeJsFunction',
|
|
244
251
|
];
|
|
245
252
|
var functionTypeSet = new Set(functionTypes);
|
|
@@ -411,7 +418,7 @@ function findUnresolvedSymbolsInNode(node, contextStack, builtin, evaluateNode)
|
|
|
411
418
|
return findUnresolvedSymbolsInNode(node[1], contextStack, builtin, evaluateNode);
|
|
412
419
|
/* v8 ignore next 2 */
|
|
413
420
|
default:
|
|
414
|
-
throw new
|
|
421
|
+
throw new LitsError("Unhandled node type: ".concat(nodeType), node[2]);
|
|
415
422
|
}
|
|
416
423
|
}
|
|
417
424
|
|
|
@@ -3791,6 +3798,26 @@ var andSpecialExpression = {
|
|
|
3791
3798
|
}
|
|
3792
3799
|
return value;
|
|
3793
3800
|
},
|
|
3801
|
+
evaluateAsNormalExpression: function (params, sourceCodeInfo) {
|
|
3802
|
+
var e_2, _a;
|
|
3803
|
+
var value = true;
|
|
3804
|
+
try {
|
|
3805
|
+
for (var params_1 = __values(params), params_1_1 = params_1.next(); !params_1_1.done; params_1_1 = params_1.next()) {
|
|
3806
|
+
var param = params_1_1.value;
|
|
3807
|
+
value = asAny(param, sourceCodeInfo);
|
|
3808
|
+
if (!value)
|
|
3809
|
+
break;
|
|
3810
|
+
}
|
|
3811
|
+
}
|
|
3812
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
3813
|
+
finally {
|
|
3814
|
+
try {
|
|
3815
|
+
if (params_1_1 && !params_1_1.done && (_a = params_1.return)) _a.call(params_1);
|
|
3816
|
+
}
|
|
3817
|
+
finally { if (e_2) throw e_2.error; }
|
|
3818
|
+
}
|
|
3819
|
+
return value;
|
|
3820
|
+
},
|
|
3794
3821
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
3795
3822
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
3796
3823
|
return getUndefinedSymbols(node[1][1], contextStack, builtin, evaluateNode);
|
|
@@ -4035,7 +4062,7 @@ var defSpecialExpression = {
|
|
|
4035
4062
|
var value = bindingNode[1][1];
|
|
4036
4063
|
var bindingValue = evaluateNode(value, contextStack);
|
|
4037
4064
|
var values = evalueateBindingNodeValues(target, bindingValue, function (Node) { return evaluateNode(Node, contextStack); });
|
|
4038
|
-
contextStack.exportValues(values);
|
|
4065
|
+
contextStack.exportValues(values, target[2]);
|
|
4039
4066
|
return null;
|
|
4040
4067
|
},
|
|
4041
4068
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
@@ -4047,7 +4074,7 @@ var defSpecialExpression = {
|
|
|
4047
4074
|
walkDefaults(target, function (defaultNode) {
|
|
4048
4075
|
addToSet(bindingResult, getUndefinedSymbols([defaultNode], contextStack, builtin, evaluateNode));
|
|
4049
4076
|
});
|
|
4050
|
-
contextStack.addValues(getAllBindingTargetNames(target));
|
|
4077
|
+
contextStack.addValues(getAllBindingTargetNames(target), target[2]);
|
|
4051
4078
|
return bindingResult;
|
|
4052
4079
|
},
|
|
4053
4080
|
};
|
|
@@ -4165,14 +4192,14 @@ var functionSpecialExpression = {
|
|
|
4165
4192
|
_b.name = functionSymbol[1],
|
|
4166
4193
|
_b.evaluatedfunction = evaluatedFunction,
|
|
4167
4194
|
_b);
|
|
4168
|
-
contextStack.addValues((_c = {}, _c[functionSymbol[1]] = litsFunction, _c));
|
|
4195
|
+
contextStack.addValues((_c = {}, _c[functionSymbol[1]] = litsFunction, _c), functionSymbol[2]);
|
|
4169
4196
|
return null;
|
|
4170
4197
|
},
|
|
4171
4198
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
4172
4199
|
var _b, _c;
|
|
4173
4200
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
4174
4201
|
var functionName = node[1][1][1];
|
|
4175
|
-
contextStack.addValues((_b = {}, _b[functionName] = true, _b));
|
|
4202
|
+
contextStack.addValues((_b = {}, _b[functionName] = true, _b), node[1][1][2]);
|
|
4176
4203
|
var newContext = (_c = {}, _c[functionName] = { value: true }, _c);
|
|
4177
4204
|
return getFunctionUnresolvedSymbols(node[1][2], contextStack, getUndefinedSymbols, builtin, evaluateNode, newContext);
|
|
4178
4205
|
},
|
|
@@ -4193,7 +4220,7 @@ var defnSpecialExpression = {
|
|
|
4193
4220
|
_b.name = functionSymbol[1],
|
|
4194
4221
|
_b.evaluatedfunction = evaluatedFunctionOverloades,
|
|
4195
4222
|
_b);
|
|
4196
|
-
contextStack.exportValues((_c = {}, _c[functionSymbol[1]] = litsFunction, _c));
|
|
4223
|
+
contextStack.exportValues((_c = {}, _c[functionSymbol[1]] = litsFunction, _c), functionSymbol[2]);
|
|
4197
4224
|
return null;
|
|
4198
4225
|
},
|
|
4199
4226
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
@@ -4201,7 +4228,7 @@ var defnSpecialExpression = {
|
|
|
4201
4228
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
4202
4229
|
var functionName = node[1][1][1];
|
|
4203
4230
|
var fn = node[1][2];
|
|
4204
|
-
contextStack.exportValues((_b = {}, _b[functionName] = true, _b));
|
|
4231
|
+
contextStack.exportValues((_b = {}, _b[functionName] = true, _b), node[1][1][2]);
|
|
4205
4232
|
var newContext = (_c = {}, _c[functionName] = { value: true }, _c);
|
|
4206
4233
|
return getFunctionUnresolvedSymbols(fn, contextStack, getUndefinedSymbols, builtin, evaluateNode, newContext);
|
|
4207
4234
|
},
|
|
@@ -4313,7 +4340,7 @@ var letSpecialExpression = {
|
|
|
4313
4340
|
var value = bindingNode[1][1];
|
|
4314
4341
|
var bindingValue = evaluateNode(value, contextStack);
|
|
4315
4342
|
var values = evalueateBindingNodeValues(target, bindingValue, function (Node) { return evaluateNode(Node, contextStack); });
|
|
4316
|
-
contextStack.addValues(values);
|
|
4343
|
+
contextStack.addValues(values, target[2]);
|
|
4317
4344
|
return null;
|
|
4318
4345
|
},
|
|
4319
4346
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
@@ -4325,7 +4352,7 @@ var letSpecialExpression = {
|
|
|
4325
4352
|
walkDefaults(target, function (defaultNode) {
|
|
4326
4353
|
addToSet(bindingResult, getUndefinedSymbols([defaultNode], contextStack, builtin, evaluateNode));
|
|
4327
4354
|
});
|
|
4328
|
-
contextStack.addValues(getAllBindingTargetNames(target));
|
|
4355
|
+
contextStack.addValues(getAllBindingTargetNames(target), target[2]);
|
|
4329
4356
|
return bindingResult;
|
|
4330
4357
|
},
|
|
4331
4358
|
};
|
|
@@ -4596,6 +4623,26 @@ var orSpecialExpression = {
|
|
|
4596
4623
|
}
|
|
4597
4624
|
return value;
|
|
4598
4625
|
},
|
|
4626
|
+
evaluateAsNormalExpression: function (params, sourceCodeInfo) {
|
|
4627
|
+
var e_2, _a;
|
|
4628
|
+
var value = false;
|
|
4629
|
+
try {
|
|
4630
|
+
for (var params_1 = __values(params), params_1_1 = params_1.next(); !params_1_1.done; params_1_1 = params_1.next()) {
|
|
4631
|
+
var param = params_1_1.value;
|
|
4632
|
+
value = asAny(param, sourceCodeInfo);
|
|
4633
|
+
if (value)
|
|
4634
|
+
break;
|
|
4635
|
+
}
|
|
4636
|
+
}
|
|
4637
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
4638
|
+
finally {
|
|
4639
|
+
try {
|
|
4640
|
+
if (params_1_1 && !params_1_1.done && (_a = params_1.return)) _a.call(params_1);
|
|
4641
|
+
}
|
|
4642
|
+
finally { if (e_2) throw e_2.error; }
|
|
4643
|
+
}
|
|
4644
|
+
return value;
|
|
4645
|
+
},
|
|
4599
4646
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
4600
4647
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
4601
4648
|
return getUndefinedSymbols(node[1][1], contextStack, builtin, evaluateNode);
|
|
@@ -4614,6 +4661,11 @@ var qqSpecialExpression = {
|
|
|
4614
4661
|
var firstResult = evaluateNode(firstNode, contextStack);
|
|
4615
4662
|
return firstResult !== null && firstResult !== void 0 ? firstResult : (secondNode ? evaluateNode(secondNode, contextStack) : null);
|
|
4616
4663
|
},
|
|
4664
|
+
evaluateAsNormalExpression: function (params, sourceCodeInfo) {
|
|
4665
|
+
var firstParam = asAny(params[0], sourceCodeInfo);
|
|
4666
|
+
var secondParam = params[1] !== undefined ? asAny(params[1], sourceCodeInfo) : null;
|
|
4667
|
+
return firstParam !== null && firstParam !== void 0 ? firstParam : secondParam;
|
|
4668
|
+
},
|
|
4617
4669
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
4618
4670
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
4619
4671
|
return getUndefinedSymbols(node[1][1].filter(function (n) { return !!n; }), contextStack, builtin, evaluateNode);
|
|
@@ -4628,6 +4680,9 @@ var recurSpecialExpression = {
|
|
|
4628
4680
|
var evaluatedParams = params.map(function (paramNode) { return evaluateNode(paramNode, contextStack); });
|
|
4629
4681
|
throw new RecurSignal(evaluatedParams);
|
|
4630
4682
|
},
|
|
4683
|
+
evaluateAsNormalExpression: function (params) {
|
|
4684
|
+
throw new RecurSignal(params);
|
|
4685
|
+
},
|
|
4631
4686
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
4632
4687
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
4633
4688
|
return getUndefinedSymbols(node[1][1], contextStack, builtin, evaluateNode);
|
|
@@ -4643,6 +4698,12 @@ var throwSpecialExpression = {
|
|
|
4643
4698
|
});
|
|
4644
4699
|
throw new UserDefinedError(message, node[2]);
|
|
4645
4700
|
},
|
|
4701
|
+
evaluateAsNormalExpression: function (params, sourceCodeInfo) {
|
|
4702
|
+
var message = asString(params[0], sourceCodeInfo, {
|
|
4703
|
+
nonEmpty: true,
|
|
4704
|
+
});
|
|
4705
|
+
throw new UserDefinedError(message, undefined);
|
|
4706
|
+
},
|
|
4646
4707
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
4647
4708
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
4648
4709
|
return getUndefinedSymbols([node[1][1]], contextStack, builtin, evaluateNode);
|
|
@@ -4710,6 +4771,24 @@ var arraySpecialExpression = {
|
|
|
4710
4771
|
}
|
|
4711
4772
|
return result;
|
|
4712
4773
|
},
|
|
4774
|
+
evaluateAsNormalExpression: function (params, sourceCodeInfo) {
|
|
4775
|
+
var e_2, _a;
|
|
4776
|
+
var result = [];
|
|
4777
|
+
try {
|
|
4778
|
+
for (var params_1 = __values(params), params_1_1 = params_1.next(); !params_1_1.done; params_1_1 = params_1.next()) {
|
|
4779
|
+
var param = params_1_1.value;
|
|
4780
|
+
result.push(asAny(param, sourceCodeInfo));
|
|
4781
|
+
}
|
|
4782
|
+
}
|
|
4783
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
4784
|
+
finally {
|
|
4785
|
+
try {
|
|
4786
|
+
if (params_1_1 && !params_1_1.done && (_a = params_1.return)) _a.call(params_1);
|
|
4787
|
+
}
|
|
4788
|
+
finally { if (e_2) throw e_2.error; }
|
|
4789
|
+
}
|
|
4790
|
+
return result;
|
|
4791
|
+
},
|
|
4713
4792
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
4714
4793
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
4715
4794
|
return getUndefinedSymbols(node[1][1], contextStack, builtin, evaluateNode);
|
|
@@ -4741,6 +4820,16 @@ var objectSpecialExpression = {
|
|
|
4741
4820
|
}
|
|
4742
4821
|
return result;
|
|
4743
4822
|
},
|
|
4823
|
+
evaluateAsNormalExpression: function (params, sourceCodeInfo) {
|
|
4824
|
+
var result = {};
|
|
4825
|
+
for (var i = 0; i < params.length; i += 2) {
|
|
4826
|
+
var key = params[i];
|
|
4827
|
+
var value = params[i + 1];
|
|
4828
|
+
assertString(key, sourceCodeInfo);
|
|
4829
|
+
result[key] = value !== null && value !== void 0 ? value : null;
|
|
4830
|
+
}
|
|
4831
|
+
return result;
|
|
4832
|
+
},
|
|
4744
4833
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
4745
4834
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
4746
4835
|
return getUndefinedSymbols(node[1][1], contextStack, builtin, evaluateNode);
|
|
@@ -4984,6 +5073,16 @@ var functionExecutors = {
|
|
|
4984
5073
|
var normalExpression = asNonUndefined(allNormalExpressions[fn.normalBuitinSymbolType], sourceCodeInfo);
|
|
4985
5074
|
return normalExpression.evaluate(params, sourceCodeInfo, contextStack, { executeFunction: executeFunction });
|
|
4986
5075
|
},
|
|
5076
|
+
SpecialBuiltin: function (fn, params, sourceCodeInfo, contextStack, _a) {
|
|
5077
|
+
var executeFunction = _a.executeFunction;
|
|
5078
|
+
var specialExpression = asNonUndefined(specialExpressions[fn.specialBuiltinSymbolType], sourceCodeInfo);
|
|
5079
|
+
if (specialExpression.evaluateAsNormalExpression) {
|
|
5080
|
+
return specialExpression.evaluateAsNormalExpression(params, sourceCodeInfo, contextStack, { executeFunction: executeFunction });
|
|
5081
|
+
}
|
|
5082
|
+
else {
|
|
5083
|
+
throw new LitsError("Special builtin function ".concat(fn.specialBuiltinSymbolType, " is not supported as normal expression."), sourceCodeInfo);
|
|
5084
|
+
}
|
|
5085
|
+
},
|
|
4987
5086
|
};
|
|
4988
5087
|
|
|
4989
5088
|
function evaluate(ast, contextStack) {
|
|
@@ -5039,7 +5138,21 @@ function evaluateReservedSymbol(node) {
|
|
|
5039
5138
|
function evaluateNormalExpression(node, contextStack) {
|
|
5040
5139
|
var sourceCodeInfo = node[2];
|
|
5041
5140
|
var paramNodes = node[1][1];
|
|
5042
|
-
var params =
|
|
5141
|
+
var params = [];
|
|
5142
|
+
paramNodes.forEach(function (paramNode) {
|
|
5143
|
+
if (isSpreadNode(paramNode)) {
|
|
5144
|
+
var spreadValue = evaluateNode(paramNode[1], contextStack);
|
|
5145
|
+
if (Array.isArray(spreadValue)) {
|
|
5146
|
+
params.push.apply(params, __spreadArray([], __read(spreadValue), false));
|
|
5147
|
+
}
|
|
5148
|
+
else {
|
|
5149
|
+
throw new LitsError("Spread operator requires an array, got ".concat(valueToString(paramNode)), paramNode[2]);
|
|
5150
|
+
}
|
|
5151
|
+
}
|
|
5152
|
+
else {
|
|
5153
|
+
params.push(evaluateNode(paramNode, contextStack));
|
|
5154
|
+
}
|
|
5155
|
+
});
|
|
5043
5156
|
if (isNormalExpressionNodeWithName(node)) {
|
|
5044
5157
|
var nameSymbol = node[1][0];
|
|
5045
5158
|
if (isNormalBuiltinSymbolNode(nameSymbol)) {
|
|
@@ -5141,19 +5254,19 @@ var ContextStackImpl = /** @class */ (function () {
|
|
|
5141
5254
|
var contexts = [{}, context];
|
|
5142
5255
|
return new ContextStackImpl({ contexts: contexts });
|
|
5143
5256
|
};
|
|
5144
|
-
ContextStackImpl.prototype.exportValues = function (values) {
|
|
5257
|
+
ContextStackImpl.prototype.exportValues = function (values, sourceCodeInfo) {
|
|
5145
5258
|
var e_1, _a;
|
|
5146
5259
|
try {
|
|
5147
5260
|
for (var _b = __values(Object.entries(values)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
5148
5261
|
var _d = __read(_c.value, 2), name_1 = _d[0], value = _d[1];
|
|
5149
5262
|
if (this.globalContext[name_1]) {
|
|
5150
|
-
throw new
|
|
5263
|
+
throw new LitsError("Cannot redefine exported value \"".concat(name_1, "\""), sourceCodeInfo);
|
|
5151
5264
|
}
|
|
5152
5265
|
if (specialExpressionKeys.includes(name_1)) {
|
|
5153
|
-
throw new
|
|
5266
|
+
throw new LitsError("Cannot shadow special expression \"".concat(name_1, "\""), sourceCodeInfo);
|
|
5154
5267
|
}
|
|
5155
5268
|
if (normalExpressionKeys.includes(name_1)) {
|
|
5156
|
-
throw new
|
|
5269
|
+
throw new LitsError("Cannot shadow builtin function \"".concat(name_1, "\""), sourceCodeInfo);
|
|
5157
5270
|
}
|
|
5158
5271
|
this.globalContext[name_1] = { value: value };
|
|
5159
5272
|
}
|
|
@@ -5165,22 +5278,22 @@ var ContextStackImpl = /** @class */ (function () {
|
|
|
5165
5278
|
}
|
|
5166
5279
|
finally { if (e_1) throw e_1.error; }
|
|
5167
5280
|
}
|
|
5168
|
-
this.addValues(values);
|
|
5281
|
+
this.addValues(values, sourceCodeInfo);
|
|
5169
5282
|
};
|
|
5170
|
-
ContextStackImpl.prototype.addValues = function (values) {
|
|
5283
|
+
ContextStackImpl.prototype.addValues = function (values, sourceCodeInfo) {
|
|
5171
5284
|
var e_2, _a;
|
|
5172
5285
|
var currentContext = this.contexts[0];
|
|
5173
5286
|
try {
|
|
5174
5287
|
for (var _b = __values(Object.entries(values)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
5175
5288
|
var _d = __read(_c.value, 2), name_2 = _d[0], value = _d[1];
|
|
5176
5289
|
if (currentContext[name_2]) {
|
|
5177
|
-
throw new
|
|
5290
|
+
throw new LitsError("Cannot redefine value \"".concat(name_2, "\""), sourceCodeInfo);
|
|
5178
5291
|
}
|
|
5179
5292
|
if (specialExpressionKeys.includes(name_2)) {
|
|
5180
|
-
throw new
|
|
5293
|
+
throw new LitsError("Cannot shadow special expression \"".concat(name_2, "\""), sourceCodeInfo);
|
|
5181
5294
|
}
|
|
5182
5295
|
if (normalExpressionKeys.includes(name_2)) {
|
|
5183
|
-
throw new
|
|
5296
|
+
throw new LitsError("Cannot shadow builtin function \"".concat(name_2, "\""), sourceCodeInfo);
|
|
5184
5297
|
}
|
|
5185
5298
|
currentContext[name_2] = { value: toAny(value) };
|
|
5186
5299
|
}
|
|
@@ -5260,18 +5373,36 @@ var ContextStackImpl = /** @class */ (function () {
|
|
|
5260
5373
|
return null;
|
|
5261
5374
|
};
|
|
5262
5375
|
ContextStackImpl.prototype.evaluateSymbol = function (node) {
|
|
5263
|
-
var _a;
|
|
5376
|
+
var _a, _b;
|
|
5264
5377
|
if (isSpecialBuiltinSymbolNode(node)) {
|
|
5265
|
-
|
|
5378
|
+
var functionType = node[1];
|
|
5379
|
+
switch (functionType) {
|
|
5380
|
+
case specialExpressionTypes['&&']:
|
|
5381
|
+
case specialExpressionTypes['||']:
|
|
5382
|
+
case specialExpressionTypes.array:
|
|
5383
|
+
case specialExpressionTypes.object:
|
|
5384
|
+
case specialExpressionTypes['defined?']:
|
|
5385
|
+
case specialExpressionTypes.recur:
|
|
5386
|
+
case specialExpressionTypes.throw:
|
|
5387
|
+
case specialExpressionTypes['??']:
|
|
5388
|
+
return _a = {},
|
|
5389
|
+
_a[FUNCTION_SYMBOL] = true,
|
|
5390
|
+
_a.functionType = 'SpecialBuiltin',
|
|
5391
|
+
_a.specialBuiltinSymbolType = functionType,
|
|
5392
|
+
_a.sourceCodeInfo = node[2],
|
|
5393
|
+
_a;
|
|
5394
|
+
default:
|
|
5395
|
+
throw new LitsError("Unknown special builtin symbol type: ".concat(functionType), node[2]);
|
|
5396
|
+
}
|
|
5266
5397
|
}
|
|
5267
5398
|
if (isNormalBuiltinSymbolNode(node)) {
|
|
5268
5399
|
var type = node[1];
|
|
5269
|
-
return
|
|
5270
|
-
|
|
5271
|
-
|
|
5272
|
-
|
|
5273
|
-
|
|
5274
|
-
|
|
5400
|
+
return _b = {},
|
|
5401
|
+
_b[FUNCTION_SYMBOL] = true,
|
|
5402
|
+
_b.functionType = 'Builtin',
|
|
5403
|
+
_b.normalBuitinSymbolType = type,
|
|
5404
|
+
_b.sourceCodeInfo = node[2],
|
|
5405
|
+
_b;
|
|
5275
5406
|
}
|
|
5276
5407
|
var lookUpResult = this.lookUp(node);
|
|
5277
5408
|
if (isContextEntry(lookUpResult))
|
|
@@ -5943,7 +6074,7 @@ function withSourceCodeInfo(node, sourceCodeInfo) {
|
|
|
5943
6074
|
}
|
|
5944
6075
|
return node;
|
|
5945
6076
|
}
|
|
5946
|
-
function getPrecedence(operatorSign) {
|
|
6077
|
+
function getPrecedence(operatorSign, sourceCodeInfo) {
|
|
5947
6078
|
switch (operatorSign) {
|
|
5948
6079
|
case '**': // exponentiation
|
|
5949
6080
|
return exponentiationPrecedence;
|
|
@@ -5982,7 +6113,7 @@ function getPrecedence(operatorSign) {
|
|
|
5982
6113
|
// leave room for binaryFunctionalOperatorPrecedence = 1
|
|
5983
6114
|
/* v8 ignore next 2 */
|
|
5984
6115
|
default:
|
|
5985
|
-
throw new
|
|
6116
|
+
throw new LitsError("Unknown binary operator: ".concat(operatorSign), sourceCodeInfo);
|
|
5986
6117
|
}
|
|
5987
6118
|
}
|
|
5988
6119
|
function createNamedNormalExpressionNode(symbolNode, params, sourceCodeInfo) {
|
|
@@ -6115,7 +6246,7 @@ var Parser = /** @class */ (function () {
|
|
|
6115
6246
|
while (!this.isAtExpressionEnd()) {
|
|
6116
6247
|
if (isA_BinaryOperatorToken(operator)) {
|
|
6117
6248
|
var name_1 = operator[1];
|
|
6118
|
-
var newPrecedece = getPrecedence(name_1);
|
|
6249
|
+
var newPrecedece = getPrecedence(name_1, operator[2]);
|
|
6119
6250
|
if (newPrecedece <= precedence
|
|
6120
6251
|
// ** (exponentiation) is right associative
|
|
6121
6252
|
&& !(newPrecedece === exponentiationPrecedence && precedence === exponentiationPrecedence)) {
|
|
@@ -6195,7 +6326,7 @@ var Parser = /** @class */ (function () {
|
|
|
6195
6326
|
this.advance();
|
|
6196
6327
|
var expression = this.parseExpression();
|
|
6197
6328
|
if (!isRParenToken(this.peek())) {
|
|
6198
|
-
throw new
|
|
6329
|
+
throw new LitsError('Expected closing parenthesis', this.peek()[2]);
|
|
6199
6330
|
}
|
|
6200
6331
|
this.advance();
|
|
6201
6332
|
return expression;
|
|
@@ -6317,7 +6448,13 @@ var Parser = /** @class */ (function () {
|
|
|
6317
6448
|
this.advance();
|
|
6318
6449
|
var params = [];
|
|
6319
6450
|
while (!this.isAtEnd() && !isRParenToken(this.peek())) {
|
|
6320
|
-
|
|
6451
|
+
if (isOperatorToken(this.peek(), '...')) {
|
|
6452
|
+
this.advance();
|
|
6453
|
+
params.push(withSourceCodeInfo([NodeTypes.Spread, this.parseExpression()], this.peek()[2]));
|
|
6454
|
+
}
|
|
6455
|
+
else {
|
|
6456
|
+
params.push(this.parseExpression());
|
|
6457
|
+
}
|
|
6321
6458
|
var nextToken = this.peek();
|
|
6322
6459
|
if (!isOperatorToken(nextToken, ',') && !isRParenToken(nextToken)) {
|
|
6323
6460
|
throw new LitsError('Expected comma or closing parenthesis', this.peek()[2]);
|
|
@@ -6373,7 +6510,7 @@ var Parser = /** @class */ (function () {
|
|
|
6373
6510
|
throw new LitsError("".concat(type, " is not allowed"), symbol[2]);
|
|
6374
6511
|
/* v8 ignore next 2 */
|
|
6375
6512
|
default:
|
|
6376
|
-
throw new
|
|
6513
|
+
throw new LitsError("Unknown special expression: ".concat(type), symbol[2]);
|
|
6377
6514
|
}
|
|
6378
6515
|
}
|
|
6379
6516
|
else if (isNormalBuiltinSymbolNode(symbol) || isNormalBuiltinSymbolNode(symbol)) {
|
|
@@ -28,6 +28,7 @@ export interface EvaluateHelpers {
|
|
|
28
28
|
}
|
|
29
29
|
export interface BuiltinSpecialExpression<T, N extends SpecialExpressionNode> {
|
|
30
30
|
evaluate: (node: N, contextStack: ContextStack, helpers: EvaluateHelpers) => T;
|
|
31
|
+
evaluateAsNormalExpression?: NormalExpressionEvaluator<T>;
|
|
31
32
|
paramCount: Count;
|
|
32
33
|
getUndefinedSymbols: (node: N, contextStack: ContextStack, params: {
|
|
33
34
|
getUndefinedSymbols: GetUndefinedSymbols;
|
|
@@ -13,7 +13,7 @@ export declare const NodeTypes: {
|
|
|
13
13
|
export type NodeType = typeof NodeTypes[keyof typeof NodeTypes];
|
|
14
14
|
export declare function getNodeTypeName(type: NodeType): keyof typeof NodeTypes;
|
|
15
15
|
export declare function isNodeType(type: unknown): type is NodeType;
|
|
16
|
-
declare const functionTypes: readonly ["UserDefined", "Partial", "Comp", "Constantly", "Juxt", "Complement", "EveryPred", "SomePred", "Fnull", "Builtin", "NativeJsFunction"];
|
|
16
|
+
declare const functionTypes: readonly ["UserDefined", "Partial", "Comp", "Constantly", "Juxt", "Complement", "EveryPred", "SomePred", "Fnull", "Builtin", "SpecialBuiltin", "NativeJsFunction"];
|
|
17
17
|
export type FunctionType = typeof functionTypes[number];
|
|
18
18
|
export declare function isFunctionType(type: unknown): type is FunctionType;
|
|
19
19
|
export {};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Any } from '../interface';
|
|
2
2
|
import type { ContextParams, LazyValue } from '../Lits/Lits';
|
|
3
3
|
import type { NativeJsFunction, SymbolNode, UserDefinedSymbolNode } from '../parser/types';
|
|
4
|
+
import type { SourceCodeInfo } from '../tokenizer/token';
|
|
4
5
|
import type { Context, LookUpResult } from './interface';
|
|
5
6
|
export type ContextStack = ContextStackImpl;
|
|
6
7
|
export declare class ContextStackImpl {
|
|
@@ -17,8 +18,8 @@ export declare class ContextStackImpl {
|
|
|
17
18
|
});
|
|
18
19
|
create(context: Context): ContextStack;
|
|
19
20
|
new(context: Context): ContextStack;
|
|
20
|
-
exportValues(values: Record<string, Any
|
|
21
|
-
addValues(values: Record<string, Any
|
|
21
|
+
exportValues(values: Record<string, Any>, sourceCodeInfo: SourceCodeInfo | undefined): void;
|
|
22
|
+
addValues(values: Record<string, Any>, sourceCodeInfo: SourceCodeInfo | undefined): void;
|
|
22
23
|
getValue(name: string): unknown;
|
|
23
24
|
lookUp(node: UserDefinedSymbolNode): LookUpResult;
|
|
24
25
|
evaluateSymbol(node: SymbolNode): Any;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Any, Arr } from '../interface';
|
|
2
|
-
import {
|
|
2
|
+
import type { LitsFunctionType } from '../parser/types';
|
|
3
3
|
import type { SourceCodeInfo } from '../tokenizer/token';
|
|
4
4
|
import type { ContextStack } from './ContextStack';
|
|
5
5
|
import type { EvaluateNode, ExecuteFunction } from './interface';
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { JsFunction } from '../Lits/Lits';
|
|
2
2
|
import type { SpecialExpressionType } from '../builtin';
|
|
3
|
+
import type { specialExpressionTypes } from '../builtin/specialExpressionTypes';
|
|
3
4
|
import type { FunctionType, NodeType, NodeTypes } from '../constants/constants';
|
|
4
5
|
import type { Context } from '../evaluator/interface';
|
|
5
6
|
import type { Any, Arr } from '../interface';
|
|
@@ -69,7 +70,11 @@ export interface NormalBuiltinFunction extends GenericLitsFunction {
|
|
|
69
70
|
functionType: 'Builtin';
|
|
70
71
|
normalBuitinSymbolType: number;
|
|
71
72
|
}
|
|
72
|
-
export
|
|
73
|
+
export interface SpecialBuiltinFunction extends GenericLitsFunction {
|
|
74
|
+
functionType: 'SpecialBuiltin';
|
|
75
|
+
specialBuiltinSymbolType: typeof specialExpressionTypes['&&'] | typeof specialExpressionTypes['||'] | typeof specialExpressionTypes['array'] | typeof specialExpressionTypes['object'] | typeof specialExpressionTypes['defined?'] | typeof specialExpressionTypes['recur'] | typeof specialExpressionTypes['throw'] | typeof specialExpressionTypes['??'];
|
|
76
|
+
}
|
|
77
|
+
export type LitsFunction = NativeJsFunction | UserDefinedFunction | NormalBuiltinFunction | SpecialBuiltinFunction | PartialFunction | CompFunction | ConstantlyFunction | JuxtFunction | ComplementFunction | EveryPredFunction | SomePredFunction | FNullFunction;
|
|
73
78
|
export type LitsFunctionType = LitsFunction['functionType'];
|
|
74
79
|
export type DebugData = {
|
|
75
80
|
token: Token;
|
|
@@ -24,11 +24,11 @@ export type WhitespaceToken = GenericToken<'Whitespace'>;
|
|
|
24
24
|
export type Token = LBraceToken | LBracketToken | LParenToken | RBraceToken | RBracketToken | RParenToken | BasePrefixedNumberToken | MultiLineCommentToken | NumberToken | OperatorToken | RegexpShorthandToken | ReservedSymbolToken | SingleLineCommentToken | StringToken | SymbolToken | WhitespaceToken;
|
|
25
25
|
export type TokenDescriptor<T extends Token> = [length: number, token?: T];
|
|
26
26
|
export interface SourceCodeInfo {
|
|
27
|
-
position
|
|
27
|
+
position: {
|
|
28
28
|
line: number;
|
|
29
29
|
column: number;
|
|
30
30
|
};
|
|
31
|
-
code
|
|
31
|
+
code: string;
|
|
32
32
|
filePath?: string;
|
|
33
33
|
}
|
|
34
34
|
export declare function isSymbolToken<T extends string>(token: Token, symbolName?: T): token is SymbolToken<T>;
|