affinirum 1.5.4 → 2.0.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 (243) hide show
  1. package/.cache/arrow-preview-vWvB5K/affinirum-arrow-functions.patch +155 -0
  2. package/.cache/function-syntax-tests.log +20 -0
  3. package/README.md +925 -355
  4. package/dst/Affinirum.d.ts +29 -19
  5. package/dst/Affinirum.js +399 -334
  6. package/dst/CallFrame.d.ts +12 -3
  7. package/dst/CallFrame.js +49 -10
  8. package/dst/Constant.d.ts +1 -3
  9. package/dst/Constant.js +3 -8
  10. package/dst/LexicalScope.d.ts +11 -9
  11. package/dst/LexicalScope.js +10 -6
  12. package/dst/ParserSpan.d.ts +11 -0
  13. package/dst/{ScriptSpan.js → ParserSpan.js} +8 -5
  14. package/dst/ParserState.d.ts +28 -49
  15. package/dst/ParserState.js +256 -489
  16. package/dst/Type.d.ts +34 -17
  17. package/dst/Type.js +211 -115
  18. package/dst/TypeParser.d.ts +9 -0
  19. package/dst/TypeParser.js +127 -0
  20. package/dst/Value.d.ts +1 -1
  21. package/dst/Value.js +21 -10
  22. package/dst/Variable.d.ts +16 -5
  23. package/dst/Variable.js +32 -7
  24. package/dst/atom/ArrayAtom.d.ts +10 -10
  25. package/dst/atom/ArrayAtom.js +62 -55
  26. package/dst/atom/Atom.d.ts +7 -0
  27. package/dst/atom/FunctionAtom.d.ts +13 -15
  28. package/dst/atom/FunctionAtom.js +53 -104
  29. package/dst/atom/FunctionSignature.d.ts +23 -0
  30. package/dst/atom/FunctionSignature.js +124 -0
  31. package/dst/atom/ObjectAtom.d.ts +11 -9
  32. package/dst/atom/ObjectAtom.js +73 -33
  33. package/dst/atom/PrimitiveAtom.d.ts +6 -7
  34. package/dst/atom/PrimitiveAtom.js +7 -10
  35. package/dst/cjs/Affinirum.d.ts +29 -19
  36. package/dst/cjs/Affinirum.js +398 -333
  37. package/dst/cjs/CallFrame.d.ts +12 -3
  38. package/dst/cjs/CallFrame.js +49 -10
  39. package/dst/cjs/Constant.d.ts +1 -3
  40. package/dst/cjs/Constant.js +3 -8
  41. package/dst/cjs/LexicalScope.d.ts +11 -9
  42. package/dst/cjs/LexicalScope.js +10 -6
  43. package/dst/cjs/ParserSpan.d.ts +11 -0
  44. package/dst/cjs/{ScriptSpan.js → ParserSpan.js} +10 -7
  45. package/dst/cjs/ParserState.d.ts +28 -49
  46. package/dst/cjs/ParserState.js +255 -488
  47. package/dst/cjs/Type.d.ts +34 -17
  48. package/dst/cjs/Type.js +211 -115
  49. package/dst/cjs/TypeParser.d.ts +9 -0
  50. package/dst/cjs/TypeParser.js +131 -0
  51. package/dst/cjs/Value.d.ts +1 -1
  52. package/dst/cjs/Value.js +21 -10
  53. package/dst/cjs/Variable.d.ts +16 -5
  54. package/dst/cjs/Variable.js +36 -9
  55. package/dst/cjs/atom/ArrayAtom.d.ts +10 -10
  56. package/dst/cjs/atom/ArrayAtom.js +62 -55
  57. package/dst/cjs/atom/Atom.d.ts +7 -0
  58. package/dst/cjs/atom/FunctionAtom.d.ts +13 -15
  59. package/dst/cjs/atom/FunctionAtom.js +54 -105
  60. package/dst/cjs/atom/FunctionSignature.d.ts +23 -0
  61. package/dst/cjs/atom/FunctionSignature.js +128 -0
  62. package/dst/cjs/atom/ObjectAtom.d.ts +11 -9
  63. package/dst/cjs/atom/ObjectAtom.js +73 -33
  64. package/dst/cjs/atom/PrimitiveAtom.d.ts +6 -7
  65. package/dst/cjs/atom/PrimitiveAtom.js +7 -10
  66. package/dst/cjs/dictionary/Constants.d.ts +2 -0
  67. package/dst/cjs/dictionary/Constants.js +27 -0
  68. package/dst/cjs/dictionary/Functions.d.ts +2 -0
  69. package/dst/cjs/{Functions.js → dictionary/Functions.js} +27 -25
  70. package/dst/cjs/native/Aggregable.js +10 -2
  71. package/dst/cjs/native/Array.d.ts +2 -3
  72. package/dst/cjs/native/Array.js +38 -39
  73. package/dst/cjs/native/Boolean.d.ts +1 -1
  74. package/dst/cjs/native/Boolean.js +13 -10
  75. package/dst/cjs/native/Buffer.d.ts +5 -5
  76. package/dst/cjs/native/Buffer.js +70 -46
  77. package/dst/cjs/native/Composite.d.ts +5 -0
  78. package/dst/cjs/native/Composite.js +35 -0
  79. package/dst/cjs/native/Enumerable.js +20 -8
  80. package/dst/cjs/native/Float.d.ts +3 -1
  81. package/dst/cjs/native/Float.js +51 -12
  82. package/dst/cjs/native/Integer.d.ts +4 -2
  83. package/dst/cjs/native/Integer.js +60 -25
  84. package/dst/cjs/native/Iterable.d.ts +0 -2
  85. package/dst/cjs/native/Iterable.js +9 -28
  86. package/dst/cjs/native/Number.d.ts +0 -2
  87. package/dst/cjs/native/Number.js +34 -48
  88. package/dst/cjs/native/Object.d.ts +1 -1
  89. package/dst/cjs/native/Object.js +5 -6
  90. package/dst/cjs/native/String.d.ts +7 -6
  91. package/dst/cjs/native/String.js +28 -27
  92. package/dst/cjs/native/Timestamp.d.ts +3 -3
  93. package/dst/cjs/native/Timestamp.js +34 -36
  94. package/dst/cjs/native/Unknown.d.ts +3 -3
  95. package/dst/cjs/native/Unknown.js +48 -17
  96. package/dst/cjs/native/notation/AN.d.ts +1 -1
  97. package/dst/cjs/native/notation/AN.js +37 -35
  98. package/dst/cjs/native/notation/IP.d.ts +1 -1
  99. package/dst/cjs/native/notation/IP.js +11 -12
  100. package/dst/cjs/native/notation/JSON.d.ts +1 -1
  101. package/dst/cjs/native/notation/JSON.js +7 -6
  102. package/dst/cjs/node/AccessNode.d.ts +24 -0
  103. package/dst/cjs/node/AccessNode.js +193 -0
  104. package/dst/cjs/node/ArrayNode.d.ts +7 -4
  105. package/dst/cjs/node/ArrayNode.js +26 -15
  106. package/dst/cjs/node/AssignableNode.d.ts +16 -0
  107. package/dst/cjs/node/AssignableNode.js +18 -0
  108. package/dst/cjs/node/AssignmentNode.d.ts +20 -0
  109. package/dst/cjs/node/AssignmentNode.js +66 -0
  110. package/dst/cjs/node/BlockNode.d.ts +5 -4
  111. package/dst/cjs/node/BlockNode.js +18 -11
  112. package/dst/cjs/node/CallNode.d.ts +7 -5
  113. package/dst/cjs/node/CallNode.js +40 -41
  114. package/dst/cjs/node/CastNode.d.ts +5 -6
  115. package/dst/cjs/node/CastNode.js +16 -17
  116. package/dst/cjs/node/ConstantNode.d.ts +4 -6
  117. package/dst/cjs/node/ConstantNode.js +9 -17
  118. package/dst/cjs/node/FinalNode.d.ts +19 -0
  119. package/dst/cjs/node/FinalNode.js +41 -0
  120. package/dst/cjs/node/FunctionNode.d.ts +17 -0
  121. package/dst/cjs/node/FunctionNode.js +51 -0
  122. package/dst/cjs/node/InvocationNode.d.ts +5 -0
  123. package/dst/cjs/node/InvocationNode.js +34 -0
  124. package/dst/cjs/node/JumpNode.d.ts +7 -9
  125. package/dst/cjs/node/JumpNode.js +12 -19
  126. package/dst/cjs/node/LoopNode.d.ts +7 -5
  127. package/dst/cjs/node/LoopNode.js +33 -22
  128. package/dst/cjs/node/MatchNode.d.ts +17 -0
  129. package/dst/cjs/node/MatchNode.js +65 -0
  130. package/dst/cjs/node/Node.d.ts +14 -0
  131. package/dst/cjs/{Node.js → node/Node.js} +17 -11
  132. package/dst/cjs/node/ObjectNode.d.ts +7 -4
  133. package/dst/cjs/node/ObjectNode.js +42 -11
  134. package/dst/cjs/node/{SwitchNode.d.ts → TernaryNode.d.ts} +6 -6
  135. package/dst/cjs/node/TernaryNode.js +45 -0
  136. package/dst/cjs/node/VariableNode.d.ts +10 -6
  137. package/dst/cjs/node/VariableNode.js +50 -16
  138. package/dst/dictionary/Constants.d.ts +2 -0
  139. package/dst/dictionary/Constants.js +24 -0
  140. package/dst/dictionary/Functions.d.ts +2 -0
  141. package/dst/{Functions.js → dictionary/Functions.js} +26 -24
  142. package/dst/native/Aggregable.js +11 -3
  143. package/dst/native/Array.d.ts +2 -3
  144. package/dst/native/Array.js +37 -38
  145. package/dst/native/Boolean.d.ts +1 -1
  146. package/dst/native/Boolean.js +12 -9
  147. package/dst/native/Buffer.d.ts +5 -5
  148. package/dst/native/Buffer.js +66 -42
  149. package/dst/native/Composite.d.ts +5 -0
  150. package/dst/native/Composite.js +32 -0
  151. package/dst/native/Enumerable.js +20 -8
  152. package/dst/native/Float.d.ts +3 -1
  153. package/dst/native/Float.js +48 -11
  154. package/dst/native/Integer.d.ts +4 -2
  155. package/dst/native/Integer.js +56 -23
  156. package/dst/native/Iterable.d.ts +0 -2
  157. package/dst/native/Iterable.js +8 -27
  158. package/dst/native/Number.d.ts +0 -2
  159. package/dst/native/Number.js +34 -46
  160. package/dst/native/Object.d.ts +1 -1
  161. package/dst/native/Object.js +4 -5
  162. package/dst/native/String.d.ts +7 -6
  163. package/dst/native/String.js +24 -24
  164. package/dst/native/Timestamp.d.ts +3 -3
  165. package/dst/native/Timestamp.js +31 -33
  166. package/dst/native/Unknown.d.ts +3 -3
  167. package/dst/native/Unknown.js +49 -18
  168. package/dst/native/notation/AN.d.ts +1 -1
  169. package/dst/native/notation/AN.js +37 -35
  170. package/dst/native/notation/IP.d.ts +1 -1
  171. package/dst/native/notation/IP.js +10 -11
  172. package/dst/native/notation/JSON.d.ts +1 -1
  173. package/dst/native/notation/JSON.js +6 -5
  174. package/dst/node/AccessNode.d.ts +24 -0
  175. package/dst/node/AccessNode.js +189 -0
  176. package/dst/node/ArrayNode.d.ts +7 -4
  177. package/dst/node/ArrayNode.js +26 -15
  178. package/dst/node/AssignableNode.d.ts +16 -0
  179. package/dst/node/AssignableNode.js +14 -0
  180. package/dst/node/AssignmentNode.d.ts +20 -0
  181. package/dst/node/AssignmentNode.js +62 -0
  182. package/dst/node/BlockNode.d.ts +5 -4
  183. package/dst/node/BlockNode.js +18 -11
  184. package/dst/node/CallNode.d.ts +7 -5
  185. package/dst/node/CallNode.js +40 -41
  186. package/dst/node/CastNode.d.ts +5 -6
  187. package/dst/node/CastNode.js +16 -17
  188. package/dst/node/ConstantNode.d.ts +4 -6
  189. package/dst/node/ConstantNode.js +9 -17
  190. package/dst/node/FinalNode.d.ts +19 -0
  191. package/dst/node/FinalNode.js +37 -0
  192. package/dst/node/FunctionNode.d.ts +17 -0
  193. package/dst/node/FunctionNode.js +47 -0
  194. package/dst/node/InvocationNode.d.ts +5 -0
  195. package/dst/node/InvocationNode.js +30 -0
  196. package/dst/node/JumpNode.d.ts +7 -9
  197. package/dst/node/JumpNode.js +12 -19
  198. package/dst/node/LoopNode.d.ts +7 -5
  199. package/dst/node/LoopNode.js +33 -22
  200. package/dst/node/MatchNode.d.ts +17 -0
  201. package/dst/node/MatchNode.js +61 -0
  202. package/dst/node/Node.d.ts +14 -0
  203. package/dst/node/Node.js +28 -0
  204. package/dst/node/ObjectNode.d.ts +7 -4
  205. package/dst/node/ObjectNode.js +42 -11
  206. package/dst/node/{SwitchNode.d.ts → TernaryNode.d.ts} +6 -6
  207. package/dst/node/TernaryNode.js +41 -0
  208. package/dst/node/VariableNode.d.ts +10 -6
  209. package/dst/node/VariableNode.js +50 -16
  210. package/package.json +4 -4
  211. package/dst/Atom.d.ts +0 -7
  212. package/dst/Constants.d.ts +0 -2
  213. package/dst/Constants.js +0 -24
  214. package/dst/Functions.d.ts +0 -2
  215. package/dst/Jump.d.ts +0 -1
  216. package/dst/Jump.js +0 -1
  217. package/dst/JumpException.d.ts +0 -9
  218. package/dst/JumpException.js +0 -14
  219. package/dst/Keywords.d.ts +0 -1
  220. package/dst/Keywords.js +0 -17
  221. package/dst/Node.d.ts +0 -14
  222. package/dst/Node.js +0 -22
  223. package/dst/ScriptSpan.d.ts +0 -10
  224. package/dst/cjs/Atom.d.ts +0 -7
  225. package/dst/cjs/Constants.d.ts +0 -2
  226. package/dst/cjs/Constants.js +0 -27
  227. package/dst/cjs/Functions.d.ts +0 -2
  228. package/dst/cjs/Jump.d.ts +0 -1
  229. package/dst/cjs/Jump.js +0 -2
  230. package/dst/cjs/JumpException.d.ts +0 -9
  231. package/dst/cjs/JumpException.js +0 -18
  232. package/dst/cjs/Keywords.d.ts +0 -1
  233. package/dst/cjs/Keywords.js +0 -20
  234. package/dst/cjs/Node.d.ts +0 -14
  235. package/dst/cjs/ScriptSpan.d.ts +0 -10
  236. package/dst/cjs/node/SwitchNode.js +0 -49
  237. package/dst/cjs/node/VariadicNode.d.ts +0 -4
  238. package/dst/cjs/node/VariadicNode.js +0 -10
  239. package/dst/node/SwitchNode.js +0 -45
  240. package/dst/node/VariadicNode.d.ts +0 -4
  241. package/dst/node/VariadicNode.js +0 -6
  242. /package/dst/{Atom.js → atom/Atom.js} +0 -0
  243. /package/dst/cjs/{Atom.js → atom/Atom.js} +0 -0
