@mojir/lits 2.1.21 → 2.1.22

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.
@@ -5,7 +5,7 @@ import type { Any, Arr } from '../interface';
5
5
  import type { SpecialExpressionNode } from '../parser/types';
6
6
  import type { SourceCodeInfo } from '../tokenizer/token';
7
7
  import type { SpecialExpressions } from '.';
8
- export type Count = number | {
8
+ export type ParamCount = number | {
9
9
  min?: number;
10
10
  max?: number;
11
11
  even?: true;
@@ -16,7 +16,7 @@ export type NormalExpressionEvaluator<T> = (params: Arr, sourceCodeInfo: SourceC
16
16
  }) => T;
17
17
  export interface BuiltinNormalExpression<T> {
18
18
  evaluate: NormalExpressionEvaluator<T>;
19
- paramCount: Count;
19
+ paramCount: ParamCount;
20
20
  aliases?: string[];
21
21
  }
22
22
  export type BuiltinNormalExpressions = Record<string, BuiltinNormalExpression<Any>>;
@@ -29,7 +29,7 @@ export interface EvaluateHelpers {
29
29
  export interface BuiltinSpecialExpression<T, N extends SpecialExpressionNode> {
30
30
  evaluate: (node: N, contextStack: ContextStack, helpers: EvaluateHelpers) => T;
31
31
  evaluateAsNormalExpression?: NormalExpressionEvaluator<T>;
32
- paramCount: Count;
32
+ paramCount: ParamCount;
33
33
  getUndefinedSymbols: (node: N, contextStack: ContextStack, params: {
34
34
  getUndefinedSymbols: GetUndefinedSymbols;
35
35
  builtin: Builtin;
@@ -1,4 +1,4 @@
1
- import type { LitsFunction, SpecialExpressionNode, SymbolNode } from '../../parser/types';
1
+ import { type LitsFunction, type SpecialExpressionNode, type SymbolNode } from '../../parser/types';
2
2
  import type { BuiltinSpecialExpression } from '../interface';
3
3
  import type { Function } from '../utils';
4
4
  import type { specialExpressionTypes } from '../specialExpressionTypes';
@@ -1,5 +1,6 @@
1
1
  import type { JsFunction } from '../Lits/Lits';
2
2
  import type { SpecialExpressionType } from '../builtin';
3
+ import type { ParamCount } from '../builtin/interface';
3
4
  import type { specialExpressionTypes } from '../builtin/specialExpressionTypes';
4
5
  import type { FunctionType, NodeType, NodeTypes } from '../constants/constants';
5
6
  import type { Context } from '../evaluator/interface';
@@ -15,6 +16,7 @@ interface GenericLitsFunction {
15
16
  [FUNCTION_SYMBOL]: true;
16
17
  sourceCodeInfo?: SourceCodeInfo;
17
18
  functionType: FunctionType;
19
+ paramCount: ParamCount;
18
20
  }
19
21
  export interface RegularExpression {
20
22
  [REGEXP_SYMBOL]: true;
@@ -1,12 +1,12 @@
1
- import type { Count } from '../builtin/interface';
1
+ import type { ParamCount } from '../builtin/interface';
2
2
  import type { UnknownRecord } from '../interface';
3
3
  import type { NormalExpressionNodeWithName } from '../parser/types';
4
4
  import type { SourceCodeInfo } from '../tokenizer/token';
5
- export declare function assertNumberOfParams(count: Count, node: NormalExpressionNodeWithName): void;
5
+ export declare function assertNumberOfParams(count: ParamCount, node: NormalExpressionNodeWithName): void;
6
6
  export declare function isNonUndefined<T>(value: T | undefined): value is T;
7
7
  export declare function asNonUndefined<T>(value: T | undefined, sourceCodeInfo?: SourceCodeInfo): T;
8
8
  export declare function assertNonUndefined<T>(value: T | undefined, sourceCodeInfo?: SourceCodeInfo): asserts value is T;
9
9
  export declare function isUnknownRecord(value: unknown): value is Record<string, unknown>;
10
10
  export declare function assertUnknownRecord(value: unknown, sourceCodeInfo?: SourceCodeInfo): asserts value is UnknownRecord;
11
11
  export declare function asUnknownRecord(value: unknown, sourceCodeInfo?: SourceCodeInfo): UnknownRecord;
12
- export declare function canBeOperator(count: Count): boolean;
12
+ export declare function canBeOperator(count: ParamCount): boolean;
@@ -1,6 +1,7 @@
1
1
  import type { Any, Coll } from '../interface';
2
- import type { NativeJsFunction } from '../parser/types';
2
+ import type { FunctionLike, NativeJsFunction, NormalExpressionNodeWithName } from '../parser/types';
3
3
  import type { SourceCodeInfo } from '../tokenizer/token';
4
+ import type { ParamCount } from '../builtin/interface';
4
5
  export declare function collHasKey(coll: unknown, key: string | number): boolean;
5
6
  export declare function compare<T extends string | number>(a: T, b: T, sourceCodeInfo: SourceCodeInfo | undefined): number;
6
7
  export declare function deepEqual(a: unknown, b: unknown, sourceCodeInfo?: SourceCodeInfo): boolean;
@@ -13,3 +14,9 @@ export declare function addToSet<T>(target: Set<T>, source: Set<T>): void;
13
14
  export declare const EPSILON = 1e-10;
14
15
  export declare function approxEqual(a: number, b: number, epsilon?: number): boolean;
15
16
  export declare function approxZero(value: number): boolean;
17
+ export declare function mergeParamCounts(count: ParamCount, node: NormalExpressionNodeWithName): void;
18
+ export declare function assertNumberOfParams(count: ParamCount, node: NormalExpressionNodeWithName): void;
19
+ export declare function paramCountAcceptsMin(paramsCount: ParamCount, nbrOfParams: number): boolean;
20
+ export declare function getCommonParamCount(params: FunctionLike[]): ParamCount | null;
21
+ export declare function getParamCount(param: FunctionLike): ParamCount;
22
+ export declare function paramCountMinus(paramCount: ParamCount, count: number): ParamCount;
@@ -896,6 +896,91 @@ function approxEqual(a, b, epsilon) {
896
896
  function approxZero(value) {
897
897
  return Math.abs(value) < EPSILON;
898
898
  }
899
+ function paramCountAccepts(paramsCount, nbrOfParams) {
900
+ if (typeof paramsCount === 'number') {
901
+ return paramsCount === nbrOfParams;
902
+ }
903
+ var min = paramsCount.min, max = paramsCount.max, even = paramsCount.even, odd = paramsCount.odd;
904
+ if (even && nbrOfParams % 2 !== 0) {
905
+ return false;
906
+ }
907
+ if (odd && nbrOfParams % 2 !== 1) {
908
+ return false;
909
+ }
910
+ if (typeof min === 'number' && nbrOfParams < min) {
911
+ return false;
912
+ }
913
+ if (typeof max === 'number' && nbrOfParams > max) {
914
+ return false;
915
+ }
916
+ return true;
917
+ }
918
+ function paramCountAcceptsMin(paramsCount, nbrOfParams) {
919
+ if (typeof paramsCount === 'number') {
920
+ return nbrOfParams >= paramsCount;
921
+ }
922
+ var min = paramsCount.min;
923
+ if (typeof min === 'number' && nbrOfParams < min) {
924
+ return false;
925
+ }
926
+ return true;
927
+ }
928
+ function getCommonParamCount(params) {
929
+ return params.reduce(function (acc, param) {
930
+ if (acc === null) {
931
+ return null;
932
+ }
933
+ var paramCount = (typeof param === 'number' || isColl(param)) ? 1 : param.paramCount;
934
+ if (typeof acc === 'number' && typeof paramCount === 'number') {
935
+ return acc === paramCount ? acc : null;
936
+ }
937
+ if (typeof paramCount === 'number') {
938
+ if (paramCountAccepts(acc, paramCount)) {
939
+ return paramCount;
940
+ }
941
+ return null;
942
+ }
943
+ if (typeof acc === 'number') {
944
+ if (paramCountAccepts(paramCount, acc)) {
945
+ return acc;
946
+ }
947
+ return null;
948
+ }
949
+ var aMin = paramCount.min, aMax = paramCount.max, aEven = paramCount.even, aOdd = paramCount.odd;
950
+ var bMin = acc.min, bMax = acc.max, bEven = acc.even, bOdd = acc.odd;
951
+ var min = typeof aMin === 'number' && typeof bMin === 'number'
952
+ ? Math.max(aMin, bMin)
953
+ : typeof aMin === 'number' ? aMin : typeof bMin === 'number' ? bMin : undefined;
954
+ var max = typeof aMax === 'number' && typeof bMax === 'number'
955
+ ? Math.min(aMax, bMax)
956
+ : typeof aMax === 'number' ? aMax : typeof bMax === 'number' ? bMax : undefined;
957
+ var even = aEven !== null && aEven !== void 0 ? aEven : bEven;
958
+ var odd = aOdd !== null && aOdd !== void 0 ? aOdd : bOdd;
959
+ if (min !== undefined && max !== undefined && min > max) {
960
+ return null;
961
+ }
962
+ if (even && odd) {
963
+ return null;
964
+ }
965
+ if (odd && min !== undefined && min < 1) {
966
+ return null;
967
+ }
968
+ return { min: min, max: max, even: even, odd: odd };
969
+ }, {});
970
+ }
971
+ function getParamCount(param) {
972
+ return (typeof param === 'number' || isColl(param)) ? 1 : param.paramCount;
973
+ }
974
+ function paramCountMinus(paramCount, count) {
975
+ if (typeof paramCount === 'number') {
976
+ return paramCount - count;
977
+ }
978
+ var min = paramCount.min === undefined ? undefined : paramCount.min - count;
979
+ var max = paramCount.max === undefined ? undefined : paramCount.max - count;
980
+ var even = paramCount.even === undefined ? undefined : count % 2 === 0 ? true : undefined;
981
+ var odd = paramCount.odd === undefined ? undefined : count % 2 === 0 ? true : undefined;
982
+ return { min: min, max: max, even: even, odd: odd };
983
+ }
899
984
 
900
985
  // isArray not needed, use Array.isArary
901
986
  function asArray(value, sourceCodeInfo) {
@@ -4559,11 +4644,13 @@ var functionalNormalExpression = {
4559
4644
  'comp': {
4560
4645
  evaluate: function (params, sourceCodeInfo) {
4561
4646
  var _a;
4647
+ params.forEach(function (param) { return assertFunctionLike(param, sourceCodeInfo); });
4562
4648
  return _a = {},
4563
4649
  _a[FUNCTION_SYMBOL] = true,
4564
4650
  _a.sourceCodeInfo = sourceCodeInfo,
4565
4651
  _a.functionType = 'Comp',
4566
4652
  _a.params = params,
4653
+ _a.paramCount = params.length > 0 ? getParamCount(params.at(-1)) : 1,
4567
4654
  _a;
4568
4655
  },
4569
4656
  paramCount: {},
@@ -4577,6 +4664,7 @@ var functionalNormalExpression = {
4577
4664
  _b.sourceCodeInfo = sourceCodeInfo,
4578
4665
  _b.functionType = 'Constantly',
4579
4666
  _b.value = toAny(value),
4667
+ _b.paramCount = {},
4580
4668
  _b;
4581
4669
  },
4582
4670
  paramCount: 1,
@@ -4584,11 +4672,17 @@ var functionalNormalExpression = {
4584
4672
  'juxt': {
4585
4673
  evaluate: function (params, sourceCodeInfo) {
4586
4674
  var _a;
4675
+ params.forEach(function (param) { return assertFunctionLike(param, sourceCodeInfo); });
4676
+ var paramCount = getCommonParamCount(params);
4677
+ if (paramCount === null) {
4678
+ throw new LitsError('All functions must accept the same number of arguments', sourceCodeInfo);
4679
+ }
4587
4680
  return _a = {},
4588
4681
  _a[FUNCTION_SYMBOL] = true,
4589
4682
  _a.sourceCodeInfo = sourceCodeInfo,
4590
4683
  _a.functionType = 'Juxt',
4591
4684
  _a.params = params,
4685
+ _a.paramCount = paramCount,
4592
4686
  _a;
4593
4687
  },
4594
4688
  paramCount: { min: 1 },
@@ -4597,11 +4691,13 @@ var functionalNormalExpression = {
4597
4691
  evaluate: function (_a, sourceCodeInfo) {
4598
4692
  var _b;
4599
4693
  var _c = __read(_a, 1), fn = _c[0];
4694
+ var fun = asFunctionLike(fn, sourceCodeInfo);
4600
4695
  return _b = {},
4601
4696
  _b[FUNCTION_SYMBOL] = true,
4602
4697
  _b.sourceCodeInfo = sourceCodeInfo,
4603
4698
  _b.functionType = 'Complement',
4604
- _b.function = asFunctionLike(fn, sourceCodeInfo),
4699
+ _b.function = fun,
4700
+ _b.paramCount = getParamCount(fun),
4605
4701
  _b;
4606
4702
  },
4607
4703
  paramCount: 1,
@@ -4614,6 +4710,7 @@ var functionalNormalExpression = {
4614
4710
  _a.sourceCodeInfo = sourceCodeInfo,
4615
4711
  _a.functionType = 'EveryPred',
4616
4712
  _a.params = params,
4713
+ _a.paramCount = 1,
4617
4714
  _a;
4618
4715
  },
4619
4716
  paramCount: { min: 1 },
@@ -4626,6 +4723,7 @@ var functionalNormalExpression = {
4626
4723
  _a.sourceCodeInfo = sourceCodeInfo,
4627
4724
  _a.functionType = 'SomePred',
4628
4725
  _a.params = params,
4726
+ _a.paramCount = 1,
4629
4727
  _a;
4630
4728
  },
4631
4729
  paramCount: { min: 1 },
@@ -4634,12 +4732,14 @@ var functionalNormalExpression = {
4634
4732
  evaluate: function (_a, sourceCodeInfo) {
4635
4733
  var _b;
4636
4734
  var _c = __read(_a), fn = _c[0], params = _c.slice(1);
4735
+ var fun = asFunctionLike(fn, sourceCodeInfo);
4637
4736
  return _b = {},
4638
4737
  _b[FUNCTION_SYMBOL] = true,
4639
4738
  _b.sourceCodeInfo = sourceCodeInfo,
4640
4739
  _b.functionType = 'Fnull',
4641
- _b.function = asFunctionLike(fn, sourceCodeInfo),
4740
+ _b.function = fun,
4642
4741
  _b.params = params,
4742
+ _b.paramCount = getParamCount(fun),
4643
4743
  _b;
4644
4744
  },
4645
4745
  paramCount: { min: 2 },
@@ -11714,12 +11814,16 @@ var functionSpecialExpression = {
11714
11814
  assertUserDefinedSymbolNode(functionSymbol, node[2]);
11715
11815
  assertNameNotDefined(functionSymbol[1], contextStack, builtin, node[2]);
11716
11816
  var evaluatedFunction = evaluateFunction(fn, contextStack, builtin, getUndefinedSymbols, evaluateNode);
11817
+ var min = evaluatedFunction[0].filter(function (arg) { return arg[0] !== bindingTargetTypes.rest && arg[1][1] === undefined; }).length;
11818
+ var max = evaluatedFunction[0].some(function (arg) { return arg[0] === bindingTargetTypes.rest; }) ? undefined : evaluatedFunction[0].length;
11819
+ var paramCount = min === max ? min : { min: min, max: max };
11717
11820
  var litsFunction = (_b = {},
11718
11821
  _b[FUNCTION_SYMBOL] = true,
11719
11822
  _b.sourceCodeInfo = node[2],
11720
11823
  _b.functionType = 'UserDefined',
11721
11824
  _b.name = functionSymbol[1],
11722
11825
  _b.evaluatedfunction = evaluatedFunction,
11826
+ _b.paramCount = paramCount,
11723
11827
  _b);
11724
11828
  contextStack.addValues((_c = {}, _c[functionSymbol[1]] = litsFunction, _c), functionSymbol[2]);
11725
11829
  return litsFunction;
@@ -11741,13 +11845,17 @@ var defnSpecialExpression = {
11741
11845
  var _d = __read(node[1], 3), functionSymbol = _d[1], fn = _d[2];
11742
11846
  assertUserDefinedSymbolNode(functionSymbol, node[2]);
11743
11847
  assertNameNotDefined(functionSymbol[1], contextStack, builtin, node[2]);
11744
- var evaluatedFunctionOverloades = evaluateFunction(fn, contextStack, builtin, getUndefinedSymbols, evaluateNode);
11848
+ var evaluatedFunction = evaluateFunction(fn, contextStack, builtin, getUndefinedSymbols, evaluateNode);
11849
+ var min = evaluatedFunction[0].filter(function (arg) { return arg[0] !== bindingTargetTypes.rest && arg[1][1] === undefined; }).length;
11850
+ var max = evaluatedFunction[0].some(function (arg) { return arg[0] === bindingTargetTypes.rest; }) ? undefined : evaluatedFunction[0].length;
11851
+ var paramCount = min === max ? min : { min: min, max: max };
11745
11852
  var litsFunction = (_b = {},
11746
11853
  _b[FUNCTION_SYMBOL] = true,
11747
11854
  _b.sourceCodeInfo = node[2],
11748
11855
  _b.functionType = 'UserDefined',
11749
11856
  _b.name = functionSymbol[1],
11750
- _b.evaluatedfunction = evaluatedFunctionOverloades,
11857
+ _b.evaluatedfunction = evaluatedFunction,
11858
+ _b.paramCount = paramCount,
11751
11859
  _b);
11752
11860
  contextStack.exportValues((_c = {}, _c[functionSymbol[1]] = litsFunction, _c), functionSymbol[2]);
11753
11861
  return litsFunction;
@@ -11769,12 +11877,16 @@ var fnSpecialExpression = {
11769
11877
  var builtin = _a.builtin, getUndefinedSymbols = _a.getUndefinedSymbols, evaluateNode = _a.evaluateNode;
11770
11878
  var fn = node[1][1];
11771
11879
  var evaluatedFunction = evaluateFunction(fn, contextStack, builtin, getUndefinedSymbols, evaluateNode);
11880
+ var min = evaluatedFunction[0].filter(function (arg) { return arg[0] !== bindingTargetTypes.rest && arg[1][1] === undefined; }).length;
11881
+ var max = evaluatedFunction[0].some(function (arg) { return arg[0] === bindingTargetTypes.rest; }) ? undefined : evaluatedFunction[0].length;
11882
+ var paramCount = min === max ? min : { min: min, max: max };
11772
11883
  var litsFunction = (_b = {},
11773
11884
  _b[FUNCTION_SYMBOL] = true,
11774
11885
  _b.sourceCodeInfo = node[2],
11775
11886
  _b.functionType = 'UserDefined',
11776
11887
  _b.name = undefined,
11777
11888
  _b.evaluatedfunction = evaluatedFunction,
11889
+ _b.paramCount = paramCount,
11778
11890
  _b);
11779
11891
  return litsFunction;
11780
11892
  },
@@ -12390,12 +12502,6 @@ new Set(specialExpressionKeys);
12390
12502
  // TODO, remove
12391
12503
  // console.log('builtin', [...specialExpressionKeys, ...normalExpressionKeys].length)
12392
12504
 
12393
- function checkParams(evaluatedFunction, nbrOfParams, sourceCodeInfo) {
12394
- var minArity = evaluatedFunction[0].filter(function (arg) { return arg[0] !== bindingTargetTypes.rest && arg[1][1] === undefined; }).length;
12395
- if (nbrOfParams < minArity) {
12396
- throw new LitsError("Unexpected number of arguments. Expected at least ".concat(minArity, ", got ").concat(nbrOfParams, "."), sourceCodeInfo);
12397
- }
12398
- }
12399
12505
  var functionExecutors = {
12400
12506
  NativeJsFunction: function (fn, params, sourceCodeInfo) {
12401
12507
  var _a;
@@ -12415,7 +12521,10 @@ var functionExecutors = {
12415
12521
  var evaluateNode = _a.evaluateNode;
12416
12522
  var _loop_1 = function () {
12417
12523
  var e_1, _b;
12418
- checkParams(fn.evaluatedfunction, params.length, sourceCodeInfo);
12524
+ if (!paramCountAcceptsMin(fn.paramCount, params.length)) {
12525
+ throw new LitsError("Expected ".concat(fn.paramCount, " arguments, got ").concat(params.length, "."), sourceCodeInfo);
12526
+ }
12527
+ // checkParams(fn.evaluatedfunction, params.length, sourceCodeInfo)
12419
12528
  var evaluatedFunction = fn.evaluatedfunction;
12420
12529
  var args = evaluatedFunction[0];
12421
12530
  var nbrOfNonRestArgs = args.filter(function (arg) { return arg[0] !== bindingTargetTypes.rest; }).length;
@@ -12712,6 +12821,7 @@ function evaluateNormalExpression(node, contextStack) {
12712
12821
  _a.params = params,
12713
12822
  _a.placeholders = placeholders,
12714
12823
  _a.sourceCodeInfo = sourceCodeInfo,
12824
+ _a.paramCount = paramCountMinus(getParamCount(fn), params.length),
12715
12825
  _a);
12716
12826
  return partialFunction;
12717
12827
  }
@@ -12734,11 +12844,12 @@ function evaluateNormalExpression(node, contextStack) {
12734
12844
  if (placeholders.length > 0) {
12735
12845
  var partialFunction = (_b = {},
12736
12846
  _b[FUNCTION_SYMBOL] = true,
12737
- _b.function = asFunctionLike(fn, sourceCodeInfo),
12847
+ _b.function = fn,
12738
12848
  _b.functionType = 'Partial',
12739
12849
  _b.params = params,
12740
12850
  _b.placeholders = placeholders,
12741
12851
  _b.sourceCodeInfo = sourceCodeInfo,
12852
+ _b.paramCount = paramCountMinus(getParamCount(fn), params.length),
12742
12853
  _b);
12743
12854
  return partialFunction;
12744
12855
  }
@@ -12960,11 +13071,13 @@ var ContextStackImpl = /** @class */ (function () {
12960
13071
  }
12961
13072
  if (isNormalBuiltinSymbolNode(node)) {
12962
13073
  var type = node[1];
13074
+ var normalExpression = allNormalExpressions[type];
12963
13075
  return _b = {},
12964
13076
  _b[FUNCTION_SYMBOL] = true,
12965
13077
  _b.functionType = 'Builtin',
12966
13078
  _b.normalBuitinSymbolType = type,
12967
13079
  _b.sourceCodeInfo = node[2],
13080
+ _b.paramCount = normalExpression.paramCount,
12968
13081
  _b;
12969
13082
  }
12970
13083
  var lookUpResult = this.lookUp(node);
@@ -13001,6 +13114,7 @@ function createContextStack(params) {
13001
13114
  name: name
13002
13115
  },
13003
13116
  _b[FUNCTION_SYMBOL] = true,
13117
+ _b.paramCount = {},
13004
13118
  _b);
13005
13119
  return acc;
13006
13120
  }, {}),