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.
- package/.cache/arrow-preview-vWvB5K/affinirum-arrow-functions.patch +155 -0
- package/.cache/function-syntax-tests.log +20 -0
- package/README.md +925 -355
- package/dst/Affinirum.d.ts +29 -19
- package/dst/Affinirum.js +399 -334
- package/dst/CallFrame.d.ts +12 -3
- package/dst/CallFrame.js +49 -10
- package/dst/Constant.d.ts +1 -3
- package/dst/Constant.js +3 -8
- package/dst/LexicalScope.d.ts +11 -9
- package/dst/LexicalScope.js +10 -6
- package/dst/ParserSpan.d.ts +11 -0
- package/dst/{ScriptSpan.js → ParserSpan.js} +8 -5
- package/dst/ParserState.d.ts +28 -49
- package/dst/ParserState.js +256 -489
- package/dst/Type.d.ts +34 -17
- package/dst/Type.js +211 -115
- package/dst/TypeParser.d.ts +9 -0
- package/dst/TypeParser.js +127 -0
- package/dst/Value.d.ts +1 -1
- package/dst/Value.js +21 -10
- package/dst/Variable.d.ts +16 -5
- package/dst/Variable.js +32 -7
- package/dst/atom/ArrayAtom.d.ts +10 -10
- package/dst/atom/ArrayAtom.js +62 -55
- package/dst/atom/Atom.d.ts +7 -0
- package/dst/atom/FunctionAtom.d.ts +13 -15
- package/dst/atom/FunctionAtom.js +53 -104
- package/dst/atom/FunctionSignature.d.ts +23 -0
- package/dst/atom/FunctionSignature.js +124 -0
- package/dst/atom/ObjectAtom.d.ts +11 -9
- package/dst/atom/ObjectAtom.js +73 -33
- package/dst/atom/PrimitiveAtom.d.ts +6 -7
- package/dst/atom/PrimitiveAtom.js +7 -10
- package/dst/cjs/Affinirum.d.ts +29 -19
- package/dst/cjs/Affinirum.js +398 -333
- package/dst/cjs/CallFrame.d.ts +12 -3
- package/dst/cjs/CallFrame.js +49 -10
- package/dst/cjs/Constant.d.ts +1 -3
- package/dst/cjs/Constant.js +3 -8
- package/dst/cjs/LexicalScope.d.ts +11 -9
- package/dst/cjs/LexicalScope.js +10 -6
- package/dst/cjs/ParserSpan.d.ts +11 -0
- package/dst/cjs/{ScriptSpan.js → ParserSpan.js} +10 -7
- package/dst/cjs/ParserState.d.ts +28 -49
- package/dst/cjs/ParserState.js +255 -488
- package/dst/cjs/Type.d.ts +34 -17
- package/dst/cjs/Type.js +211 -115
- package/dst/cjs/TypeParser.d.ts +9 -0
- package/dst/cjs/TypeParser.js +131 -0
- package/dst/cjs/Value.d.ts +1 -1
- package/dst/cjs/Value.js +21 -10
- package/dst/cjs/Variable.d.ts +16 -5
- package/dst/cjs/Variable.js +36 -9
- package/dst/cjs/atom/ArrayAtom.d.ts +10 -10
- package/dst/cjs/atom/ArrayAtom.js +62 -55
- package/dst/cjs/atom/Atom.d.ts +7 -0
- package/dst/cjs/atom/FunctionAtom.d.ts +13 -15
- package/dst/cjs/atom/FunctionAtom.js +54 -105
- package/dst/cjs/atom/FunctionSignature.d.ts +23 -0
- package/dst/cjs/atom/FunctionSignature.js +128 -0
- package/dst/cjs/atom/ObjectAtom.d.ts +11 -9
- package/dst/cjs/atom/ObjectAtom.js +73 -33
- package/dst/cjs/atom/PrimitiveAtom.d.ts +6 -7
- package/dst/cjs/atom/PrimitiveAtom.js +7 -10
- package/dst/cjs/dictionary/Constants.d.ts +2 -0
- package/dst/cjs/dictionary/Constants.js +27 -0
- package/dst/cjs/dictionary/Functions.d.ts +2 -0
- package/dst/cjs/{Functions.js → dictionary/Functions.js} +27 -25
- package/dst/cjs/native/Aggregable.js +10 -2
- package/dst/cjs/native/Array.d.ts +2 -3
- package/dst/cjs/native/Array.js +38 -39
- package/dst/cjs/native/Boolean.d.ts +1 -1
- package/dst/cjs/native/Boolean.js +13 -10
- package/dst/cjs/native/Buffer.d.ts +5 -5
- package/dst/cjs/native/Buffer.js +70 -46
- package/dst/cjs/native/Composite.d.ts +5 -0
- package/dst/cjs/native/Composite.js +35 -0
- package/dst/cjs/native/Enumerable.js +20 -8
- package/dst/cjs/native/Float.d.ts +3 -1
- package/dst/cjs/native/Float.js +51 -12
- package/dst/cjs/native/Integer.d.ts +4 -2
- package/dst/cjs/native/Integer.js +60 -25
- package/dst/cjs/native/Iterable.d.ts +0 -2
- package/dst/cjs/native/Iterable.js +9 -28
- package/dst/cjs/native/Number.d.ts +0 -2
- package/dst/cjs/native/Number.js +34 -48
- package/dst/cjs/native/Object.d.ts +1 -1
- package/dst/cjs/native/Object.js +5 -6
- package/dst/cjs/native/String.d.ts +7 -6
- package/dst/cjs/native/String.js +28 -27
- package/dst/cjs/native/Timestamp.d.ts +3 -3
- package/dst/cjs/native/Timestamp.js +34 -36
- package/dst/cjs/native/Unknown.d.ts +3 -3
- package/dst/cjs/native/Unknown.js +48 -17
- package/dst/cjs/native/notation/AN.d.ts +1 -1
- package/dst/cjs/native/notation/AN.js +37 -35
- package/dst/cjs/native/notation/IP.d.ts +1 -1
- package/dst/cjs/native/notation/IP.js +11 -12
- package/dst/cjs/native/notation/JSON.d.ts +1 -1
- package/dst/cjs/native/notation/JSON.js +7 -6
- package/dst/cjs/node/AccessNode.d.ts +24 -0
- package/dst/cjs/node/AccessNode.js +193 -0
- package/dst/cjs/node/ArrayNode.d.ts +7 -4
- package/dst/cjs/node/ArrayNode.js +26 -15
- package/dst/cjs/node/AssignableNode.d.ts +16 -0
- package/dst/cjs/node/AssignableNode.js +18 -0
- package/dst/cjs/node/AssignmentNode.d.ts +20 -0
- package/dst/cjs/node/AssignmentNode.js +66 -0
- package/dst/cjs/node/BlockNode.d.ts +5 -4
- package/dst/cjs/node/BlockNode.js +18 -11
- package/dst/cjs/node/CallNode.d.ts +7 -5
- package/dst/cjs/node/CallNode.js +40 -41
- package/dst/cjs/node/CastNode.d.ts +5 -6
- package/dst/cjs/node/CastNode.js +16 -17
- package/dst/cjs/node/ConstantNode.d.ts +4 -6
- package/dst/cjs/node/ConstantNode.js +9 -17
- package/dst/cjs/node/FinalNode.d.ts +19 -0
- package/dst/cjs/node/FinalNode.js +41 -0
- package/dst/cjs/node/FunctionNode.d.ts +17 -0
- package/dst/cjs/node/FunctionNode.js +51 -0
- package/dst/cjs/node/InvocationNode.d.ts +5 -0
- package/dst/cjs/node/InvocationNode.js +34 -0
- package/dst/cjs/node/JumpNode.d.ts +7 -9
- package/dst/cjs/node/JumpNode.js +12 -19
- package/dst/cjs/node/LoopNode.d.ts +7 -5
- package/dst/cjs/node/LoopNode.js +33 -22
- package/dst/cjs/node/MatchNode.d.ts +17 -0
- package/dst/cjs/node/MatchNode.js +65 -0
- package/dst/cjs/node/Node.d.ts +14 -0
- package/dst/cjs/{Node.js → node/Node.js} +17 -11
- package/dst/cjs/node/ObjectNode.d.ts +7 -4
- package/dst/cjs/node/ObjectNode.js +42 -11
- package/dst/cjs/node/{SwitchNode.d.ts → TernaryNode.d.ts} +6 -6
- package/dst/cjs/node/TernaryNode.js +45 -0
- package/dst/cjs/node/VariableNode.d.ts +10 -6
- package/dst/cjs/node/VariableNode.js +50 -16
- package/dst/dictionary/Constants.d.ts +2 -0
- package/dst/dictionary/Constants.js +24 -0
- package/dst/dictionary/Functions.d.ts +2 -0
- package/dst/{Functions.js → dictionary/Functions.js} +26 -24
- package/dst/native/Aggregable.js +11 -3
- package/dst/native/Array.d.ts +2 -3
- package/dst/native/Array.js +37 -38
- package/dst/native/Boolean.d.ts +1 -1
- package/dst/native/Boolean.js +12 -9
- package/dst/native/Buffer.d.ts +5 -5
- package/dst/native/Buffer.js +66 -42
- package/dst/native/Composite.d.ts +5 -0
- package/dst/native/Composite.js +32 -0
- package/dst/native/Enumerable.js +20 -8
- package/dst/native/Float.d.ts +3 -1
- package/dst/native/Float.js +48 -11
- package/dst/native/Integer.d.ts +4 -2
- package/dst/native/Integer.js +56 -23
- package/dst/native/Iterable.d.ts +0 -2
- package/dst/native/Iterable.js +8 -27
- package/dst/native/Number.d.ts +0 -2
- package/dst/native/Number.js +34 -46
- package/dst/native/Object.d.ts +1 -1
- package/dst/native/Object.js +4 -5
- package/dst/native/String.d.ts +7 -6
- package/dst/native/String.js +24 -24
- package/dst/native/Timestamp.d.ts +3 -3
- package/dst/native/Timestamp.js +31 -33
- package/dst/native/Unknown.d.ts +3 -3
- package/dst/native/Unknown.js +49 -18
- package/dst/native/notation/AN.d.ts +1 -1
- package/dst/native/notation/AN.js +37 -35
- package/dst/native/notation/IP.d.ts +1 -1
- package/dst/native/notation/IP.js +10 -11
- package/dst/native/notation/JSON.d.ts +1 -1
- package/dst/native/notation/JSON.js +6 -5
- package/dst/node/AccessNode.d.ts +24 -0
- package/dst/node/AccessNode.js +189 -0
- package/dst/node/ArrayNode.d.ts +7 -4
- package/dst/node/ArrayNode.js +26 -15
- package/dst/node/AssignableNode.d.ts +16 -0
- package/dst/node/AssignableNode.js +14 -0
- package/dst/node/AssignmentNode.d.ts +20 -0
- package/dst/node/AssignmentNode.js +62 -0
- package/dst/node/BlockNode.d.ts +5 -4
- package/dst/node/BlockNode.js +18 -11
- package/dst/node/CallNode.d.ts +7 -5
- package/dst/node/CallNode.js +40 -41
- package/dst/node/CastNode.d.ts +5 -6
- package/dst/node/CastNode.js +16 -17
- package/dst/node/ConstantNode.d.ts +4 -6
- package/dst/node/ConstantNode.js +9 -17
- package/dst/node/FinalNode.d.ts +19 -0
- package/dst/node/FinalNode.js +37 -0
- package/dst/node/FunctionNode.d.ts +17 -0
- package/dst/node/FunctionNode.js +47 -0
- package/dst/node/InvocationNode.d.ts +5 -0
- package/dst/node/InvocationNode.js +30 -0
- package/dst/node/JumpNode.d.ts +7 -9
- package/dst/node/JumpNode.js +12 -19
- package/dst/node/LoopNode.d.ts +7 -5
- package/dst/node/LoopNode.js +33 -22
- package/dst/node/MatchNode.d.ts +17 -0
- package/dst/node/MatchNode.js +61 -0
- package/dst/node/Node.d.ts +14 -0
- package/dst/node/Node.js +28 -0
- package/dst/node/ObjectNode.d.ts +7 -4
- package/dst/node/ObjectNode.js +42 -11
- package/dst/node/{SwitchNode.d.ts → TernaryNode.d.ts} +6 -6
- package/dst/node/TernaryNode.js +41 -0
- package/dst/node/VariableNode.d.ts +10 -6
- package/dst/node/VariableNode.js +50 -16
- package/package.json +4 -4
- package/dst/Atom.d.ts +0 -7
- package/dst/Constants.d.ts +0 -2
- package/dst/Constants.js +0 -24
- package/dst/Functions.d.ts +0 -2
- package/dst/Jump.d.ts +0 -1
- package/dst/Jump.js +0 -1
- package/dst/JumpException.d.ts +0 -9
- package/dst/JumpException.js +0 -14
- package/dst/Keywords.d.ts +0 -1
- package/dst/Keywords.js +0 -17
- package/dst/Node.d.ts +0 -14
- package/dst/Node.js +0 -22
- package/dst/ScriptSpan.d.ts +0 -10
- package/dst/cjs/Atom.d.ts +0 -7
- package/dst/cjs/Constants.d.ts +0 -2
- package/dst/cjs/Constants.js +0 -27
- package/dst/cjs/Functions.d.ts +0 -2
- package/dst/cjs/Jump.d.ts +0 -1
- package/dst/cjs/Jump.js +0 -2
- package/dst/cjs/JumpException.d.ts +0 -9
- package/dst/cjs/JumpException.js +0 -18
- package/dst/cjs/Keywords.d.ts +0 -1
- package/dst/cjs/Keywords.js +0 -20
- package/dst/cjs/Node.d.ts +0 -14
- package/dst/cjs/ScriptSpan.d.ts +0 -10
- package/dst/cjs/node/SwitchNode.js +0 -49
- package/dst/cjs/node/VariadicNode.d.ts +0 -4
- package/dst/cjs/node/VariadicNode.js +0 -10
- package/dst/node/SwitchNode.js +0 -45
- package/dst/node/VariadicNode.d.ts +0 -4
- package/dst/node/VariadicNode.js +0 -6
- /package/dst/{Atom.js → atom/Atom.js} +0 -0
- /package/dst/cjs/{Atom.js → atom/Atom.js} +0 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { Type } from "../Type.js";
|
|
2
|
+
export class FunctionSignature {
|
|
3
|
+
_retType;
|
|
4
|
+
_argTypes;
|
|
5
|
+
_minArity;
|
|
6
|
+
_isVariadic;
|
|
7
|
+
constructor(retType, argTypes = [], variadic = false) {
|
|
8
|
+
this._retType = retType;
|
|
9
|
+
this._argTypes = [...argTypes];
|
|
10
|
+
this._isVariadic = variadic;
|
|
11
|
+
let minArity = variadic ? Math.max(0, this._argTypes.length - 1) : this._argTypes.length;
|
|
12
|
+
while (minArity > 0 && this._argTypes[minArity - 1].isOptional) {
|
|
13
|
+
--minArity;
|
|
14
|
+
}
|
|
15
|
+
this._minArity = minArity;
|
|
16
|
+
if (this._isVariadic && this._argTypes.length > 0 && !this._argTypes[this._argTypes.length - 1].isUniformArray) {
|
|
17
|
+
throw new Error("variadic function argument must be of uniform array type");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
get retType() {
|
|
21
|
+
return this._retType;
|
|
22
|
+
}
|
|
23
|
+
get argTypes() {
|
|
24
|
+
return this._argTypes;
|
|
25
|
+
}
|
|
26
|
+
get minArity() {
|
|
27
|
+
return this._minArity;
|
|
28
|
+
}
|
|
29
|
+
get maxArity() {
|
|
30
|
+
return this._isVariadic ? Number.POSITIVE_INFINITY : this._argTypes.length;
|
|
31
|
+
}
|
|
32
|
+
get arity() {
|
|
33
|
+
return this._argTypes.length;
|
|
34
|
+
}
|
|
35
|
+
get isVariadic() {
|
|
36
|
+
return this._isVariadic;
|
|
37
|
+
}
|
|
38
|
+
get isGeneric() {
|
|
39
|
+
return this._retType.isAny && this._argTypes.length === 0 && this._isVariadic;
|
|
40
|
+
}
|
|
41
|
+
argType(index) {
|
|
42
|
+
return this._argTypes[index] ?? (this._isVariadic ? this._argTypes[this._argTypes.length - 1] : undefined);
|
|
43
|
+
}
|
|
44
|
+
paramType(index) {
|
|
45
|
+
if (!this._isVariadic) {
|
|
46
|
+
return this.argType(index);
|
|
47
|
+
}
|
|
48
|
+
if (!this._argTypes.length) {
|
|
49
|
+
return Type.Any;
|
|
50
|
+
}
|
|
51
|
+
const variadicIndex = this._argTypes.length - 1;
|
|
52
|
+
if (index < variadicIndex) {
|
|
53
|
+
return this.argType(index);
|
|
54
|
+
}
|
|
55
|
+
return this.argType(variadicIndex)?.arrayAtomValType(index - variadicIndex);
|
|
56
|
+
}
|
|
57
|
+
acceptsVariadic(argTypes) {
|
|
58
|
+
const index = this.isVariadic ? this.arity - 1 : -1;
|
|
59
|
+
if (index < 0) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
const restType = Type.arrayType(argTypes.slice(index));
|
|
63
|
+
const tail = this.argType(index) ?? Type.Any;
|
|
64
|
+
return tail.accepts(restType);
|
|
65
|
+
}
|
|
66
|
+
equate(signature) {
|
|
67
|
+
return this._isVariadic === signature._isVariadic &&
|
|
68
|
+
this._argTypes.length === signature._argTypes.length &&
|
|
69
|
+
this._retType.equals(signature._retType) &&
|
|
70
|
+
this._argTypes.every((type, index) => type.equals(signature._argTypes[index]));
|
|
71
|
+
}
|
|
72
|
+
accept(signature) {
|
|
73
|
+
if (this.isGeneric || signature.isGeneric) {
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
if (this._isVariadic !== signature._isVariadic ||
|
|
77
|
+
this._isVariadic && this.arity !== signature.arity ||
|
|
78
|
+
!this._retType.accepts(signature._retType) ||
|
|
79
|
+
signature.minArity > this.minArity) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
const argc = Math.min(this.arity, signature.arity);
|
|
83
|
+
for (let i = 0; i < argc; ++i) {
|
|
84
|
+
const type = this.argType(i);
|
|
85
|
+
const argType = signature.argType(i);
|
|
86
|
+
if (type && (!argType || !argType.accepts(type))) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
intersect(signature) {
|
|
93
|
+
if (this.isGeneric || signature.isGeneric) {
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
if (this._isVariadic !== signature._isVariadic ||
|
|
97
|
+
this._isVariadic && this.arity !== signature.arity ||
|
|
98
|
+
!this._retType.intersects(signature._retType) ||
|
|
99
|
+
this.minArity > signature.maxArity ||
|
|
100
|
+
this.maxArity < signature.minArity) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
const argc = Math.max(this.minArity, signature.minArity);
|
|
104
|
+
for (let i = 0; i < argc; ++i) {
|
|
105
|
+
const type = this.argType(i);
|
|
106
|
+
const argType = signature.argType(i);
|
|
107
|
+
if (!type || !argType || !type.intersects(argType)) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
weight() {
|
|
114
|
+
return this._argTypes.reduce((acc, type) => acc + type.weight(), this._retType.weight());
|
|
115
|
+
}
|
|
116
|
+
toString() {
|
|
117
|
+
if (this.isGeneric) {
|
|
118
|
+
return "{:}";
|
|
119
|
+
}
|
|
120
|
+
const argTypes = this._argTypes.map((type, ix) => this._isVariadic && ix === this.arity - 1 ? `...${type.arrayAtomValType()}` : type.toString()).join(",");
|
|
121
|
+
const retType = this._retType.toString();
|
|
122
|
+
return `{(${argTypes}):${retType}}`;
|
|
123
|
+
}
|
|
124
|
+
}
|
package/dst/atom/ObjectAtom.d.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import { IAtom } from "
|
|
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: [string, Type][];
|
|
5
|
-
constructor(valTypes?: [string, Type][]);
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
protected readonly _valTypes: Type | readonly (readonly [string, Type])[];
|
|
5
|
+
constructor(valTypes?: Type | readonly (readonly [key: string, valType: Type])[]);
|
|
6
|
+
get isUniform(): boolean;
|
|
7
|
+
get isRecord(): boolean;
|
|
8
|
+
get types(): Type | readonly (readonly [string, Type])[];
|
|
9
|
+
valTypes(): readonly (readonly [string | undefined, Type])[];
|
|
10
|
+
valType(key: string | undefined): Type | undefined;
|
|
11
|
+
equals(atom: IAtom): boolean;
|
|
12
|
+
accepts(atom: IAtom): boolean;
|
|
13
|
+
intersects(atom: IAtom): boolean;
|
|
12
14
|
weight(): number;
|
|
13
15
|
toString(): string;
|
|
14
16
|
}
|
package/dst/atom/ObjectAtom.js
CHANGED
|
@@ -2,60 +2,100 @@ import { Type } from "../Type.js";
|
|
|
2
2
|
export class ObjectAtom {
|
|
3
3
|
_valTypes;
|
|
4
4
|
constructor(valTypes = []) {
|
|
5
|
-
|
|
5
|
+
if (Array.isArray(valTypes)) {
|
|
6
|
+
if (valTypes.length === 0) {
|
|
7
|
+
this._valTypes = Type.Any;
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
const keys = [];
|
|
11
|
+
const types = [];
|
|
12
|
+
for (const [key, type] of valTypes) {
|
|
13
|
+
if (keys.includes(key)) {
|
|
14
|
+
throw Error(`duplicate object key ${key}`);
|
|
15
|
+
}
|
|
16
|
+
keys.push(key);
|
|
17
|
+
types.push([key, type]);
|
|
18
|
+
}
|
|
19
|
+
this._valTypes = types;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
this._valTypes = valTypes;
|
|
24
|
+
}
|
|
6
25
|
}
|
|
7
|
-
get
|
|
8
|
-
return this._valTypes
|
|
26
|
+
get isUniform() {
|
|
27
|
+
return !Array.isArray(this._valTypes);
|
|
9
28
|
}
|
|
10
|
-
|
|
29
|
+
get isRecord() {
|
|
30
|
+
return Array.isArray(this._valTypes);
|
|
31
|
+
}
|
|
32
|
+
get types() {
|
|
11
33
|
return this._valTypes;
|
|
12
34
|
}
|
|
35
|
+
valTypes() {
|
|
36
|
+
const types = this._valTypes;
|
|
37
|
+
return types instanceof Type ? [[undefined, types]] : types;
|
|
38
|
+
}
|
|
13
39
|
valType(key) {
|
|
14
|
-
|
|
40
|
+
const types = this._valTypes;
|
|
41
|
+
return types instanceof Type ? types : types.find(([name]) => name === key)?.[1];
|
|
15
42
|
}
|
|
16
|
-
|
|
43
|
+
equals(atom) {
|
|
17
44
|
if (!(atom instanceof ObjectAtom)) {
|
|
18
45
|
return false;
|
|
19
46
|
}
|
|
20
|
-
|
|
47
|
+
const types = this._valTypes;
|
|
48
|
+
const other = atom._valTypes;
|
|
49
|
+
if (types instanceof Type) {
|
|
50
|
+
return other instanceof Type && types.equals(other);
|
|
51
|
+
}
|
|
52
|
+
return !(other instanceof Type) &&
|
|
53
|
+
types.length === other.length &&
|
|
54
|
+
types.every(([key, type]) => type.equals(atom.valType(key)));
|
|
21
55
|
}
|
|
22
|
-
|
|
23
|
-
if (atom instanceof ObjectAtom) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return true;
|
|
56
|
+
accepts(atom) {
|
|
57
|
+
if (!(atom instanceof ObjectAtom)) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const types = this._valTypes;
|
|
61
|
+
const other = atom._valTypes;
|
|
62
|
+
if (types instanceof Type) {
|
|
63
|
+
return atom.valTypes().every(([, type]) => types.accepts(type));
|
|
34
64
|
}
|
|
35
|
-
|
|
65
|
+
if (other instanceof Type) {
|
|
66
|
+
return types.every(([, type]) => type.isOptional && type.accepts(other));
|
|
67
|
+
}
|
|
68
|
+
return types.every(([key, type]) => type.accepts(atom.valType(key) ?? Type.Null));
|
|
36
69
|
}
|
|
37
|
-
|
|
70
|
+
intersects(atom) {
|
|
38
71
|
if (!(atom instanceof ObjectAtom)) {
|
|
39
72
|
return false;
|
|
40
73
|
}
|
|
41
|
-
|
|
42
|
-
|
|
74
|
+
const types = this._valTypes;
|
|
75
|
+
const other = atom._valTypes;
|
|
76
|
+
if (types instanceof Type) {
|
|
77
|
+
// Any two uniform objects can both be empty.
|
|
78
|
+
return other instanceof Type || atom.intersects(this);
|
|
43
79
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (atype && !type.intersect(atype)) {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
80
|
+
if (other instanceof Type) {
|
|
81
|
+
return types.every(([, type]) => type.isOptional || type.intersects(other));
|
|
49
82
|
}
|
|
50
|
-
return
|
|
83
|
+
return types.every(([key, type]) => {
|
|
84
|
+
const otherType = atom.valType(key);
|
|
85
|
+
return !otherType || type.intersects(otherType) || type.isOptional && otherType.isOptional;
|
|
86
|
+
});
|
|
51
87
|
}
|
|
52
88
|
weight() {
|
|
53
|
-
return 0x100000000 + this.
|
|
89
|
+
return 0x100000000 + this.valTypes().reduce((acc, [, type]) => acc + type.weight(), 0);
|
|
54
90
|
}
|
|
55
91
|
toString() {
|
|
56
|
-
|
|
57
|
-
|
|
92
|
+
const types = this._valTypes;
|
|
93
|
+
if (types instanceof Type) {
|
|
94
|
+
return types.isAny ? "[:]" : `[...:${types}]`;
|
|
58
95
|
}
|
|
59
|
-
return `[${
|
|
96
|
+
return `[${types.map(([key, type]) => {
|
|
97
|
+
const normalized = key.replace(/[\\"]/g, "\\$&");
|
|
98
|
+
return `"${normalized}":${type}`;
|
|
99
|
+
}).join(",")}]`;
|
|
60
100
|
}
|
|
61
101
|
}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { IAtom } from "
|
|
2
|
-
export type Primitive = "
|
|
1
|
+
import { IAtom } from "./Atom.js";
|
|
2
|
+
export type Primitive = "null" | "boolean" | "timestamp" | "float" | "integer" | "buffer" | "string";
|
|
3
3
|
export declare class PrimitiveAtom implements IAtom {
|
|
4
4
|
protected readonly _primitive: Primitive;
|
|
5
|
-
constructor(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
intersect(atom: IAtom): boolean;
|
|
5
|
+
constructor(primitive?: Primitive);
|
|
6
|
+
equals(atom: IAtom): boolean;
|
|
7
|
+
accepts(atom: IAtom): boolean;
|
|
8
|
+
intersects(atom: IAtom): boolean;
|
|
10
9
|
weight(): number;
|
|
11
10
|
toString(): string;
|
|
12
11
|
}
|
|
@@ -1,29 +1,26 @@
|
|
|
1
1
|
export class PrimitiveAtom {
|
|
2
2
|
_primitive;
|
|
3
|
-
constructor(
|
|
4
|
-
this._primitive =
|
|
3
|
+
constructor(primitive = "null") {
|
|
4
|
+
this._primitive = primitive;
|
|
5
5
|
}
|
|
6
|
-
|
|
7
|
-
return this._primitive === "void";
|
|
8
|
-
}
|
|
9
|
-
equate(atom) {
|
|
6
|
+
equals(atom) {
|
|
10
7
|
if (atom instanceof PrimitiveAtom) {
|
|
11
8
|
return atom._primitive === this._primitive;
|
|
12
9
|
}
|
|
13
10
|
return false;
|
|
14
11
|
}
|
|
15
|
-
|
|
12
|
+
accepts(atom) {
|
|
16
13
|
if (atom instanceof PrimitiveAtom) {
|
|
17
14
|
return atom._primitive === this._primitive;
|
|
18
15
|
}
|
|
19
16
|
return false;
|
|
20
17
|
}
|
|
21
|
-
|
|
22
|
-
return this.
|
|
18
|
+
intersects(atom) {
|
|
19
|
+
return this.equals(atom);
|
|
23
20
|
}
|
|
24
21
|
weight() {
|
|
25
22
|
switch (this._primitive) {
|
|
26
|
-
case "
|
|
23
|
+
case "null": return 0x1;
|
|
27
24
|
case "boolean": return 0x2;
|
|
28
25
|
case "timestamp": return 0x4;
|
|
29
26
|
case "float": return 0x8;
|
package/dst/cjs/Affinirum.d.ts
CHANGED
|
@@ -1,34 +1,39 @@
|
|
|
1
|
-
import { Constant } from "./Constant.js";
|
|
2
|
-
import { Variable } from "./Variable.js";
|
|
3
|
-
import { Value } from "./Value.js";
|
|
4
1
|
import { Type } from "./Type.js";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
2
|
+
import { Value } from "./Value.js";
|
|
3
|
+
import { Constant } from "./Constant.js";
|
|
4
|
+
import { InputVariable } from "./Variable.js";
|
|
5
|
+
import { ParserSpan } from "./ParserSpan.js";
|
|
9
6
|
import { ParserState } from "./ParserState.js";
|
|
10
7
|
import { LexicalScope } from "./LexicalScope.js";
|
|
8
|
+
import { TypeParser } from "./TypeParser.js";
|
|
9
|
+
import { Node } from "./node/Node.js";
|
|
10
|
+
import { FinalNode } from "./node/FinalNode.js";
|
|
11
11
|
export declare class Affinirum {
|
|
12
12
|
static readonly keywords: string[];
|
|
13
13
|
protected readonly _script: string;
|
|
14
14
|
protected readonly _strict: boolean;
|
|
15
|
+
protected readonly _timeout: number;
|
|
16
|
+
protected readonly _scope: LexicalScope;
|
|
15
17
|
protected readonly _root: Node;
|
|
18
|
+
protected readonly _type: TypeParser;
|
|
16
19
|
protected readonly _vartypes: Map<string, Type>;
|
|
17
|
-
protected readonly _varspans: Map<string,
|
|
18
|
-
protected readonly _variables: Map<string,
|
|
20
|
+
protected readonly _varspans: Map<string, ParserSpan>;
|
|
21
|
+
protected readonly _variables: Map<string, InputVariable>;
|
|
19
22
|
protected readonly _constants: Map<string, Record<string, Constant>>;
|
|
20
23
|
protected readonly _functions: Map<string, Constant>;
|
|
21
|
-
protected
|
|
24
|
+
protected _results: FinalNode[];
|
|
22
25
|
/**
|
|
23
|
-
Creates compiled expression. Any parsed
|
|
26
|
+
Creates compiled expression. Any parsed identifier not recognized as a constant or a function will be compiled as a variable.
|
|
24
27
|
@param script Math expression to compile.
|
|
25
|
-
@param config Optional expected type, strict mode,
|
|
28
|
+
@param config Optional expected type, strict mode, default evaluation timeout in milliseconds, and variable types.
|
|
26
29
|
If expected type is provided then expression return type is matched against it.
|
|
27
30
|
If strict mode is set then undeclared variables will not be allowed in expression.
|
|
31
|
+
The default timeout is zero, which disables the timeout.
|
|
28
32
|
*/
|
|
29
33
|
constructor(script: string, config?: {
|
|
30
34
|
type?: Type;
|
|
31
35
|
strict?: boolean;
|
|
36
|
+
timeout?: number;
|
|
32
37
|
variables?: Record<string, Type>;
|
|
33
38
|
});
|
|
34
39
|
/**
|
|
@@ -45,17 +50,17 @@ export declare class Affinirum {
|
|
|
45
50
|
*/
|
|
46
51
|
toString(): string;
|
|
47
52
|
/**
|
|
48
|
-
Returns
|
|
49
|
-
@returns Record with variable names and types.
|
|
53
|
+
Returns the configured or inferred type of each referenced input variable.
|
|
50
54
|
*/
|
|
51
55
|
variables(): Record<string, Type>;
|
|
52
56
|
/**
|
|
53
57
|
Evaluates the compiled expression using the provided input values.
|
|
54
58
|
@param values Values for the expression's input variables. Omitted when there are no input variables.
|
|
59
|
+
@param timeout Evaluation timeout in milliseconds. Omitted or non-positive values fallback to the configured default.
|
|
55
60
|
@returns The evaluated result.
|
|
56
61
|
@throws If an input value is missing or evaluation fails.
|
|
57
62
|
*/
|
|
58
|
-
evaluate(values?: Record<string, Value
|
|
63
|
+
evaluate(values?: Record<string, Value>, timeout?: number): Value;
|
|
59
64
|
/** Parses an Affinirum Notation string into a JavaScript value.
|
|
60
65
|
@param value Affinirum Notation string to parse.
|
|
61
66
|
@returns The parsed value.
|
|
@@ -69,20 +74,25 @@ export declare class Affinirum {
|
|
|
69
74
|
*/
|
|
70
75
|
static format(value: Value, whitespace?: string): string;
|
|
71
76
|
protected _list(state: ParserState, scope: LexicalScope): Node;
|
|
77
|
+
protected _declaration(state: ParserState, scope: LexicalScope): Node;
|
|
72
78
|
protected _unit(state: ParserState, scope: LexicalScope): Node;
|
|
73
79
|
protected _coalescence(state: ParserState, scope: LexicalScope): Node;
|
|
74
80
|
protected _disjunction(state: ParserState, scope: LexicalScope): Node;
|
|
75
81
|
protected _conjunction(state: ParserState, scope: LexicalScope): Node;
|
|
76
82
|
protected _negation(state: ParserState, scope: LexicalScope): Node;
|
|
77
83
|
protected _comparison(state: ParserState, scope: LexicalScope): Node;
|
|
84
|
+
protected _range(state: ParserState, scope: LexicalScope): Node;
|
|
78
85
|
protected _aggregate(state: ParserState, scope: LexicalScope): Node;
|
|
79
86
|
protected _product(state: ParserState, scope: LexicalScope): Node;
|
|
80
87
|
protected _signum(state: ParserState, scope: LexicalScope): Node;
|
|
81
88
|
protected _factor(state: ParserState, scope: LexicalScope): Node;
|
|
82
|
-
protected
|
|
89
|
+
protected _postfix(state: ParserState, scope: LexicalScope): Node;
|
|
83
90
|
protected _term(state: ParserState, scope: LexicalScope): Node;
|
|
91
|
+
protected _identifier(state: ParserState, scope: LexicalScope): Node;
|
|
84
92
|
protected _function(state: ParserState, scope: LexicalScope): Node;
|
|
85
|
-
protected
|
|
86
|
-
protected
|
|
87
|
-
protected
|
|
93
|
+
protected _block(state: ParserState, scope: LexicalScope): Node;
|
|
94
|
+
protected _composite(state: ParserState, scope: LexicalScope): Node;
|
|
95
|
+
protected _ternary(state: ParserState, scope: LexicalScope): Node;
|
|
96
|
+
protected _match(state: ParserState, scope: LexicalScope): Node;
|
|
97
|
+
protected _loop(state: ParserState, scope: LexicalScope): Node;
|
|
88
98
|
}
|