package/dst/Value.d.ts CHANGED
@@ -2,4 +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;
5
+ export declare const normalize: (value: Value, composites?: WeakMap<object, Value>) => Value;
package/dst/Value.js CHANGED
@@ -1,19 +1,30 @@
1
- const normalizeValue = (value, objects) => {
1
+ export const normalize = (value, composites) => {
2
2
  if (typeof value === "bigint") {
3
3
  return BigInt.asIntN(64, value);
4
4
  }
5
- if (value == null || typeof value !== "object" || value instanceof Date || value instanceof ArrayBuffer || objects.has(value)) {
5
+ if (value == null || typeof value !== "object" || value instanceof Date || value instanceof ArrayBuffer) {
6
6
  return value;
7
7
  }
8
- objects.add(value);
9
- if (Array.isArray(value)) {
10
- value.forEach((i, ix) => value[ix] = normalizeValue(i, objects));
8
+ composites ??= new WeakMap();
9
+ const existing = composites.get(value);
10
+ if (existing !== undefined) {
11
+ return existing;
11
12
  }
12
- else {
13
- for (const key of Object.keys(value)) {
14
- value[key] = normalizeValue(value[key], objects);
13
+ if (Array.isArray(value)) {
14
+ const copy = new Array(value.length);
15
+ composites.set(value, copy);
16
+ for (const key of Object.getOwnPropertyNames(value)) {
17
+ const index = Number(key);
18
+ if (Number.isInteger(index) && index >= 0 && index < copy.length && String(index) === key) {
19
+ copy[index] = normalize(value[index], composites);
20
+ }
15
21
  }
22
+ return copy;
23
+ }
24
+ const copy = Object.create(null);
25
+ composites.set(value, copy);
26
+ for (const key of Object.keys(value)) {
27
+ copy[key] = normalize(value[key], composites);
16
28
  }
17
- return value;
29
+ return copy;
18
30
  };
19
- export const normalize = (value) => normalizeValue(value, new WeakSet());
package/dst/Variable.d.ts CHANGED
@@ -1,9 +1,20 @@
1
1
  import { Type } from "./Type.js";
2
- export declare class Variable {
3
- protected _type: Type;
4
- protected readonly _assignable: boolean;
5
- constructor(_type?: Type, _assignable?: boolean);
2
+ export declare abstract class Variable {
3
+ protected _type: Type | undefined;
4
+ constructor(type?: Type);
6
5
  get type(): Type;
7
- set type(type: Type);
6
+ get declared(): boolean;
7
+ abstract declare(type: Type): void;
8
+ }
9
+ export declare class LocalVariable extends Variable {
10
+ protected readonly _assignable: boolean;
11
+ constructor(type?: Type, assignable?: boolean);
12
+ declare(type: Type): void;
8
13
  get assignable(): boolean;
9
14
  }
15
+ export declare class InputVariable extends Variable {
16
+ protected readonly _declared: boolean;
17
+ constructor(type?: Type);
18
+ declare(type: Type): void;
19
+ get declared(): boolean;
20
+ }
package/dst/Variable.js CHANGED
@@ -1,18 +1,43 @@
1
1
  import { Type } from "./Type.js";
2
2
  export class Variable {
3
3
  _type;
4
- _assignable;
5
- constructor(_type = Type.Unknown, _assignable = true) {
6
- this._type = _type;
7
- this._assignable = _assignable;
4
+ constructor(type) {
5
+ this._type = type;
8
6
  }
9
7
  get type() {
10
- return this._type;
8
+ return this._type ?? Type.Any;
11
9
  }
12
- set type(type) {
13
- this._type = type;
10
+ get declared() {
11
+ return Boolean(this._type);
12
+ }
13
+ }
14
+ export class LocalVariable extends Variable {
15
+ _assignable;
16
+ constructor(type, assignable) {
17
+ super(type);
18
+ this._assignable = assignable ?? false;
19
+ }
20
+ declare(type) {
21
+ if (!this._type) {
22
+ this._type = type;
23
+ }
14
24
  }
15
25
  get assignable() {
16
26
  return this._assignable;
17
27
  }
18
28
  }
29
+ export class InputVariable extends Variable {
30
+ _declared;
31
+ constructor(type) {
32
+ super(type);
33
+ this._declared = Boolean(type);
34
+ }
35
+ declare(type) {
36
+ if (!this._declared) {
37
+ this._type = this._type ? Type.union(this._type, type) : type;
38
+ }
39
+ }
40
+ get declared() {
41
+ return this._declared;
42
+ }
43
+ }
@@ -1,16 +1,16 @@
1
- import { IAtom } from "../Atom.js";
1
+ import { IAtom } from "./Atom.js";
2
2
  import { Type } from "../Type.js";
3
3
  export declare class ArrayAtom implements IAtom {
4
- protected readonly _valTypes: Type[];
5
- constructor(_valTypes?: Type[]);
6
- private get _undefined();
7
- private _intersectLength;
4
+ protected readonly _valTypes: Type | readonly Type[];
5
+ constructor(valTypes?: Type | readonly Type[]);
6
+ get isUniform(): boolean;
8
7
  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;
8
+ get types(): Type | readonly Type[];
9
+ valTypes(): readonly Type[];
10
+ valType(index: number | undefined): Type | undefined;
11
+ equals(atom: IAtom): boolean;
12
+ accepts(atom: IAtom): boolean;
13
+ intersects(atom: IAtom): boolean;
14
14
  weight(): number;
15
15
  toString(): string;
16
16
  }
@@ -1,73 +1,79 @@
1
1
  import { Type } from "../Type.js";
2
2
  export class ArrayAtom {
3
3
  _valTypes;
4
- constructor(_valTypes = []) {
5
- this._valTypes = _valTypes;
6
- }
7
- get _undefined() {
8
- return this._valTypes.length === 0;
4
+ constructor(valTypes = []) {
5
+ if (Array.isArray(valTypes)) {
6
+ this._valTypes = valTypes.length === 0 ? Type.Any : [...valTypes];
7
+ }
8
+ else {
9
+ this._valTypes = valTypes;
10
+ }
9
11
  }
10
- _intersectLength(length) {
11
- return !this.isTuple || this._valTypes.slice(length).every((type) => type.isOptional);
12
+ get isUniform() {
13
+ return !Array.isArray(this._valTypes);
12
14
  }
13
15
  get isTuple() {
14
- return this._valTypes.length > 1;
16
+ return Array.isArray(this._valTypes);
15
17
  }
16
- valTypes() {
18
+ get types() {
17
19
  return this._valTypes;
18
20
  }
21
+ valTypes() {
22
+ const types = this._valTypes;
23
+ return types instanceof Type ? [types] : types;
24
+ }
19
25
  valType(index) {
20
- return this._valTypes[index];
26
+ const types = this._valTypes;
27
+ if (types instanceof Type) {
28
+ return types;
29
+ }
30
+ return index == null ? undefined : types[index];
21
31
  }
22
- equate(atom) {
23
- return atom instanceof ArrayAtom
24
- && this._valTypes.length === atom._valTypes.length
25
- && this._valTypes.every((type, index) => type.equate(atom._valTypes[index]));
32
+ equals(atom) {
33
+ if (!(atom instanceof ArrayAtom)) {
34
+ return false;
35
+ }
36
+ const types = this._valTypes;
37
+ const other = atom._valTypes;
38
+ if (types instanceof Type) {
39
+ return other instanceof Type && types.equals(other);
40
+ }
41
+ return !(other instanceof Type) &&
42
+ types.length === other.length &&
43
+ types.every((type, index) => type.equals(other[index]));
26
44
  }
27
- accept(atom) {
28
- if (atom instanceof ArrayAtom) {
29
- if (this._undefined || atom._undefined) {
30
- return true;
31
- }
32
- if (this.isTuple) {
33
- for (let i = 0; i < this._valTypes.length; ++i) {
34
- const vtype = atom.isTuple
35
- ? atom.valType(i) ?? Type.Void
36
- : atom.valType(0);
37
- if (!vtype || !this._valTypes[i].accept(vtype)) {
38
- return false;
39
- }
40
- }
41
- }
42
- else {
43
- for (let i = 0; i < atom.valTypes().length; ++i) {
44
- const vtype = atom.valType(i);
45
- if (!vtype || !this._valTypes[0].accept(vtype)) {
46
- return false;
47
- }
48
- }
49
- }
50
- return true;
45
+ accepts(atom) {
46
+ if (!(atom instanceof ArrayAtom)) {
47
+ return false;
48
+ }
49
+ const types = this._valTypes;
50
+ const other = atom._valTypes;
51
+ if (types instanceof Type) {
52
+ return atom.valTypes().every((type) => types.accepts(type));
51
53
  }
52
- return false;
54
+ if (other instanceof Type) {
55
+ return types.every((type) => type.isOptional && type.accepts(other));
56
+ }
57
+ return types.every((type, index) => type.accepts(other[index] ?? Type.Null));
53
58
  }
54
- intersect(atom) {
59
+ intersects(atom) {
55
60
  if (!(atom instanceof ArrayAtom)) {
56
61
  return false;
57
62
  }
58
- if (this._undefined || atom._undefined) {
59
- return true;
63
+ const types = this._valTypes;
64
+ const other = atom._valTypes;
65
+ if (types instanceof Type) {
66
+ // Any two uniform arrays can both be empty.
67
+ return other instanceof Type || atom.intersects(this);
60
68
  }
61
- const length = this.isTuple
62
- ? atom.isTuple ? Math.min(this._valTypes.length, atom._valTypes.length) : this._valTypes.length
63
- : atom.isTuple ? atom._valTypes.length : 1;
64
- for (let i = 0; i < length; ++i) {
65
- const type = this.isTuple ? this._valTypes[i] : this._valTypes[0];
66
- const atype = atom.isTuple ? atom._valTypes[i] : atom._valTypes[0];
67
- if (!type.intersect(atype)) {
68
- return (this.isTuple || atom.isTuple)
69
- && this._intersectLength(i)
70
- && atom._intersectLength(i);
69
+ for (let index = 0; index < types.length; ++index) {
70
+ const otherType = other instanceof Type ? other : other[index];
71
+ if (!otherType) {
72
+ break;
73
+ }
74
+ if (!types[index].intersects(otherType)) {
75
+ return types.slice(index).every((type) => type.isOptional) &&
76
+ (other instanceof Type || other.slice(index).every((type) => type.isOptional));
71
77
  }
72
78
  }
73
79
  return true;
@@ -76,9 +82,10 @@ export class ArrayAtom {
76
82
  return 0x10000 + this.valTypes().reduce((acc, i) => acc + i.weight(), 0);
77
83
  }
78
84
  toString() {
79
- if (this._undefined) {
80
- return "array";
85
+ const types = this._valTypes;
86
+ if (types instanceof Type) {
87
+ return types.isAny ? "[]" : `[...${types}]`;
81
88
  }
82
- return `[${this._valTypes.join(",")}]`;
89
+ return `[${types.join(",")}]`;
83
90
  }
84
91
  }
@@ -0,0 +1,7 @@
1
+ export interface IAtom {
2
+ equals(atom: IAtom): boolean;
3
+ accepts(atom: IAtom): boolean;
4
+ intersects(atom: IAtom): boolean;
5
+ weight(): number;
6
+ toString(): string;
7
+ }
@@ -1,22 +1,20 @@
1
- import { IAtom } from "../Atom.js";
1
+ import { IAtom } from "./Atom.js";
2
2
  import { Type } from "../Type.js";
3
+ import { FunctionSignature } from "./FunctionSignature.js";
4
+ export { FunctionSignature };
3
5
  export declare class FunctionAtom implements IAtom {
4
- protected readonly _retType: Type;
5
- protected readonly _argTypes: Type[];
6
- protected readonly _isVariadic: boolean;
7
- protected readonly _minArity: number;
8
- constructor(_retType?: Type, _argTypes?: Type[], _isVariadic?: boolean);
9
- private get _undefined();
10
- get retType(): Type;
11
- get minArity(): number;
12
- get maxArity(): number;
6
+ protected readonly _signatures: readonly FunctionSignature[];
7
+ protected readonly _isVolatile: boolean;
8
+ constructor(signatures: readonly [retType: Type, argTypes: readonly Type[], variadic?: boolean][], volatile?: boolean);
13
9
  get isVariadic(): boolean;
14
- get arity(): number;
10
+ get isVolatile(): boolean;
11
+ signatures(argc?: number): FunctionSignature[];
12
+ retType(): Type;
15
13
  argType(index: number): Type | undefined;
16
- paramType(index: number): Type | undefined;
17
- equate(atom: IAtom): boolean;
18
- accept(atom: IAtom): boolean;
19
- intersect(atom: IAtom): boolean;
14
+ firstArgOptional(): FunctionAtom;
15
+ equals(atom: IAtom): boolean;
16
+ accepts(atom: IAtom): boolean;
17
+ intersects(atom: IAtom): boolean;
20
18
  weight(): number;
21
19
  toString(): string;
22
20
  }
@@ -1,125 +1,74 @@
1
1
  import { Type } from "../Type.js";
2
+ import { FunctionSignature } from "./FunctionSignature.js";
3
+ export { FunctionSignature };
2
4
  export class FunctionAtom {
3
- _retType;
4
- _argTypes;
5
- _isVariadic;
6
- _minArity;
7
- constructor(_retType = Type.Unknown, _argTypes = [], _isVariadic = false) {
8
- this._retType = _retType;
9
- this._argTypes = _argTypes;
10
- this._isVariadic = _isVariadic;
11
- let ix = 0;
12
- while (ix < _argTypes.length && !_argTypes[ix].isOptional) {
13
- ++ix;
5
+ _signatures;
6
+ _isVolatile;
7
+ constructor(signatures, volatile = false) {
8
+ if (!signatures.length) {
9
+ throw new Error("a function must have at least one signature");
14
10
  }
15
- this._minArity = ix;
16
- if (ix < _argTypes.length) {
17
- while (ix < _argTypes.length && _argTypes[ix].isOptional) {
18
- ++ix;
19
- }
20
- if (ix < _argTypes.length) {
21
- throw new Error("a required parameter illegally follows an optional one");
11
+ const unique = [];
12
+ for (const s of signatures) {
13
+ const signature = new FunctionSignature(...s);
14
+ if (!unique.some((i) => i.equate(signature))) {
15
+ unique.push(signature);
22
16
  }
23
17
  }
18
+ this._signatures = unique;
19
+ this._isVolatile = volatile;
24
20
  }
25
- get _undefined() {
26
- return this._retType.isUnknown && this._argTypes.length === 0 && this._isVariadic;
27
- }
28
- get retType() {
29
- return this._retType;
30
- }
31
- get minArity() {
32
- return this._minArity;
21
+ get isVariadic() {
22
+ return this._signatures.every((signature) => signature.isVariadic);
33
23
  }
34
- get maxArity() {
35
- return this._isVariadic ? Number.POSITIVE_INFINITY : this._argTypes.length;
24
+ get isVolatile() {
25
+ return this._isVolatile;
36
26
  }
37
- get isVariadic() {
38
- return this._isVariadic;
27
+ signatures(argc) {
28
+ return this._signatures.filter((signature) => argc === undefined || signature.minArity <= argc && argc <= signature.maxArity);
39
29
  }
40
- get arity() {
41
- return this._argTypes.length;
30
+ retType() {
31
+ return Type.union(...this._signatures.map((signature) => signature.retType));
42
32
  }
43
33
  argType(index) {
44
- return this._argTypes[index] ?? (this._isVariadic ? this._argTypes[this._argTypes.length - 1] : undefined);
34
+ const types = this._signatures.map((signature) => signature.argType(index)).filter((type) => type !== undefined);
35
+ return types.length ? Type.union(...types) : undefined;
45
36
  }
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);
37
+ firstArgOptional() {
38
+ const signatures = this._signatures.flatMap((signature) => {
39
+ const retType = signature.retType.toOptional();
40
+ const argTypes = [...signature.argTypes];
41
+ if (signature.isVariadic && signature.arity === 1) {
42
+ return argTypes[0].arrayAtoms().map((array) => {
43
+ const itemType = array.valTypes()[0];
44
+ return [retType, [itemType.toOptional(), Type.arrayType(itemType)], true];
45
+ });
46
+ }
47
+ if (argTypes.length) {
48
+ argTypes[0] = argTypes[0].toOptional();
49
+ }
50
+ return [[retType, argTypes, signature.isVariadic]];
51
+ });
52
+ return new FunctionAtom(signatures, this._isVolatile);
59
53
  }
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]));
54
+ equals(atom) {
55
+ return atom instanceof FunctionAtom &&
56
+ this._isVolatile === atom._isVolatile &&
57
+ this._signatures.length === atom._signatures.length &&
58
+ this._signatures.every((signature) => atom._signatures.some((i) => signature.equate(i)));
66
59
  }
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))) {
86
- return false;
87
- }
88
- }
89
- return true;
60
+ accepts(atom) {
61
+ return atom instanceof FunctionAtom &&
62
+ this._signatures.every((signature) => atom._signatures.some((i) => signature.accept(i)));
90
63
  }
91
- intersect(atom) {
92
- if (!(atom instanceof FunctionAtom)) {
93
- return false;
94
- }
95
- if (this._undefined || atom._undefined) {
96
- return true;
97
- }
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
+ intersects(atom) {
65
+ return atom instanceof FunctionAtom &&
66
+ this._signatures.some((signature) => atom._signatures.some((i) => signature.intersect(i)));
114
67
  }
115
68
  weight() {
116
- return 0x1000000000000 + this._argTypes.reduce((acc, i) => acc + i.weight(), this._retType.weight());
69
+ return 0x1000000000000 + this._signatures.reduce((acc, signature) => acc + signature.weight(), 0);
117
70
  }
118
71
  toString() {
119
- if (this._undefined) {
120
- return "function";
121
- }
122
- const argTypes = this._argTypes.map((i, ix) => ix === this._argTypes.length - 1 && this.isVariadic ? `...${i.toString()}` : i.toString()).join(",");
123
- return `~(${argTypes}):${this._retType.toString()}`;
72
+ return this._signatures.map((signature) => signature.toString()).join("&");
124
73
  }
125
74
  }
@@ -0,0 +1,23 @@
1
+ import { Type } from "../Type.js";
2
+ export declare class FunctionSignature {
3
+ protected readonly _retType: Type;
4
+ protected readonly _argTypes: readonly Type[];
5
+ protected readonly _minArity: number;
6
+ protected readonly _isVariadic: boolean;
7
+ constructor(retType: Type, argTypes?: readonly Type[], variadic?: boolean);
8
+ get retType(): Type;
9
+ get argTypes(): readonly Type[];
10
+ get minArity(): number;
11
+ get maxArity(): number;
12
+ get arity(): number;
13
+ get isVariadic(): boolean;
14
+ get isGeneric(): boolean;
15
+ argType(index: number): Type | undefined;
16
+ paramType(index: number): Type | undefined;
17
+ acceptsVariadic(argTypes: readonly Type[]): boolean;
18
+ equate(signature: FunctionSignature): boolean;
19
+ accept(signature: FunctionSignature): boolean;
20
+ intersect(signature: FunctionSignature): boolean;
21
+ weight(): number;
22
+ toString(): string;
23
+ }