affinirum 1.4.2 → 1.5.1

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.
Files changed (89) hide show
  1. package/README.md +226 -154
  2. package/dst/Affinirum.js +24 -15
  3. package/dst/Atom.d.ts +3 -1
  4. package/dst/Constant.js +1 -1
  5. package/dst/Functions.js +7 -4
  6. package/dst/Keywords.js +4 -4
  7. package/dst/ParserState.d.ts +2 -1
  8. package/dst/ParserState.js +43 -82
  9. package/dst/Type.d.ts +11 -7
  10. package/dst/Type.js +113 -38
  11. package/dst/Value.d.ts +1 -0
  12. package/dst/Value.js +19 -1
  13. package/dst/atom/ArrayAtom.d.ts +7 -3
  14. package/dst/atom/ArrayAtom.js +53 -8
  15. package/dst/atom/FunctionAtom.d.ts +4 -1
  16. package/dst/atom/FunctionAtom.js +65 -15
  17. package/dst/atom/ObjectAtom.d.ts +7 -5
  18. package/dst/atom/ObjectAtom.js +35 -13
  19. package/dst/atom/PrimitiveAtom.d.ts +3 -1
  20. package/dst/atom/PrimitiveAtom.js +10 -1
  21. package/dst/cjs/Affinirum.js +24 -15
  22. package/dst/cjs/Constant.js +1 -1
  23. package/dst/cjs/Functions.js +6 -3
  24. package/dst/cjs/Keywords.js +4 -4
  25. package/dst/cjs/ParserState.js +42 -81
  26. package/dst/cjs/Type.js +113 -38
  27. package/dst/cjs/Value.js +21 -0
  28. package/dst/cjs/atom/ArrayAtom.js +53 -8
  29. package/dst/cjs/atom/FunctionAtom.js +65 -15
  30. package/dst/cjs/atom/ObjectAtom.js +35 -13
  31. package/dst/cjs/atom/PrimitiveAtom.js +10 -1
  32. package/dst/cjs/native/Aggregable.js +3 -2
  33. package/dst/cjs/native/Array.js +10 -8
  34. package/dst/cjs/native/Boolean.js +8 -5
  35. package/dst/cjs/native/Buffer.js +32 -3
  36. package/dst/cjs/native/Enumerable.js +7 -4
  37. package/dst/cjs/native/Float.js +26 -13
  38. package/dst/cjs/native/Integer.js +64 -28
  39. package/dst/cjs/native/Iterable.js +8 -5
  40. package/dst/cjs/native/Number.js +62 -33
  41. package/dst/cjs/native/Object.js +2 -1
  42. package/dst/cjs/native/String.js +71 -24
  43. package/dst/cjs/native/Timestamp.js +76 -18
  44. package/dst/cjs/native/Unknown.js +1 -1
  45. package/dst/cjs/native/notation/AN.js +152 -11
  46. package/dst/cjs/native/notation/IP.js +4 -3
  47. package/dst/cjs/native/notation/JSON.js +1 -1
  48. package/dst/cjs/node/ArrayNode.js +6 -6
  49. package/dst/cjs/node/BlockNode.js +5 -4
  50. package/dst/cjs/node/CallNode.js +28 -22
  51. package/dst/cjs/node/CastNode.js +42 -0
  52. package/dst/cjs/node/ObjectNode.js +2 -4
  53. package/dst/cjs/node/SwitchNode.js +10 -10
  54. package/dst/cjs/node/VariableNode.js +3 -1
  55. package/dst/native/Aggregable.js +3 -2
  56. package/dst/native/Array.js +10 -8
  57. package/dst/native/Boolean.d.ts +1 -0
  58. package/dst/native/Boolean.js +7 -4
  59. package/dst/native/Buffer.d.ts +2 -0
  60. package/dst/native/Buffer.js +29 -2
  61. package/dst/native/Enumerable.js +7 -4
  62. package/dst/native/Float.d.ts +2 -0
  63. package/dst/native/Float.js +25 -12
  64. package/dst/native/Integer.d.ts +5 -1
  65. package/dst/native/Integer.js +61 -27
  66. package/dst/native/Iterable.js +8 -5
  67. package/dst/native/Number.d.ts +4 -3
  68. package/dst/native/Number.js +57 -32
  69. package/dst/native/Object.js +2 -1
  70. package/dst/native/String.d.ts +2 -7
  71. package/dst/native/String.js +68 -16
  72. package/dst/native/Timestamp.d.ts +4 -2
  73. package/dst/native/Timestamp.js +73 -17
  74. package/dst/native/Unknown.js +1 -1
  75. package/dst/native/notation/AN.d.ts +1 -0
  76. package/dst/native/notation/AN.js +152 -12
  77. package/dst/native/notation/IP.js +4 -3
  78. package/dst/native/notation/JSON.js +1 -1
  79. package/dst/node/ArrayNode.d.ts +1 -1
  80. package/dst/node/ArrayNode.js +6 -6
  81. package/dst/node/BlockNode.js +5 -4
  82. package/dst/node/CallNode.js +28 -22
  83. package/dst/node/CastNode.d.ts +15 -0
  84. package/dst/node/CastNode.js +38 -0
  85. package/dst/node/ObjectNode.d.ts +1 -1
  86. package/dst/node/ObjectNode.js +2 -4
  87. package/dst/node/SwitchNode.js +10 -10
  88. package/dst/node/VariableNode.js +3 -1
  89. package/package.json +5 -4
