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.
Files changed (121) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +301 -0
  3. package/dst/Affinirum.d.ts +73 -0
  4. package/dst/Affinirum.js +536 -0
  5. package/dst/Constant.d.ts +15 -0
  6. package/dst/Constant.js +26 -0
  7. package/dst/Constants.d.ts +2 -0
  8. package/dst/Constants.js +22 -0
  9. package/dst/Functions.d.ts +2 -0
  10. package/dst/Functions.js +85 -0
  11. package/dst/Keywords.d.ts +1 -0
  12. package/dst/Keywords.js +17 -0
  13. package/dst/Node.d.ts +13 -0
  14. package/dst/Node.js +22 -0
  15. package/dst/ParserFrame.d.ts +13 -0
  16. package/dst/ParserFrame.js +80 -0
  17. package/dst/ParserState.d.ts +54 -0
  18. package/dst/ParserState.js +562 -0
  19. package/dst/StaticScope.d.ts +13 -0
  20. package/dst/StaticScope.js +44 -0
  21. package/dst/Type.d.ts +72 -0
  22. package/dst/Type.js +196 -0
  23. package/dst/Value.d.ts +3 -0
  24. package/dst/Value.js +1 -0
  25. package/dst/Variable.d.ts +13 -0
  26. package/dst/Variable.js +25 -0
  27. package/dst/atom/ArrayAtom.d.ts +11 -0
  28. package/dst/atom/ArrayAtom.js +38 -0
  29. package/dst/atom/FunctionAtom.d.ts +17 -0
  30. package/dst/atom/FunctionAtom.js +54 -0
  31. package/dst/atom/ObjectAtom.d.ts +11 -0
  32. package/dst/atom/ObjectAtom.js +44 -0
  33. package/dst/atom/PrimitiveAtom.d.ts +8 -0
  34. package/dst/atom/PrimitiveAtom.js +26 -0
  35. package/dst/cjs/Affinirum.js +540 -0
  36. package/dst/cjs/Constant.js +30 -0
  37. package/dst/cjs/Constants.js +25 -0
  38. package/dst/cjs/Functions.js +88 -0
  39. package/dst/cjs/Keywords.js +20 -0
  40. package/dst/cjs/Node.js +26 -0
  41. package/dst/cjs/ParserFrame.js +84 -0
  42. package/dst/cjs/ParserState.js +566 -0
  43. package/dst/cjs/StaticScope.js +48 -0
  44. package/dst/cjs/Type.js +200 -0
  45. package/dst/cjs/Value.js +2 -0
  46. package/dst/cjs/Variable.js +29 -0
  47. package/dst/cjs/atom/ArrayAtom.js +42 -0
  48. package/dst/cjs/atom/FunctionAtom.js +58 -0
  49. package/dst/cjs/atom/ObjectAtom.js +48 -0
  50. package/dst/cjs/atom/PrimitiveAtom.js +30 -0
  51. package/dst/cjs/constant/Array.js +93 -0
  52. package/dst/cjs/constant/Boolean.js +25 -0
  53. package/dst/cjs/constant/Buffer.js +68 -0
  54. package/dst/cjs/constant/Enumerable.js +34 -0
  55. package/dst/cjs/constant/Float.js +85 -0
  56. package/dst/cjs/constant/Integer.js +104 -0
  57. package/dst/cjs/constant/Iterable.js +24 -0
  58. package/dst/cjs/constant/Number.js +59 -0
  59. package/dst/cjs/constant/Object.js +13 -0
  60. package/dst/cjs/constant/String.js +197 -0
  61. package/dst/cjs/constant/Timestamp.js +56 -0
  62. package/dst/cjs/constant/Unknown.js +101 -0
  63. package/dst/cjs/constant/notation/AN.js +46 -0
  64. package/dst/cjs/constant/notation/JSON.js +14 -0
  65. package/dst/cjs/index.js +19 -0
  66. package/dst/cjs/node/ArrayNode.js +34 -0
  67. package/dst/cjs/node/BlockNode.js +33 -0
  68. package/dst/cjs/node/CallNode.js +54 -0
  69. package/dst/cjs/node/ConstantNode.js +33 -0
  70. package/dst/cjs/node/LoopNode.js +34 -0
  71. package/dst/cjs/node/ObjectNode.js +42 -0
  72. package/dst/cjs/node/SwitchNode.js +45 -0
  73. package/dst/cjs/node/VariableNode.js +30 -0
  74. package/dst/cjs/package.json +3 -0
  75. package/dst/constant/Array.d.ts +22 -0
  76. package/dst/constant/Array.js +90 -0
  77. package/dst/constant/Boolean.d.ts +11 -0
  78. package/dst/constant/Boolean.js +22 -0
  79. package/dst/constant/Buffer.d.ts +10 -0
  80. package/dst/constant/Buffer.js +61 -0
  81. package/dst/constant/Enumerable.d.ts +5 -0
  82. package/dst/constant/Enumerable.js +31 -0
  83. package/dst/constant/Float.d.ts +22 -0
  84. package/dst/constant/Float.js +80 -0
  85. package/dst/constant/Integer.d.ts +10 -0
  86. package/dst/constant/Integer.js +100 -0
  87. package/dst/constant/Iterable.d.ts +3 -0
  88. package/dst/constant/Iterable.js +21 -0
  89. package/dst/constant/Number.d.ts +14 -0
  90. package/dst/constant/Number.js +56 -0
  91. package/dst/constant/Object.d.ts +7 -0
  92. package/dst/constant/Object.js +10 -0
  93. package/dst/constant/String.d.ts +34 -0
  94. package/dst/constant/String.js +179 -0
  95. package/dst/constant/Timestamp.d.ts +20 -0
  96. package/dst/constant/Timestamp.js +50 -0
  97. package/dst/constant/Unknown.d.ts +10 -0
  98. package/dst/constant/Unknown.js +95 -0
  99. package/dst/constant/notation/AN.d.ts +6 -0
  100. package/dst/constant/notation/AN.js +42 -0
  101. package/dst/constant/notation/JSON.d.ts +7 -0
  102. package/dst/constant/notation/JSON.js +10 -0
  103. package/dst/index.d.ts +3 -0
  104. package/dst/index.js +3 -0
  105. package/dst/node/ArrayNode.d.ts +12 -0
  106. package/dst/node/ArrayNode.js +30 -0
  107. package/dst/node/BlockNode.d.ts +12 -0
  108. package/dst/node/BlockNode.js +29 -0
  109. package/dst/node/CallNode.d.ts +14 -0
  110. package/dst/node/CallNode.js +50 -0
  111. package/dst/node/ConstantNode.d.ts +15 -0
  112. package/dst/node/ConstantNode.js +29 -0
  113. package/dst/node/LoopNode.d.ts +13 -0
  114. package/dst/node/LoopNode.js +30 -0
  115. package/dst/node/ObjectNode.d.ts +13 -0
  116. package/dst/node/ObjectNode.js +38 -0
  117. package/dst/node/SwitchNode.d.ts +14 -0
  118. package/dst/node/SwitchNode.js +41 -0
  119. package/dst/node/VariableNode.d.ts +14 -0
  120. package/dst/node/VariableNode.js +26 -0
  121. package/package.json +67 -0
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.funcFormat = exports.funcEncode = exports.funcNotEqual = exports.funcEqual = exports.funcCoalesce = exports.format = exports.encode = exports.equate = void 0;
4
+ const Constant_js_1 = require("../Constant.js");
5
+ const Type_js_1 = require("../Type.js");
6
+ const Float_js_1 = require("./Float.js");
7
+ const Integer_js_1 = require("./Integer.js");
8
+ const Buffer_js_1 = require("./Buffer.js");
9
+ const String_js_1 = require("./String.js");
10
+ const Timestamp_js_1 = require("./Timestamp.js");
11
+ const equate = (value1, value2) => {
12
+ if (value1 == null || value2 == null) {
13
+ return value1 == value2;
14
+ }
15
+ if (typeof value1 !== typeof value2) {
16
+ if (typeof value1 === 'number' && typeof value2 === 'bigint') {
17
+ return value1 === Number(value2);
18
+ }
19
+ if (typeof value1 === 'bigint' && typeof value2 === 'number') {
20
+ return Number.isInteger(value2) && value1 === BigInt(value2);
21
+ }
22
+ return false;
23
+ }
24
+ if (typeof value1 === 'number') {
25
+ return isNaN(value1) && isNaN(value2) ? true : value1 === value2;
26
+ }
27
+ if (typeof value1 === 'boolean' || typeof value1 === 'bigint' || typeof value1 === 'string' || typeof value1 === 'function') {
28
+ return value1 === value2;
29
+ }
30
+ if (value1 instanceof Date && value2 instanceof Date) {
31
+ return value1.getTime() === value2.getTime();
32
+ }
33
+ if (value1 instanceof ArrayBuffer && value2 instanceof ArrayBuffer) {
34
+ return (0, Buffer_js_1.equateBuffers)(value1, value2);
35
+ }
36
+ if (Array.isArray(value1) && Array.isArray(value2)) {
37
+ if (value1.length === value2.length) {
38
+ for (let i = 0; i < value1.length; ++i) {
39
+ if (!(0, exports.equate)(value1[i], value2[i])) {
40
+ return false;
41
+ }
42
+ }
43
+ return true;
44
+ }
45
+ return false;
46
+ }
47
+ const props = new Set([...Object.getOwnPropertyNames(value1), ...Object.getOwnPropertyNames(value2)]);
48
+ for (const prop of props) {
49
+ if (!(0, exports.equate)(value1[prop], value2[prop])) {
50
+ return false;
51
+ }
52
+ }
53
+ return true;
54
+ };
55
+ exports.equate = equate;
56
+ const encode = (value, encoding) => value == null
57
+ ? new Uint8Array(0).buffer
58
+ : typeof value === 'boolean'
59
+ ? new Uint8Array([value ? 255 : 0]).buffer
60
+ : value instanceof Date
61
+ ? (0, Timestamp_js_1.encodeTimestamp)(value, encoding ?? 'int64')
62
+ : typeof value === 'number'
63
+ ? (0, Float_js_1.encodeFloat)(value, encoding ?? 'float64')
64
+ : typeof value === 'bigint'
65
+ ? (0, Integer_js_1.encodeInteger)(value, encoding ?? 'int64')
66
+ : value instanceof ArrayBuffer
67
+ ? value
68
+ : typeof value === 'string'
69
+ ? (0, String_js_1.encodeString)(value, encoding ?? 'utf8')
70
+ : Array.isArray(value)
71
+ ? value.map((i) => (0, exports.encode)(i, encoding)).reduce((acc, val) => (0, Buffer_js_1.concatBuffers)(acc, val))
72
+ : typeof value === 'object'
73
+ ? Object.entries(value).map(([k, v]) => (0, Buffer_js_1.concatBuffers)((0, exports.encode)(k, encoding), (0, exports.encode)(v, encoding))).reduce((acc, val) => (0, Buffer_js_1.concatBuffers)(acc, val))
74
+ : new Uint8Array(0).buffer;
75
+ exports.encode = encode;
76
+ const format = (value, radix, separator = '') => value == null
77
+ ? 'null'
78
+ : typeof value === 'boolean'
79
+ ? value.toString()
80
+ : value instanceof Date
81
+ ? (0, Timestamp_js_1.formatTimestamp)(value, radix ? Number(radix) : undefined)
82
+ : typeof value === 'number'
83
+ ? (0, Float_js_1.formatFloat)(value, radix ? Number(radix) : undefined)
84
+ : typeof value === 'bigint'
85
+ ? value.toString(radix ? Number(radix) : undefined)
86
+ : value instanceof ArrayBuffer
87
+ ? (0, Buffer_js_1.formatBuffer)(value)
88
+ : typeof value === 'string'
89
+ ? value
90
+ : Array.isArray(value)
91
+ ? value.map((i) => (0, exports.format)(i, radix)).join(separator)
92
+ : typeof value === 'object'
93
+ ? Object.entries(value).map(([k, v]) => `${(0, exports.format)(k)}${radix}${(0, exports.format)(v)}`).join(separator)
94
+ : 'function';
95
+ exports.format = format;
96
+ const typeEquator = Type_js_1.Type.functionType(Type_js_1.Type.Boolean, [Type_js_1.Type.Unknown, Type_js_1.Type.Unknown]);
97
+ exports.funcCoalesce = new Constant_js_1.Constant((value, valueOtherwise) => value ?? valueOtherwise, Type_js_1.Type.union(Type_js_1.Type.functionType(Type_js_1.Type.Unknown, [Type_js_1.Type.Unknown, Type_js_1.Type.Unknown]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalFloat, [Type_js_1.Type.OptionalFloat, Type_js_1.Type.OptionalFloat]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalBoolean, [Type_js_1.Type.OptionalBoolean, Type_js_1.Type.OptionalBoolean]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalTimestamp, [Type_js_1.Type.OptionalTimestamp, Type_js_1.Type.OptionalTimestamp]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalInteger, [Type_js_1.Type.OptionalInteger, Type_js_1.Type.OptionalInteger]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalBuffer, [Type_js_1.Type.OptionalBuffer, Type_js_1.Type.OptionalBuffer]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalString, [Type_js_1.Type.OptionalString, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalArray, [Type_js_1.Type.OptionalArray, Type_js_1.Type.OptionalArray]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalObject, [Type_js_1.Type.OptionalObject, Type_js_1.Type.OptionalObject]), Type_js_1.Type.functionType(Type_js_1.Type.OptionalFunction, [Type_js_1.Type.OptionalFunction, Type_js_1.Type.OptionalFunction])));
98
+ exports.funcEqual = new Constant_js_1.Constant((value1, value2) => (0, exports.equate)(value1, value2), typeEquator);
99
+ exports.funcNotEqual = new Constant_js_1.Constant((value1, value2) => !(0, exports.equate)(value1, value2), typeEquator);
100
+ exports.funcEncode = new Constant_js_1.Constant((value, encoding) => (0, exports.encode)(value, encoding), Type_js_1.Type.union(Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Boolean]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Timestamp, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Float, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Integer, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.String, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Array, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.Buffer, [Type_js_1.Type.Object, Type_js_1.Type.OptionalString])));
101
+ exports.funcFormat = new Constant_js_1.Constant((value, radix, separator = '') => (0, exports.format)(value, radix == null ? undefined : Number(radix), separator), Type_js_1.Type.union(Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Boolean]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Timestamp, Type_js_1.Type.OptionalInteger]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Float, Type_js_1.Type.OptionalInteger]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Integer, Type_js_1.Type.OptionalInteger]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Buffer]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Array, Type_js_1.Type.OptionalInteger, Type_js_1.Type.OptionalString]), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Object, Type_js_1.Type.OptionalInteger, Type_js_1.Type.OptionalString])));
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.constAN = exports.formatAN = void 0;
4
+ const Constant_js_1 = require("../../Constant.js");
5
+ const Type_js_1 = require("../../Type.js");
6
+ const Float_js_1 = require("../Float.js");
7
+ const Buffer_js_1 = require("../Buffer.js");
8
+ const formatAN = (value, whitespace) => {
9
+ if (value == null) {
10
+ return 'null';
11
+ }
12
+ if (typeof value === 'boolean') {
13
+ return value.toString();
14
+ }
15
+ if (value instanceof Date) {
16
+ return `@${value.toISOString()}`;
17
+ }
18
+ if (typeof value === 'number') {
19
+ return (0, Float_js_1.formatFloat)(value);
20
+ }
21
+ if (typeof value === 'bigint') {
22
+ return value.toString();
23
+ }
24
+ if (value instanceof ArrayBuffer) {
25
+ return `#${(0, Buffer_js_1.formatBuffer)(value)}`;
26
+ }
27
+ if (typeof value === 'string') {
28
+ return `"${value}"`;
29
+ }
30
+ if (Array.isArray(value)) {
31
+ const [prefix, suffix] = whitespace ? ['\n' + whitespace, '\n'] : ['', ''];
32
+ const lines = value.map((i) => `${prefix}${(0, exports.formatAN)(i, whitespace).split('\n').join(prefix)}`);
33
+ return `[${lines.join(',')}${suffix}]`;
34
+ }
35
+ if (typeof value === 'object') {
36
+ const [prefix, suffix] = whitespace ? ['\n' + whitespace, '\n'] : ['', ''];
37
+ const lines = Object.entries(value).map(([k, v]) => `${prefix}"${k}":${(0, exports.formatAN)(v, whitespace).split('\n').join(prefix)}`);
38
+ return `[${lines.join(',')}${suffix}]`;
39
+ }
40
+ return 'function';
41
+ };
42
+ exports.formatAN = formatAN;
43
+ const funcFormatAN = new Constant_js_1.Constant((value, whitespace) => (0, exports.formatAN)(value ?? null, whitespace), Type_js_1.Type.functionType(Type_js_1.Type.String, [Type_js_1.Type.Unknown, Type_js_1.Type.OptionalString]));
44
+ exports.constAN = {
45
+ Format: funcFormatAN,
46
+ };
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.constJSON = exports.formatJSON = void 0;
4
+ const Constant_js_1 = require("../../Constant.js");
5
+ const Type_js_1 = require("../../Type.js");
6
+ const typeJson = Type_js_1.Type.union(Type_js_1.Type.Void, Type_js_1.Type.Boolean, Type_js_1.Type.Timestamp, Type_js_1.Type.Float, Type_js_1.Type.Integer, Type_js_1.Type.String, Type_js_1.Type.Array, Type_js_1.Type.Object);
7
+ const formatJSON = (value, whitespace) => JSON.stringify(value, (_key, value) => typeof value === 'bigint' ? value.toString() : value, whitespace);
8
+ exports.formatJSON = formatJSON;
9
+ const funcFormatJSON = new Constant_js_1.Constant((value, whitespace) => (0, exports.formatJSON)(value ?? null, whitespace), Type_js_1.Type.functionType(Type_js_1.Type.String, [typeJson, Type_js_1.Type.OptionalString]));
10
+ const funcParseJSON = new Constant_js_1.Constant((value) => JSON.parse(value), Type_js_1.Type.functionType(typeJson, [Type_js_1.Type.String]));
11
+ exports.constJSON = {
12
+ Format: funcFormatJSON,
13
+ Parse: funcParseJSON,
14
+ };
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Value.js"), exports);
18
+ __exportStar(require("./Type.js"), exports);
19
+ __exportStar(require("./Affinirum.js"), exports);
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayNode = void 0;
4
+ const Node_js_1 = require("../Node.js");
5
+ const Constant_js_1 = require("../Constant.js");
6
+ const ConstantNode_js_1 = require("./ConstantNode.js");
7
+ const Type_js_1 = require("../Type.js");
8
+ class ArrayNode extends Node_js_1.Node {
9
+ _subnodes;
10
+ constructor(frame, _subnodes) {
11
+ super(frame);
12
+ this._subnodes = _subnodes;
13
+ }
14
+ get type() {
15
+ return Type_js_1.Type.Array;
16
+ }
17
+ compile(type) {
18
+ this.reduceType(type);
19
+ let constant = true;
20
+ for (let i = 0; i < this._subnodes.length; ++i) {
21
+ this._subnodes[i] = this._subnodes[i].compile(Type_js_1.Type.Unknown);
22
+ constant &&= this._subnodes[i].constant;
23
+ }
24
+ return constant ? new ConstantNode_js_1.ConstantNode(this, new Constant_js_1.Constant(this.evaluate(), this.type)) : this;
25
+ }
26
+ evaluate() {
27
+ return this._subnodes.map((s) => s.evaluate());
28
+ }
29
+ toString(ident = 0) {
30
+ const subnodes = this._subnodes.map((s) => s.toString(ident + 1)).join('\n');
31
+ return `${super.toString(ident)} array node subnodes:\n${subnodes}`;
32
+ }
33
+ }
34
+ exports.ArrayNode = ArrayNode;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BlockNode = void 0;
4
+ const Node_js_1 = require("../Node.js");
5
+ const Constant_js_1 = require("../Constant.js");
6
+ const ConstantNode_js_1 = require("./ConstantNode.js");
7
+ const Type_js_1 = require("../Type.js");
8
+ class BlockNode extends Node_js_1.Node {
9
+ _subnodes;
10
+ constructor(frame, _subnodes) {
11
+ super(frame);
12
+ this._subnodes = _subnodes;
13
+ }
14
+ get type() {
15
+ return this._subnodes[this._subnodes.length - 1].type;
16
+ }
17
+ compile(type) {
18
+ let constant = true;
19
+ for (let i = 0, last = this._subnodes.length - 1; i < this._subnodes.length; ++i) {
20
+ this._subnodes[i] = this._subnodes[i].compile(i < last ? Type_js_1.Type.Unknown : type);
21
+ constant &&= this._subnodes[i].constant;
22
+ }
23
+ return constant ? new ConstantNode_js_1.ConstantNode(this, new Constant_js_1.Constant(this.evaluate(), this.type)) : this;
24
+ }
25
+ evaluate() {
26
+ return this._subnodes.map((s) => s.evaluate())[this._subnodes.length - 1];
27
+ }
28
+ toString(ident = 0) {
29
+ const subnodes = this._subnodes.map((s) => s.toString(ident + 1)).join('\n');
30
+ return `${super.toString(ident)} block node subnodes:\n${subnodes}`;
31
+ }
32
+ }
33
+ exports.BlockNode = BlockNode;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CallNode = void 0;
4
+ const Node_js_1 = require("../Node.js");
5
+ const Constant_js_1 = require("../Constant.js");
6
+ const ConstantNode_js_1 = require("./ConstantNode.js");
7
+ const Type_js_1 = require("../Type.js");
8
+ class CallNode extends Node_js_1.Node {
9
+ _fnode;
10
+ _subnodes;
11
+ _type;
12
+ constructor(frame, _fnode, _subnodes) {
13
+ super(frame);
14
+ this._fnode = _fnode;
15
+ this._subnodes = _subnodes;
16
+ this._type = this._fnode.type.mergeFunctionAtom(Type_js_1.Type.Unknown, _subnodes.length)?.retType ?? Type_js_1.Type.Unknown;
17
+ }
18
+ get type() {
19
+ return this._type;
20
+ }
21
+ compile(type) {
22
+ this._fnode = this._fnode.compile(this._fnode.type);
23
+ const mergedFunctionAtom = this._fnode.type.mergeFunctionAtom(type, this._subnodes.length);
24
+ if (!mergedFunctionAtom) {
25
+ this.throwError(`function type ${this._fnode.type} mismatch with expected return type ${type} and ${this._subnodes.length} arguments`);
26
+ }
27
+ this._type = mergedFunctionAtom.retType;
28
+ if (this._subnodes.length < mergedFunctionAtom.minArity) {
29
+ this.throwError(`function requires ${mergedFunctionAtom.minArity} arguments not ${this._subnodes.length}`);
30
+ }
31
+ if (this._subnodes.length > mergedFunctionAtom.maxArity) {
32
+ this.throwError(`function requires ${mergedFunctionAtom.maxArity} arguments not ${this._subnodes.length}`);
33
+ }
34
+ let constant = this._fnode.constant;
35
+ for (let i = 0; i < this._subnodes.length; ++i) {
36
+ this._subnodes[i] = this._subnodes[i].compile(mergedFunctionAtom.argType(i));
37
+ constant &&= this._subnodes[i].constant;
38
+ }
39
+ return constant ? new ConstantNode_js_1.ConstantNode(this, new Constant_js_1.Constant(this.evaluate(), this.type)) : this;
40
+ }
41
+ evaluate() {
42
+ const func = this._fnode.evaluate();
43
+ if (typeof func !== 'function') {
44
+ this.throwError(`function expected not ${Type_js_1.Type.of(func)}`);
45
+ }
46
+ return func(...this._subnodes.map((node) => node.evaluate()));
47
+ }
48
+ toString(ident = 0) {
49
+ const subnodes = this._subnodes.map((s) => s.toString(ident + 1)).join('\n');
50
+ return `${super.toString(ident)} invocation node fnode:\n${this._fnode.toString(ident + 1)}\n`
51
+ + `${super.toString(ident)} invocation node subnodes:\n${subnodes}`;
52
+ }
53
+ }
54
+ exports.CallNode = CallNode;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConstantNode = void 0;
4
+ const Node_js_1 = require("../Node.js");
5
+ class ConstantNode extends Node_js_1.Node {
6
+ _constant;
7
+ _subnode;
8
+ constructor(frame, _constant, _subnode) {
9
+ super(frame);
10
+ this._constant = _constant;
11
+ this._subnode = _subnode;
12
+ }
13
+ get type() {
14
+ return this._constant.type;
15
+ }
16
+ compile(type) {
17
+ this._constant.type = this.reduceType(type);
18
+ this._subnode = this._subnode?.compile(this._constant.type.mergeFunctionAtomRetType(type));
19
+ return this;
20
+ }
21
+ evaluate() {
22
+ return this._constant.value;
23
+ }
24
+ get constant() {
25
+ return !this._subnode && this._constant.deterministic;
26
+ }
27
+ toString(ident = 0) {
28
+ return this._subnode
29
+ ? `${super.toString(ident)} constant node subnode:\n${this._subnode.toString(ident + 1)}`
30
+ : `${super.toString(ident)} constant node`;
31
+ }
32
+ }
33
+ exports.ConstantNode = ConstantNode;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LoopNode = void 0;
4
+ const Node_js_1 = require("../Node.js");
5
+ const Type_js_1 = require("../Type.js");
6
+ class LoopNode extends Node_js_1.Node {
7
+ _cnode;
8
+ _subnode;
9
+ constructor(frame, _cnode, _subnode) {
10
+ super(frame);
11
+ this._cnode = _cnode;
12
+ this._subnode = _subnode;
13
+ }
14
+ get type() {
15
+ return this._subnode.type;
16
+ }
17
+ compile(type) {
18
+ this._cnode = this._cnode.compile(Type_js_1.Type.Boolean);
19
+ this._subnode = this._subnode.compile(type);
20
+ return this;
21
+ }
22
+ evaluate() {
23
+ let value;
24
+ while (this._cnode.evaluate()) {
25
+ value = this._subnode.evaluate();
26
+ }
27
+ return value;
28
+ }
29
+ toString(ident = 0) {
30
+ return `${super.toString(ident)} loop node cnode:\n${this._cnode.toString(ident + 1)}\n`
31
+ + `${super.toString(ident)} subnode:\n${this._subnode.toString(ident + 1)}`;
32
+ }
33
+ }
34
+ exports.LoopNode = LoopNode;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ObjectNode = void 0;
4
+ const Node_js_1 = require("../Node.js");
5
+ const Constant_js_1 = require("../Constant.js");
6
+ const ConstantNode_js_1 = require("./ConstantNode.js");
7
+ const Type_js_1 = require("../Type.js");
8
+ class ObjectNode extends Node_js_1.Node {
9
+ _subnodes;
10
+ _type;
11
+ constructor(frame, _subnodes) {
12
+ super(frame);
13
+ this._subnodes = _subnodes;
14
+ this._type = Type_js_1.Type.Object;
15
+ }
16
+ get type() {
17
+ return this._type;
18
+ }
19
+ compile(type) {
20
+ this._type = this.reduceType(type);
21
+ let constant = true;
22
+ for (let i = 0; i < this._subnodes.length; ++i) {
23
+ this._subnodes[i][0] = this._subnodes[i][0].compile(Type_js_1.Type.String);
24
+ constant &&= this._subnodes[i][0].constant;
25
+ this._subnodes[i][1] = this._subnodes[i][1].compile(Type_js_1.Type.Unknown);
26
+ constant &&= this._subnodes[i][1].constant;
27
+ }
28
+ return constant ? new ConstantNode_js_1.ConstantNode(this, new Constant_js_1.Constant(this.evaluate(), this.type)) : this;
29
+ }
30
+ evaluate() {
31
+ const obj = {};
32
+ for (const [key, value] of this._subnodes) {
33
+ obj[key.evaluate()] = value.evaluate();
34
+ }
35
+ return obj;
36
+ }
37
+ toString(ident = 0) {
38
+ const subnodes = this._subnodes.map(([k, v]) => `${k.toString(ident + 1)}:\n${v.toString(ident + 1)}`).join('\n');
39
+ return `${super.toString(ident)} object node subnodes:\n${subnodes}`;
40
+ }
41
+ }
42
+ exports.ObjectNode = ObjectNode;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SwitchNode = void 0;
4
+ const Node_js_1 = require("../Node.js");
5
+ const Constant_js_1 = require("../Constant.js");
6
+ const ConstantNode_js_1 = require("./ConstantNode.js");
7
+ const Type_js_1 = require("../Type.js");
8
+ class SwitchNode extends Node_js_1.Node {
9
+ _cnode;
10
+ _subnodes;
11
+ _type;
12
+ constructor(frame, _cnode, _subnodes) {
13
+ super(frame);
14
+ this._cnode = _cnode;
15
+ this._subnodes = _subnodes;
16
+ if (_subnodes.length !== 2) {
17
+ this.throwError(`switch requires 2 subnodes not ${_subnodes.length}`);
18
+ }
19
+ this._type = Type_js_1.Type.union(...this._subnodes.map((i) => i.type));
20
+ }
21
+ get type() {
22
+ return Type_js_1.Type.union(...this._subnodes.map((i) => i.type));
23
+ }
24
+ compile(type) {
25
+ this._cnode = this._cnode.compile(Type_js_1.Type.Boolean);
26
+ this._type = type;
27
+ let constant = this._cnode.constant;
28
+ for (let i = 0; i < this._subnodes.length; ++i) {
29
+ this._subnodes[i] = this._subnodes[i].compile(type);
30
+ constant &&= this._subnodes[i].constant;
31
+ }
32
+ return constant
33
+ ? new ConstantNode_js_1.ConstantNode(this, new Constant_js_1.Constant(this.evaluate(), this.type))
34
+ : this;
35
+ }
36
+ evaluate() {
37
+ return this._cnode.evaluate() ? this._subnodes[0].evaluate() : this._subnodes[1].evaluate();
38
+ }
39
+ toString(ident = 0) {
40
+ const subnodes = this._subnodes.map((s) => s.toString(ident + 1)).join('\n');
41
+ return `${super.toString(ident)} switch node cnode:\n${this._cnode.toString(ident + 1)}\n`
42
+ + `${super.toString(ident)} switch node subnodes:\n${subnodes}`;
43
+ }
44
+ }
45
+ exports.SwitchNode = SwitchNode;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VariableNode = void 0;
4
+ const Node_js_1 = require("../Node.js");
5
+ class VariableNode extends Node_js_1.Node {
6
+ _variable;
7
+ _subnode;
8
+ constructor(frame, _variable, _subnode) {
9
+ super(frame);
10
+ this._variable = _variable;
11
+ this._subnode = _subnode;
12
+ }
13
+ get type() {
14
+ return this._variable.type;
15
+ }
16
+ compile(type) {
17
+ this._subnode = this._subnode?.compile(type);
18
+ this._variable.type = this.reduceType(this._subnode?.type ?? type);
19
+ return this;
20
+ }
21
+ evaluate() {
22
+ return this._subnode ? this._variable.value = this._subnode.evaluate() : this._variable.value;
23
+ }
24
+ toString(ident = 0) {
25
+ return this._subnode
26
+ ? `${super.toString(ident)} variable node subnode:\n${this._subnode.toString(ident + 1)}`
27
+ : `${super.toString(ident)} variable node`;
28
+ }
29
+ }
30
+ exports.VariableNode = VariableNode;
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,22 @@
1
+ import { Constant } from '../Constant.js';
2
+ export declare const funcFirst: Constant;
3
+ export declare const funcLast: Constant;
4
+ export declare const funcFirstIndex: Constant;
5
+ export declare const funcLastIndex: Constant;
6
+ export declare const funcEvery: Constant;
7
+ export declare const funcAny: Constant;
8
+ export declare const funcFlatten: Constant;
9
+ export declare const funcReverse: Constant;
10
+ export declare const funcMutate: Constant;
11
+ export declare const funcFilter: Constant;
12
+ export declare const funcReduce: Constant;
13
+ export declare const funcCompose: Constant;
14
+ export declare const funcPrepend: Constant;
15
+ export declare const funcAppend: Constant;
16
+ export declare const constArray: {
17
+ Join: Constant;
18
+ Range: Constant;
19
+ Unique: Constant;
20
+ Intersection: Constant;
21
+ Difference: Constant;
22
+ };
@@ -0,0 +1,90 @@
1
+ import { Constant } from '../Constant.js';
2
+ import { Type } from '../Type.js';
3
+ import { equate } from './Unknown.js';
4
+ const typePredicate = Type.functionType(Type.Boolean, [Type.Unknown, Type.OptionalInteger, Type.OptionalArray]);
5
+ const typeItemFinder = Type.functionType(Type.Unknown, [Type.Array, typePredicate]);
6
+ const typeIndexFinder = Type.functionType(Type.OptionalInteger, [Type.Array, typePredicate]);
7
+ const typeConditionSet = Type.functionType(Type.Boolean, [Type.Array, typePredicate]);
8
+ const typeVariadicInsert = Type.functionType(Type.Array, [Type.Array, Type.Unknown], true);
9
+ const typeArrayOperator = Type.functionType(Type.Array, [Type.Array, Type.Array]);
10
+ export const funcFirst = new Constant((value, predicate) => value?.find((v, i, a) => predicate(v, BigInt(i), a)), typeItemFinder);
11
+ export const funcLast = new Constant((value, predicate) => {
12
+ if (value == null) {
13
+ return undefined;
14
+ }
15
+ for (let i = value.length - 1; i >= 0; --i) {
16
+ if (predicate(value[i], BigInt(i), value)) {
17
+ return value[i];
18
+ }
19
+ }
20
+ return undefined;
21
+ }, typeItemFinder);
22
+ export const funcFirstIndex = new Constant((value, predicate) => {
23
+ if (value == null) {
24
+ return undefined;
25
+ }
26
+ const ix = value.findIndex((v, i, a) => predicate(v, BigInt(i), a));
27
+ return ix < 0 ? undefined : BigInt.asIntN(64, BigInt(ix));
28
+ }, typeIndexFinder);
29
+ export const funcLastIndex = new Constant((value, predicate) => {
30
+ if (value == null) {
31
+ return undefined;
32
+ }
33
+ for (let i = value.length - 1; i >= 0; i--) {
34
+ if (predicate(value[i], BigInt(i), value)) {
35
+ return BigInt.asIntN(64, BigInt(i));
36
+ }
37
+ }
38
+ return undefined;
39
+ }, typeIndexFinder);
40
+ export const funcEvery = new Constant((value, predicate) => value?.every((v, i, a) => predicate(v, BigInt(i), a)), typeConditionSet);
41
+ export const funcAny = new Constant((value, predicate) => value?.some((v, i, a) => predicate(v, BigInt(i), a)), typeConditionSet);
42
+ export const funcFlatten = new Constant((values, depth = 1n) => values?.flat(Number(depth)), Type.functionType(Type.Array, [Type.Array, Type.OptionalInteger]));
43
+ export const funcReverse = new Constant((value) => [...value].reverse(), Type.functionType(Type.Array, [Type.Array]));
44
+ export const funcMutate = new Constant((value, transform) => value?.map((v, i, a) => transform(v, BigInt(i), a)), Type.functionType(Type.Array, [Type.Array, Type.functionType(Type.Unknown, [Type.Unknown, Type.OptionalInteger, Type.OptionalArray])]));
45
+ export const funcFilter = new Constant((value, predicate) => value?.filter((v, i, a) => predicate(v, BigInt(i), a)), Type.functionType(Type.Array, [Type.Array, typePredicate]));
46
+ export const funcReduce = new Constant((value, reducer, initial) => initial != null
47
+ ? 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.functionType(Type.Unknown, [Type.Array, Type.functionType(Type.Unknown, [Type.Unknown, Type.Unknown, Type.OptionalInteger, Type.OptionalArray])]));
48
+ export const funcCompose = new Constant((value, callback) => {
49
+ if (value == null) {
50
+ return undefined;
51
+ }
52
+ const obj = {};
53
+ for (let i = 0; i < value.length; ++i) {
54
+ const key = value[i];
55
+ obj[key] = callback(obj, key, BigInt(i), value);
56
+ }
57
+ return obj;
58
+ }, Type.functionType(Type.Object, [Type.Array, Type.functionType(Type.Unknown, [Type.Object, Type.String, Type.OptionalInteger, Type.OptionalArray])]));
59
+ export const funcPrepend = new Constant((value, ...items) => {
60
+ value.unshift(items);
61
+ return value;
62
+ }, typeVariadicInsert);
63
+ export const funcAppend = new Constant((value, ...items) => {
64
+ value.push(items);
65
+ return value;
66
+ }, typeVariadicInsert);
67
+ const funcJoin = new Constant((...values) => values.flat(Infinity).reduce((acc, val) => [...acc, val], []), Type.functionType(Type.Array, [Type.Array], true));
68
+ const funcRange = new Constant((value1, value2) => {
69
+ const min = value1 < value2 ? value1 : value2;
70
+ const max = value1 > value2 ? value1 : value2;
71
+ return [...Array(Number(max - min)).keys()].map((i) => BigInt(i) + min);
72
+ }, Type.functionType(Type.Array, [Type.Integer, Type.Integer]));
73
+ const funcUnique = new Constant((value) => {
74
+ const result = [];
75
+ value.forEach((i) => {
76
+ if (result.every((v) => !equate(v, i))) {
77
+ result.push(i);
78
+ }
79
+ });
80
+ return result;
81
+ }, Type.functionType(Type.Array, [Type.Array]));
82
+ const funcIntersection = new Constant((value1, value2) => value1.filter((i) => value2.some((v) => equate(v, i))), typeArrayOperator);
83
+ const funcDifference = new Constant((value1, value2) => [...value1.filter((i) => value2.every((v) => !equate(v, i))), ...value2.filter((i) => value1.every((v) => !equate(v, i)))], typeArrayOperator);
84
+ export const constArray = {
85
+ Join: funcJoin,
86
+ Range: funcRange,
87
+ Unique: funcUnique,
88
+ Intersection: funcIntersection,
89
+ Difference: funcDifference,
90
+ };
@@ -0,0 +1,11 @@
1
+ import { Constant } from '../Constant.js';
2
+ export declare const funcOr: Constant;
3
+ export declare const funcAnd: Constant;
4
+ export declare const funcNot: Constant;
5
+ export declare const constBoolean: {
6
+ Or: Constant;
7
+ And: Constant;
8
+ Not: Constant;
9
+ Decode: Constant;
10
+ Parse: Constant;
11
+ };
@@ -0,0 +1,22 @@
1
+ import { Constant } from '../Constant.js';
2
+ import { Type } from '../Type.js';
3
+ const typeBooleanOrArray = Type.union(Type.Boolean, Type.arrayType([Type.Boolean]));
4
+ const typeBooleanLogic = Type.functionType(Type.Boolean, [typeBooleanOrArray], true);
5
+ export const funcOr = new Constant((...values) => values.flat().some((v) => v), typeBooleanLogic);
6
+ export const funcAnd = new Constant((...values) => values.flat().every((v) => v), typeBooleanLogic);
7
+ export const funcNot = new Constant((value) => !value, Type.functionType(Type.Boolean, [Type.Boolean]));
8
+ const funcDecodeBoolean = new Constant((value, byteOffset) => Boolean(new Uint8Array(value)[byteOffset == null ? 0 : Number(byteOffset)]), Type.functionType(Type.OptionalBoolean, [Type.Buffer, Type.OptionalInteger]));
9
+ const funcParseBoolean = new Constant((value) => {
10
+ if (value == null) {
11
+ return undefined;
12
+ }
13
+ const v = value.toLowerCase();
14
+ return v === 'true' ? true : v === 'false' ? false : undefined;
15
+ }, Type.functionType(Type.OptionalBoolean, [Type.String]));
16
+ export const constBoolean = {
17
+ Or: funcOr,
18
+ And: funcAnd,
19
+ Not: funcNot,
20
+ Decode: funcDecodeBoolean,
21
+ Parse: funcParseBoolean,
22
+ };