affinirum 1.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/LICENSE +661 -0
- package/README.md +301 -0
- package/dst/Affinirum.d.ts +73 -0
- package/dst/Affinirum.js +536 -0
- package/dst/Constant.d.ts +15 -0
- package/dst/Constant.js +26 -0
- package/dst/Constants.d.ts +2 -0
- package/dst/Constants.js +22 -0
- package/dst/Functions.d.ts +2 -0
- package/dst/Functions.js +85 -0
- package/dst/Keywords.d.ts +1 -0
- package/dst/Keywords.js +17 -0
- package/dst/Node.d.ts +13 -0
- package/dst/Node.js +22 -0
- package/dst/ParserFrame.d.ts +13 -0
- package/dst/ParserFrame.js +80 -0
- package/dst/ParserState.d.ts +54 -0
- package/dst/ParserState.js +562 -0
- package/dst/StaticScope.d.ts +13 -0
- package/dst/StaticScope.js +44 -0
- package/dst/Type.d.ts +72 -0
- package/dst/Type.js +196 -0
- package/dst/Value.d.ts +3 -0
- package/dst/Value.js +1 -0
- package/dst/Variable.d.ts +13 -0
- package/dst/Variable.js +25 -0
- package/dst/atom/ArrayAtom.d.ts +11 -0
- package/dst/atom/ArrayAtom.js +38 -0
- package/dst/atom/FunctionAtom.d.ts +17 -0
- package/dst/atom/FunctionAtom.js +54 -0
- package/dst/atom/ObjectAtom.d.ts +11 -0
- package/dst/atom/ObjectAtom.js +44 -0
- package/dst/atom/PrimitiveAtom.d.ts +8 -0
- package/dst/atom/PrimitiveAtom.js +26 -0
- package/dst/cjs/Affinirum.js +540 -0
- package/dst/cjs/Constant.js +30 -0
- package/dst/cjs/Constants.js +25 -0
- package/dst/cjs/Functions.js +88 -0
- package/dst/cjs/Keywords.js +20 -0
- package/dst/cjs/Node.js +26 -0
- package/dst/cjs/ParserFrame.js +84 -0
- package/dst/cjs/ParserState.js +566 -0
- package/dst/cjs/StaticScope.js +48 -0
- package/dst/cjs/Type.js +200 -0
- package/dst/cjs/Value.js +2 -0
- package/dst/cjs/Variable.js +29 -0
- package/dst/cjs/atom/ArrayAtom.js +42 -0
- package/dst/cjs/atom/FunctionAtom.js +58 -0
- package/dst/cjs/atom/ObjectAtom.js +48 -0
- package/dst/cjs/atom/PrimitiveAtom.js +30 -0
- package/dst/cjs/constant/Array.js +93 -0
- package/dst/cjs/constant/Boolean.js +25 -0
- package/dst/cjs/constant/Buffer.js +68 -0
- package/dst/cjs/constant/Enumerable.js +34 -0
- package/dst/cjs/constant/Float.js +85 -0
- package/dst/cjs/constant/Integer.js +104 -0
- package/dst/cjs/constant/Iterable.js +24 -0
- package/dst/cjs/constant/Number.js +59 -0
- package/dst/cjs/constant/Object.js +13 -0
- package/dst/cjs/constant/String.js +197 -0
- package/dst/cjs/constant/Timestamp.js +56 -0
- package/dst/cjs/constant/Unknown.js +101 -0
- package/dst/cjs/constant/notation/AN.js +46 -0
- package/dst/cjs/constant/notation/JSON.js +14 -0
- package/dst/cjs/index.js +19 -0
- package/dst/cjs/node/ArrayNode.js +34 -0
- package/dst/cjs/node/BlockNode.js +33 -0
- package/dst/cjs/node/CallNode.js +54 -0
- package/dst/cjs/node/ConstantNode.js +33 -0
- package/dst/cjs/node/LoopNode.js +34 -0
- package/dst/cjs/node/ObjectNode.js +42 -0
- package/dst/cjs/node/SwitchNode.js +45 -0
- package/dst/cjs/node/VariableNode.js +30 -0
- package/dst/cjs/package.json +3 -0
- package/dst/constant/Array.d.ts +22 -0
- package/dst/constant/Array.js +90 -0
- package/dst/constant/Boolean.d.ts +11 -0
- package/dst/constant/Boolean.js +22 -0
- package/dst/constant/Buffer.d.ts +10 -0
- package/dst/constant/Buffer.js +61 -0
- package/dst/constant/Enumerable.d.ts +5 -0
- package/dst/constant/Enumerable.js +31 -0
- package/dst/constant/Float.d.ts +22 -0
- package/dst/constant/Float.js +80 -0
- package/dst/constant/Integer.d.ts +10 -0
- package/dst/constant/Integer.js +100 -0
- package/dst/constant/Iterable.d.ts +3 -0
- package/dst/constant/Iterable.js +21 -0
- package/dst/constant/Number.d.ts +14 -0
- package/dst/constant/Number.js +56 -0
- package/dst/constant/Object.d.ts +7 -0
- package/dst/constant/Object.js +10 -0
- package/dst/constant/String.d.ts +34 -0
- package/dst/constant/String.js +179 -0
- package/dst/constant/Timestamp.d.ts +20 -0
- package/dst/constant/Timestamp.js +50 -0
- package/dst/constant/Unknown.d.ts +10 -0
- package/dst/constant/Unknown.js +95 -0
- package/dst/constant/notation/AN.d.ts +6 -0
- package/dst/constant/notation/AN.js +42 -0
- package/dst/constant/notation/JSON.d.ts +7 -0
- package/dst/constant/notation/JSON.js +10 -0
- package/dst/index.d.ts +3 -0
- package/dst/index.js +3 -0
- package/dst/node/ArrayNode.d.ts +12 -0
- package/dst/node/ArrayNode.js +30 -0
- package/dst/node/BlockNode.d.ts +12 -0
- package/dst/node/BlockNode.js +29 -0
- package/dst/node/CallNode.d.ts +14 -0
- package/dst/node/CallNode.js +50 -0
- package/dst/node/ConstantNode.d.ts +15 -0
- package/dst/node/ConstantNode.js +29 -0
- package/dst/node/LoopNode.d.ts +13 -0
- package/dst/node/LoopNode.js +30 -0
- package/dst/node/ObjectNode.d.ts +13 -0
- package/dst/node/ObjectNode.js +38 -0
- package/dst/node/SwitchNode.d.ts +14 -0
- package/dst/node/SwitchNode.js +41 -0
- package/dst/node/VariableNode.d.ts +14 -0
- package/dst/node/VariableNode.js +26 -0
- package/package.json +67 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StaticScope = void 0;
|
|
4
|
+
class StaticScope {
|
|
5
|
+
_superscope;
|
|
6
|
+
_subscopes = [];
|
|
7
|
+
_variables = new Map();
|
|
8
|
+
_locals = new Set();
|
|
9
|
+
has(name) {
|
|
10
|
+
return this._variables.has(name) || Boolean(this._superscope?.has(name));
|
|
11
|
+
}
|
|
12
|
+
get(name) {
|
|
13
|
+
return this._variables.get(name) ?? this._superscope?.get(name);
|
|
14
|
+
}
|
|
15
|
+
global(name, variable) {
|
|
16
|
+
if (this._superscope) {
|
|
17
|
+
this._superscope.global(name, variable);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
this._variables.set(name, variable);
|
|
21
|
+
}
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
local(name, variable) {
|
|
25
|
+
this._variables.set(name, variable);
|
|
26
|
+
this._locals.add(name);
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
subscope(variables) {
|
|
30
|
+
const scope = new StaticScope();
|
|
31
|
+
scope._superscope = this;
|
|
32
|
+
this._subscopes.push(scope);
|
|
33
|
+
for (const [name, variable] of variables) {
|
|
34
|
+
scope.local(name, variable);
|
|
35
|
+
}
|
|
36
|
+
return scope;
|
|
37
|
+
}
|
|
38
|
+
variables() {
|
|
39
|
+
const variables = {};
|
|
40
|
+
for (const [name, variable] of this._variables) {
|
|
41
|
+
if (!this._locals.has(name)) {
|
|
42
|
+
variables[name] = variable;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return variables;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.StaticScope = StaticScope;
|
package/dst/cjs/Type.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Type = void 0;
|
|
4
|
+
const PrimitiveAtom_js_1 = require("./atom/PrimitiveAtom.js");
|
|
5
|
+
const ArrayAtom_js_1 = require("./atom/ArrayAtom.js");
|
|
6
|
+
const ObjectAtom_js_1 = require("./atom/ObjectAtom.js");
|
|
7
|
+
const FunctionAtom_js_1 = require("./atom/FunctionAtom.js");
|
|
8
|
+
class Type {
|
|
9
|
+
_atoms;
|
|
10
|
+
constructor(_atoms = []) {
|
|
11
|
+
this._atoms = _atoms;
|
|
12
|
+
}
|
|
13
|
+
get isUnknown() {
|
|
14
|
+
return this._atoms.length === 0;
|
|
15
|
+
}
|
|
16
|
+
get isAtom() {
|
|
17
|
+
return this._atoms.length === 1;
|
|
18
|
+
}
|
|
19
|
+
get isOptional() {
|
|
20
|
+
return this.isUnknown || this._atoms.length > 1 && this._atoms.some((i) => i.match(Type.Void._atoms[0]));
|
|
21
|
+
}
|
|
22
|
+
get isVoid() {
|
|
23
|
+
return this.isAtom && this._atoms[0].match(Type.Void._atoms[0]);
|
|
24
|
+
}
|
|
25
|
+
get isBoolean() {
|
|
26
|
+
return this.isAtom && this._atoms[0].match(Type.Boolean._atoms[0]);
|
|
27
|
+
}
|
|
28
|
+
get isTimestamp() {
|
|
29
|
+
return this.isAtom && this._atoms[0].match(Type.Timestamp._atoms[0]);
|
|
30
|
+
}
|
|
31
|
+
get isFloat() {
|
|
32
|
+
return this.isAtom && this._atoms[0].match(Type.Float._atoms[0]);
|
|
33
|
+
}
|
|
34
|
+
get isInteger() {
|
|
35
|
+
return this.isAtom && this._atoms[0].match(Type.Integer._atoms[0]);
|
|
36
|
+
}
|
|
37
|
+
get isBuffer() {
|
|
38
|
+
return this.isAtom && this._atoms[0].match(Type.Buffer._atoms[0]);
|
|
39
|
+
}
|
|
40
|
+
get isString() {
|
|
41
|
+
return this.isAtom && this._atoms[0].match(Type.String._atoms[0]);
|
|
42
|
+
}
|
|
43
|
+
get isArray() {
|
|
44
|
+
return this._atoms.every((i) => i instanceof ArrayAtom_js_1.ArrayAtom);
|
|
45
|
+
}
|
|
46
|
+
get isObject() {
|
|
47
|
+
return this._atoms.every((i) => i instanceof ObjectAtom_js_1.ObjectAtom);
|
|
48
|
+
}
|
|
49
|
+
get isFunction() {
|
|
50
|
+
return this._atoms.every((i) => i instanceof FunctionAtom_js_1.FunctionAtom);
|
|
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;
|
|
58
|
+
}
|
|
59
|
+
mergeFunctionAtomRetType(type) {
|
|
60
|
+
const atoms = this._atoms.filter((i) => i instanceof FunctionAtom_js_1.FunctionAtom && (!i.retType || i.retType.match(type)));
|
|
61
|
+
return Type.union(...atoms.map((i) => i.retType ?? Type.Unknown));
|
|
62
|
+
}
|
|
63
|
+
mergeFunctionAtom(type, argc) {
|
|
64
|
+
if (this.isUnknown || type.isVoid) {
|
|
65
|
+
return Type._functionAtom(type, Array.from({ length: argc }).map(() => Type.Unknown), false);
|
|
66
|
+
}
|
|
67
|
+
const atoms = this._atoms.filter((i) => i instanceof FunctionAtom_js_1.FunctionAtom && i.minArity <= argc && i.maxArity >= argc && (!i.retType || i.retType.match(type)));
|
|
68
|
+
if (!atoms.length) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
return Type._functionAtom(Type.union(...atoms.map((i) => i.retType ?? Type.Unknown)), Array.from({ length: argc }).map((_, ix) => Type.union(...atoms.map((i) => i.argType(ix)))), atoms.some((i) => i.isVariadic));
|
|
72
|
+
}
|
|
73
|
+
reduce(mask) {
|
|
74
|
+
if (mask.isUnknown || mask.isVoid) {
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
if (this.isUnknown) {
|
|
78
|
+
return mask;
|
|
79
|
+
}
|
|
80
|
+
const list = this._atoms.filter((i) => mask.match(i));
|
|
81
|
+
return list.length === 0
|
|
82
|
+
? undefined
|
|
83
|
+
: list.length === this._atoms.length
|
|
84
|
+
? this
|
|
85
|
+
: new Type(list);
|
|
86
|
+
}
|
|
87
|
+
match(type) {
|
|
88
|
+
if (type instanceof Type) {
|
|
89
|
+
return this.isUnknown || type.isUnknown || type.isVoid || this._atoms.some((i) => type._atoms.some((j) => i.match(j)));
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
return this._atoms.some((i) => i.match(type));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
weight() {
|
|
96
|
+
return this._atoms.reduce((acc, i) => acc + i.weight(), 0);
|
|
97
|
+
}
|
|
98
|
+
toString() {
|
|
99
|
+
return this._atoms.length
|
|
100
|
+
? this._atoms.sort((a, b) => a.weight() - b.weight()).map((i) => i.toString()).join('|')
|
|
101
|
+
: '??';
|
|
102
|
+
}
|
|
103
|
+
static union(...types) {
|
|
104
|
+
if (types.some((i) => i.isUnknown)) {
|
|
105
|
+
return Type.Unknown;
|
|
106
|
+
}
|
|
107
|
+
const type = new Type([]);
|
|
108
|
+
types.map((i) => i._atoms).flat().forEach((i) => {
|
|
109
|
+
if (!type.match(i)) {
|
|
110
|
+
type._atoms.push(i);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
return type;
|
|
114
|
+
}
|
|
115
|
+
static of(value) {
|
|
116
|
+
return value == null
|
|
117
|
+
? Type.Void
|
|
118
|
+
: typeof value === 'boolean'
|
|
119
|
+
? Type.Boolean
|
|
120
|
+
: value instanceof Date
|
|
121
|
+
? Type.Timestamp
|
|
122
|
+
: typeof value === 'number'
|
|
123
|
+
? Type.Float
|
|
124
|
+
: typeof value === 'bigint'
|
|
125
|
+
? Type.Integer
|
|
126
|
+
: value instanceof ArrayBuffer
|
|
127
|
+
? Type.Buffer
|
|
128
|
+
: typeof value === 'string'
|
|
129
|
+
? Type.String
|
|
130
|
+
: Array.isArray(value)
|
|
131
|
+
? Type.Array
|
|
132
|
+
: typeof value === 'object'
|
|
133
|
+
? Type.Object
|
|
134
|
+
: Type.Function;
|
|
135
|
+
}
|
|
136
|
+
static isPrimitiveType(value) {
|
|
137
|
+
return value == null
|
|
138
|
+
|| typeof value === 'boolean'
|
|
139
|
+
|| value instanceof Date
|
|
140
|
+
|| typeof value === 'number'
|
|
141
|
+
|| typeof value === 'bigint'
|
|
142
|
+
|| value instanceof ArrayBuffer
|
|
143
|
+
|| typeof value === 'string';
|
|
144
|
+
}
|
|
145
|
+
static arrayType(itemTypes = []) {
|
|
146
|
+
return new Type([new ArrayAtom_js_1.ArrayAtom(itemTypes)]);
|
|
147
|
+
}
|
|
148
|
+
static objectType(propTypes = {}) {
|
|
149
|
+
return new Type([new ObjectAtom_js_1.ObjectAtom(propTypes)]);
|
|
150
|
+
}
|
|
151
|
+
static functionType(retType, argTypes = [], isVariadic = false) {
|
|
152
|
+
return new Type([Type._functionAtom(retType, argTypes, isVariadic)]);
|
|
153
|
+
}
|
|
154
|
+
static Unknown = new Type();
|
|
155
|
+
static Void = Type._primitiveType('void');
|
|
156
|
+
static Boolean = Type._primitiveType('boolean');
|
|
157
|
+
static OptionalBoolean = Type.union(Type.Void, Type.Boolean);
|
|
158
|
+
static Timestamp = Type._primitiveType('timestamp');
|
|
159
|
+
static OptionalTimestamp = Type.union(Type.Void, Type.Timestamp);
|
|
160
|
+
static Float = Type._primitiveType('float');
|
|
161
|
+
static OptionalFloat = Type.union(Type.Void, Type.Float);
|
|
162
|
+
static Integer = Type._primitiveType('integer');
|
|
163
|
+
static OptionalInteger = Type.union(Type.Void, Type.Integer);
|
|
164
|
+
static Buffer = Type._primitiveType('buffer');
|
|
165
|
+
static OptionalBuffer = Type.union(Type.Void, Type.Buffer);
|
|
166
|
+
static String = Type._primitiveType('string');
|
|
167
|
+
static OptionalString = Type.union(Type.Void, Type.String);
|
|
168
|
+
static Array = Type.arrayType();
|
|
169
|
+
static OptionalArray = Type.union(Type.Void, Type.Array);
|
|
170
|
+
static Object = Type.objectType();
|
|
171
|
+
static OptionalObject = Type.union(Type.Void, Type.Object);
|
|
172
|
+
static Function = Type.functionType();
|
|
173
|
+
static OptionalFunction = Type.union(Type.Void, Type.Function);
|
|
174
|
+
static Number = Type.union(Type.Float, Type.Integer);
|
|
175
|
+
static OptionalNumber = Type.union(Type.Void, Type.Float, Type.Integer);
|
|
176
|
+
static Enumerable = Type.union(Type.Buffer, Type.String, Type.Array);
|
|
177
|
+
static OptionalEnumerable = Type.union(Type.Void, Type.Buffer, Type.String, Type.Array);
|
|
178
|
+
static Iterable = Type.union(Type.Buffer, Type.String, Type.Array, Type.Object);
|
|
179
|
+
static OptionalIterable = Type.union(Type.Void, Type.Buffer, Type.String, Type.Array, Type.Object);
|
|
180
|
+
static _primitiveType(primitive) {
|
|
181
|
+
return new Type([new PrimitiveAtom_js_1.PrimitiveAtom(primitive)]);
|
|
182
|
+
}
|
|
183
|
+
static _functionAtom(retType, argTypes = [], isVariadic = false) {
|
|
184
|
+
let ix = 0;
|
|
185
|
+
while (ix < argTypes.length && !argTypes[ix].isOptional) {
|
|
186
|
+
++ix;
|
|
187
|
+
}
|
|
188
|
+
const minArity = ix;
|
|
189
|
+
if (ix < argTypes.length) {
|
|
190
|
+
while (ix < argTypes.length && argTypes[ix].isOptional) {
|
|
191
|
+
++ix;
|
|
192
|
+
}
|
|
193
|
+
if (ix < argTypes.length) {
|
|
194
|
+
throw new Error('a required parameter illegally follows an optional one');
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return new FunctionAtom_js_1.FunctionAtom(retType, argTypes, minArity, isVariadic);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
exports.Type = Type;
|
package/dst/cjs/Value.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Variable = void 0;
|
|
4
|
+
const Type_js_1 = require("./Type.js");
|
|
5
|
+
class Variable {
|
|
6
|
+
_type;
|
|
7
|
+
_constant;
|
|
8
|
+
_value;
|
|
9
|
+
constructor(_type = Type_js_1.Type.Unknown, _constant = false) {
|
|
10
|
+
this._type = _type;
|
|
11
|
+
this._constant = _constant;
|
|
12
|
+
}
|
|
13
|
+
get value() {
|
|
14
|
+
return this._value;
|
|
15
|
+
}
|
|
16
|
+
set value(value) {
|
|
17
|
+
this._value = value;
|
|
18
|
+
}
|
|
19
|
+
get type() {
|
|
20
|
+
return this._type;
|
|
21
|
+
}
|
|
22
|
+
set type(type) {
|
|
23
|
+
this._type = type;
|
|
24
|
+
}
|
|
25
|
+
get constant() {
|
|
26
|
+
return this._constant;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.Variable = Variable;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ArrayAtom = void 0;
|
|
4
|
+
class ArrayAtom {
|
|
5
|
+
_itemTypes;
|
|
6
|
+
constructor(_itemTypes = []) {
|
|
7
|
+
this._itemTypes = _itemTypes;
|
|
8
|
+
}
|
|
9
|
+
get empty() {
|
|
10
|
+
return this._itemTypes.length === 0;
|
|
11
|
+
}
|
|
12
|
+
subtypes() {
|
|
13
|
+
return this._itemTypes;
|
|
14
|
+
}
|
|
15
|
+
itemType(index) {
|
|
16
|
+
return this._itemTypes[index];
|
|
17
|
+
}
|
|
18
|
+
match(type) {
|
|
19
|
+
if (type instanceof ArrayAtom) {
|
|
20
|
+
if (this.empty || type.empty) {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
if (this.subtypes().length !== type.subtypes().length) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
for (let i = 0; i < this._itemTypes.length; ++i) {
|
|
27
|
+
if (!this._itemTypes[i].match(type._itemTypes[i])) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
weight() {
|
|
36
|
+
return 0x10000 + this.subtypes().reduce((acc, i) => acc + i.weight(), 0);
|
|
37
|
+
}
|
|
38
|
+
toString() {
|
|
39
|
+
return `[${this._itemTypes.join(',')}]`;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.ArrayAtom = ArrayAtom;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FunctionAtom = void 0;
|
|
4
|
+
class FunctionAtom {
|
|
5
|
+
_retType;
|
|
6
|
+
_argTypes;
|
|
7
|
+
_minArity;
|
|
8
|
+
_isVariadic;
|
|
9
|
+
constructor(_retType, _argTypes = [], _minArity = _argTypes.length, _isVariadic = false) {
|
|
10
|
+
this._retType = _retType;
|
|
11
|
+
this._argTypes = _argTypes;
|
|
12
|
+
this._minArity = _minArity;
|
|
13
|
+
this._isVariadic = _isVariadic;
|
|
14
|
+
}
|
|
15
|
+
get retType() {
|
|
16
|
+
return this._retType;
|
|
17
|
+
}
|
|
18
|
+
get minArity() {
|
|
19
|
+
return this._minArity;
|
|
20
|
+
}
|
|
21
|
+
get maxArity() {
|
|
22
|
+
return this._isVariadic ? Number.POSITIVE_INFINITY : this._argTypes.length;
|
|
23
|
+
}
|
|
24
|
+
get isVariadic() {
|
|
25
|
+
return this._isVariadic;
|
|
26
|
+
}
|
|
27
|
+
subtypes() {
|
|
28
|
+
return this._retType ? [this._retType, ...this._argTypes] : [];
|
|
29
|
+
}
|
|
30
|
+
argType(index) {
|
|
31
|
+
return this._argTypes[index] ?? this._argTypes[this._argTypes.length - 1];
|
|
32
|
+
}
|
|
33
|
+
match(type) {
|
|
34
|
+
if (type instanceof FunctionAtom) {
|
|
35
|
+
if (!this._retType || !type._retType) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
if (!this._retType.match(type._retType) || this.minArity > type.maxArity || this.maxArity < type.minArity) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
for (let i = 0, argc = Math.max(this.minArity, type.minArity); i < argc; ++i) {
|
|
42
|
+
if (!this.argType(i).match(type.argType(i))) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
weight() {
|
|
51
|
+
return 0x1000000000000 + this.subtypes().reduce((acc, i) => acc + i.weight(), 0);
|
|
52
|
+
}
|
|
53
|
+
toString() {
|
|
54
|
+
const argTypes = this._argTypes.map((i) => i.toString()).join(',');
|
|
55
|
+
return this._retType ? `~${this._retType.toString()}(${argTypes}${this.isVariadic ? '...' : ''})` : '~()';
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.FunctionAtom = FunctionAtom;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ObjectAtom = void 0;
|
|
4
|
+
class ObjectAtom {
|
|
5
|
+
_propTypes;
|
|
6
|
+
constructor(_propTypes = {}) {
|
|
7
|
+
this._propTypes = _propTypes;
|
|
8
|
+
}
|
|
9
|
+
get empty() {
|
|
10
|
+
return Object.keys(this._propTypes).length === 0;
|
|
11
|
+
}
|
|
12
|
+
subtypes() {
|
|
13
|
+
return Object.values(this._propTypes);
|
|
14
|
+
}
|
|
15
|
+
propType(property) {
|
|
16
|
+
return this._propTypes[property];
|
|
17
|
+
}
|
|
18
|
+
match(type) {
|
|
19
|
+
if (type instanceof ObjectAtom) {
|
|
20
|
+
if (this.empty || type.empty) {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
for (const prop in this._propTypes) {
|
|
24
|
+
if (!type.propType(prop)) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
if (!this._propTypes[prop].match(type._propTypes[prop])) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
for (const prop in type._propTypes) {
|
|
32
|
+
if (!this.propType(prop)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
weight() {
|
|
41
|
+
return 0x100000000 + this.subtypes().reduce((acc, i) => acc + i.weight(), 0);
|
|
42
|
+
}
|
|
43
|
+
toString() {
|
|
44
|
+
const entries = Object.entries(this._propTypes);
|
|
45
|
+
return entries.length ? `[${entries.map((i) => i[0] + ':' + i[1].toString()).join(',')}]` : '[:]';
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.ObjectAtom = ObjectAtom;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PrimitiveAtom = void 0;
|
|
4
|
+
class PrimitiveAtom {
|
|
5
|
+
_primitive;
|
|
6
|
+
constructor(_primitive = 'void') {
|
|
7
|
+
this._primitive = _primitive;
|
|
8
|
+
}
|
|
9
|
+
match(type) {
|
|
10
|
+
if (type instanceof PrimitiveAtom) {
|
|
11
|
+
return type._primitive === this._primitive;
|
|
12
|
+
}
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
weight() {
|
|
16
|
+
switch (this._primitive) {
|
|
17
|
+
case 'void': return 0x0;
|
|
18
|
+
case 'boolean': return 0x2;
|
|
19
|
+
case 'timestamp': return 0x3;
|
|
20
|
+
case 'float': return 0x8;
|
|
21
|
+
case 'integer': return 0x10;
|
|
22
|
+
case 'buffer': return 0x20;
|
|
23
|
+
case 'string': return 0x30;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
toString() {
|
|
27
|
+
return this._primitive;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.PrimitiveAtom = PrimitiveAtom;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.constArray = exports.funcAppend = exports.funcPrepend = exports.funcCompose = exports.funcReduce = exports.funcFilter = exports.funcMutate = exports.funcReverse = exports.funcFlatten = exports.funcAny = exports.funcEvery = exports.funcLastIndex = exports.funcFirstIndex = exports.funcLast = exports.funcFirst = void 0;
|
|
4
|
+
const Constant_js_1 = require("../Constant.js");
|
|
5
|
+
const Type_js_1 = require("../Type.js");
|
|
6
|
+
const Unknown_js_1 = require("./Unknown.js");
|
|
7
|
+
const typePredicate = Type_js_1.Type.functionType(Type_js_1.Type.Boolean, [Type_js_1.Type.Unknown, Type_js_1.Type.OptionalInteger, Type_js_1.Type.OptionalArray]);
|
|
8
|
+
const typeItemFinder = Type_js_1.Type.functionType(Type_js_1.Type.Unknown, [Type_js_1.Type.Array, typePredicate]);
|
|
9
|
+
const typeIndexFinder = Type_js_1.Type.functionType(Type_js_1.Type.OptionalInteger, [Type_js_1.Type.Array, typePredicate]);
|
|
10
|
+
const typeConditionSet = Type_js_1.Type.functionType(Type_js_1.Type.Boolean, [Type_js_1.Type.Array, typePredicate]);
|
|
11
|
+
const typeVariadicInsert = Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_1.Type.Array, Type_js_1.Type.Unknown], true);
|
|
12
|
+
const typeArrayOperator = Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_1.Type.Array, Type_js_1.Type.Array]);
|
|
13
|
+
exports.funcFirst = new Constant_js_1.Constant((value, predicate) => value?.find((v, i, a) => predicate(v, BigInt(i), a)), typeItemFinder);
|
|
14
|
+
exports.funcLast = new Constant_js_1.Constant((value, predicate) => {
|
|
15
|
+
if (value == null) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
for (let i = value.length - 1; i >= 0; --i) {
|
|
19
|
+
if (predicate(value[i], BigInt(i), value)) {
|
|
20
|
+
return value[i];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}, typeItemFinder);
|
|
25
|
+
exports.funcFirstIndex = new Constant_js_1.Constant((value, predicate) => {
|
|
26
|
+
if (value == null) {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
const ix = value.findIndex((v, i, a) => predicate(v, BigInt(i), a));
|
|
30
|
+
return ix < 0 ? undefined : BigInt.asIntN(64, BigInt(ix));
|
|
31
|
+
}, typeIndexFinder);
|
|
32
|
+
exports.funcLastIndex = new Constant_js_1.Constant((value, predicate) => {
|
|
33
|
+
if (value == null) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
for (let i = value.length - 1; i >= 0; i--) {
|
|
37
|
+
if (predicate(value[i], BigInt(i), value)) {
|
|
38
|
+
return BigInt.asIntN(64, BigInt(i));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42
|
+
}, typeIndexFinder);
|
|
43
|
+
exports.funcEvery = new Constant_js_1.Constant((value, predicate) => value?.every((v, i, a) => predicate(v, BigInt(i), a)), typeConditionSet);
|
|
44
|
+
exports.funcAny = new Constant_js_1.Constant((value, predicate) => value?.some((v, i, a) => predicate(v, BigInt(i), a)), typeConditionSet);
|
|
45
|
+
exports.funcFlatten = new Constant_js_1.Constant((values, depth = 1n) => values?.flat(Number(depth)), Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_1.Type.Array, Type_js_1.Type.OptionalInteger]));
|
|
46
|
+
exports.funcReverse = new Constant_js_1.Constant((value) => [...value].reverse(), Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_1.Type.Array]));
|
|
47
|
+
exports.funcMutate = new Constant_js_1.Constant((value, transform) => value?.map((v, i, a) => transform(v, BigInt(i), a)), Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_1.Type.Array, Type_js_1.Type.functionType(Type_js_1.Type.Unknown, [Type_js_1.Type.Unknown, Type_js_1.Type.OptionalInteger, Type_js_1.Type.OptionalArray])]));
|
|
48
|
+
exports.funcFilter = new Constant_js_1.Constant((value, predicate) => value?.filter((v, i, a) => predicate(v, BigInt(i), a)), Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_1.Type.Array, typePredicate]));
|
|
49
|
+
exports.funcReduce = new Constant_js_1.Constant((value, reducer, initial) => initial != null
|
|
50
|
+
? value?.reduce((p, v, i, a) => reducer(p, v, BigInt(i), a), initial) : value?.reduce((p, v, i, a) => reducer(p, v, BigInt(i), a)), Type_js_1.Type.functionType(Type_js_1.Type.Unknown, [Type_js_1.Type.Array, Type_js_1.Type.functionType(Type_js_1.Type.Unknown, [Type_js_1.Type.Unknown, Type_js_1.Type.Unknown, Type_js_1.Type.OptionalInteger, Type_js_1.Type.OptionalArray])]));
|
|
51
|
+
exports.funcCompose = new Constant_js_1.Constant((value, callback) => {
|
|
52
|
+
if (value == null) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
const obj = {};
|
|
56
|
+
for (let i = 0; i < value.length; ++i) {
|
|
57
|
+
const key = value[i];
|
|
58
|
+
obj[key] = callback(obj, key, BigInt(i), value);
|
|
59
|
+
}
|
|
60
|
+
return obj;
|
|
61
|
+
}, Type_js_1.Type.functionType(Type_js_1.Type.Object, [Type_js_1.Type.Array, Type_js_1.Type.functionType(Type_js_1.Type.Unknown, [Type_js_1.Type.Object, Type_js_1.Type.String, Type_js_1.Type.OptionalInteger, Type_js_1.Type.OptionalArray])]));
|
|
62
|
+
exports.funcPrepend = new Constant_js_1.Constant((value, ...items) => {
|
|
63
|
+
value.unshift(items);
|
|
64
|
+
return value;
|
|
65
|
+
}, typeVariadicInsert);
|
|
66
|
+
exports.funcAppend = new Constant_js_1.Constant((value, ...items) => {
|
|
67
|
+
value.push(items);
|
|
68
|
+
return value;
|
|
69
|
+
}, typeVariadicInsert);
|
|
70
|
+
const funcJoin = new Constant_js_1.Constant((...values) => values.flat(Infinity).reduce((acc, val) => [...acc, val], []), Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_1.Type.Array], true));
|
|
71
|
+
const funcRange = new Constant_js_1.Constant((value1, value2) => {
|
|
72
|
+
const min = value1 < value2 ? value1 : value2;
|
|
73
|
+
const max = value1 > value2 ? value1 : value2;
|
|
74
|
+
return [...Array(Number(max - min)).keys()].map((i) => BigInt(i) + min);
|
|
75
|
+
}, Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_1.Type.Integer, Type_js_1.Type.Integer]));
|
|
76
|
+
const funcUnique = new Constant_js_1.Constant((value) => {
|
|
77
|
+
const result = [];
|
|
78
|
+
value.forEach((i) => {
|
|
79
|
+
if (result.every((v) => !(0, Unknown_js_1.equate)(v, i))) {
|
|
80
|
+
result.push(i);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
return result;
|
|
84
|
+
}, Type_js_1.Type.functionType(Type_js_1.Type.Array, [Type_js_1.Type.Array]));
|
|
85
|
+
const funcIntersection = new Constant_js_1.Constant((value1, value2) => value1.filter((i) => value2.some((v) => (0, Unknown_js_1.equate)(v, i))), typeArrayOperator);
|
|
86
|
+
const funcDifference = new Constant_js_1.Constant((value1, value2) => [...value1.filter((i) => value2.every((v) => !(0, Unknown_js_1.equate)(v, i))), ...value2.filter((i) => value1.every((v) => !(0, Unknown_js_1.equate)(v, i)))], typeArrayOperator);
|
|
87
|
+
exports.constArray = {
|
|
88
|
+
Join: funcJoin,
|
|
89
|
+
Range: funcRange,
|
|
90
|
+
Unique: funcUnique,
|
|
91
|
+
Intersection: funcIntersection,
|
|
92
|
+
Difference: funcDifference,
|
|
93
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.constBoolean = exports.funcNot = exports.funcAnd = exports.funcOr = void 0;
|
|
4
|
+
const Constant_js_1 = require("../Constant.js");
|
|
5
|
+
const Type_js_1 = require("../Type.js");
|
|
6
|
+
const typeBooleanOrArray = Type_js_1.Type.union(Type_js_1.Type.Boolean, Type_js_1.Type.arrayType([Type_js_1.Type.Boolean]));
|
|
7
|
+
const typeBooleanLogic = Type_js_1.Type.functionType(Type_js_1.Type.Boolean, [typeBooleanOrArray], true);
|
|
8
|
+
exports.funcOr = new Constant_js_1.Constant((...values) => values.flat().some((v) => v), typeBooleanLogic);
|
|
9
|
+
exports.funcAnd = new Constant_js_1.Constant((...values) => values.flat().every((v) => v), typeBooleanLogic);
|
|
10
|
+
exports.funcNot = new Constant_js_1.Constant((value) => !value, Type_js_1.Type.functionType(Type_js_1.Type.Boolean, [Type_js_1.Type.Boolean]));
|
|
11
|
+
const funcDecodeBoolean = new Constant_js_1.Constant((value, byteOffset) => Boolean(new Uint8Array(value)[byteOffset == null ? 0 : Number(byteOffset)]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalBoolean, [Type_js_1.Type.Buffer, Type_js_1.Type.OptionalInteger]));
|
|
12
|
+
const funcParseBoolean = new Constant_js_1.Constant((value) => {
|
|
13
|
+
if (value == null) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
const v = value.toLowerCase();
|
|
17
|
+
return v === 'true' ? true : v === 'false' ? false : undefined;
|
|
18
|
+
}, Type_js_1.Type.functionType(Type_js_1.Type.OptionalBoolean, [Type_js_1.Type.String]));
|
|
19
|
+
exports.constBoolean = {
|
|
20
|
+
Or: exports.funcOr,
|
|
21
|
+
And: exports.funcAnd,
|
|
22
|
+
Not: exports.funcNot,
|
|
23
|
+
Decode: funcDecodeBoolean,
|
|
24
|
+
Parse: funcParseBoolean,
|
|
25
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.constBuffer = exports.funcByte = exports.parseBuffer = exports.formatBuffer = exports.concatBuffers = exports.equateBuffers = void 0;
|
|
4
|
+
const Constant_js_1 = require("../Constant.js");
|
|
5
|
+
const Type_js_1 = require("../Type.js");
|
|
6
|
+
const equateBuffers = (value1, value2) => {
|
|
7
|
+
if (value1.byteLength !== value2.byteLength) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
const dv1 = new DataView(value1);
|
|
11
|
+
const dv2 = new DataView(value2);
|
|
12
|
+
const length = dv1.byteLength;
|
|
13
|
+
for (let lfi = length - 3, i = 0; i < length;) {
|
|
14
|
+
if (i < lfi) {
|
|
15
|
+
if (dv1.getUint32(i) !== dv2.getUint32(i)) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
i += 4;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
if (dv1.getUint8(i) !== dv2.getUint8(i)) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
++i;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return true;
|
|
28
|
+
};
|
|
29
|
+
exports.equateBuffers = equateBuffers;
|
|
30
|
+
const concatBuffers = (value1, value2) => {
|
|
31
|
+
const bytes = new Uint8Array(value1.byteLength + value2.byteLength);
|
|
32
|
+
bytes.set(new Uint8Array(value1), 0);
|
|
33
|
+
bytes.set(new Uint8Array(value2), value1.byteLength);
|
|
34
|
+
return bytes.buffer;
|
|
35
|
+
};
|
|
36
|
+
exports.concatBuffers = concatBuffers;
|
|
37
|
+
const formatBuffer = (value) => {
|
|
38
|
+
if (value == null) {
|
|
39
|
+
return '';
|
|
40
|
+
}
|
|
41
|
+
const bytes = new Uint8Array(value);
|
|
42
|
+
let str = '';
|
|
43
|
+
for (let i = 0; i < bytes.byteLength; ++i) {
|
|
44
|
+
str += bytes[i].toString(16).padStart(2, '0');
|
|
45
|
+
}
|
|
46
|
+
return str;
|
|
47
|
+
};
|
|
48
|
+
exports.formatBuffer = formatBuffer;
|
|
49
|
+
const parseBuffer = (value) => {
|
|
50
|
+
if (value == null) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
const bytes = new Uint8Array(Math.ceil(value.length / 2));
|
|
54
|
+
for (let i = 0, c = 0; c < value.length; ++i) {
|
|
55
|
+
bytes[i] = Number.parseInt(value.slice(c, c += 2), 16);
|
|
56
|
+
}
|
|
57
|
+
return bytes.buffer;
|
|
58
|
+
};
|
|
59
|
+
exports.parseBuffer = parseBuffer;
|
|
60
|
+
exports.funcByte = new Constant_js_1.Constant((value, pos) => value == null
|
|
61
|
+
? undefined
|
|
62
|
+
: value.slice(Number(pos), Number(pos) + 1), Type_js_1.Type.functionType(Type_js_1.Type.OptionalBuffer, [Type_js_1.Type.Buffer, Type_js_1.Type.Integer]));
|
|
63
|
+
const funcRandomBuffer = new Constant_js_1.Constant((value) => value == null || value < 0 ? undefined : crypto.getRandomValues(new Uint8Array(Number(value))), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Integer]), false);
|
|
64
|
+
const funcParseBuffer = new Constant_js_1.Constant((value) => (0, exports.parseBuffer)(value), Type_js_1.Type.functionType(Type_js_1.Type.OptionalBuffer, [Type_js_1.Type.String]));
|
|
65
|
+
exports.constBuffer = {
|
|
66
|
+
Random: funcRandomBuffer,
|
|
67
|
+
Parse: funcParseBuffer,
|
|
68
|
+
};
|