@@ -4,10 +4,11 @@ exports.ParserState = void 0;
4
4
  const Boolean_js_1 = require("./native/Boolean.js");
5
5
  const Buffer_js_1 = require("./native/Buffer.js");
6
6
  const Aggregable_js_1 = require("./native/Aggregable.js");
7
- const String_js_1 = require("./native/String.js");
8
- const Constant_js_1 = require("./Constant.js");
9
7
  const Number_js_1 = require("./native/Number.js");
10
8
  const Unknown_js_1 = require("./native/Unknown.js");
9
+ const String_js_1 = require("./native/String.js");
10
+ const Timestamp_js_1 = require("./native/Timestamp.js");
11
+ const Constant_js_1 = require("./Constant.js");
11
12
  const Type_js_1 = require("./Type.js");
12
13
  const ParserFrame_js_1 = require("./ParserFrame.js");
13
14
  class Literal {
@@ -42,10 +43,11 @@ const idBracesClose = Symbol();
42
43
  const idSemicolon = Symbol();
43
44
  const idColon = Symbol();
44
45
  const idComma = Symbol();
46
+ const idTilda = Symbol();
45
47
  const idDot = Symbol();
46
48
  const idQuestion = Symbol();
47
49
  const idEllipsis = Symbol();
48
- const idTilda = Symbol();
50
+ const idCast = Symbol();
49
51
  const idVar = Symbol();
50
52
  const idVal = Symbol();
51
53
  const idIf = Symbol();
@@ -117,6 +119,9 @@ class ParserState extends ParserFrame_js_1.ParserFrame {
117
119
  get isComma() {
118
120
  return this._fragment === idComma;
119
121
  }
122
+ get isTilda() {
123
+ return this._fragment === idTilda;
124
+ }
120
125
  get isDot() {
121
126
  return this._fragment === idDot;
122
127
  }
@@ -126,8 +131,8 @@ class ParserState extends ParserFrame_js_1.ParserFrame {
126
131
  get isEllipsis() {
127
132
  return this._fragment === idEllipsis;
128
133
  }
129
- get isTilda() {
130
- return this._fragment === idTilda;
134
+ get isCast() {
135
+ return this._fragment === idCast;
131
136
  }
132
137
  get isVar() {
133
138
  return this._fragment === idVar;
@@ -243,7 +248,15 @@ class ParserState extends ParserFrame_js_1.ParserFrame {
243
248
  this._fragment = idSemicolon;
244
249
  break;
245
250
  case ":":
246
- this._fragment = idColon;
251
+ switch (this._expr.charAt(this._end)) {
252
+ case ":":
253
+ ++this._end;
254
+ this._fragment = idCast;
255
+ break;
256
+ default:
257
+ this._fragment = idColon;
258
+ break;
259
+ }
247
260
  break;
248
261
  case ",":
249
262
  this._fragment = idComma;
@@ -440,53 +453,25 @@ class ParserState extends ParserFrame_js_1.ParserFrame {
440
453
  break;
441
454
  }
442
455
  break;
443
- case "@":
444
- while ((0, String_js_1.isDateSymbol)(this._expr.charAt(this._end))) {
445
- ++this._end;
446
- }
447
- if ((0, String_js_1.isDateTimeSeparatorSymbol)(this._expr.charAt(this._end))) {
448
- ++this._end;
449
- while ((0, String_js_1.isTimeSymbol)(this._expr.charAt(this._end))) {
450
- ++this._end;
451
- }
452
- if (this._expr.charAt(this._end) === "."
453
- && (0, String_js_1.isNumericSymbol)(this._expr.charAt(this._end + 1))) {
454
- ++this._end;
455
- while ((0, String_js_1.isNumericSymbol)(this._expr.charAt(this._end))) {
456
- ++this._end;
457
- }
458
- if (this._expr.charAt(this._end) === "Z") {
459
- ++this._end;
460
- }
461
- }
462
- }
463
- this._fragment = new Literal(new Date(this._expr.substring(this._start + 1, this._end)));
456
+ case "@": {
457
+ const [ts, tsEnd] = (0, Timestamp_js_1.parseLiteralTimestamp)(this._expr, this._start);
458
+ this._fragment = new Literal(ts);
459
+ this._end = tsEnd;
464
460
  break;
465
- case "`":
466
- while (this._expr.charAt(this._end) !== ""
467
- && this._expr.charAt(this._end) !== c) {
468
- ++this._end;
469
- }
470
- if (this._end >= this._expr.length) {
471
- this._start = this._expr.length;
472
- throw new Error(`missing closing buffer quotation mark ${c}`);
473
- }
474
- this._fragment = new Literal((0, Buffer_js_1.parseBuffer)((0, String_js_1.replaceWith)(this._expr.substring(this._start + 1, this._end), "", " ", "\t", "\n")));
475
- ++this._end;
461
+ }
462
+ case "`": {
463
+ const [buf, bufEnd] = (0, Buffer_js_1.parseLiteralBuffer)(this._expr, this._start);
464
+ this._fragment = new Literal(buf);
465
+ this._end = bufEnd;
476
466
  break;
467
+ }
477
468
  case "\"":
478
- case "'":
479
- while (this._expr.charAt(this._end) !== ""
480
- && this._expr.charAt(this._end) !== c) {
481
- ++this._end;
482
- }
483
- if (this._end >= this._expr.length) {
484
- this._start = this._expr.length;
485
- throw new Error(`missing closing string quotation mark ${c}`);
486
- }
487
- this._fragment = new Literal((0, String_js_1.replaceWith)(this._expr.substring(this._start + 1, this._end), "", "\\\n"));
488
- ++this._end;
469
+ case "'": {
470
+ const [str, strEnd] = (0, String_js_1.parseLiteralString)(this._expr, this._start);
471
+ this._fragment = new Literal(str);
472
+ this._end = strEnd;
489
473
  break;
474
+ }
490
475
  default:
491
476
  if ((0, String_js_1.isTokenStartSymbol)(c)) {
492
477
  while ((0, String_js_1.isTokenSymbol)(this._expr.charAt(this._end))) {
@@ -506,15 +491,19 @@ class ParserState extends ParserFrame_js_1.ParserFrame {
506
491
  case "void":
507
492
  this._fragment = Type_js_1.Type.Void;
508
493
  break;
494
+ case "bool":
509
495
  case "boolean":
510
496
  this._fragment = Type_js_1.Type.Boolean;
511
497
  break;
498
+ case "time":
512
499
  case "timestamp":
513
500
  this._fragment = Type_js_1.Type.Timestamp;
514
501
  break;
502
+ case "f64":
515
503
  case "float":
516
504
  this._fragment = Type_js_1.Type.Float;
517
505
  break;
506
+ case "i64":
518
507
  case "integer":
519
508
  this._fragment = Type_js_1.Type.Integer;
520
509
  break;
@@ -559,38 +548,10 @@ class ParserState extends ParserFrame_js_1.ParserFrame {
559
548
  break;
560
549
  }
561
550
  }
562
- else if ((0, String_js_1.isNumericSymbol)(c)) {
563
- let integer = true;
564
- while ((0, String_js_1.isNumericSymbol)(this._expr.charAt(this._end))) {
565
- ++this._end;
566
- }
567
- if (this._expr.charAt(this._end) === ".") {
568
- ++this._end;
569
- if ((0, String_js_1.isNumericSymbol)(this._expr.charAt(this._end))) {
570
- ++this._end;
571
- while ((0, String_js_1.isNumericSymbol)(this._expr.charAt(this._end))) {
572
- ++this._end;
573
- }
574
- integer = false;
575
- }
576
- else {
577
- --this._end;
578
- }
579
- }
580
- if (this._expr.charAt(this._end) === "e") {
581
- ++this._end;
582
- if ((0, String_js_1.isNumericSymbol)(this._expr.charAt(this._end))
583
- || (0, String_js_1.isSignSymbol)(this._expr.charAt(this._end))) {
584
- ++this._end;
585
- while ((0, String_js_1.isNumericSymbol)(this._expr.charAt(this._end))) {
586
- ++this._end;
587
- }
588
- }
589
- integer = false;
590
- }
591
- this._fragment = integer
592
- ? new Literal(BigInt(this._expr.substring(this._start, this._end)))
593
- : new Literal(parseFloat(this._expr.substring(this._start, this._end)));
551
+ else if ((0, Number_js_1.isNumericSymbol)(c)) {
552
+ const [num, numEnd] = (0, Number_js_1.parseLiteralNumber)(this._expr, this._start);
553
+ this._fragment = new Literal(num);
554
+ this._end = numEnd;
594
555
  }
595
556
  else {
596
557
  throw new Error(`unknown symbol ${c}`);
package/dst/cjs/Type.js CHANGED
@@ -17,54 +17,92 @@ class Type {
17
17
  return this._atoms.length === 1;
18
18
  }
19
19
  get isOptional() {
20
- return this.isUnknown || this._atoms.length > 1 && this._atoms.some((i) => i.match(Type.Void._atoms[0]));
20
+ return this.isUnknown || this._atoms.length > 1 && this._atoms.some((a) => a.equate(Type.Void._atoms[0]));
21
21
  }
22
22
  get isVoid() {
23
- return this.isAtom && this._atoms[0].match(Type.Void._atoms[0]);
23
+ return this.isAtom && this._atoms[0].equate(Type.Void._atoms[0]);
24
24
  }
25
25
  get isBoolean() {
26
- return this.isAtom && this._atoms[0].match(Type.Boolean._atoms[0]);
26
+ return this.isAtom && this._atoms[0].equate(Type.Boolean._atoms[0]);
27
27
  }
28
28
  get isTimestamp() {
29
- return this.isAtom && this._atoms[0].match(Type.Timestamp._atoms[0]);
29
+ return this.isAtom && this._atoms[0].equate(Type.Timestamp._atoms[0]);
30
30
  }
31
31
  get isFloat() {
32
- return this.isAtom && this._atoms[0].match(Type.Float._atoms[0]);
32
+ return this.isAtom && this._atoms[0].equate(Type.Float._atoms[0]);
33
33
  }
34
34
  get isInteger() {
35
- return this.isAtom && this._atoms[0].match(Type.Integer._atoms[0]);
35
+ return this.isAtom && this._atoms[0].equate(Type.Integer._atoms[0]);
36
36
  }
37
37
  get isBuffer() {
38
- return this.isAtom && this._atoms[0].match(Type.Buffer._atoms[0]);
38
+ return this.isAtom && this._atoms[0].equate(Type.Buffer._atoms[0]);
39
39
  }
40
40
  get isString() {
41
- return this.isAtom && this._atoms[0].match(Type.String._atoms[0]);
41
+ return this.isAtom && this._atoms[0].equate(Type.String._atoms[0]);
42
42
  }
43
43
  get isArray() {
44
- return this._atoms.every((i) => i instanceof ArrayAtom_js_1.ArrayAtom);
44
+ return this._atoms.every((a) => a instanceof ArrayAtom_js_1.ArrayAtom);
45
45
  }
46
46
  get isObject() {
47
- return this._atoms.every((i) => i instanceof ObjectAtom_js_1.ObjectAtom);
47
+ return this._atoms.every((a) => a instanceof ObjectAtom_js_1.ObjectAtom);
48
48
  }
49
49
  get isFunction() {
50
- return this._atoms.every((i) => i instanceof FunctionAtom_js_1.FunctionAtom);
50
+ return this._atoms.every((a) => a instanceof FunctionAtom_js_1.FunctionAtom);
51
51
  }
52
- get isNumeric() {
53
- return this._atoms.length === 2
54
- && (this._atoms[0].match(Type.Float._atoms[0]) || this._atoms[0].match(Type.Integer._atoms[0]));
55
- }
56
- toOptional() {
57
- return this._atoms.length ? Type.union(Type.Void, this) : this;
52
+ equate(type) {
53
+ if (this._atoms.length !== type?._atoms.length) {
54
+ return false;
55
+ }
56
+ const unmatched = [...type._atoms];
57
+ for (const atom of this._atoms) {
58
+ const index = unmatched.findIndex((a) => atom.equate(a));
59
+ if (index < 0) {
60
+ return false;
61
+ }
62
+ unmatched.splice(index, 1);
63
+ }
64
+ return true;
58
65
  }
59
- functionAtomRetType(type) {
60
- const atoms = this._atoms.filter((i) => i instanceof FunctionAtom_js_1.FunctionAtom && i.retType.match(type));
61
- return Type.union(...atoms.map((i) => i.retType));
66
+ accept(type) {
67
+ if (type instanceof Type) {
68
+ if (this.isUnknown || type.isUnknown) {
69
+ return true;
70
+ }
71
+ return type._atoms.every((a) => this._atoms.some((atom) => atom.accept(a)));
72
+ }
73
+ return this._atoms.some((a) => a.accept(type));
62
74
  }
63
- functionAtoms(type, argc) {
75
+ acceptValue(value) {
64
76
  if (this.isUnknown) {
65
- return Type.Function._atoms;
77
+ return true;
66
78
  }
67
- return this._atoms.filter((i) => i instanceof FunctionAtom_js_1.FunctionAtom && i.minArity <= argc && i.maxArity >= argc && (type.isVoid || i.retType.match(type)));
79
+ const valueAtom = Type.of(value)._atoms[0];
80
+ return this._atoms.some((atom) => {
81
+ if (!atom.accept(valueAtom)) {
82
+ return false;
83
+ }
84
+ if (atom instanceof ArrayAtom_js_1.ArrayAtom) {
85
+ const values = value;
86
+ const types = atom.valTypes();
87
+ if (!types.length) {
88
+ return true;
89
+ }
90
+ if (atom.isTuple) {
91
+ return types.every((type, ix) => type.acceptValue(Object.hasOwn(values, ix) ? values[ix] : undefined));
92
+ }
93
+ for (let i = 0; i < values.length; ++i) {
94
+ if (!types[0].acceptValue(Object.hasOwn(values, i) ? values[i] : undefined)) {
95
+ return false;
96
+ }
97
+ }
98
+ return true;
99
+ }
100
+ if (atom instanceof ObjectAtom_js_1.ObjectAtom) {
101
+ const values = value;
102
+ return atom.valTypes().every(([key, type]) => type.acceptValue(Object.hasOwn(values, key) ? values[key] : undefined));
103
+ }
104
+ return true;
105
+ });
68
106
  }
69
107
  reduce(mask) {
70
108
  if (mask.isUnknown || mask.isVoid) {
@@ -73,37 +111,74 @@ class Type {
73
111
  if (this.isUnknown) {
74
112
  return mask;
75
113
  }
76
- const list = this._atoms.filter((i) => mask.match(i));
114
+ const list = this._atoms.filter((a) => mask.accept(a));
77
115
  return list.length === 0
78
116
  ? undefined
79
117
  : list.length === this._atoms.length
80
118
  ? this
81
119
  : new Type(list);
82
120
  }
83
- match(type) {
84
- if (type instanceof Type) {
85
- return this.isUnknown || type.isUnknown || type.isVoid || this._atoms.some((i) => type._atoms.some((j) => i.match(j)));
86
- }
87
- else {
88
- return this._atoms.some((i) => i.match(type));
121
+ intersect(type) {
122
+ if (this.isUnknown || type.isUnknown) {
123
+ return true;
89
124
  }
125
+ return this._atoms.some((a) => type._atoms.some((atom) => a.intersect(atom)));
90
126
  }
91
127
  weight() {
92
- return this._atoms.reduce((acc, i) => acc + i.weight(), 0);
128
+ return this._atoms.reduce((acc, a) => acc + a.weight(), 0);
129
+ }
130
+ arrayAtomValType(index) {
131
+ const atoms = this._atoms.filter((a) => a instanceof ArrayAtom_js_1.ArrayAtom);
132
+ if (!atoms.length) {
133
+ return Type.Unknown;
134
+ }
135
+ const valTypes = [];
136
+ for (const atom of atoms) {
137
+ const valType = atom.isTuple
138
+ ? atom.valType(index)
139
+ : atom.valType(0);
140
+ if (!valType) {
141
+ return Type.Unknown;
142
+ }
143
+ valTypes.push(valType);
144
+ }
145
+ return Type.union(...valTypes);
146
+ }
147
+ functionAtomRetType(type) {
148
+ const atoms = this._atoms.filter((a) => a instanceof FunctionAtom_js_1.FunctionAtom && type.accept(a.retType));
149
+ return Type.union(...atoms.map((a) => a.retType));
150
+ }
151
+ functionAtoms(type, argc) {
152
+ if (this.isUnknown) {
153
+ return Type.Function._atoms;
154
+ }
155
+ return this._atoms.filter((a) => a instanceof FunctionAtom_js_1.FunctionAtom && a.minArity <= argc && a.maxArity >= argc && (type.isVoid || type.accept(a.retType)));
156
+ }
157
+ toOptional() {
158
+ return this._atoms.length ? Type.union(Type.Void, this) : this;
159
+ }
160
+ toMandatory() {
161
+ if (!this.isOptional || this.isUnknown) {
162
+ return this;
163
+ }
164
+ return new Type(this._atoms.filter((a) => !a.equate(Type.Void._atoms[0])));
93
165
  }
94
166
  toString() {
95
167
  return this._atoms.length
96
- ? this._atoms.sort((a, b) => a.weight() - b.weight()).map((i) => i.toString()).join("|")
168
+ ? [...this._atoms]
169
+ .sort((a, b) => a.weight() - b.weight())
170
+ .map((a) => a.toString())
171
+ .join("|")
97
172
  : "??";
98
173
  }
99
174
  static union(...types) {
100
- if (types.some((i) => i.isUnknown)) {
175
+ if (types.some((t) => t.isUnknown)) {
101
176
  return Type.Unknown;
102
177
  }
103
178
  const type = new Type([]);
104
- types.map((i) => i._atoms).flat().forEach((i) => {
105
- if (!type.match(i)) {
106
- type._atoms.push(i);
179
+ types.flatMap((t) => t._atoms).forEach((a) => {
180
+ if (!type._atoms.some((atom) => atom.equate(a))) {
181
+ type._atoms.push(a);
107
182
  }
108
183
  });
109
184
  return type;
@@ -129,7 +204,7 @@ class Type {
129
204
  ? Type.Object
130
205
  : Type.Function;
131
206
  }
132
- static isPrimitiveType(value) {
207
+ static isPrimitive(value) {
133
208
  return value == null
134
209
  || typeof value === "boolean"
135
210
  || value instanceof Date
@@ -144,7 +219,7 @@ class Type {
144
219
  static arrayType(itemTypes = []) {
145
220
  return new Type([new ArrayAtom_js_1.ArrayAtom(itemTypes)]);
146
221
  }
147
- static objectType(propTypes = Object.create(null)) {
222
+ static objectType(propTypes = []) {
148
223
  return new Type([new ObjectAtom_js_1.ObjectAtom(propTypes)]);
149
224
  }
150
225
  static functionType(retType = Type.Unknown, argTypes = [], isVariadic = false) {
package/dst/cjs/Value.js CHANGED
@@ -1,2 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.normalize = void 0;
4
+ const normalizeValue = (value, objects) => {
5
+ if (typeof value === "bigint") {
6
+ return BigInt.asIntN(64, value);
7
+ }
8
+ if (value == null || typeof value !== "object" || value instanceof Date || value instanceof ArrayBuffer || objects.has(value)) {
9
+ return value;
10
+ }
11
+ objects.add(value);
12
+ if (Array.isArray(value)) {
13
+ value.forEach((i, ix) => value[ix] = normalizeValue(i, objects));
14
+ }
15
+ else {
16
+ for (const key of Object.keys(value)) {
17
+ value[key] = normalizeValue(value[key], objects);
18
+ }
19
+ }
20
+ return value;
21
+ };
22
+ const normalize = (value) => normalizeValue(value, new WeakSet());
23
+ exports.normalize = normalize;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ArrayAtom = void 0;
4
+ const Type_js_1 = require("../Type.js");
4
5
  class ArrayAtom {
5
6
  _valTypes;
6
7
  constructor(_valTypes = []) {
@@ -9,29 +10,73 @@ class ArrayAtom {
9
10
  get _undefined() {
10
11
  return this._valTypes.length === 0;
11
12
  }
12
- subtypes() {
13
+ _intersectLength(length) {
14
+ return !this.isTuple || this._valTypes.slice(length).every((type) => type.isOptional);
15
+ }
16
+ get isTuple() {
17
+ return this._valTypes.length > 1;
18
+ }
19
+ valTypes() {
13
20
  return this._valTypes;
14
21
  }
15
- valueType(index) {
22
+ valType(index) {
16
23
  return this._valTypes[index];
17
24
  }
18
- match(atom) {
25
+ equate(atom) {
26
+ return atom instanceof ArrayAtom
27
+ && this._valTypes.length === atom._valTypes.length
28
+ && this._valTypes.every((type, index) => type.equate(atom._valTypes[index]));
29
+ }
30
+ accept(atom) {
19
31
  if (atom instanceof ArrayAtom) {
20
32
  if (this._undefined || atom._undefined) {
21
33
  return true;
22
34
  }
23
- for (let i = 0; i < this._valTypes.length; ++i) {
24
- const vtype = atom.valueType(i);
25
- if (!vtype || !this._valTypes[i].match(vtype)) {
26
- return false;
35
+ if (this.isTuple) {
36
+ for (let i = 0; i < this._valTypes.length; ++i) {
37
+ const vtype = atom.isTuple
38
+ ? atom.valType(i) ?? Type_js_1.Type.Void
39
+ : atom.valType(0);
40
+ if (!vtype || !this._valTypes[i].accept(vtype)) {
41
+ return false;
42
+ }
43
+ }
44
+ }
45
+ else {
46
+ for (let i = 0; i < atom.valTypes().length; ++i) {
47
+ const vtype = atom.valType(i);
48
+ if (!vtype || !this._valTypes[0].accept(vtype)) {
49
+ return false;
50
+ }
27
51
  }
28
52
  }
29
53
  return true;
30
54
  }
31
55
  return false;
32
56
  }
57
+ intersect(atom) {
58
+ if (!(atom instanceof ArrayAtom)) {
59
+ return false;
60
+ }
61
+ if (this._undefined || atom._undefined) {
62
+ return true;
63
+ }
64
+ const length = this.isTuple
65
+ ? atom.isTuple ? Math.min(this._valTypes.length, atom._valTypes.length) : this._valTypes.length
66
+ : atom.isTuple ? atom._valTypes.length : 1;
67
+ for (let i = 0; i < length; ++i) {
68
+ const type = this.isTuple ? this._valTypes[i] : this._valTypes[0];
69
+ const atype = atom.isTuple ? atom._valTypes[i] : atom._valTypes[0];
70
+ if (!type.intersect(atype)) {
71
+ return (this.isTuple || atom.isTuple)
72
+ && this._intersectLength(i)
73
+ && atom._intersectLength(i);
74
+ }
75
+ }
76
+ return true;
77
+ }
33
78
  weight() {
34
- return 0x10000 + this.subtypes().reduce((acc, i) => acc + i.weight(), 0);
79
+ return 0x10000 + this.valTypes().reduce((acc, i) => acc + i.weight(), 0);
35
80
  }
36
81
  toString() {
37
82
  if (this._undefined) {
@@ -46,27 +46,77 @@ class FunctionAtom {
46
46
  argType(index) {
47
47
  return this._argTypes[index] ?? (this._isVariadic ? this._argTypes[this._argTypes.length - 1] : undefined);
48
48
  }
49
- match(atom) {
50
- if (atom instanceof FunctionAtom) {
51
- if (this._undefined || atom._undefined) {
52
- return true;
53
- }
54
- if (!this._retType.match(atom._retType) || this.minArity > atom.maxArity || this.maxArity < atom.minArity) {
49
+ paramType(index) {
50
+ if (!this._isVariadic) {
51
+ return this.argType(index);
52
+ }
53
+ if (!this._argTypes.length) {
54
+ return Type_js_1.Type.Unknown;
55
+ }
56
+ const variadicIndex = this._argTypes.length - 1;
57
+ if (index < variadicIndex) {
58
+ return this.argType(index);
59
+ }
60
+ return this.argType(variadicIndex)
61
+ ?.arrayAtomValType(index - variadicIndex);
62
+ }
63
+ equate(atom) {
64
+ return atom instanceof FunctionAtom
65
+ && this._isVariadic === atom._isVariadic
66
+ && this._argTypes.length === atom._argTypes.length
67
+ && this._retType.equate(atom._retType)
68
+ && this._argTypes.every((type, index) => type.equate(atom._argTypes[index]));
69
+ }
70
+ accept(atom) {
71
+ if (!(atom instanceof FunctionAtom)) {
72
+ return false;
73
+ }
74
+ if (this._undefined || atom._undefined) {
75
+ return true;
76
+ }
77
+ if (this._isVariadic !== atom._isVariadic
78
+ || this._isVariadic && this.arity !== atom.arity
79
+ || !this._retType.accept(atom._retType)
80
+ || this.minArity > atom.maxArity
81
+ || this.maxArity < atom.minArity) {
82
+ return false;
83
+ }
84
+ const argc = Math.min(this.arity, atom.arity);
85
+ for (let i = 0; i < argc; ++i) {
86
+ const type = this.argType(i)?.toMandatory();
87
+ const atype = atom.argType(i);
88
+ if (type && (!atype || !atype.accept(type))) {
55
89
  return false;
56
90
  }
57
- const argc = Math.max(this.minArity, atom.minArity);
58
- for (let i = 0; i < argc; ++i) {
59
- const atype = atom.argType(i);
60
- if (!atype || !this.argType(i)?.match(atype)) {
61
- return false;
62
- }
63
- }
91
+ }
92
+ return true;
93
+ }
94
+ intersect(atom) {
95
+ if (!(atom instanceof FunctionAtom)) {
96
+ return false;
97
+ }
98
+ if (this._undefined || atom._undefined) {
64
99
  return true;
65
100
  }
66
- return false;
101
+ if (this._isVariadic !== atom._isVariadic
102
+ || this._isVariadic && this.arity !== atom.arity
103
+ || !this._retType.intersect(atom._retType)
104
+ || this.minArity > atom.maxArity
105
+ || this.maxArity < atom.minArity) {
106
+ return false;
107
+ }
108
+ const argc = Math.max(this.minArity, atom.minArity);
109
+ for (let i = 0; i < argc; ++i) {
110
+ const type = this.argType(i);
111
+ const atype = atom.argType(i);
112
+ if (!type || !atype || !type.intersect(atype)) {
113
+ return false;
114
+ }
115
+ }
116
+ return true;
67
117
  }
68
118
  weight() {
69
- return 0x1000000000000 + (this._retType ? [this._retType, ...this._argTypes] : []).reduce((acc, i) => acc + i.weight(), 0);
119
+ return 0x1000000000000 + this._argTypes.reduce((acc, i) => acc + i.weight(), this._retType.weight());
70
120
  }
71
121
  toString() {
72
122
  if (this._undefined) {