affinirum 1.4.2 → 1.5.0

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 (75) hide show
  1. package/README.md +214 -151
  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 +22 -5
  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 +50 -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 +22 -5
  26. package/dst/cjs/Type.js +113 -38
  27. package/dst/cjs/Value.js +21 -0
  28. package/dst/cjs/atom/ArrayAtom.js +50 -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 +2 -1
  33. package/dst/cjs/native/Array.js +10 -8
  34. package/dst/cjs/native/Boolean.js +5 -4
  35. package/dst/cjs/native/Enumerable.js +5 -4
  36. package/dst/cjs/native/Float.js +9 -8
  37. package/dst/cjs/native/Integer.js +23 -15
  38. package/dst/cjs/native/Iterable.js +2 -1
  39. package/dst/cjs/native/Number.js +18 -33
  40. package/dst/cjs/native/Object.js +2 -1
  41. package/dst/cjs/native/String.js +3 -2
  42. package/dst/cjs/native/Timestamp.js +2 -1
  43. package/dst/cjs/native/notation/IP.js +3 -2
  44. package/dst/cjs/node/ArrayNode.js +6 -6
  45. package/dst/cjs/node/CallNode.js +27 -23
  46. package/dst/cjs/node/CastNode.js +42 -0
  47. package/dst/cjs/node/ObjectNode.js +2 -4
  48. package/dst/cjs/node/SwitchNode.js +3 -1
  49. package/dst/cjs/node/VariableNode.js +3 -1
  50. package/dst/native/Aggregable.js +2 -1
  51. package/dst/native/Array.js +10 -8
  52. package/dst/native/Boolean.d.ts +1 -0
  53. package/dst/native/Boolean.js +4 -3
  54. package/dst/native/Enumerable.js +5 -4
  55. package/dst/native/Float.d.ts +2 -0
  56. package/dst/native/Float.js +8 -7
  57. package/dst/native/Integer.d.ts +5 -1
  58. package/dst/native/Integer.js +20 -14
  59. package/dst/native/Iterable.js +2 -1
  60. package/dst/native/Number.d.ts +0 -3
  61. package/dst/native/Number.js +17 -32
  62. package/dst/native/Object.js +2 -1
  63. package/dst/native/String.js +3 -2
  64. package/dst/native/Timestamp.js +2 -1
  65. package/dst/native/notation/IP.js +3 -2
  66. package/dst/node/ArrayNode.d.ts +1 -1
  67. package/dst/node/ArrayNode.js +6 -6
  68. package/dst/node/CallNode.js +27 -23
  69. package/dst/node/CastNode.d.ts +15 -0
  70. package/dst/node/CastNode.js +38 -0
  71. package/dst/node/ObjectNode.d.ts +1 -1
  72. package/dst/node/ObjectNode.js +2 -4
  73. package/dst/node/SwitchNode.js +3 -1
  74. package/dst/node/VariableNode.js +3 -1
  75. package/package.json +5 -4
package/dst/Functions.js CHANGED
@@ -1,9 +1,12 @@
1
1
  import { funcAdd } from "./native/Aggregable.js";
2
2
  import { funcFirst, funcLast, funcFirstIndex, funcLastIndex, funcEvery, funcAny, funcFlatten, funcReverse, funcDerive, funcFilter, funcReduce, funcCompose, funcPrepend, funcAppend } from "./native/Array.js";
3
+ import { funcBoolean } from "./native/Boolean.js";
3
4
  import { funcByte } from "./native/Buffer.js";
4
5
  import { funcSlice, funcSplice, funcInject } from "./native/Enumerable.js";
6
+ import { funcFloat } from "./native/Float.js";
7
+ import { funcInteger } from "./native/Integer.js";
5
8
  import { funcLength, funcContains, funcAt, funcHas } from "./native/Iterable.js";
