@mojir/lits 2.1.2 → 2.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/dist/cli/cli.js +215 -59
- 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 +210 -58
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +210 -58
- package/dist/index.js.map +1 -1
- package/dist/lits.iife.js +210 -58
- 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 +207 -55
- package/dist/testFramework.esm.js.map +1 -1
- package/dist/testFramework.js +207 -55
- package/dist/testFramework.js.map +1 -1
- package/package.json +1 -1
|
@@ -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>;
|
|
@@ -96,9 +96,15 @@ function getCodeMarker(sourceCodeInfo) {
|
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
function getLitsErrorMessage(message, sourceCodeInfo) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
99
|
+
if (!sourceCodeInfo) {
|
|
100
|
+
return message;
|
|
101
|
+
}
|
|
102
|
+
var location = "".concat(sourceCodeInfo.position.line, ":").concat(sourceCodeInfo.position.column);
|
|
103
|
+
var filePathLine = sourceCodeInfo.filePath
|
|
104
|
+
? "\n".concat(sourceCodeInfo.filePath, ":").concat(location)
|
|
105
|
+
: "\nLocation ".concat(location);
|
|
106
|
+
var codeLine = "\n".concat(sourceCodeInfo.code);
|
|
107
|
+
var codeMarker = "\n".concat(getCodeMarker(sourceCodeInfo));
|
|
102
108
|
return "".concat(message).concat(filePathLine).concat(codeLine).concat(codeMarker);
|
|
103
109
|
}
|
|
104
110
|
var RecurSignal = /** @class */ (function (_super) {
|
|
@@ -233,6 +239,7 @@ var functionTypes = [
|
|
|
233
239
|
'SomePred',
|
|
234
240
|
'Fnull',
|
|
235
241
|
'Builtin',
|
|
242
|
+
'SpecialBuiltin',
|
|
236
243
|
'NativeJsFunction',
|
|
237
244
|
];
|
|
238
245
|
var functionTypeSet = new Set(functionTypes);
|
|
@@ -404,7 +411,7 @@ function findUnresolvedSymbolsInNode(node, contextStack, builtin, evaluateNode)
|
|
|
404
411
|
return findUnresolvedSymbolsInNode(node[1], contextStack, builtin, evaluateNode);
|
|
405
412
|
/* v8 ignore next 2 */
|
|
406
413
|
default:
|
|
407
|
-
throw new
|
|
414
|
+
throw new LitsError("Unhandled node type: ".concat(nodeType), node[2]);
|
|
408
415
|
}
|
|
409
416
|
}
|
|
410
417
|
|
|
@@ -667,6 +674,10 @@ function isLitsFunction(value) {
|
|
|
667
674
|
return false;
|
|
668
675
|
return !!value[FUNCTION_SYMBOL];
|
|
669
676
|
}
|
|
677
|
+
function asLitsFunction(value, sourceCodeInfo) {
|
|
678
|
+
assertLitsFunction(value, sourceCodeInfo);
|
|
679
|
+
return value;
|
|
680
|
+
}
|
|
670
681
|
function assertLitsFunction(value, sourceCodeInfo) {
|
|
671
682
|
if (!isLitsFunction(value))
|
|
672
683
|
throw getAssertionError('LitsFunction', value, sourceCodeInfo);
|
|
@@ -1355,26 +1366,37 @@ var sequenceNormalExpression = {
|
|
|
1355
1366
|
paramCount: 1,
|
|
1356
1367
|
},
|
|
1357
1368
|
'map': {
|
|
1358
|
-
evaluate: function (
|
|
1359
|
-
var
|
|
1360
|
-
var
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1369
|
+
evaluate: function (params, sourceCodeInfo, contextStack, _a) {
|
|
1370
|
+
var executeFunction = _a.executeFunction;
|
|
1371
|
+
var fn = asLitsFunction(params.at(-1));
|
|
1372
|
+
var seqs = params.slice(0, -1);
|
|
1373
|
+
assertSeq(seqs[0], sourceCodeInfo);
|
|
1374
|
+
var isString = typeof seqs[0] === 'string';
|
|
1375
|
+
var len = seqs[0].length;
|
|
1376
|
+
seqs.slice(1).forEach(function (seq) {
|
|
1377
|
+
if (isString) {
|
|
1378
|
+
assertString(seq, sourceCodeInfo);
|
|
1379
|
+
}
|
|
1380
|
+
else {
|
|
1381
|
+
assertArray(seq, sourceCodeInfo);
|
|
1382
|
+
}
|
|
1383
|
+
len = Math.min(len, seq.length);
|
|
1384
|
+
});
|
|
1385
|
+
var paramArray = [];
|
|
1386
|
+
var _loop_1 = function (i) {
|
|
1387
|
+
paramArray.push(seqs.map(function (seq) { return seq[i]; }));
|
|
1388
|
+
};
|
|
1389
|
+
for (var i = 0; i < len; i++) {
|
|
1390
|
+
_loop_1(i);
|
|
1365
1391
|
}
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
.map(function (elem) {
|
|
1370
|
-
var newVal = executeFunction(fn, [elem], contextStack, sourceCodeInfo);
|
|
1371
|
-
assertString(newVal, sourceCodeInfo, { char: true });
|
|
1372
|
-
return newVal;
|
|
1373
|
-
})
|
|
1374
|
-
.join('');
|
|
1392
|
+
var mapped = paramArray.map(function (p) { return executeFunction(fn, p, contextStack, sourceCodeInfo); });
|
|
1393
|
+
if (!isString) {
|
|
1394
|
+
return mapped;
|
|
1375
1395
|
}
|
|
1396
|
+
mapped.forEach(function (char) { return assertString(char, sourceCodeInfo); });
|
|
1397
|
+
return mapped.join('');
|
|
1376
1398
|
},
|
|
1377
|
-
paramCount: 2,
|
|
1399
|
+
paramCount: { min: 2 },
|
|
1378
1400
|
},
|
|
1379
1401
|
'pop': {
|
|
1380
1402
|
evaluate: function (_a, sourceCodeInfo) {
|
|
@@ -1924,7 +1946,7 @@ var sequenceNormalExpression = {
|
|
|
1924
1946
|
assertSeq(input, sourceCodeInfo);
|
|
1925
1947
|
if (Array.isArray(input)) {
|
|
1926
1948
|
var result = [];
|
|
1927
|
-
var
|
|
1949
|
+
var _loop_2 = function (item) {
|
|
1928
1950
|
assertAny(item, sourceCodeInfo);
|
|
1929
1951
|
if (!result.some(function (existingItem) { return deepEqual(existingItem, item, sourceCodeInfo); })) {
|
|
1930
1952
|
result.push(item);
|
|
@@ -1933,7 +1955,7 @@ var sequenceNormalExpression = {
|
|
|
1933
1955
|
try {
|
|
1934
1956
|
for (var input_1 = __values(input), input_1_1 = input_1.next(); !input_1_1.done; input_1_1 = input_1.next()) {
|
|
1935
1957
|
var item = input_1_1.value;
|
|
1936
|
-
|
|
1958
|
+
_loop_2(item);
|
|
1937
1959
|
}
|
|
1938
1960
|
}
|
|
1939
1961
|
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
@@ -3769,6 +3791,26 @@ var andSpecialExpression = {
|
|
|
3769
3791
|
}
|
|
3770
3792
|
return value;
|
|
3771
3793
|
},
|
|
3794
|
+
evaluateAsNormalExpression: function (params, sourceCodeInfo) {
|
|
3795
|
+
var e_2, _a;
|
|
3796
|
+
var value = true;
|
|
3797
|
+
try {
|
|
3798
|
+
for (var params_1 = __values(params), params_1_1 = params_1.next(); !params_1_1.done; params_1_1 = params_1.next()) {
|
|
3799
|
+
var param = params_1_1.value;
|
|
3800
|
+
value = asAny(param, sourceCodeInfo);
|
|
3801
|
+
if (!value)
|
|
3802
|
+
break;
|
|
3803
|
+
}
|
|
3804
|
+
}
|
|
3805
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
3806
|
+
finally {
|
|
3807
|
+
try {
|
|
3808
|
+
if (params_1_1 && !params_1_1.done && (_a = params_1.return)) _a.call(params_1);
|
|
3809
|
+
}
|
|
3810
|
+
finally { if (e_2) throw e_2.error; }
|
|
3811
|
+
}
|
|
3812
|
+
return value;
|
|
3813
|
+
},
|
|
3772
3814
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
3773
3815
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
3774
3816
|
return getUndefinedSymbols(node[1][1], contextStack, builtin, evaluateNode);
|
|
@@ -4013,7 +4055,7 @@ var defSpecialExpression = {
|
|
|
4013
4055
|
var value = bindingNode[1][1];
|
|
4014
4056
|
var bindingValue = evaluateNode(value, contextStack);
|
|
4015
4057
|
var values = evalueateBindingNodeValues(target, bindingValue, function (Node) { return evaluateNode(Node, contextStack); });
|
|
4016
|
-
contextStack.exportValues(values);
|
|
4058
|
+
contextStack.exportValues(values, target[2]);
|
|
4017
4059
|
return null;
|
|
4018
4060
|
},
|
|
4019
4061
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
@@ -4025,7 +4067,7 @@ var defSpecialExpression = {
|
|
|
4025
4067
|
walkDefaults(target, function (defaultNode) {
|
|
4026
4068
|
addToSet(bindingResult, getUndefinedSymbols([defaultNode], contextStack, builtin, evaluateNode));
|
|
4027
4069
|
});
|
|
4028
|
-
contextStack.addValues(getAllBindingTargetNames(target));
|
|
4070
|
+
contextStack.addValues(getAllBindingTargetNames(target), target[2]);
|
|
4029
4071
|
return bindingResult;
|
|
4030
4072
|
},
|
|
4031
4073
|
};
|
|
@@ -4143,14 +4185,14 @@ var functionSpecialExpression = {
|
|
|
4143
4185
|
_b.name = functionSymbol[1],
|
|
4144
4186
|
_b.evaluatedfunction = evaluatedFunction,
|
|
4145
4187
|
_b);
|
|
4146
|
-
contextStack.addValues((_c = {}, _c[functionSymbol[1]] = litsFunction, _c));
|
|
4188
|
+
contextStack.addValues((_c = {}, _c[functionSymbol[1]] = litsFunction, _c), functionSymbol[2]);
|
|
4147
4189
|
return null;
|
|
4148
4190
|
},
|
|
4149
4191
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
4150
4192
|
var _b, _c;
|
|
4151
4193
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
4152
4194
|
var functionName = node[1][1][1];
|
|
4153
|
-
contextStack.addValues((_b = {}, _b[functionName] = true, _b));
|
|
4195
|
+
contextStack.addValues((_b = {}, _b[functionName] = true, _b), node[1][1][2]);
|
|
4154
4196
|
var newContext = (_c = {}, _c[functionName] = { value: true }, _c);
|
|
4155
4197
|
return getFunctionUnresolvedSymbols(node[1][2], contextStack, getUndefinedSymbols, builtin, evaluateNode, newContext);
|
|
4156
4198
|
},
|
|
@@ -4171,7 +4213,7 @@ var defnSpecialExpression = {
|
|
|
4171
4213
|
_b.name = functionSymbol[1],
|
|
4172
4214
|
_b.evaluatedfunction = evaluatedFunctionOverloades,
|
|
4173
4215
|
_b);
|
|
4174
|
-
contextStack.exportValues((_c = {}, _c[functionSymbol[1]] = litsFunction, _c));
|
|
4216
|
+
contextStack.exportValues((_c = {}, _c[functionSymbol[1]] = litsFunction, _c), functionSymbol[2]);
|
|
4175
4217
|
return null;
|
|
4176
4218
|
},
|
|
4177
4219
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
@@ -4179,7 +4221,7 @@ var defnSpecialExpression = {
|
|
|
4179
4221
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
4180
4222
|
var functionName = node[1][1][1];
|
|
4181
4223
|
var fn = node[1][2];
|
|
4182
|
-
contextStack.exportValues((_b = {}, _b[functionName] = true, _b));
|
|
4224
|
+
contextStack.exportValues((_b = {}, _b[functionName] = true, _b), node[1][1][2]);
|
|
4183
4225
|
var newContext = (_c = {}, _c[functionName] = { value: true }, _c);
|
|
4184
4226
|
return getFunctionUnresolvedSymbols(fn, contextStack, getUndefinedSymbols, builtin, evaluateNode, newContext);
|
|
4185
4227
|
},
|
|
@@ -4291,7 +4333,7 @@ var letSpecialExpression = {
|
|
|
4291
4333
|
var value = bindingNode[1][1];
|
|
4292
4334
|
var bindingValue = evaluateNode(value, contextStack);
|
|
4293
4335
|
var values = evalueateBindingNodeValues(target, bindingValue, function (Node) { return evaluateNode(Node, contextStack); });
|
|
4294
|
-
contextStack.addValues(values);
|
|
4336
|
+
contextStack.addValues(values, target[2]);
|
|
4295
4337
|
return null;
|
|
4296
4338
|
},
|
|
4297
4339
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
@@ -4303,7 +4345,7 @@ var letSpecialExpression = {
|
|
|
4303
4345
|
walkDefaults(target, function (defaultNode) {
|
|
4304
4346
|
addToSet(bindingResult, getUndefinedSymbols([defaultNode], contextStack, builtin, evaluateNode));
|
|
4305
4347
|
});
|
|
4306
|
-
contextStack.addValues(getAllBindingTargetNames(target));
|
|
4348
|
+
contextStack.addValues(getAllBindingTargetNames(target), target[2]);
|
|
4307
4349
|
return bindingResult;
|
|
4308
4350
|
},
|
|
4309
4351
|
};
|
|
@@ -4574,6 +4616,26 @@ var orSpecialExpression = {
|
|
|
4574
4616
|
}
|
|
4575
4617
|
return value;
|
|
4576
4618
|
},
|
|
4619
|
+
evaluateAsNormalExpression: function (params, sourceCodeInfo) {
|
|
4620
|
+
var e_2, _a;
|
|
4621
|
+
var value = false;
|
|
4622
|
+
try {
|
|
4623
|
+
for (var params_1 = __values(params), params_1_1 = params_1.next(); !params_1_1.done; params_1_1 = params_1.next()) {
|
|
4624
|
+
var param = params_1_1.value;
|
|
4625
|
+
value = asAny(param, sourceCodeInfo);
|
|
4626
|
+
if (value)
|
|
4627
|
+
break;
|
|
4628
|
+
}
|
|
4629
|
+
}
|
|
4630
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
4631
|
+
finally {
|
|
4632
|
+
try {
|
|
4633
|
+
if (params_1_1 && !params_1_1.done && (_a = params_1.return)) _a.call(params_1);
|
|
4634
|
+
}
|
|
4635
|
+
finally { if (e_2) throw e_2.error; }
|
|
4636
|
+
}
|
|
4637
|
+
return value;
|
|
4638
|
+
},
|
|
4577
4639
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
4578
4640
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
4579
4641
|
return getUndefinedSymbols(node[1][1], contextStack, builtin, evaluateNode);
|
|
@@ -4592,6 +4654,11 @@ var qqSpecialExpression = {
|
|
|
4592
4654
|
var firstResult = evaluateNode(firstNode, contextStack);
|
|
4593
4655
|
return firstResult !== null && firstResult !== void 0 ? firstResult : (secondNode ? evaluateNode(secondNode, contextStack) : null);
|
|
4594
4656
|
},
|
|
4657
|
+
evaluateAsNormalExpression: function (params, sourceCodeInfo) {
|
|
4658
|
+
var firstParam = asAny(params[0], sourceCodeInfo);
|
|
4659
|
+
var secondParam = params[1] !== undefined ? asAny(params[1], sourceCodeInfo) : null;
|
|
4660
|
+
return firstParam !== null && firstParam !== void 0 ? firstParam : secondParam;
|
|
4661
|
+
},
|
|
4595
4662
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
4596
4663
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
4597
4664
|
return getUndefinedSymbols(node[1][1].filter(function (n) { return !!n; }), contextStack, builtin, evaluateNode);
|
|
@@ -4606,6 +4673,9 @@ var recurSpecialExpression = {
|
|
|
4606
4673
|
var evaluatedParams = params.map(function (paramNode) { return evaluateNode(paramNode, contextStack); });
|
|
4607
4674
|
throw new RecurSignal(evaluatedParams);
|
|
4608
4675
|
},
|
|
4676
|
+
evaluateAsNormalExpression: function (params) {
|
|
4677
|
+
throw new RecurSignal(params);
|
|
4678
|
+
},
|
|
4609
4679
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
4610
4680
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
4611
4681
|
return getUndefinedSymbols(node[1][1], contextStack, builtin, evaluateNode);
|
|
@@ -4621,6 +4691,12 @@ var throwSpecialExpression = {
|
|
|
4621
4691
|
});
|
|
4622
4692
|
throw new UserDefinedError(message, node[2]);
|
|
4623
4693
|
},
|
|
4694
|
+
evaluateAsNormalExpression: function (params, sourceCodeInfo) {
|
|
4695
|
+
var message = asString(params[0], sourceCodeInfo, {
|
|
4696
|
+
nonEmpty: true,
|
|
4697
|
+
});
|
|
4698
|
+
throw new UserDefinedError(message, undefined);
|
|
4699
|
+
},
|
|
4624
4700
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
4625
4701
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
4626
4702
|
return getUndefinedSymbols([node[1][1]], contextStack, builtin, evaluateNode);
|
|
@@ -4688,6 +4764,24 @@ var arraySpecialExpression = {
|
|
|
4688
4764
|
}
|
|
4689
4765
|
return result;
|
|
4690
4766
|
},
|
|
4767
|
+
evaluateAsNormalExpression: function (params, sourceCodeInfo) {
|
|
4768
|
+
var e_2, _a;
|
|
4769
|
+
var result = [];
|
|
4770
|
+
try {
|
|
4771
|
+
for (var params_1 = __values(params), params_1_1 = params_1.next(); !params_1_1.done; params_1_1 = params_1.next()) {
|
|
4772
|
+
var param = params_1_1.value;
|
|
4773
|
+
result.push(asAny(param, sourceCodeInfo));
|
|
4774
|
+
}
|
|
4775
|
+
}
|
|
4776
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
4777
|
+
finally {
|
|
4778
|
+
try {
|
|
4779
|
+
if (params_1_1 && !params_1_1.done && (_a = params_1.return)) _a.call(params_1);
|
|
4780
|
+
}
|
|
4781
|
+
finally { if (e_2) throw e_2.error; }
|
|
4782
|
+
}
|
|
4783
|
+
return result;
|
|
4784
|
+
},
|
|
4691
4785
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
4692
4786
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
4693
4787
|
return getUndefinedSymbols(node[1][1], contextStack, builtin, evaluateNode);
|
|
@@ -4719,6 +4813,16 @@ var objectSpecialExpression = {
|
|
|
4719
4813
|
}
|
|
4720
4814
|
return result;
|
|
4721
4815
|
},
|
|
4816
|
+
evaluateAsNormalExpression: function (params, sourceCodeInfo) {
|
|
4817
|
+
var result = {};
|
|
4818
|
+
for (var i = 0; i < params.length; i += 2) {
|
|
4819
|
+
var key = params[i];
|
|
4820
|
+
var value = params[i + 1];
|
|
4821
|
+
assertString(key, sourceCodeInfo);
|
|
4822
|
+
result[key] = value !== null && value !== void 0 ? value : null;
|
|
4823
|
+
}
|
|
4824
|
+
return result;
|
|
4825
|
+
},
|
|
4722
4826
|
getUndefinedSymbols: function (node, contextStack, _a) {
|
|
4723
4827
|
var getUndefinedSymbols = _a.getUndefinedSymbols, builtin = _a.builtin, evaluateNode = _a.evaluateNode;
|
|
4724
4828
|
return getUndefinedSymbols(node[1][1], contextStack, builtin, evaluateNode);
|
|
@@ -4962,6 +5066,16 @@ var functionExecutors = {
|
|
|
4962
5066
|
var normalExpression = asNonUndefined(allNormalExpressions[fn.normalBuitinSymbolType], sourceCodeInfo);
|
|
4963
5067
|
return normalExpression.evaluate(params, sourceCodeInfo, contextStack, { executeFunction: executeFunction });
|
|
4964
5068
|
},
|
|
5069
|
+
SpecialBuiltin: function (fn, params, sourceCodeInfo, contextStack, _a) {
|
|
5070
|
+
var executeFunction = _a.executeFunction;
|
|
5071
|
+
var specialExpression = asNonUndefined(specialExpressions[fn.specialBuiltinSymbolType], sourceCodeInfo);
|
|
5072
|
+
if (specialExpression.evaluateAsNormalExpression) {
|
|
5073
|
+
return specialExpression.evaluateAsNormalExpression(params, sourceCodeInfo, contextStack, { executeFunction: executeFunction });
|
|
5074
|
+
}
|
|
5075
|
+
else {
|
|
5076
|
+
throw new LitsError("Special builtin function ".concat(fn.specialBuiltinSymbolType, " is not supported as normal expression."), sourceCodeInfo);
|
|
5077
|
+
}
|
|
5078
|
+
},
|
|
4965
5079
|
};
|
|
4966
5080
|
|
|
4967
5081
|
function evaluate(ast, contextStack) {
|
|
@@ -5017,7 +5131,21 @@ function evaluateReservedSymbol(node) {
|
|
|
5017
5131
|
function evaluateNormalExpression(node, contextStack) {
|
|
5018
5132
|
var sourceCodeInfo = node[2];
|
|
5019
5133
|
var paramNodes = node[1][1];
|
|
5020
|
-
var params =
|
|
5134
|
+
var params = [];
|
|
5135
|
+
paramNodes.forEach(function (paramNode) {
|
|
5136
|
+
if (isSpreadNode(paramNode)) {
|
|
5137
|
+
var spreadValue = evaluateNode(paramNode[1], contextStack);
|
|
5138
|
+
if (Array.isArray(spreadValue)) {
|
|
5139
|
+
params.push.apply(params, __spreadArray([], __read(spreadValue), false));
|
|
5140
|
+
}
|
|
5141
|
+
else {
|
|
5142
|
+
throw new LitsError("Spread operator requires an array, got ".concat(valueToString(paramNode)), paramNode[2]);
|
|
5143
|
+
}
|
|
5144
|
+
}
|
|
5145
|
+
else {
|
|
5146
|
+
params.push(evaluateNode(paramNode, contextStack));
|
|
5147
|
+
}
|
|
5148
|
+
});
|
|
5021
5149
|
if (isNormalExpressionNodeWithName(node)) {
|
|
5022
5150
|
var nameSymbol = node[1][0];
|
|
5023
5151
|
if (isNormalBuiltinSymbolNode(nameSymbol)) {
|
|
@@ -5119,19 +5247,19 @@ var ContextStackImpl = /** @class */ (function () {
|
|
|
5119
5247
|
var contexts = [{}, context];
|
|
5120
5248
|
return new ContextStackImpl({ contexts: contexts });
|
|
5121
5249
|
};
|
|
5122
|
-
ContextStackImpl.prototype.exportValues = function (values) {
|
|
5250
|
+
ContextStackImpl.prototype.exportValues = function (values, sourceCodeInfo) {
|
|
5123
5251
|
var e_1, _a;
|
|
5124
5252
|
try {
|
|
5125
5253
|
for (var _b = __values(Object.entries(values)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
5126
5254
|
var _d = __read(_c.value, 2), name_1 = _d[0], value = _d[1];
|
|
5127
5255
|
if (this.globalContext[name_1]) {
|
|
5128
|
-
throw new
|
|
5256
|
+
throw new LitsError("Cannot redefine exported value \"".concat(name_1, "\""), sourceCodeInfo);
|
|
5129
5257
|
}
|
|
5130
5258
|
if (specialExpressionKeys.includes(name_1)) {
|
|
5131
|
-
throw new
|
|
5259
|
+
throw new LitsError("Cannot shadow special expression \"".concat(name_1, "\""), sourceCodeInfo);
|
|
5132
5260
|
}
|
|
5133
5261
|
if (normalExpressionKeys.includes(name_1)) {
|
|
5134
|
-
throw new
|
|
5262
|
+
throw new LitsError("Cannot shadow builtin function \"".concat(name_1, "\""), sourceCodeInfo);
|
|
5135
5263
|
}
|
|
5136
5264
|
this.globalContext[name_1] = { value: value };
|
|
5137
5265
|
}
|
|
@@ -5143,22 +5271,22 @@ var ContextStackImpl = /** @class */ (function () {
|
|
|
5143
5271
|
}
|
|
5144
5272
|
finally { if (e_1) throw e_1.error; }
|
|
5145
5273
|
}
|
|
5146
|
-
this.addValues(values);
|
|
5274
|
+
this.addValues(values, sourceCodeInfo);
|
|
5147
5275
|
};
|
|
5148
|
-
ContextStackImpl.prototype.addValues = function (values) {
|
|
5276
|
+
ContextStackImpl.prototype.addValues = function (values, sourceCodeInfo) {
|
|
5149
5277
|
var e_2, _a;
|
|
5150
5278
|
var currentContext = this.contexts[0];
|
|
5151
5279
|
try {
|
|
5152
5280
|
for (var _b = __values(Object.entries(values)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
5153
5281
|
var _d = __read(_c.value, 2), name_2 = _d[0], value = _d[1];
|
|
5154
5282
|
if (currentContext[name_2]) {
|
|
5155
|
-
throw new
|
|
5283
|
+
throw new LitsError("Cannot redefine value \"".concat(name_2, "\""), sourceCodeInfo);
|
|
5156
5284
|
}
|
|
5157
5285
|
if (specialExpressionKeys.includes(name_2)) {
|
|
5158
|
-
throw new
|
|
5286
|
+
throw new LitsError("Cannot shadow special expression \"".concat(name_2, "\""), sourceCodeInfo);
|
|
5159
5287
|
}
|
|
5160
5288
|
if (normalExpressionKeys.includes(name_2)) {
|
|
5161
|
-
throw new
|
|
5289
|
+
throw new LitsError("Cannot shadow builtin function \"".concat(name_2, "\""), sourceCodeInfo);
|
|
5162
5290
|
}
|
|
5163
5291
|
currentContext[name_2] = { value: toAny(value) };
|
|
5164
5292
|
}
|
|
@@ -5238,18 +5366,36 @@ var ContextStackImpl = /** @class */ (function () {
|
|
|
5238
5366
|
return null;
|
|
5239
5367
|
};
|
|
5240
5368
|
ContextStackImpl.prototype.evaluateSymbol = function (node) {
|
|
5241
|
-
var _a;
|
|
5369
|
+
var _a, _b;
|
|
5242
5370
|
if (isSpecialBuiltinSymbolNode(node)) {
|
|
5243
|
-
|
|
5371
|
+
var functionType = node[1];
|
|
5372
|
+
switch (functionType) {
|
|
5373
|
+
case specialExpressionTypes['&&']:
|
|
5374
|
+
case specialExpressionTypes['||']:
|
|
5375
|
+
case specialExpressionTypes.array:
|
|
5376
|
+
case specialExpressionTypes.object:
|
|
5377
|
+
case specialExpressionTypes['defined?']:
|
|
5378
|
+
case specialExpressionTypes.recur:
|
|
5379
|
+
case specialExpressionTypes.throw:
|
|
5380
|
+
case specialExpressionTypes['??']:
|
|
5381
|
+
return _a = {},
|
|
5382
|
+
_a[FUNCTION_SYMBOL] = true,
|
|
5383
|
+
_a.functionType = 'SpecialBuiltin',
|
|
5384
|
+
_a.specialBuiltinSymbolType = functionType,
|
|
5385
|
+
_a.sourceCodeInfo = node[2],
|
|
5386
|
+
_a;
|
|
5387
|
+
default:
|
|
5388
|
+
throw new LitsError("Unknown special builtin symbol type: ".concat(functionType), node[2]);
|
|
5389
|
+
}
|
|
5244
5390
|
}
|
|
5245
5391
|
if (isNormalBuiltinSymbolNode(node)) {
|
|
5246
5392
|
var type = node[1];
|
|
5247
|
-
return
|
|
5248
|
-
|
|
5249
|
-
|
|
5250
|
-
|
|
5251
|
-
|
|
5252
|
-
|
|
5393
|
+
return _b = {},
|
|
5394
|
+
_b[FUNCTION_SYMBOL] = true,
|
|
5395
|
+
_b.functionType = 'Builtin',
|
|
5396
|
+
_b.normalBuitinSymbolType = type,
|
|
5397
|
+
_b.sourceCodeInfo = node[2],
|
|
5398
|
+
_b;
|
|
5253
5399
|
}
|
|
5254
5400
|
var lookUpResult = this.lookUp(node);
|
|
5255
5401
|
if (isContextEntry(lookUpResult))
|
|
@@ -5921,7 +6067,7 @@ function withSourceCodeInfo(node, sourceCodeInfo) {
|
|
|
5921
6067
|
}
|
|
5922
6068
|
return node;
|
|
5923
6069
|
}
|
|
5924
|
-
function getPrecedence(operatorSign) {
|
|
6070
|
+
function getPrecedence(operatorSign, sourceCodeInfo) {
|
|
5925
6071
|
switch (operatorSign) {
|
|
5926
6072
|
case '**': // exponentiation
|
|
5927
6073
|
return exponentiationPrecedence;
|
|
@@ -5960,7 +6106,7 @@ function getPrecedence(operatorSign) {
|
|
|
5960
6106
|
// leave room for binaryFunctionalOperatorPrecedence = 1
|
|
5961
6107
|
/* v8 ignore next 2 */
|
|
5962
6108
|
default:
|
|
5963
|
-
throw new
|
|
6109
|
+
throw new LitsError("Unknown binary operator: ".concat(operatorSign), sourceCodeInfo);
|
|
5964
6110
|
}
|
|
5965
6111
|
}
|
|
5966
6112
|
function createNamedNormalExpressionNode(symbolNode, params, sourceCodeInfo) {
|
|
@@ -6093,7 +6239,7 @@ var Parser = /** @class */ (function () {
|
|
|
6093
6239
|
while (!this.isAtExpressionEnd()) {
|
|
6094
6240
|
if (isA_BinaryOperatorToken(operator)) {
|
|
6095
6241
|
var name_1 = operator[1];
|
|
6096
|
-
var newPrecedece = getPrecedence(name_1);
|
|
6242
|
+
var newPrecedece = getPrecedence(name_1, operator[2]);
|
|
6097
6243
|
if (newPrecedece <= precedence
|
|
6098
6244
|
// ** (exponentiation) is right associative
|
|
6099
6245
|
&& !(newPrecedece === exponentiationPrecedence && precedence === exponentiationPrecedence)) {
|
|
@@ -6173,7 +6319,7 @@ var Parser = /** @class */ (function () {
|
|
|
6173
6319
|
this.advance();
|
|
6174
6320
|
var expression = this.parseExpression();
|
|
6175
6321
|
if (!isRParenToken(this.peek())) {
|
|
6176
|
-
throw new
|
|
6322
|
+
throw new LitsError('Expected closing parenthesis', this.peek()[2]);
|
|
6177
6323
|
}
|
|
6178
6324
|
this.advance();
|
|
6179
6325
|
return expression;
|
|
@@ -6295,7 +6441,13 @@ var Parser = /** @class */ (function () {
|
|
|
6295
6441
|
this.advance();
|
|
6296
6442
|
var params = [];
|
|
6297
6443
|
while (!this.isAtEnd() && !isRParenToken(this.peek())) {
|
|
6298
|
-
|
|
6444
|
+
if (isOperatorToken(this.peek(), '...')) {
|
|
6445
|
+
this.advance();
|
|
6446
|
+
params.push(withSourceCodeInfo([NodeTypes.Spread, this.parseExpression()], this.peek()[2]));
|
|
6447
|
+
}
|
|
6448
|
+
else {
|
|
6449
|
+
params.push(this.parseExpression());
|
|
6450
|
+
}
|
|
6299
6451
|
var nextToken = this.peek();
|
|
6300
6452
|
if (!isOperatorToken(nextToken, ',') && !isRParenToken(nextToken)) {
|
|
6301
6453
|
throw new LitsError('Expected comma or closing parenthesis', this.peek()[2]);
|
|
@@ -6351,7 +6503,7 @@ var Parser = /** @class */ (function () {
|
|
|
6351
6503
|
throw new LitsError("".concat(type, " is not allowed"), symbol[2]);
|
|
6352
6504
|
/* v8 ignore next 2 */
|
|
6353
6505
|
default:
|
|
6354
|
-
throw new
|
|
6506
|
+
throw new LitsError("Unknown special expression: ".concat(type), symbol[2]);
|
|
6355
6507
|
}
|
|
6356
6508
|
}
|
|
6357
6509
|
else if (isNormalBuiltinSymbolNode(symbol) || isNormalBuiltinSymbolNode(symbol)) {
|