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.
Files changed (75) hide show
  1. package/README.md +214 -151
  2. package/dst/Affinirum.js +24 -15
  3. package/dst/Atom.d.ts +3 -1
  4. package/dst/Constant.js +1 -1
  5. package/dst/Functions.js +7 -4
  6. package/dst/Keywords.js +4 -4
  7. package/dst/ParserState.d.ts +2 -1
  8. package/dst/ParserState.js +22 -5
  9. package/dst/Type.d.ts +11 -7
  10. package/dst/Type.js +113 -38
  11. package/dst/Value.d.ts +1 -0
  12. package/dst/Value.js +19 -1
  13. package/dst/atom/ArrayAtom.d.ts +7 -3
  14. package/dst/atom/ArrayAtom.js +50 -8
  15. package/dst/atom/FunctionAtom.d.ts +4 -1
  16. package/dst/atom/FunctionAtom.js +65 -15
  17. package/dst/atom/ObjectAtom.d.ts +7 -5
  18. package/dst/atom/ObjectAtom.js +35 -13
  19. package/dst/atom/PrimitiveAtom.d.ts +3 -1
  20. package/dst/atom/PrimitiveAtom.js +10 -1
  21. package/dst/cjs/Affinirum.js +24 -15
  22. package/dst/cjs/Constant.js +1 -1
  23. package/dst/cjs/Functions.js +6 -3
  24. package/dst/cjs/Keywords.js +4 -4
  25. package/dst/cjs/ParserState.js +22 -5
  26. package/dst/cjs/Type.js +113 -38
  27. package/dst/cjs/Value.js +21 -0
  28. package/dst/cjs/atom/ArrayAtom.js +50 -8
  29. package/dst/cjs/atom/FunctionAtom.js +65 -15
  30. package/dst/cjs/atom/ObjectAtom.js +35 -13
  31. package/dst/cjs/atom/PrimitiveAtom.js +10 -1
  32. package/dst/cjs/native/Aggregable.js +2 -1
  33. package/dst/cjs/native/Array.js +10 -8
  34. package/dst/cjs/native/Boolean.js +5 -4
  35. package/dst/cjs/native/Enumerable.js +5 -4
  36. package/dst/cjs/native/Float.js +9 -8
  37. package/dst/cjs/native/Integer.js +23 -15
  38. package/dst/cjs/native/Iterable.js +2 -1
  39. package/dst/cjs/native/Number.js +18 -33
  40. package/dst/cjs/native/Object.js +2 -1
  41. package/dst/cjs/native/String.js +3 -2
  42. package/dst/cjs/native/Timestamp.js +2 -1
  43. package/dst/cjs/native/notation/IP.js +3 -2
  44. package/dst/cjs/node/ArrayNode.js +6 -6
  45. package/dst/cjs/node/CallNode.js +27 -23
  46. package/dst/cjs/node/CastNode.js +42 -0
  47. package/dst/cjs/node/ObjectNode.js +2 -4
  48. package/dst/cjs/node/SwitchNode.js +3 -1
  49. package/dst/cjs/node/VariableNode.js +3 -1
  50. package/dst/native/Aggregable.js +2 -1
  51. package/dst/native/Array.js +10 -8
  52. package/dst/native/Boolean.d.ts +1 -0
  53. package/dst/native/Boolean.js +4 -3
  54. package/dst/native/Enumerable.js +5 -4
  55. package/dst/native/Float.d.ts +2 -0
  56. package/dst/native/Float.js +8 -7
  57. package/dst/native/Integer.d.ts +5 -1
  58. package/dst/native/Integer.js +20 -14
  59. package/dst/native/Iterable.js +2 -1
  60. package/dst/native/Number.d.ts +0 -3
  61. package/dst/native/Number.js +17 -32
  62. package/dst/native/Object.js +2 -1
  63. package/dst/native/String.js +3 -2
  64. package/dst/native/Timestamp.js +2 -1
  65. package/dst/native/notation/IP.js +3 -2
  66. package/dst/node/ArrayNode.d.ts +1 -1
  67. package/dst/node/ArrayNode.js +6 -6
  68. package/dst/node/CallNode.js +27 -23
  69. package/dst/node/CastNode.d.ts +15 -0
  70. package/dst/node/CastNode.js +38 -0
  71. package/dst/node/ObjectNode.d.ts +1 -1
  72. package/dst/node/ObjectNode.js +2 -4
  73. package/dst/node/SwitchNode.js +3 -1
  74. package/dst/node/VariableNode.js +3 -1
  75. package/package.json +5 -4