6
- import { funcGreaterThan, funcLessThan, funcGreaterOrEqual, funcLessOrEqual, funcSubtract, funcMultiply, funcDivide, funcRemainder, funcModulo, funcPower, funcRoot, funcNegate, funcCast, funcCastToFloat, funcCastToInteger } from "./native/Number.js";
9
+ import { funcGreaterThan, funcLessThan, funcGreaterOrEqual, funcLessOrEqual, funcSubtract, funcMultiply, funcDivide, funcRemainder, funcModulo, funcPower, funcRoot, funcNegate } from "./native/Number.js";
7
10
  import { funcEntries, funcKeys, funcValues } from "./native/Object.js";
8
11
  import { funcLike, funcUnlike, funcStartsWith, funcEndsWith, funcChar, funcCharCode, funcTrim, funcTrimStart, funcTrimEnd, funcLowerCase, funcUpperCase, funcSplit, funcReplaceWith } from "./native/String.js";
9
12
  import { funcYear, funcMonth, funcMonthIndex, funcWeekdayIndex, funcDay, funcHour, funcMinute, funcSecond, funcMillisecond, funcEpochTime, funcDaysSince, funcHoursSince, funcMinutesSince, funcSecondsSince } from "./native/Timestamp.js";
@@ -50,9 +53,9 @@ export const Functions = [
50
53
  ["Power", funcPower],
51
54
  ["Root", funcRoot],
52
55
  ["Negate", funcNegate],
53
- ["Cast", funcCast],
54
- ["CastToFloat", funcCastToFloat],
55
- ["CastToInteger", funcCastToInteger],
56
+ ["Boolean", funcBoolean],
57
+ ["Float", funcFloat],
58
+ ["Integer", funcInteger],
56
59
  // Object
57
60
  ["Entries", funcEntries],
58
61
  ["Keys", funcKeys],
package/dst/Keywords.js CHANGED
@@ -1,10 +1,10 @@
1
1
  export const Keywords = [
2
2
  "true", "false",
3
3
  "null", "void",
4
- "boolean",
5
- "timestamp",
6
- "float",
7
- "integer",
4
+ "bool", "boolean",
5
+ "time", "timestamp",
6
+ "f64", "float",
7
+ "i64", "integer",
8
8
  "buffer",
9
9
  "string",
10
10
  "array",
@@ -32,10 +32,11 @@ export declare class ParserState extends ParserFrame {
32
32
  get isSemicolon(): boolean;
33
33
  get isColon(): boolean;
34
34
  get isComma(): boolean;
35
+ get isTilda(): boolean;
35
36
  get isDot(): boolean;
36
37
  get isQuestion(): boolean;
37
38
  get isEllipsis(): boolean;
38
- get isTilda(): boolean;
39
+ get isCast(): boolean;
39
40
  get isVar(): boolean;
40
41
  get isVal(): boolean;
41
42
  get isIf(): boolean;
@@ -5,6 +5,7 @@ import { isSignSymbol, isTokenStartSymbol, isNumericSymbol, isTokenSymbol, isDat
5
5
  import { Constant } from "./Constant.js";
6
6
  import { funcGreaterThan, funcLessThan, funcGreaterOrEqual, funcLessOrEqual, funcSubtract, funcMultiply, funcDivide, funcRemainder, funcPower } from "./native/Number.js";
7
7
  import { funcCoalesce, funcEqual, funcNotEqual } from "./native/Unknown.js";
8
+ import { boundInteger } from "./native/Integer.js";
8
9
  import { Type } from "./Type.js";
9
10
  import { ParserFrame } from "./ParserFrame.js";
10
11
  class Literal {
@@ -39,10 +40,11 @@ const idBracesClose = Symbol();
39
40
  const idSemicolon = Symbol();
40
41
  const idColon = Symbol();
41
42
  const idComma = Symbol();
43
+ const idTilda = Symbol();
42
44
  const idDot = Symbol();
43
45
  const idQuestion = Symbol();
44
46
  const idEllipsis = Symbol();
45
- const idTilda = Symbol();
47
+ const idCast = Symbol();
46
48
  const idVar = Symbol();
47
49
  const idVal = Symbol();
48
50
  const idIf = Symbol();
@@ -114,6 +116,9 @@ export class ParserState extends ParserFrame {
114
116
  get isComma() {
115
117
  return this._fragment === idComma;
116
118
  }
119
+ get isTilda() {
120
+ return this._fragment === idTilda;
121
+ }
117
122
  get isDot() {
118
123
  return this._fragment === idDot;
119
124
  }
@@ -123,8 +128,8 @@ export class ParserState extends ParserFrame {
123
128
  get isEllipsis() {
124
129
  return this._fragment === idEllipsis;
125
130
  }
126
- get isTilda() {
127
- return this._fragment === idTilda;
131
+ get isCast() {
132
+ return this._fragment === idCast;
128
133
  }
129
134
  get isVar() {
130
135
  return this._fragment === idVar;
@@ -240,7 +245,15 @@ export class ParserState extends ParserFrame {
240
245
  this._fragment = idSemicolon;
241
246
  break;
242
247
  case ":":
243
- this._fragment = idColon;
248
+ switch (this._expr.charAt(this._end)) {
249
+ case ":":
250
+ ++this._end;
251
+ this._fragment = idCast;
252
+ break;
253
+ default:
254
+ this._fragment = idColon;
255
+ break;
256
+ }
244
257
  break;
245
258
  case ",":
246
259
  this._fragment = idComma;
@@ -503,15 +516,19 @@ export class ParserState extends ParserFrame {
503
516
  case "void":
504
517
  this._fragment = Type.Void;
505
518
  break;
519
+ case "bool":
506
520
  case "boolean":
507
521
  this._fragment = Type.Boolean;
508
522
  break;
523
+ case "time":
509
524
  case "timestamp":
510
525
  this._fragment = Type.Timestamp;
511
526
  break;
527
+ case "f64":
512
528
  case "float":
513
529
  this._fragment = Type.Float;
514
530
  break;
531
+ case "i64":
515
532
  case "integer":
516
533
  this._fragment = Type.Integer;
517
534
  break;
@@ -586,7 +603,7 @@ export class ParserState extends ParserFrame {
586
603
  integer = false;
587
604
  }
588
605
  this._fragment = integer
589
- ? new Literal(BigInt(this._expr.substring(this._start, this._end)))
606
+ ? new Literal(boundInteger(BigInt(this._expr.substring(this._start, this._end))))
590
607
  : new Literal(parseFloat(this._expr.substring(this._start, this._end)));
591
608
  }
592
609
  else {
package/dst/Type.d.ts CHANGED
@@ -20,20 +20,24 @@ export declare class Type {
20
20
  get isArray(): boolean;
21
21
  get isObject(): boolean;
22
22
  get isFunction(): boolean;
23
- get isNumeric(): boolean;
24
- toOptional(): Type;
25
- functionAtomRetType(type: Type): Type;
26
- functionAtoms(type: Type, argc: number): FunctionAtom[];
23
+ equate(type: Type | undefined): boolean;
24
+ accept(type: Type | Atom): boolean;
25
+ acceptValue(value: Value): boolean;
27
26
  reduce(mask: Type): Type | undefined;
28
- match(type: Type | Atom): boolean;
27
+ intersect(type: Type): boolean;
29
28
  weight(): number;
29
+ arrayAtomValType(index: number): Type;
30
+ functionAtomRetType(type: Type): Type;
31
+ functionAtoms(type: Type, argc: number): FunctionAtom[];
32
+ toOptional(): Type;
33
+ toMandatory(): Type;
30
34
  toString(): string;
31
35
  static union(...types: Type[]): Type;
32
36
  static of(value: Value): Type;
33
- static isPrimitiveType(value: Value): value is string | number | bigint | boolean | Date | ArrayBuffer | null | undefined;
37
+ static isPrimitive(value: Value): value is string | number | bigint | boolean | Date | ArrayBuffer | null | undefined;
34
38
  static primitiveType(primitive: Primitive): Type;
35
39
  static arrayType(itemTypes?: Type[]): Type;
36
- static objectType(propTypes?: Record<string, Type>): Type;
40
+ static objectType(propTypes?: [string, Type][]): Type;
37
41
  static functionType(retType?: Type, argTypes?: Type[], isVariadic?: boolean): Type;
38
42
  static readonly Unknown: Type;
39
43
  static readonly Void: Type;
package/dst/Type.js CHANGED
@@ -14,54 +14,92 @@ export class Type {
14
14
  return this._atoms.length === 1;
15
15
  }
16
16
  get isOptional() {
17
- return this.isUnknown || this._atoms.length > 1 && this._atoms.some((i) => i.match(Type.Void._atoms[0]));
17
+ return this.isUnknown || this._atoms.length > 1 && this._atoms.some((a) => a.equate(Type.Void._atoms[0]));
18
18
  }
19
19
  get isVoid() {
20
- return this.isAtom && this._atoms[0].match(Type.Void._atoms[0]);
20
+ return this.isAtom && this._atoms[0].equate(Type.Void._atoms[0]);
21
21
  }
22
22
  get isBoolean() {
23
- return this.isAtom && this._atoms[0].match(Type.Boolean._atoms[0]);
23
+ return this.isAtom && this._atoms[0].equate(Type.Boolean._atoms[0]);
24
24
  }
25
25
  get isTimestamp() {
26
- return this.isAtom && this._atoms[0].match(Type.Timestamp._atoms[0]);
26
+ return this.isAtom && this._atoms[0].equate(Type.Timestamp._atoms[0]);
27
27
  }
28
28
  get isFloat() {
29
- return this.isAtom && this._atoms[0].match(Type.Float._atoms[0]);
29
+ return this.isAtom && this._atoms[0].equate(Type.Float._atoms[0]);
30
30
  }
31
31
  get isInteger() {
32
- return this.isAtom && this._atoms[0].match(Type.Integer._atoms[0]);
32
+ return this.isAtom && this._atoms[0].equate(Type.Integer._atoms[0]);
33
33
  }
34
34
  get isBuffer() {
35
- return this.isAtom && this._atoms[0].match(Type.Buffer._atoms[0]);
35
+ return this.isAtom && this._atoms[0].equate(Type.Buffer._atoms[0]);
36
36
  }
37
37
  get isString() {
38
- return this.isAtom && this._atoms[0].match(Type.String._atoms[0]);
38
+ return this.isAtom && this._atoms[0].equate(Type.String._atoms[0]);
39
39
  }
40
40
  get isArray() {
41
- return this._atoms.every((i) => i instanceof ArrayAtom);
41
+ return this._atoms.every((a) => a instanceof ArrayAtom);
42
42
  }
43
43
  get isObject() {
44
- return this._atoms.every((i) => i instanceof ObjectAtom);
44
+ return this._atoms.every((a) => a instanceof ObjectAtom);
45
45
  }
46
46
  get isFunction() {
47
- return this._atoms.every((i) => i instanceof FunctionAtom);
47
+ return this._atoms.every((a) => a instanceof FunctionAtom);
48
48
  }
49
- get isNumeric() {
50
- return this._atoms.length === 2
51
- && (this._atoms[0].match(Type.Float._atoms[0]) || this._atoms[0].match(Type.Integer._atoms[0]));
52
- }
53
- toOptional() {
54
- return this._atoms.length ? Type.union(Type.Void, this) : this;
49
+ equate(type) {
50
+ if (this._atoms.length !== type?._atoms.length) {
51
+ return false;
52
+ }
53
+ const unmatched = [...type._atoms];
54
+ for (const atom of this._atoms) {
55
+ const index = unmatched.findIndex((a) => atom.equate(a));
56
+ if (index < 0) {
57
+ return false;
58
+ }
59
+ unmatched.splice(index, 1);
60
+ }
61
+ return true;
55
62
  }
56
- functionAtomRetType(type) {
57
- const atoms = this._atoms.filter((i) => i instanceof FunctionAtom && i.retType.match(type));
58
- return Type.union(...atoms.map((i) => i.retType));
63
+ accept(type) {
64
+ if (type instanceof Type) {
65
+ if (this.isUnknown || type.isUnknown) {
66
+ return true;
67
+ }
68
+ return type._atoms.every((a) => this._atoms.some((atom) => atom.accept(a)));
69
+ }
70
+ return this._atoms.some((a) => a.accept(type));
59
71
  }
60
- functionAtoms(type, argc) {
72
+ acceptValue(value) {
61
73
  if (this.isUnknown) {
62
- return Type.Function._atoms;
74
+ return true;
63
75
  }
64
- return this._atoms.filter((i) => i instanceof FunctionAtom && i.minArity <= argc && i.maxArity >= argc && (type.isVoid || i.retType.match(type)));
76
+ const valueAtom = Type.of(value)._atoms[0];
77
+ return this._atoms.some((atom) => {
78
+ if (!atom.accept(valueAtom)) {
79
+ return false;
80
+ }
81
+ if (atom instanceof ArrayAtom) {
82
+ const values = value;
83
+ const types = atom.valTypes();
84
+ if (!types.length) {
85
+ return true;
86
+ }
87
+ if (atom.isTuple) {
88
+ return types.every((type, ix) => type.acceptValue(Object.hasOwn(values, ix) ? values[ix] : undefined));
89
+ }
90
+ for (let i = 0; i < values.length; ++i) {
91
+ if (!types[0].acceptValue(Object.hasOwn(values, i) ? values[i] : undefined)) {
92
+ return false;
93
+ }
94
+ }
95
+ return true;
96
+ }
97
+ if (atom instanceof ObjectAtom) {
98
+ const values = value;
99
+ return atom.valTypes().every(([key, type]) => type.acceptValue(Object.hasOwn(values, key) ? values[key] : undefined));
100
+ }
101
+ return true;
102
+ });
65
103
  }
66
104
  reduce(mask) {
67
105
  if (mask.isUnknown || mask.isVoid) {
@@ -70,37 +108,74 @@ export class Type {
70
108
  if (this.isUnknown) {
71
109
  return mask;
72
110
  }
73
- const list = this._atoms.filter((i) => mask.match(i));
111
+ const list = this._atoms.filter((a) => mask.accept(a));
74
112
  return list.length === 0
75
113
  ? undefined
76
114
  : list.length === this._atoms.length
77
115
  ? this
78
116
  : new Type(list);
79
117
  }
80
- match(type) {
81
- if (type instanceof Type) {
82
- return this.isUnknown || type.isUnknown || type.isVoid || this._atoms.some((i) => type._atoms.some((j) => i.match(j)));
83
- }
84
- else {
85
- return this._atoms.some((i) => i.match(type));
118
+ intersect(type) {
119
+ if (this.isUnknown || type.isUnknown) {
120
+ return true;
86
121
  }
122
+ return this._atoms.some((a) => type._atoms.some((atom) => a.intersect(atom)));
87
123
  }
88
124
  weight() {
89
- return this._atoms.reduce((acc, i) => acc + i.weight(), 0);
125
+ return this._atoms.reduce((acc, a) => acc + a.weight(), 0);
126
+ }
127
+ arrayAtomValType(index) {
128
+ const atoms = this._atoms.filter((a) => a instanceof ArrayAtom);
129
+ if (!atoms.length) {
130
+ return Type.Unknown;
131
+ }
132
+ const valTypes = [];
133
+ for (const atom of atoms) {
134
+ const valType = atom.isTuple
135
+ ? atom.valType(index)
136
+ : atom.valType(0);
137
+ if (!valType) {
138
+ return Type.Unknown;
139
+ }
140
+ valTypes.push(valType);
141
+ }
142
+ return Type.union(...valTypes);
143
+ }
144
+ functionAtomRetType(type) {
145
+ const atoms = this._atoms.filter((a) => a instanceof FunctionAtom && type.accept(a.retType));
146
+ return Type.union(...atoms.map((a) => a.retType));
147
+ }
148
+ functionAtoms(type, argc) {
149
+ if (this.isUnknown) {
150
+ return Type.Function._atoms;
151
+ }
152
+ return this._atoms.filter((a) => a instanceof FunctionAtom && a.minArity <= argc && a.maxArity >= argc && (type.isVoid || type.accept(a.retType)));
153
+ }
154
+ toOptional() {
155
+ return this._atoms.length ? Type.union(Type.Void, this) : this;
156
+ }
157
+ toMandatory() {
158
+ if (!this.isOptional || this.isUnknown) {
159
+ return this;
160
+ }
161
+ return new Type(this._atoms.filter((a) => !a.equate(Type.Void._atoms[0])));
90
162
  }
91
163
  toString() {
92
164
  return this._atoms.length
93
- ? this._atoms.sort((a, b) => a.weight() - b.weight()).map((i) => i.toString()).join("|")
165
+ ? [...this._atoms]
166
+ .sort((a, b) => a.weight() - b.weight())
167
+ .map((a) => a.toString())
168
+ .join("|")
94
169
  : "??";
95
170
  }
96
171
  static union(...types) {
97
- if (types.some((i) => i.isUnknown)) {
172
+ if (types.some((t) => t.isUnknown)) {
98
173
  return Type.Unknown;
99
174
  }
100
175
  const type = new Type([]);
101
- types.map((i) => i._atoms).flat().forEach((i) => {
102
- if (!type.match(i)) {
103
- type._atoms.push(i);
176
+ types.flatMap((t) => t._atoms).forEach((a) => {
177
+ if (!type._atoms.some((atom) => atom.equate(a))) {
178
+ type._atoms.push(a);
104
179
  }
105
180
  });
106
181
  return type;
@@ -126,7 +201,7 @@ export class Type {
126
201
  ? Type.Object
127
202
  : Type.Function;
128
203
  }
129
- static isPrimitiveType(value) {
204
+ static isPrimitive(value) {
130
205
  return value == null
131
206
  || typeof value === "boolean"
132
207
  || value instanceof Date
@@ -141,7 +216,7 @@ export class Type {
141
216
  static arrayType(itemTypes = []) {
142
217
  return new Type([new ArrayAtom(itemTypes)]);
143
218
  }
144
- static objectType(propTypes = Object.create(null)) {
219
+ static objectType(propTypes = []) {
145
220
  return new Type([new ObjectAtom(propTypes)]);
146
221
  }
147
222
  static functionType(retType = Type.Unknown, argTypes = [], isVariadic = false) {
package/dst/Value.d.ts CHANGED
@@ -2,3 +2,4 @@ export type PrimitiveValue = undefined | null | number | boolean | Date | bigint
2
2
  export type Value = PrimitiveValue | Value[] | {
3
3
  [key: string]: Value;
4
4
  } | ((...args: any[]) => Value);
5
+ export declare const normalize: (value: Value) => Value;
package/dst/Value.js CHANGED
@@ -1 +1,19 @@
1
- export {};
1
+ const normalizeValue = (value, objects) => {
2
+ if (typeof value === "bigint") {
3
+ return BigInt.asIntN(64, value);
4
+ }
5
+ if (value == null || typeof value !== "object" || value instanceof Date || value instanceof ArrayBuffer || objects.has(value)) {
6
+ return value;
7
+ }
8
+ objects.add(value);
9
+ if (Array.isArray(value)) {
10
+ value.forEach((i, ix) => value[ix] = normalizeValue(i, objects));
11
+ }
12
+ else {
13
+ for (const key of Object.keys(value)) {
14
+ value[key] = normalizeValue(value[key], objects);
15
+ }
16
+ }
17
+ return value;
18
+ };
19
+ export const normalize = (value) => normalizeValue(value, new WeakSet());
@@ -4,9 +4,13 @@ export declare class ArrayAtom implements IAtom {
4
4
  protected readonly _valTypes: Type[];
5
5
  constructor(_valTypes?: Type[]);
6
6
  private get _undefined();
7
- subtypes(): Type[];
8
- valueType(index: number): Type | undefined;
9
- match(atom: IAtom): boolean;
7
+ private _intersectLength;
8
+ get isTuple(): boolean;
9
+ valTypes(): Type[];
10
+ valType(index: number): Type | undefined;
11
+ equate(atom: IAtom): boolean;
12
+ accept(atom: IAtom): boolean;
13
+ intersect(atom: IAtom): boolean;
10
14
  weight(): number;
11
15
  toString(): string;
12
16
  }
@@ -6,29 +6,71 @@ export class ArrayAtom {
6
6
  get _undefined() {
7
7
  return this._valTypes.length === 0;
8
8
  }
9
- subtypes() {
9
+ _intersectLength(length) {
10
+ return !this.isTuple || this._valTypes.slice(length).every((type) => type.isOptional);
11
+ }
12
+ get isTuple() {
13
+ return this._valTypes.length > 1;
14
+ }
15
+ valTypes() {
10
16
  return this._valTypes;
11
17
  }
12
- valueType(index) {
18
+ valType(index) {
13
19
  return this._valTypes[index];
14
20
  }
15
- match(atom) {
21
+ equate(atom) {
22
+ return atom instanceof ArrayAtom
23
+ && this._valTypes.length === atom._valTypes.length
24
+ && this._valTypes.every((type, index) => type.equate(atom._valTypes[index]));
25
+ }
26
+ accept(atom) {
16
27
  if (atom instanceof ArrayAtom) {
17
28
  if (this._undefined || atom._undefined) {
18
29
  return true;
19
30
  }
20
- for (let i = 0; i < this._valTypes.length; ++i) {
21
- const vtype = atom.valueType(i);
22
- if (!vtype || !this._valTypes[i].match(vtype)) {
23
- return false;
31
+ if (this.isTuple) {
32
+ for (let i = 0; i < this._valTypes.length; ++i) {
33
+ const vtype = atom.isTuple ? atom.valType(i) : atom.valType(0);
34
+ if (!vtype || !this._valTypes[i].accept(vtype)) {
35
+ return false;
36
+ }
37
+ }
38
+ }
39
+ else {
40
+ for (let i = 0; i < atom.valTypes().length; ++i) {
41
+ const vtype = atom.valType(i);
42
+ if (!vtype || !this._valTypes[0].accept(vtype)) {
43
+ return false;
44
+ }
24
45
  }
25
46
  }
26
47
  return true;
27
48
  }
28
49
  return false;
29
50
  }
51
+ intersect(atom) {
52
+ if (!(atom instanceof ArrayAtom)) {
53
+ return false;
54
+ }
55
+ if (this._undefined || atom._undefined) {
56
+ return true;
57
+ }
58
+ const length = this.isTuple
59
+ ? atom.isTuple ? Math.min(this._valTypes.length, atom._valTypes.length) : this._valTypes.length
60
+ : atom.isTuple ? atom._valTypes.length : 1;
61
+ for (let i = 0; i < length; ++i) {
62
+ const type = this.isTuple ? this._valTypes[i] : this._valTypes[0];
63
+ const atype = atom.isTuple ? atom._valTypes[i] : atom._valTypes[0];
64
+ if (!type.intersect(atype)) {
65
+ return (this.isTuple || atom.isTuple)
66
+ && this._intersectLength(i)
67
+ && atom._intersectLength(i);
68
+ }
69
+ }
70
+ return true;
71
+ }
30
72
  weight() {
31
- return 0x10000 + this.subtypes().reduce((acc, i) => acc + i.weight(), 0);
73
+ return 0x10000 + this.valTypes().reduce((acc, i) => acc + i.weight(), 0);
32
74
  }
33
75
  toString() {
34
76
  if (this._undefined) {
@@ -13,7 +13,10 @@ export declare class FunctionAtom implements IAtom {
13
13
  get isVariadic(): boolean;
14
14
  get arity(): number;
15
15
  argType(index: number): Type | undefined;
16
- match(atom: IAtom): boolean;
16
+ paramType(index: number): Type | undefined;
17
+ equate(atom: IAtom): boolean;
18
+ accept(atom: IAtom): boolean;
19
+ intersect(atom: IAtom): boolean;
17
20
  weight(): number;
18
21
  toString(): string;
19
22
  }
@@ -43,27 +43,77 @@ export class FunctionAtom {
43
43
  argType(index) {
44
44
  return this._argTypes[index] ?? (this._isVariadic ? this._argTypes[this._argTypes.length - 1] : undefined);
45
45
  }
46
- match(atom) {
47
- if (atom instanceof FunctionAtom) {
48
- if (this._undefined || atom._undefined) {
49
- return true;
50
- }
51
- if (!this._retType.match(atom._retType) || this.minArity > atom.maxArity || this.maxArity < atom.minArity) {
46
+ paramType(index) {
47
+ if (!this._isVariadic) {
48
+ return this.argType(index);
49
+ }
50
+ if (!this._argTypes.length) {
51
+ return Type.Unknown;
52
+ }
53
+ const variadicIndex = this._argTypes.length - 1;
54
+ if (index < variadicIndex) {
55
+ return this.argType(index);
56
+ }
57
+ return this.argType(variadicIndex)
58
+ ?.arrayAtomValType(index - variadicIndex);
59
+ }
60
+ equate(atom) {
61
+ return atom instanceof FunctionAtom
62
+ && this._isVariadic === atom._isVariadic
63
+ && this._argTypes.length === atom._argTypes.length
64
+ && this._retType.equate(atom._retType)
65
+ && this._argTypes.every((type, index) => type.equate(atom._argTypes[index]));
66
+ }
67
+ accept(atom) {
68
+ if (!(atom instanceof FunctionAtom)) {
69
+ return false;
70
+ }
71
+ if (this._undefined || atom._undefined) {
72
+ return true;
73
+ }
74
+ if (this._isVariadic !== atom._isVariadic
75
+ || this._isVariadic && this.arity !== atom.arity
76
+ || !this._retType.accept(atom._retType)
77
+ || this.minArity > atom.maxArity
78
+ || this.maxArity < atom.minArity) {
79
+ return false;
80
+ }
81
+ const argc = Math.min(this.arity, atom.arity);
82
+ for (let i = 0; i < argc; ++i) {
83
+ const type = this.argType(i)?.toMandatory();
84
+ const atype = atom.argType(i);
85
+ if (type && (!atype || !atype.accept(type))) {
52
86
  return false;
53
87
  }
54
- const argc = Math.max(this.minArity, atom.minArity);
55
- for (let i = 0; i < argc; ++i) {
56
- const atype = atom.argType(i);
57
- if (!atype || !this.argType(i)?.match(atype)) {
58
- return false;
59
- }
60
- }
88
+ }
89
+ return true;
90
+ }
91
+ intersect(atom) {
92
+ if (!(atom instanceof FunctionAtom)) {
93
+ return false;
94
+ }
95
+ if (this._undefined || atom._undefined) {
61
96
  return true;
62
97
  }
63
- return false;
98
+ if (this._isVariadic !== atom._isVariadic
99
+ || this._isVariadic && this.arity !== atom.arity
100
+ || !this._retType.intersect(atom._retType)
101
+ || this.minArity > atom.maxArity
102
+ || this.maxArity < atom.minArity) {
103
+ return false;
104
+ }
105
+ const argc = Math.max(this.minArity, atom.minArity);
106
+ for (let i = 0; i < argc; ++i) {
107
+ const type = this.argType(i);
108
+ const atype = atom.argType(i);
109
+ if (!type || !atype || !type.intersect(atype)) {
110
+ return false;
111
+ }
112
+ }
113
+ return true;
64
114
  }
65
115
  weight() {
66
- return 0x1000000000000 + (this._retType ? [this._retType, ...this._argTypes] : []).reduce((acc, i) => acc + i.weight(), 0);
116
+ return 0x1000000000000 + this._argTypes.reduce((acc, i) => acc + i.weight(), this._retType.weight());
67
117
  }
68
118
  toString() {
69
119
  if (this._undefined) {
@@ -1,12 +1,14 @@
1
1
  import { IAtom } from "../Atom.js";
2
2
  import { Type } from "../Type.js";
3
3
  export declare class ObjectAtom implements IAtom {
4
- protected readonly _valTypes: Record<string, Type>;
5
- constructor(_valTypes?: Record<string, Type>);
4
+ protected readonly _valTypes: [string, Type][];
5
+ constructor(valTypes?: [string, Type][]);
6
6
  private get _undefined();
7
- subtypes(): Type[];
8
- valueType(key: string): Type | undefined;
9
- match(atom: IAtom): boolean;
7
+ valTypes(): [string, Type][];
8
+ valType(key: string): Type | undefined;
9
+ equate(atom: IAtom): boolean;
10
+ accept(atom: IAtom): boolean;
11
+ intersect(atom: IAtom): boolean;
10
12
  weight(): number;
11
13
  toString(): string;
12
14
  }