affinirum 1.4.2 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +214 -151
- package/dst/Affinirum.js +24 -15
- package/dst/Atom.d.ts +3 -1
- package/dst/Constant.js +1 -1
- package/dst/Functions.js +7 -4
- package/dst/Keywords.js +4 -4
- package/dst/ParserState.d.ts +2 -1
- package/dst/ParserState.js +22 -5
- package/dst/Type.d.ts +11 -7
- package/dst/Type.js +113 -38
- package/dst/Value.d.ts +1 -0
- package/dst/Value.js +19 -1
- package/dst/atom/ArrayAtom.d.ts +7 -3
- package/dst/atom/ArrayAtom.js +50 -8
- package/dst/atom/FunctionAtom.d.ts +4 -1
- package/dst/atom/FunctionAtom.js +65 -15
- package/dst/atom/ObjectAtom.d.ts +7 -5
- package/dst/atom/ObjectAtom.js +35 -13
- package/dst/atom/PrimitiveAtom.d.ts +3 -1
- package/dst/atom/PrimitiveAtom.js +10 -1
- package/dst/cjs/Affinirum.js +24 -15
- package/dst/cjs/Constant.js +1 -1
- package/dst/cjs/Functions.js +6 -3
- package/dst/cjs/Keywords.js +4 -4
- package/dst/cjs/ParserState.js +22 -5
- package/dst/cjs/Type.js +113 -38
- package/dst/cjs/Value.js +21 -0
- package/dst/cjs/atom/ArrayAtom.js +50 -8
- package/dst/cjs/atom/FunctionAtom.js +65 -15
- package/dst/cjs/atom/ObjectAtom.js +35 -13
- package/dst/cjs/atom/PrimitiveAtom.js +10 -1
- package/dst/cjs/native/Aggregable.js +2 -1
- package/dst/cjs/native/Array.js +10 -8
- package/dst/cjs/native/Boolean.js +5 -4
- package/dst/cjs/native/Enumerable.js +5 -4
- package/dst/cjs/native/Float.js +9 -8
- package/dst/cjs/native/Integer.js +23 -15
- package/dst/cjs/native/Iterable.js +2 -1
- package/dst/cjs/native/Number.js +18 -33
- package/dst/cjs/native/Object.js +2 -1
- package/dst/cjs/native/String.js +3 -2
- package/dst/cjs/native/Timestamp.js +2 -1
- package/dst/cjs/native/notation/IP.js +3 -2
- package/dst/cjs/node/ArrayNode.js +6 -6
- package/dst/cjs/node/CallNode.js +27 -23
- package/dst/cjs/node/CastNode.js +42 -0
- package/dst/cjs/node/ObjectNode.js +2 -4
- package/dst/cjs/node/SwitchNode.js +3 -1
- package/dst/cjs/node/VariableNode.js +3 -1
- package/dst/native/Aggregable.js +2 -1
- package/dst/native/Array.js +10 -8
- package/dst/native/Boolean.d.ts +1 -0
- package/dst/native/Boolean.js +4 -3
- package/dst/native/Enumerable.js +5 -4
- package/dst/native/Float.d.ts +2 -0
- package/dst/native/Float.js +8 -7
- package/dst/native/Integer.d.ts +5 -1
- package/dst/native/Integer.js +20 -14
- package/dst/native/Iterable.js +2 -1
- package/dst/native/Number.d.ts +0 -3
- package/dst/native/Number.js +17 -32
- package/dst/native/Object.js +2 -1
- package/dst/native/String.js +3 -2
- package/dst/native/Timestamp.js +2 -1
- package/dst/native/notation/IP.js +3 -2
- package/dst/node/ArrayNode.d.ts +1 -1
- package/dst/node/ArrayNode.js +6 -6
- package/dst/node/CallNode.js +27 -23
- package/dst/node/CastNode.d.ts +15 -0
- package/dst/node/CastNode.js +38 -0
- package/dst/node/ObjectNode.d.ts +1 -1
- package/dst/node/ObjectNode.js +2 -4
- package/dst/node/SwitchNode.js +3 -1
- package/dst/node/VariableNode.js +3 -1
- package/package.json +5 -4
package/dst/atom/ObjectAtom.js
CHANGED
|
@@ -1,25 +1,32 @@
|
|
|
1
|
+
import { Type } from "../Type.js";
|
|
1
2
|
export class ObjectAtom {
|
|
2
3
|
_valTypes;
|
|
3
|
-
constructor(
|
|
4
|
-
this._valTypes =
|
|
4
|
+
constructor(valTypes = []) {
|
|
5
|
+
this._valTypes = [...new Map(valTypes)];
|
|
5
6
|
}
|
|
6
7
|
get _undefined() {
|
|
7
|
-
return
|
|
8
|
+
return this._valTypes.length === 0;
|
|
8
9
|
}
|
|
9
|
-
|
|
10
|
-
return
|
|
10
|
+
valTypes() {
|
|
11
|
+
return this._valTypes;
|
|
11
12
|
}
|
|
12
|
-
|
|
13
|
-
return this._valTypes[key];
|
|
13
|
+
valType(key) {
|
|
14
|
+
return this._valTypes.find((i) => i[0] === key)?.[1];
|
|
14
15
|
}
|
|
15
|
-
|
|
16
|
+
equate(atom) {
|
|
17
|
+
if (!(atom instanceof ObjectAtom)) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
return this._valTypes.length === atom._valTypes.length && this._valTypes.every(([key, type]) => type.equate(atom.valType(key)));
|
|
21
|
+
}
|
|
22
|
+
accept(atom) {
|
|
16
23
|
if (atom instanceof ObjectAtom) {
|
|
17
24
|
if (this._undefined || atom._undefined) {
|
|
18
25
|
return true;
|
|
19
26
|
}
|
|
20
|
-
for (const key
|
|
21
|
-
const
|
|
22
|
-
if (!
|
|
27
|
+
for (const [key, type] of this._valTypes) {
|
|
28
|
+
const atype = atom.valType(key) ?? Type.Void;
|
|
29
|
+
if (!type.accept(atype)) {
|
|
23
30
|
return false;
|
|
24
31
|
}
|
|
25
32
|
}
|
|
@@ -27,13 +34,28 @@ export class ObjectAtom {
|
|
|
27
34
|
}
|
|
28
35
|
return false;
|
|
29
36
|
}
|
|
37
|
+
intersect(atom) {
|
|
38
|
+
if (!(atom instanceof ObjectAtom)) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
if (this._undefined || atom._undefined) {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
for (const [key, type] of this._valTypes) {
|
|
45
|
+
const atype = atom.valType(key);
|
|
46
|
+
if (atype && !type.intersect(atype)) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
30
52
|
weight() {
|
|
31
|
-
return 0x100000000 + this.
|
|
53
|
+
return 0x100000000 + this._valTypes.reduce((acc, i) => acc + i[1].weight(), 0);
|
|
32
54
|
}
|
|
33
55
|
toString() {
|
|
34
56
|
if (this._undefined) {
|
|
35
57
|
return "object";
|
|
36
58
|
}
|
|
37
|
-
return `[${
|
|
59
|
+
return `[${this._valTypes.map(([k, v]) => `"${k}":${v}`).join(",")}]`;
|
|
38
60
|
}
|
|
39
61
|
}
|
|
@@ -4,7 +4,9 @@ export declare class PrimitiveAtom implements IAtom {
|
|
|
4
4
|
protected readonly _primitive: Primitive;
|
|
5
5
|
constructor(_primitive?: Primitive);
|
|
6
6
|
get undef(): boolean;
|
|
7
|
-
|
|
7
|
+
equate(atom: IAtom): boolean;
|
|
8
|
+
accept(atom: IAtom): boolean;
|
|
9
|
+
intersect(atom: IAtom): boolean;
|
|
8
10
|
weight(): number;
|
|
9
11
|
toString(): string;
|
|
10
12
|
}
|
|
@@ -6,12 +6,21 @@ export class PrimitiveAtom {
|
|
|
6
6
|
get undef() {
|
|
7
7
|
return this._primitive === "void";
|
|
8
8
|
}
|
|
9
|
-
|
|
9
|
+
equate(atom) {
|
|
10
10
|
if (atom instanceof PrimitiveAtom) {
|
|
11
11
|
return atom._primitive === this._primitive;
|
|
12
12
|
}
|
|
13
13
|
return false;
|
|
14
14
|
}
|
|
15
|
+
accept(atom) {
|
|
16
|
+
if (atom instanceof PrimitiveAtom) {
|
|
17
|
+
return atom._primitive === this._primitive;
|
|
18
|
+
}
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
intersect(atom) {
|
|
22
|
+
return this.equate(atom);
|
|
23
|
+
}
|
|
15
24
|
weight() {
|
|
16
25
|
switch (this._primitive) {
|
|
17
26
|
case "void": return 0x1;
|
package/dst/cjs/Affinirum.js
CHANGED
|
@@ -9,6 +9,7 @@ const Number_js_1 = require("./native/Number.js");
|
|
|
9
9
|
const Unknown_js_1 = require("./native/Unknown.js");
|
|
10
10
|
const Constant_js_1 = require("./Constant.js");
|
|
11
11
|
const Variable_js_1 = require("./Variable.js");
|
|
12
|
+
const Value_js_1 = require("./Value.js");
|
|
12
13
|
const Type_js_1 = require("./Type.js");
|
|
13
14
|
const Keywords_js_1 = require("./Keywords.js");
|
|
14
15
|
const Constants_js_1 = require("./Constants.js");
|
|
@@ -16,6 +17,7 @@ const Functions_js_1 = require("./Functions.js");
|
|
|
16
17
|
const ArrayNode_js_1 = require("./node/ArrayNode.js");
|
|
17
18
|
const BlockNode_js_1 = require("./node/BlockNode.js");
|
|
18
19
|
const CallNode_js_1 = require("./node/CallNode.js");
|
|
20
|
+
const CastNode_js_1 = require("./node/CastNode.js");
|
|
19
21
|
const ConstantNode_js_1 = require("./node/ConstantNode.js");
|
|
20
22
|
const JumpNode_js_1 = require("./node/JumpNode.js");
|
|
21
23
|
const LoopNode_js_1 = require("./node/LoopNode.js");
|
|
@@ -96,23 +98,23 @@ class Affinirum {
|
|
|
96
98
|
evaluate(values) {
|
|
97
99
|
const variables = this._scope.variables();
|
|
98
100
|
for (const name in variables) {
|
|
99
|
-
if (!Object.
|
|
101
|
+
if (!values || !Object.hasOwn(values, name)) {
|
|
100
102
|
this._varframes.get(name)?.throwError(`undefined variable ${name}:\n`);
|
|
101
103
|
}
|
|
102
104
|
const variable = variables[name];
|
|
103
|
-
const value = values?.[name] ?? undefined;
|
|
104
|
-
if (!variable.type.
|
|
105
|
+
const value = (0, Value_js_1.normalize)(values?.[name] ?? undefined);
|
|
106
|
+
if (!variable.type.acceptValue(value)) {
|
|
105
107
|
this._varframes.get(name)?.throwError(`unexpected type ${Type_js_1.Type.of(value)} for variable ${name} of type ${variable.type}:\n`);
|
|
106
108
|
}
|
|
107
109
|
variable.value = value;
|
|
108
110
|
}
|
|
109
111
|
try {
|
|
110
|
-
return this._root.evaluate();
|
|
112
|
+
return (0, Value_js_1.normalize)(this._root.evaluate() ?? undefined);
|
|
111
113
|
}
|
|
112
114
|
catch (e) {
|
|
113
115
|
if (e instanceof JumpException_js_1.JumpException) {
|
|
114
116
|
if (e.jump === "exit") {
|
|
115
|
-
return e.value;
|
|
117
|
+
return (0, Value_js_1.normalize)(e.value ?? undefined);
|
|
116
118
|
}
|
|
117
119
|
else {
|
|
118
120
|
throw this._root.throwError(`unexpected ${e.jump} jump`);
|
|
@@ -238,9 +240,9 @@ class Affinirum {
|
|
|
238
240
|
return node;
|
|
239
241
|
}
|
|
240
242
|
_accessor(state, scope) {
|
|
243
|
+
const frame = state.starts();
|
|
241
244
|
let node = this._term(state, scope);
|
|
242
|
-
while (state.isDot || state.isQuestion || state.isParenthesesOpen || state.isBracketsOpen) {
|
|
243
|
-
const frame = state.starts();
|
|
245
|
+
while (state.isDot || state.isQuestion || state.isParenthesesOpen || state.isBracketsOpen || state.isCast) {
|
|
244
246
|
if (state.isDot || state.isQuestion) { // property access or property existance operator
|
|
245
247
|
const operator = state.isDot ? Iterable_js_1.funcAt : Iterable_js_1.funcHas;
|
|
246
248
|
if (state.next().isLiteral && (typeof state.literal.value === "string" || typeof state.literal.value === "bigint")) {
|
|
@@ -286,13 +288,20 @@ class Affinirum {
|
|
|
286
288
|
break;
|
|
287
289
|
}
|
|
288
290
|
}
|
|
291
|
+
state.closeParentheses();
|
|
289
292
|
node = new CallNode_js_1.CallNode(frame.ends(state), node, subnodes);
|
|
290
|
-
state.
|
|
293
|
+
state.next();
|
|
291
294
|
}
|
|
292
295
|
else if (state.isBracketsOpen) { // index access operator
|
|
293
|
-
const
|
|
294
|
-
|
|
295
|
-
|
|
296
|
+
const subnodes = [node, this._unit(state.next(), scope)];
|
|
297
|
+
state.closeBrackets();
|
|
298
|
+
const fnode = new ConstantNode_js_1.ConstantNode(frame.ends(state), Iterable_js_1.funcAt);
|
|
299
|
+
node = new CallNode_js_1.CallNode(frame, fnode, subnodes);
|
|
300
|
+
state.next();
|
|
301
|
+
}
|
|
302
|
+
else if (state.isCast) { // type cast operator
|
|
303
|
+
const type = this._type(state.next(), scope);
|
|
304
|
+
node = new CastNode_js_1.CastNode(frame.ends(state), node, type);
|
|
296
305
|
}
|
|
297
306
|
}
|
|
298
307
|
return node;
|
|
@@ -465,8 +474,8 @@ class Affinirum {
|
|
|
465
474
|
state.throwError("unexpected expression token");
|
|
466
475
|
}
|
|
467
476
|
_function(state, scope) {
|
|
468
|
-
state.next().openParentheses();
|
|
469
477
|
const frame = state.starts();
|
|
478
|
+
state.next().openParentheses();
|
|
470
479
|
let variadic = false;
|
|
471
480
|
const variables = new Map();
|
|
472
481
|
while (!state.next().isParenthesesClose) {
|
|
@@ -504,8 +513,8 @@ class Affinirum {
|
|
|
504
513
|
const args = Array.from(variables.values());
|
|
505
514
|
const subnode = this._unit(state, scope.subscope(variables));
|
|
506
515
|
const func = (...values) => {
|
|
507
|
-
args.forEach((arg, ix) => arg.value = values[ix]);
|
|
508
|
-
return subnode.evaluate();
|
|
516
|
+
args.forEach((arg, ix) => arg.value = (0, Value_js_1.normalize)(values[ix]));
|
|
517
|
+
return (0, Value_js_1.normalize)(subnode.evaluate());
|
|
509
518
|
};
|
|
510
519
|
const constant = new Constant_js_1.Constant(func, Type_js_1.Type.functionType(retType, args.map((v) => v.type), variadic));
|
|
511
520
|
return new ConstantNode_js_1.ConstantNode(frame, constant, subnode);
|
|
@@ -557,7 +566,7 @@ class Affinirum {
|
|
|
557
566
|
}
|
|
558
567
|
state.closeBrackets().next();
|
|
559
568
|
return colon
|
|
560
|
-
? Type_js_1.Type.objectType(
|
|
569
|
+
? Type_js_1.Type.objectType(itemKeyTypes.map(([key, type]) => [key, type]))
|
|
561
570
|
: Type_js_1.Type.arrayType(itemKeyTypes.map(([, v]) => v));
|
|
562
571
|
}
|
|
563
572
|
else if (state.isTilda) { // function type
|
package/dst/cjs/Constant.js
CHANGED
|
@@ -15,7 +15,7 @@ class Constant {
|
|
|
15
15
|
return this._value;
|
|
16
16
|
}
|
|
17
17
|
get type() {
|
|
18
|
-
return Type_js_1.Type.
|
|
18
|
+
return Type_js_1.Type.isPrimitive(this._value) ? Type_js_1.Type.of(this._value) : this._type;
|
|
19
19
|
}
|
|
20
20
|
set type(type) {
|
|
21
21
|
this._type = type;
|
package/dst/cjs/Functions.js
CHANGED
|
@@ -3,8 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Functions = void 0;
|
|
4
4
|
const Aggregable_js_1 = require("./native/Aggregable.js");
|
|
5
5
|
const Array_js_1 = require("./native/Array.js");
|
|
6
|
+
const Boolean_js_1 = require("./native/Boolean.js");
|
|
6
7
|
const Buffer_js_1 = require("./native/Buffer.js");
|
|
7
8
|
const Enumerable_js_1 = require("./native/Enumerable.js");
|
|
9
|
+
const Float_js_1 = require("./native/Float.js");
|
|
10
|
+
const Integer_js_1 = require("./native/Integer.js");
|
|
8
11
|
const Iterable_js_1 = require("./native/Iterable.js");
|
|
9
12
|
const Number_js_1 = require("./native/Number.js");
|
|
10
13
|
const Object_js_1 = require("./native/Object.js");
|
|
@@ -53,9 +56,9 @@ exports.Functions = [
|
|
|
53
56
|
["Power", Number_js_1.funcPower],
|
|
54
57
|
["Root", Number_js_1.funcRoot],
|
|
55
58
|
["Negate", Number_js_1.funcNegate],
|
|
56
|
-
["
|
|
57
|
-
["
|
|
58
|
-
["
|
|
59
|
+
["Boolean", Boolean_js_1.funcBoolean],
|
|
60
|
+
["Float", Float_js_1.funcFloat],
|
|
61
|
+
["Integer", Integer_js_1.funcInteger],
|
|
59
62
|
// Object
|
|
60
63
|
["Entries", Object_js_1.funcEntries],
|
|
61
64
|
["Keys", Object_js_1.funcKeys],
|
package/dst/cjs/Keywords.js
CHANGED
|
@@ -4,10 +4,10 @@ exports.Keywords = void 0;
|
|
|
4
4
|
exports.Keywords = [
|
|
5
5
|
"true", "false",
|
|
6
6
|
"null", "void",
|
|
7
|
-
"boolean",
|
|
8
|
-
"timestamp",
|
|
9
|
-
"float",
|
|
10
|
-
"integer",
|
|
7
|
+
"bool", "boolean",
|
|
8
|
+
"time", "timestamp",
|
|
9
|
+
"f64", "float",
|
|
10
|
+
"i64", "integer",
|
|
11
11
|
"buffer",
|
|
12
12
|
"string",
|
|
13
13
|
"array",
|
package/dst/cjs/ParserState.js
CHANGED
|
@@ -8,6 +8,7 @@ const String_js_1 = require("./native/String.js");
|
|
|
8
8
|
const Constant_js_1 = require("./Constant.js");
|
|
9
9
|
const Number_js_1 = require("./native/Number.js");
|
|
10
10
|
const Unknown_js_1 = require("./native/Unknown.js");
|
|
11
|
+
const Integer_js_1 = require("./native/Integer.js");
|
|
11
12
|
const Type_js_1 = require("./Type.js");
|
|
12
13
|
const ParserFrame_js_1 = require("./ParserFrame.js");
|
|
13
14
|
class Literal {
|
|
@@ -42,10 +43,11 @@ const idBracesClose = Symbol();
|
|
|
42
43
|
const idSemicolon = Symbol();
|
|
43
44
|
const idColon = Symbol();
|
|
44
45
|
const idComma = Symbol();
|
|
46
|
+
const idTilda = Symbol();
|
|
45
47
|
const idDot = Symbol();
|
|
46
48
|
const idQuestion = Symbol();
|
|
47
49
|
const idEllipsis = Symbol();
|
|
48
|
-
const
|
|
50
|
+
const idCast = Symbol();
|
|
49
51
|
const idVar = Symbol();
|
|
50
52
|
const idVal = Symbol();
|
|
51
53
|
const idIf = Symbol();
|
|
@@ -117,6 +119,9 @@ class ParserState extends ParserFrame_js_1.ParserFrame {
|
|
|
117
119
|
get isComma() {
|
|
118
120
|
return this._fragment === idComma;
|
|
119
121
|
}
|
|
122
|
+
get isTilda() {
|
|
123
|
+
return this._fragment === idTilda;
|
|
124
|
+
}
|
|
120
125
|
get isDot() {
|
|
121
126
|
return this._fragment === idDot;
|
|
122
127
|
}
|
|
@@ -126,8 +131,8 @@ class ParserState extends ParserFrame_js_1.ParserFrame {
|
|
|
126
131
|
get isEllipsis() {
|
|
127
132
|
return this._fragment === idEllipsis;
|
|
128
133
|
}
|
|
129
|
-
get
|
|
130
|
-
return this._fragment ===
|
|
134
|
+
get isCast() {
|
|
135
|
+
return this._fragment === idCast;
|
|
131
136
|
}
|
|
132
137
|
get isVar() {
|
|
133
138
|
return this._fragment === idVar;
|
|
@@ -243,7 +248,15 @@ class ParserState extends ParserFrame_js_1.ParserFrame {
|
|
|
243
248
|
this._fragment = idSemicolon;
|
|
244
249
|
break;
|
|
245
250
|
case ":":
|
|
246
|
-
this.
|
|
251
|
+
switch (this._expr.charAt(this._end)) {
|
|
252
|
+
case ":":
|
|
253
|
+
++this._end;
|
|
254
|
+
this._fragment = idCast;
|
|
255
|
+
break;
|
|
256
|
+
default:
|
|
257
|
+
this._fragment = idColon;
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
247
260
|
break;
|
|
248
261
|
case ",":
|
|
249
262
|
this._fragment = idComma;
|
|
@@ -506,15 +519,19 @@ class ParserState extends ParserFrame_js_1.ParserFrame {
|
|
|
506
519
|
case "void":
|
|
507
520
|
this._fragment = Type_js_1.Type.Void;
|
|
508
521
|
break;
|
|
522
|
+
case "bool":
|
|
509
523
|
case "boolean":
|
|
510
524
|
this._fragment = Type_js_1.Type.Boolean;
|
|
511
525
|
break;
|
|
526
|
+
case "time":
|
|
512
527
|
case "timestamp":
|
|
513
528
|
this._fragment = Type_js_1.Type.Timestamp;
|
|
514
529
|
break;
|
|
530
|
+
case "f64":
|
|
515
531
|
case "float":
|
|
516
532
|
this._fragment = Type_js_1.Type.Float;
|
|
517
533
|
break;
|
|
534
|
+
case "i64":
|
|
518
535
|
case "integer":
|
|
519
536
|
this._fragment = Type_js_1.Type.Integer;
|
|
520
537
|
break;
|
|
@@ -589,7 +606,7 @@ class ParserState extends ParserFrame_js_1.ParserFrame {
|
|
|
589
606
|
integer = false;
|
|
590
607
|
}
|
|
591
608
|
this._fragment = integer
|
|
592
|
-
? new Literal(BigInt(this._expr.substring(this._start, this._end)))
|
|
609
|
+
? new Literal((0, Integer_js_1.boundInteger)(BigInt(this._expr.substring(this._start, this._end))))
|
|
593
610
|
: new Literal(parseFloat(this._expr.substring(this._start, this._end)));
|
|
594
611
|
}
|
|
595
612
|
else {
|
package/dst/cjs/Type.js
CHANGED
|
@@ -17,54 +17,92 @@ class Type {
|
|
|
17
17
|
return this._atoms.length === 1;
|
|
18
18
|
}
|
|
19
19
|
get isOptional() {
|
|
20
|
-
return this.isUnknown || this._atoms.length > 1 && this._atoms.some((
|
|
20
|
+
return this.isUnknown || this._atoms.length > 1 && this._atoms.some((a) => a.equate(Type.Void._atoms[0]));
|
|
21
21
|
}
|
|
22
22
|
get isVoid() {
|
|
23
|
-
return this.isAtom && this._atoms[0].
|
|
23
|
+
return this.isAtom && this._atoms[0].equate(Type.Void._atoms[0]);
|
|
24
24
|
}
|
|
25
25
|
get isBoolean() {
|
|
26
|
-
return this.isAtom && this._atoms[0].
|
|
26
|
+
return this.isAtom && this._atoms[0].equate(Type.Boolean._atoms[0]);
|
|
27
27
|
}
|
|
28
28
|
get isTimestamp() {
|
|
29
|
-
return this.isAtom && this._atoms[0].
|
|
29
|
+
return this.isAtom && this._atoms[0].equate(Type.Timestamp._atoms[0]);
|
|
30
30
|
}
|
|
31
31
|
get isFloat() {
|
|
32
|
-
return this.isAtom && this._atoms[0].
|
|
32
|
+
return this.isAtom && this._atoms[0].equate(Type.Float._atoms[0]);
|
|
33
33
|
}
|
|
34
34
|
get isInteger() {
|
|
35
|
-
return this.isAtom && this._atoms[0].
|
|
35
|
+
return this.isAtom && this._atoms[0].equate(Type.Integer._atoms[0]);
|
|
36
36
|
}
|
|
37
37
|
get isBuffer() {
|
|
38
|
-
return this.isAtom && this._atoms[0].
|
|
38
|
+
return this.isAtom && this._atoms[0].equate(Type.Buffer._atoms[0]);
|
|
39
39
|
}
|
|
40
40
|
get isString() {
|
|
41
|
-
return this.isAtom && this._atoms[0].
|
|
41
|
+
return this.isAtom && this._atoms[0].equate(Type.String._atoms[0]);
|
|
42
42
|
}
|
|
43
43
|
get isArray() {
|
|
44
|
-
return this._atoms.every((
|
|
44
|
+
return this._atoms.every((a) => a instanceof ArrayAtom_js_1.ArrayAtom);
|
|
45
45
|
}
|
|
46
46
|
get isObject() {
|
|
47
|
-
return this._atoms.every((
|
|
47
|
+
return this._atoms.every((a) => a instanceof ObjectAtom_js_1.ObjectAtom);
|
|
48
48
|
}
|
|
49
49
|
get isFunction() {
|
|
50
|
-
return this._atoms.every((
|
|
50
|
+
return this._atoms.every((a) => a instanceof FunctionAtom_js_1.FunctionAtom);
|
|
51
51
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
52
|
+
equate(type) {
|
|
53
|
+
if (this._atoms.length !== type?._atoms.length) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
const unmatched = [...type._atoms];
|
|
57
|
+
for (const atom of this._atoms) {
|
|
58
|
+
const index = unmatched.findIndex((a) => atom.equate(a));
|
|
59
|
+
if (index < 0) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
unmatched.splice(index, 1);
|
|
63
|
+
}
|
|
64
|
+
return true;
|
|
58
65
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
66
|
+
accept(type) {
|
|
67
|
+
if (type instanceof Type) {
|
|
68
|
+
if (this.isUnknown || type.isUnknown) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
return type._atoms.every((a) => this._atoms.some((atom) => atom.accept(a)));
|
|
72
|
+
}
|
|
73
|
+
return this._atoms.some((a) => a.accept(type));
|
|
62
74
|
}
|
|
63
|
-
|
|
75
|
+
acceptValue(value) {
|
|
64
76
|
if (this.isUnknown) {
|
|
65
|
-
return
|
|
77
|
+
return true;
|
|
66
78
|
}
|
|
67
|
-
|
|
79
|
+
const valueAtom = Type.of(value)._atoms[0];
|
|
80
|
+
return this._atoms.some((atom) => {
|
|
81
|
+
if (!atom.accept(valueAtom)) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
if (atom instanceof ArrayAtom_js_1.ArrayAtom) {
|
|
85
|
+
const values = value;
|
|
86
|
+
const types = atom.valTypes();
|
|
87
|
+
if (!types.length) {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
if (atom.isTuple) {
|
|
91
|
+
return types.every((type, ix) => type.acceptValue(Object.hasOwn(values, ix) ? values[ix] : undefined));
|
|
92
|
+
}
|
|
93
|
+
for (let i = 0; i < values.length; ++i) {
|
|
94
|
+
if (!types[0].acceptValue(Object.hasOwn(values, i) ? values[i] : undefined)) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
if (atom instanceof ObjectAtom_js_1.ObjectAtom) {
|
|
101
|
+
const values = value;
|
|
102
|
+
return atom.valTypes().every(([key, type]) => type.acceptValue(Object.hasOwn(values, key) ? values[key] : undefined));
|
|
103
|
+
}
|
|
104
|
+
return true;
|
|
105
|
+
});
|
|
68
106
|
}
|
|
69
107
|
reduce(mask) {
|
|
70
108
|
if (mask.isUnknown || mask.isVoid) {
|
|
@@ -73,37 +111,74 @@ class Type {
|
|
|
73
111
|
if (this.isUnknown) {
|
|
74
112
|
return mask;
|
|
75
113
|
}
|
|
76
|
-
const list = this._atoms.filter((
|
|
114
|
+
const list = this._atoms.filter((a) => mask.accept(a));
|
|
77
115
|
return list.length === 0
|
|
78
116
|
? undefined
|
|
79
117
|
: list.length === this._atoms.length
|
|
80
118
|
? this
|
|
81
119
|
: new Type(list);
|
|
82
120
|
}
|
|
83
|
-
|
|
84
|
-
if (
|
|
85
|
-
return
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
return this._atoms.some((i) => i.match(type));
|
|
121
|
+
intersect(type) {
|
|
122
|
+
if (this.isUnknown || type.isUnknown) {
|
|
123
|
+
return true;
|
|
89
124
|
}
|
|
125
|
+
return this._atoms.some((a) => type._atoms.some((atom) => a.intersect(atom)));
|
|
90
126
|
}
|
|
91
127
|
weight() {
|
|
92
|
-
return this._atoms.reduce((acc,
|
|
128
|
+
return this._atoms.reduce((acc, a) => acc + a.weight(), 0);
|
|
129
|
+
}
|
|
130
|
+
arrayAtomValType(index) {
|
|
131
|
+
const atoms = this._atoms.filter((a) => a instanceof ArrayAtom_js_1.ArrayAtom);
|
|
132
|
+
if (!atoms.length) {
|
|
133
|
+
return Type.Unknown;
|
|
134
|
+
}
|
|
135
|
+
const valTypes = [];
|
|
136
|
+
for (const atom of atoms) {
|
|
137
|
+
const valType = atom.isTuple
|
|
138
|
+
? atom.valType(index)
|
|
139
|
+
: atom.valType(0);
|
|
140
|
+
if (!valType) {
|
|
141
|
+
return Type.Unknown;
|
|
142
|
+
}
|
|
143
|
+
valTypes.push(valType);
|
|
144
|
+
}
|
|
145
|
+
return Type.union(...valTypes);
|
|
146
|
+
}
|
|
147
|
+
functionAtomRetType(type) {
|
|
148
|
+
const atoms = this._atoms.filter((a) => a instanceof FunctionAtom_js_1.FunctionAtom && type.accept(a.retType));
|
|
149
|
+
return Type.union(...atoms.map((a) => a.retType));
|
|
150
|
+
}
|
|
151
|
+
functionAtoms(type, argc) {
|
|
152
|
+
if (this.isUnknown) {
|
|
153
|
+
return Type.Function._atoms;
|
|
154
|
+
}
|
|
155
|
+
return this._atoms.filter((a) => a instanceof FunctionAtom_js_1.FunctionAtom && a.minArity <= argc && a.maxArity >= argc && (type.isVoid || type.accept(a.retType)));
|
|
156
|
+
}
|
|
157
|
+
toOptional() {
|
|
158
|
+
return this._atoms.length ? Type.union(Type.Void, this) : this;
|
|
159
|
+
}
|
|
160
|
+
toMandatory() {
|
|
161
|
+
if (!this.isOptional || this.isUnknown) {
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
return new Type(this._atoms.filter((a) => !a.equate(Type.Void._atoms[0])));
|
|
93
165
|
}
|
|
94
166
|
toString() {
|
|
95
167
|
return this._atoms.length
|
|
96
|
-
? this._atoms
|
|
168
|
+
? [...this._atoms]
|
|
169
|
+
.sort((a, b) => a.weight() - b.weight())
|
|
170
|
+
.map((a) => a.toString())
|
|
171
|
+
.join("|")
|
|
97
172
|
: "??";
|
|
98
173
|
}
|
|
99
174
|
static union(...types) {
|
|
100
|
-
if (types.some((
|
|
175
|
+
if (types.some((t) => t.isUnknown)) {
|
|
101
176
|
return Type.Unknown;
|
|
102
177
|
}
|
|
103
178
|
const type = new Type([]);
|
|
104
|
-
types.
|
|
105
|
-
if (!type.
|
|
106
|
-
type._atoms.push(
|
|
179
|
+
types.flatMap((t) => t._atoms).forEach((a) => {
|
|
180
|
+
if (!type._atoms.some((atom) => atom.equate(a))) {
|
|
181
|
+
type._atoms.push(a);
|
|
107
182
|
}
|
|
108
183
|
});
|
|
109
184
|
return type;
|
|
@@ -129,7 +204,7 @@ class Type {
|
|
|
129
204
|
? Type.Object
|
|
130
205
|
: Type.Function;
|
|
131
206
|
}
|
|
132
|
-
static
|
|
207
|
+
static isPrimitive(value) {
|
|
133
208
|
return value == null
|
|
134
209
|
|| typeof value === "boolean"
|
|
135
210
|
|| value instanceof Date
|
|
@@ -144,7 +219,7 @@ class Type {
|
|
|
144
219
|
static arrayType(itemTypes = []) {
|
|
145
220
|
return new Type([new ArrayAtom_js_1.ArrayAtom(itemTypes)]);
|
|
146
221
|
}
|
|
147
|
-
static objectType(propTypes =
|
|
222
|
+
static objectType(propTypes = []) {
|
|
148
223
|
return new Type([new ObjectAtom_js_1.ObjectAtom(propTypes)]);
|
|
149
224
|
}
|
|
150
225
|
static functionType(retType = Type.Unknown, argTypes = [], isVariadic = false) {
|
package/dst/cjs/Value.js
CHANGED
|
@@ -1,2 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalize = void 0;
|
|
4
|
+
const normalizeValue = (value, objects) => {
|
|
5
|
+
if (typeof value === "bigint") {
|
|
6
|
+
return BigInt.asIntN(64, value);
|
|
7
|
+
}
|
|
8
|
+
if (value == null || typeof value !== "object" || value instanceof Date || value instanceof ArrayBuffer || objects.has(value)) {
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
objects.add(value);
|
|
12
|
+
if (Array.isArray(value)) {
|
|
13
|
+
value.forEach((i, ix) => value[ix] = normalizeValue(i, objects));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
for (const key of Object.keys(value)) {
|
|
17
|
+
value[key] = normalizeValue(value[key], objects);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return value;
|
|
21
|
+
};
|
|
22
|
+
const normalize = (value) => normalizeValue(value, new WeakSet());
|
|
23
|
+
exports.normalize = normalize;
|