@@ -1,10 +1,11 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  import { Type } from "../Type.js";
3
3
  const typeAccessor = Type.functionType(Type.Array, [Type.Object]);
4
+ const typeObjectOrArray = Type.union(Type.Object, Type.arrayType([Type.Object]));
4
5
  export const funcEntries = new Constant((value) => value ? Object.entries(value) : undefined, typeAccessor);
5
6
  export const funcKeys = new Constant((value) => value ? Object.keys(value) : undefined, Type.functionType(Type.arrayType([Type.String]), [Type.Object]));
6
7
  export const funcValues = new Constant((value) => value ? Object.values(value) : undefined, typeAccessor);
7
- const funcMerge = new Constant((...values) => values.flat().reduce((acc, val) => Object.assign(acc, val), Object.create(null)), Type.functionType(Type.Object, [Type.union(Type.arrayType([Type.Object]), Type.Object)], true));
8
+ const funcMerge = new Constant((values) => values.flat().reduce((acc, val) => Object.assign(acc, val), Object.create(null)), Type.functionType(Type.Object, [Type.arrayType([typeObjectOrArray])], true));
8
9
  export const constObject = Object.assign(Object.create(null), {
9
10
  Merge: funcMerge,
10
11
  });
@@ -1,5 +1,6 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  import { Type } from "../Type.js";
3
+ import { createInteger } from "./Integer.js";
3
4
  export const isSignSymbol = (c) => c === "+" || c === "-";
4
5
  export const isNumericSymbol = (c) => c >= "0" && c <= "9";
5
6
  export const isAlphanumericSymbol = (c) => c >= "a" && c <= "z" || c >= "0" && c <= "9";
@@ -203,14 +204,14 @@ export const funcChar = new Constant((value, pos) => value == null
203
204
  : value.charAt(pos < 0 ? value.length + Number(pos) : Number(pos)), Type.functionType(Type.OptionalString, [Type.String, Type.Integer]));
204
205
  export const funcCharCode = new Constant((value, pos) => value == null
205
206
  ? undefined
206
- : value.charCodeAt(pos < 0 ? value.length + Number(pos) : Number(pos)), Type.functionType(Type.OptionalInteger, [Type.String, Type.Float]));
207
+ : createInteger(value.charCodeAt(pos < 0 ? value.length + Number(pos) : Number(pos))), Type.functionType(Type.OptionalInteger, [Type.String, Type.Integer]));
207
208
  export const funcTrim = new Constant((value) => value?.trim(), typeStringMutator);
208
209
  export const funcTrimStart = new Constant((value) => value?.trimStart(), typeStringMutator);
209
210
  export const funcTrimEnd = new Constant((value) => value?.trimEnd(), typeStringMutator);
210
211
  export const funcLowerCase = new Constant((value) => value?.toLowerCase(), typeStringMutator);
211
212
  export const funcUpperCase = new Constant((value) => value?.toUpperCase(), typeStringMutator);
212
213
  export const funcSplit = new Constant((value, separator = " ") => value?.split(separator) ?? [], Type.functionType(Type.arrayType([Type.String]), [Type.String, Type.OptionalString]));
213
- export const funcReplaceWith = new Constant((value, replacement, ...search) => replaceWith(value, replacement, ...search.flat()), Type.functionType(Type.String, [Type.String, Type.String, typeStringOrArray], true));
214
+ export const funcReplaceWith = new Constant((value, replacement, search) => replaceWith(value, replacement, ...search.flat()), Type.functionType(Type.String, [Type.String, Type.String, Type.arrayType([typeStringOrArray])], true));
214
215
  const funcAlphanum = new Constant((value) => {
215
216
  if (!value) {
216
217
  return undefined;
@@ -1,5 +1,6 @@
1
1
  import { Constant } from "../Constant.js";
2
2
  import { Type } from "../Type.js";
3
+ import { boundInteger } from "./Integer.js";
3
4
  const typeTimestampPart = Type.functionType(Type.Integer, [Type.Timestamp, Type.OptionalBoolean]);
4
5
  const typeTimestampSince = Type.functionType(Type.Float, [Type.Timestamp, Type.Timestamp]);
5
6
  export const encodeTimestamp = (value, encoding = "i64") => {
@@ -78,7 +79,7 @@ export const funcHour = new Constant((value, utc = false) => value ? BigInt(utc
78
79
  export const funcMinute = new Constant((value, utc = false) => value ? BigInt(utc ? value.getUTCMinutes() : value.getMinutes()) : undefined, typeTimestampPart);
79
80
  export const funcSecond = new Constant((value, utc = false) => value ? BigInt(utc ? value.getUTCSeconds() : value.getSeconds()) : undefined, typeTimestampPart);
80
81
  export const funcMillisecond = new Constant((value, utc = false) => value ? BigInt(utc ? value.getUTCMilliseconds() : value.getMilliseconds()) : undefined, typeTimestampPart);
81
- export const funcEpochTime = new Constant((value, epoch = new Date(0)) => value ? BigInt.asIntN(64, BigInt(value.getTime() - epoch.getTime())) : undefined, Type.functionType(Type.Integer, [Type.Timestamp, Type.OptionalTimestamp]));
82
+ export const funcEpochTime = new Constant((value, epoch = new Date(0)) => value ? boundInteger(BigInt(value.getTime() - epoch.getTime())) : undefined, Type.functionType(Type.Integer, [Type.Timestamp, Type.OptionalTimestamp]));
82
83
  export const funcDaysSince = new Constant((value1, value2) => (value1?.getTime() - value2?.getTime()) / 86400000.0, typeTimestampSince);
83
84
  export const funcHoursSince = new Constant((value1, value2) => (value1?.getTime() - value2?.getTime()) / 3600000.0, typeTimestampSince);
84
85
  export const funcMinutesSince = new Constant((value1, value2) => (value1?.getTime() - value2?.getTime()) / 60000.0, typeTimestampSince);
@@ -192,7 +192,8 @@ class IPSet {
192
192
  }
193
193
  }
194
194
  }
195
- const funcMatchIP = new Constant((values, ...search) => {
195
+ const typeStringOrArray = Type.union(Type.String, Type.arrayType([Type.String]));
196
+ const funcMatchIP = new Constant((values, search) => {
196
197
  try {
197
198
  const ipset = new IPSet().add(values);
198
199
  for (const ip of search.flat()) {
@@ -206,7 +207,7 @@ const funcMatchIP = new Constant((values, ...search) => {
206
207
  }
207
208
  catch { }
208
209
  return false;
209
- }, Type.functionType(Type.Boolean, [Type.Array, Type.union(Type.String, Type.Array)], true));
210
+ }, Type.functionType(Type.Boolean, [Type.arrayType([Type.String]), Type.arrayType([typeStringOrArray])], true));
210
211
  const funcEncodeIP = new Constant((value) => {
211
212
  if (value == null) {
212
213
  return EmptyBuffer;
@@ -5,7 +5,7 @@ import { Type } from "../Type.js";
5
5
  export declare class ArrayNode extends Node {
6
6
  protected _subnodes: Node[];
7
7
  protected _type: Type;
8
- constructor(frame: ParserFrame, _subnodes: Node[], generic?: boolean);
8
+ constructor(frame: ParserFrame, _subnodes: Node[]);
9
9
  get type(): Type;
10
10
  compile(type: Type): Node;
11
11
  evaluate(): Value;
@@ -5,23 +5,23 @@ import { Type } from "../Type.js";
5
5
  export class ArrayNode extends Node {
6
6
  _subnodes;
7
7
  _type;
8
- constructor(frame, _subnodes, generic = false) {
8
+ constructor(frame, _subnodes) {
9
9
  super(frame);
10
10
  this._subnodes = _subnodes;
11
- this._type = generic
12
- ? Type.Array
13
- : Type.arrayType(_subnodes.map((i) => i.type));
11
+ this._type = Type.arrayType(_subnodes.map((i) => i.type));
14
12
  }
15
13
  get type() {
16
14
  return this._type;
17
15
  }
18
16
  compile(type) {
19
- this._type = this.reduceType(type);
20
17
  let constant = true;
21
18
  for (let i = 0; i < this._subnodes.length; ++i) {
22
- this._subnodes[i] = this._subnodes[i].compile(Type.Unknown);
19
+ const valType = type.arrayAtomValType(i);
20
+ this._subnodes[i] = this._subnodes[i].compile(valType);
23
21
  constant &&= this._subnodes[i].constant;
24
22
  }
23
+ this._type = Type.arrayType(this._subnodes.map((node) => node.type));
24
+ this._type = this.reduceType(type);
25
25
  if (constant) {
26
26
  return new ConstantNode(this, new Constant(this.evaluate(), this.type));
27
27
  }
@@ -2,6 +2,7 @@ import { Node } from "../Node.js";
2
2
  import { Constant } from "../Constant.js";
3
3
  import { ConstantNode } from "./ConstantNode.js";
4
4
  import { ArrayNode } from "./ArrayNode.js";
5
+ import { normalize } from "../Value.js";
5
6
  import { Type } from "../Type.js";
6
7
  import { JumpException } from "../JumpException.js";
7
8
  export class CallNode extends Node {
@@ -14,7 +15,7 @@ export class CallNode extends Node {
14
15
  this._subnodes = _subnodes;
15
16
  const functionAtoms = this._fnode.type.functionAtoms(Type.Unknown, this._subnodes.length);
16
17
  if (!functionAtoms.length) {
17
- this.throwError(`function ${this._fnode.type} does not take ${this._subnodes.length} arguments)`);
18
+ this.throwError(`value of type ${this._fnode.type} cannot be called with ${this._subnodes.length} argument(s)`);
18
19
  }
19
20
  this._type = Type.union(...functionAtoms.map((i) => i.retType));
20
21
  }
@@ -23,29 +24,32 @@ export class CallNode extends Node {
23
24
  }
24
25
  compile(type) {
25
26
  this._fnode = this._fnode.compile(this._fnode.type);
26
- const functionAtoms = this._fnode.type.functionAtoms(type, this._subnodes.length);
27
- if (!functionAtoms.length) {
28
- this.throwError(`function ${this._fnode.type} does not take ${this._subnodes.length} arguments returning ${type})`);
27
+ const functionAtoms_ = this._fnode.type.functionAtoms(type, this._subnodes.length);
28
+ let functionAtoms = functionAtoms_.filter((atom) => !atom.isVariadic);
29
+ if (!functionAtoms.length || !this._subnodes.every((node, index) => node.type.intersect(Type.union(...functionAtoms.map((atom) => atom.paramType(index) ?? Type.Unknown))))) {
30
+ functionAtoms = functionAtoms_.filter((atom) => atom.isVariadic);
31
+ }
32
+ if (!functionAtoms.length || !this._subnodes.every((node, index) => node.type.intersect(Type.union(...functionAtoms.map((atom) => atom.paramType(index) ?? Type.Unknown))))) {
33
+ this.throwError(`value of type ${this._fnode.type} cannot be called with ${this._subnodes.length} argument(s) and return a type compatible with ${type}`);
29
34
  }
30
35
  this._type = Type.union(...functionAtoms.map((i) => i.retType));
31
- if (this._fnode.constant) {
32
- let constant = true;
33
- for (let i = 0; i < this._subnodes.length; ++i) {
34
- const argTypes = functionAtoms.map((a) => a.argType(i));
35
- const argType = Type.union(...argTypes.filter((t) => t != null));
36
- this._subnodes[i] = this._subnodes[i].compile(argType);
37
- constant &&= this._subnodes[i].constant;
38
- }
39
- if (constant) {
40
- return new ConstantNode(this, new Constant(this.evaluate(), this.type));
41
- }
36
+ const arity = functionAtoms[0].isVariadic
37
+ ? Math.max(...functionAtoms.map((atom) => atom.arity))
38
+ : 0;
39
+ if (arity > 0) {
40
+ const frame = this._subnodes[arity - 1].starts();
41
+ const vnodes = this._subnodes.splice(arity - 1);
42
+ this._subnodes.push(new ArrayNode(frame.ends(vnodes[vnodes.length - 1]), vnodes));
42
43
  }
43
- else {
44
- const arity = functionAtoms.filter((i) => i.isVariadic).map((i) => i.arity).reduce((acc, val) => Math.max(acc, val), 0);
45
- if (arity > 0) {
46
- const frame = this._subnodes[arity - 1].starts();
47
- this._subnodes = this._subnodes.slice(0, arity - 1).concat(new ArrayNode(frame.ends(this._subnodes[this._subnodes.length - 1]), this._subnodes.slice(arity - 1)));
48
- }
44
+ let constant = this._fnode.constant;
45
+ for (let i = 0; i < this._subnodes.length; ++i) {
46
+ const argTypes = functionAtoms.map((a) => a.argType(i));
47
+ const argType = Type.union(...argTypes.filter((t) => t != null));
48
+ this._subnodes[i] = this._subnodes[i].compile(argType);
49
+ constant &&= this._subnodes[i].constant;
50
+ }
51
+ if (constant) {
52
+ return new ConstantNode(this, new Constant(this.evaluate(), this.type));
49
53
  }
50
54
  return this;
51
55
  }
@@ -55,12 +59,12 @@ export class CallNode extends Node {
55
59
  this.throwError(`function expected not ${Type.of(func)}`);
56
60
  }
57
61
  try {
58
- return func(...this._subnodes.map((node) => node.evaluate()));
62
+ return normalize(func(...this._subnodes.map((node) => node.evaluate())));
59
63
  }
60
64
  catch (e) {
61
65
  if (e instanceof JumpException) {
62
66
  if (e.jump === "return") {
63
- return e.value;
67
+ return normalize(e.value);
64
68
  }
65
69
  if (e.jump === "stop" || e.jump === "next") {
66
70
  throw this.throwError(`unexpected ${e.jump} jump`);
@@ -0,0 +1,15 @@
1
+ import { Node } from "../Node.js";
2
+ import { ParserFrame } from "../ParserFrame.js";
3
+ import { Type } from "../Type.js";
4
+ import { Value } from "../Value.js";
5
+ export declare class CastNode extends Node {
6
+ protected _subnode: Node;
7
+ protected readonly _target: Type;
8
+ protected _type: Type;
9
+ constructor(frame: ParserFrame, _subnode: Node, _target: Type);
10
+ get type(): Type;
11
+ compile(type: Type): Node;
12
+ evaluate(): Value;
13
+ get constant(): boolean;
14
+ toString(ident?: number): string;
15
+ }
@@ -0,0 +1,38 @@
1
+ import { Node } from "../Node.js";
2
+ import { Type } from "../Type.js";
3
+ export class CastNode extends Node {
4
+ _subnode;
5
+ _target;
6
+ _type;
7
+ constructor(frame, _subnode, _target) {
8
+ super(frame);
9
+ this._subnode = _subnode;
10
+ this._target = _target;
11
+ this._type = _target;
12
+ }
13
+ get type() {
14
+ return this._type;
15
+ }
16
+ compile(type) {
17
+ this._subnode = this._subnode.compile(Type.Unknown);
18
+ if (!this._subnode.type.intersect(this._target)) {
19
+ this.throwError(`cannot cast type ${this._subnode.type} to ${this._target}`);
20
+ }
21
+ this._type = this._target;
22
+ this._type = this.reduceType(type);
23
+ return this;
24
+ }
25
+ evaluate() {
26
+ const value = this._subnode.evaluate();
27
+ if (!this._type.acceptValue(value)) {
28
+ this.throwError(`cannot cast value of type ${Type.of(value)} to ${this._type}`);
29
+ }
30
+ return value;
31
+ }
32
+ get constant() {
33
+ return this._subnode.constant;
34
+ }
35
+ toString(ident = 0) {
36
+ return `${super.toString(ident)} cast node target <${this._target}>:\n${this._subnode.toString(ident + 1)}`;
37
+ }
38
+ }
@@ -5,7 +5,7 @@ import { Type } from "../Type.js";
5
5
  export declare class ObjectNode extends Node {
6
6
  protected _subnodes: [Node, Node][];
7
7
  protected _type: Type;
8
- constructor(frame: ParserFrame, _subnodes: [Node, Node][], generic?: boolean);
8
+ constructor(frame: ParserFrame, _subnodes: [Node, Node][]);
9
9
  get type(): Type;
10
10
  compile(type: Type): Node;
11
11
  evaluate(): Value;
@@ -5,12 +5,10 @@ import { Type } from "../Type.js";
5
5
  export class ObjectNode extends Node {
6
6
  _subnodes;
7
7
  _type;
8
- constructor(frame, _subnodes, generic = false) {
8
+ constructor(frame, _subnodes) {
9
9
  super(frame);
10
10
  this._subnodes = _subnodes;
11
- this._type = generic
12
- ? Type.Object
13
- : Type.objectType(Object.fromEntries(_subnodes.filter(([i]) => i instanceof ConstantNode && i.constant).map(([k, v]) => [k.evaluate(), v.type])));
11
+ this._type = Type.objectType(_subnodes.filter(([i]) => i instanceof ConstantNode && i.constant).map(([k, v]) => [k.evaluate(), v.type]));
14
12
  }
15
13
  get type() {
16
14
  return this._type;
@@ -34,7 +34,9 @@ export class SwitchNode extends Node {
34
34
  return this;
35
35
  }
36
36
  evaluate() {
37
- return this._cnode.evaluate() ? this._subnodes[0].evaluate() : this._subnodes[1].evaluate();
37
+ return this._cnode.evaluate()
38
+ ? this._subnodes[0].evaluate()
39
+ : this._subnodes[1].evaluate();
38
40
  }
39
41
  toString(ident = 0) {
40
42
  const subnodes = this._subnodes.map((s) => s.toString(ident + 1)).join("\n");
@@ -16,7 +16,9 @@ export class VariableNode extends Node {
16
16
  return this;
17
17
  }
18
18
  evaluate() {
19
- return this._subnode ? this._variable.value = this._subnode.evaluate() : this._variable.value;
19
+ return this._subnode
20
+ ? this._variable.value = this._subnode.evaluate()
21
+ : this._variable.value;
20
22
  }
21
23
  toString(ident = 0) {
22
24
  return this._subnode
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "affinirum",
3
- "version": "1.4.2",
3
+ "version": "1.5.0",
4
4
  "description": "Affinirum Scripting Language",
5
5
  "main": "dst/cjs/index.js",
6
6
  "module": "dst/index.js",
@@ -57,12 +57,13 @@
57
57
  "dependencies": {},
58
58
  "devDependencies": {
59
59
  "@eslint/js": "10.0.1",
60
+ "@stylistic/eslint-plugin": "~5.10.0",
60
61
  "@types/jasmine": "~6.0.0",
61
62
  "copyfiles": "~2.4.1",
62
- "eslint": "~10.6.0",
63
- "jasmine": "~6.3.0",
63
+ "eslint": "~10.11.0",
64
+ "jasmine": "~7.0.0",
64
65
  "rimraf": "~6.1.3",
65
66
  "typescript": "~6.0.3",
66
- "typescript-eslint": "~8.62.1"
67
+ "typescript-eslint": "~8.70.1"
67
68
  }
68
69
  }