@jplorg/jpl 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 (215) hide show
  1. package/README.md +34 -0
  2. package/lib/applyDefaults.js +34 -0
  3. package/lib/builtins/funcContains.js +25 -0
  4. package/lib/builtins/funcEndsWith.js +21 -0
  5. package/lib/builtins/funcError.js +11 -0
  6. package/lib/builtins/funcFromJSON.js +26 -0
  7. package/lib/builtins/funcHas.js +24 -0
  8. package/lib/builtins/funcIn.js +24 -0
  9. package/lib/builtins/funcKeys.js +20 -0
  10. package/lib/builtins/funcLength.js +24 -0
  11. package/lib/builtins/funcNow.js +10 -0
  12. package/lib/builtins/funcStartsWith.js +21 -0
  13. package/lib/builtins/funcToJSON.js +10 -0
  14. package/lib/builtins/funcToNumber.js +26 -0
  15. package/lib/builtins/funcToString.js +10 -0
  16. package/lib/builtins/funcTrim.js +17 -0
  17. package/lib/builtins/funcTrimEnd.js +17 -0
  18. package/lib/builtins/funcTrimStart.js +17 -0
  19. package/lib/builtins/funcType.js +10 -0
  20. package/lib/builtins/funcVoid.js +10 -0
  21. package/lib/builtins/index.js +158 -0
  22. package/lib/builtins/internals.js +8 -0
  23. package/lib/builtins/math.js +40 -0
  24. package/lib/builtins/native.js +164 -0
  25. package/lib/createSingleton.js +14 -0
  26. package/lib/index.js +80 -0
  27. package/lib/interpreter/index.js +57 -0
  28. package/lib/interpreter/parse.js +2377 -0
  29. package/lib/interpreter/parseFunction.js +24 -0
  30. package/lib/interpreter/system.js +10 -0
  31. package/lib/interpreter/util.js +349 -0
  32. package/lib/library/apply.js +142 -0
  33. package/lib/library/definition.js +9 -0
  34. package/lib/library/errors/adaptErrors.js +34 -0
  35. package/lib/library/errors/enclosure.js +16 -0
  36. package/lib/library/errors/error.js +18 -0
  37. package/lib/library/errors/execution.js +20 -0
  38. package/lib/library/errors/fatal.js +15 -0
  39. package/lib/library/errors/runtime.js +78 -0
  40. package/lib/library/errors/syntax.js +15 -0
  41. package/lib/library/functions.js +82 -0
  42. package/lib/library/index.js +336 -0
  43. package/lib/library/mux.js +106 -0
  44. package/lib/library/ops/index.js +60 -0
  45. package/lib/library/ops/op.js +180 -0
  46. package/lib/library/ops/opa.js +41 -0
  47. package/lib/library/ops/opc.js +59 -0
  48. package/lib/library/ops/opm.js +50 -0
  49. package/lib/library/ops/opu.js +77 -0
  50. package/lib/library/runtimeScope.js +38 -0
  51. package/lib/library/runtimeSignal.js +72 -0
  52. package/lib/library/types.js +305 -0
  53. package/lib/program/index.js +72 -0
  54. package/lib/program/ops/index.js +61 -0
  55. package/lib/program/ops/opAccess/index.js +55 -0
  56. package/lib/program/ops/opAccess/opaField.js +50 -0
  57. package/lib/program/ops/opAccess/opaFunction.js +50 -0
  58. package/lib/program/ops/opAccess/opaIter.js +31 -0
  59. package/lib/program/ops/opAccess/opaSlice.js +44 -0
  60. package/lib/program/ops/opAnd.js +30 -0
  61. package/lib/program/ops/opArrayConstructor.js +20 -0
  62. package/lib/program/ops/opAssignment/index.js +90 -0
  63. package/lib/program/ops/opAssignment/opaAssignField.js +125 -0
  64. package/lib/program/ops/opAssignment/opaAssignIter.js +69 -0
  65. package/lib/program/ops/opAssignment/opaAssignSlice.js +99 -0
  66. package/lib/program/ops/opAssignment/opuAddition.js +31 -0
  67. package/lib/program/ops/opAssignment/opuDivision.js +31 -0
  68. package/lib/program/ops/opAssignment/opuMultiplication.js +31 -0
  69. package/lib/program/ops/opAssignment/opuNullCoalescence.js +25 -0
  70. package/lib/program/ops/opAssignment/opuRemainder.js +31 -0
  71. package/lib/program/ops/opAssignment/opuSet.js +19 -0
  72. package/lib/program/ops/opAssignment/opuSubtraction.js +31 -0
  73. package/lib/program/ops/opAssignment/opuUpdate.js +19 -0
  74. package/lib/program/ops/opCalculation/index.js +57 -0
  75. package/lib/program/ops/opCalculation/opmAddition.js +49 -0
  76. package/lib/program/ops/opCalculation/opmDivision.js +42 -0
  77. package/lib/program/ops/opCalculation/opmMultiplication.js +40 -0
  78. package/lib/program/ops/opCalculation/opmRemainder.js +34 -0
  79. package/lib/program/ops/opCalculation/opmSubtraction.js +53 -0
  80. package/lib/program/ops/opComparison/index.js +59 -0
  81. package/lib/program/ops/opComparison/opcEqual.js +19 -0
  82. package/lib/program/ops/opComparison/opcGreater.js +19 -0
  83. package/lib/program/ops/opComparison/opcGreaterEqual.js +19 -0
  84. package/lib/program/ops/opComparison/opcLess.js +19 -0
  85. package/lib/program/ops/opComparison/opcLessEqual.js +19 -0
  86. package/lib/program/ops/opComparison/opcUnequal.js +19 -0
  87. package/lib/program/ops/opConstant.js +18 -0
  88. package/lib/program/ops/opConstantFalse.js +16 -0
  89. package/lib/program/ops/opConstantNull.js +16 -0
  90. package/lib/program/ops/opConstantTrue.js +16 -0
  91. package/lib/program/ops/opFunctionDefinition.js +21 -0
  92. package/lib/program/ops/opIf.js +38 -0
  93. package/lib/program/ops/opInterpolatedString.js +24 -0
  94. package/lib/program/ops/opNegation.js +25 -0
  95. package/lib/program/ops/opNot.js +16 -0
  96. package/lib/program/ops/opNullCoalescence.js +34 -0
  97. package/lib/program/ops/opNumber.js +18 -0
  98. package/lib/program/ops/opObjectConstructor.js +37 -0
  99. package/lib/program/ops/opOr.js +30 -0
  100. package/lib/program/ops/opOutputConcat.js +19 -0
  101. package/lib/program/ops/opString.js +18 -0
  102. package/lib/program/ops/opTry.js +37 -0
  103. package/lib/program/ops/opVariable.js +20 -0
  104. package/lib/program/ops/opVariableDefinition.js +24 -0
  105. package/lib/program/ops/opVoid.js +16 -0
  106. package/lib/program/ops/utils.js +35 -0
  107. package/lib/runtime/index.js +258 -0
  108. package/package.json +67 -0
  109. package/repl.js +212 -0
  110. package/src/applyDefaults.js +35 -0
  111. package/src/builtins/funcContains.js +27 -0
  112. package/src/builtins/funcEndsWith.js +21 -0
  113. package/src/builtins/funcError.js +7 -0
  114. package/src/builtins/funcFromJSON.js +24 -0
  115. package/src/builtins/funcHas.js +25 -0
  116. package/src/builtins/funcIn.js +25 -0
  117. package/src/builtins/funcKeys.js +20 -0
  118. package/src/builtins/funcLength.js +26 -0
  119. package/src/builtins/funcNow.js +5 -0
  120. package/src/builtins/funcStartsWith.js +21 -0
  121. package/src/builtins/funcToJSON.js +5 -0
  122. package/src/builtins/funcToNumber.js +25 -0
  123. package/src/builtins/funcToString.js +5 -0
  124. package/src/builtins/funcTrim.js +16 -0
  125. package/src/builtins/funcTrimEnd.js +16 -0
  126. package/src/builtins/funcTrimStart.js +16 -0
  127. package/src/builtins/funcType.js +5 -0
  128. package/src/builtins/funcVoid.js +5 -0
  129. package/src/builtins/index.js +145 -0
  130. package/src/builtins/internals.js +9 -0
  131. package/src/builtins/math.js +44 -0
  132. package/src/builtins/native.js +19 -0
  133. package/src/createSingleton.js +9 -0
  134. package/src/index.js +44 -0
  135. package/src/interpreter/index.js +48 -0
  136. package/src/interpreter/parse.js +1473 -0
  137. package/src/interpreter/parseFunction.js +20 -0
  138. package/src/interpreter/system.js +5 -0
  139. package/src/interpreter/util.js +237 -0
  140. package/src/library/apply.js +135 -0
  141. package/src/library/definition.js +3 -0
  142. package/src/library/errors/adaptErrors.js +28 -0
  143. package/src/library/errors/enclosure.js +11 -0
  144. package/src/library/errors/error.js +14 -0
  145. package/src/library/errors/execution.js +15 -0
  146. package/src/library/errors/fatal.js +10 -0
  147. package/src/library/errors/runtime.js +71 -0
  148. package/src/library/errors/syntax.js +10 -0
  149. package/src/library/functions.js +73 -0
  150. package/src/library/index.js +41 -0
  151. package/src/library/mux.js +98 -0
  152. package/src/library/ops/index.js +5 -0
  153. package/src/library/ops/op.js +174 -0
  154. package/src/library/ops/opa.js +35 -0
  155. package/src/library/ops/opc.js +53 -0
  156. package/src/library/ops/opm.js +44 -0
  157. package/src/library/ops/opu.js +71 -0
  158. package/src/library/runtimeScope.js +35 -0
  159. package/src/library/runtimeSignal.js +67 -0
  160. package/src/library/types.js +299 -0
  161. package/src/program/index.js +80 -0
  162. package/src/program/ops/index.js +82 -0
  163. package/src/program/ops/opAccess/index.js +55 -0
  164. package/src/program/ops/opAccess/opaField.js +58 -0
  165. package/src/program/ops/opAccess/opaFunction.js +52 -0
  166. package/src/program/ops/opAccess/opaIter.js +31 -0
  167. package/src/program/ops/opAccess/opaSlice.js +58 -0
  168. package/src/program/ops/opAnd.js +32 -0
  169. package/src/program/ops/opArrayConstructor.js +16 -0
  170. package/src/program/ops/opAssignment/index.js +102 -0
  171. package/src/program/ops/opAssignment/opaAssignField.js +156 -0
  172. package/src/program/ops/opAssignment/opaAssignIter.js +74 -0
  173. package/src/program/ops/opAssignment/opaAssignSlice.js +157 -0
  174. package/src/program/ops/opAssignment/opuAddition.js +29 -0
  175. package/src/program/ops/opAssignment/opuDivision.js +29 -0
  176. package/src/program/ops/opAssignment/opuMultiplication.js +29 -0
  177. package/src/program/ops/opAssignment/opuNullCoalescence.js +26 -0
  178. package/src/program/ops/opAssignment/opuRemainder.js +29 -0
  179. package/src/program/ops/opAssignment/opuSet.js +15 -0
  180. package/src/program/ops/opAssignment/opuSubtraction.js +29 -0
  181. package/src/program/ops/opAssignment/opuUpdate.js +15 -0
  182. package/src/program/ops/opCalculation/index.js +64 -0
  183. package/src/program/ops/opCalculation/opmAddition.js +63 -0
  184. package/src/program/ops/opCalculation/opmDivision.js +47 -0
  185. package/src/program/ops/opCalculation/opmMultiplication.js +52 -0
  186. package/src/program/ops/opCalculation/opmRemainder.js +49 -0
  187. package/src/program/ops/opCalculation/opmSubtraction.js +72 -0
  188. package/src/program/ops/opComparison/index.js +67 -0
  189. package/src/program/ops/opComparison/opcEqual.js +17 -0
  190. package/src/program/ops/opComparison/opcGreater.js +17 -0
  191. package/src/program/ops/opComparison/opcGreaterEqual.js +17 -0
  192. package/src/program/ops/opComparison/opcLess.js +17 -0
  193. package/src/program/ops/opComparison/opcLessEqual.js +17 -0
  194. package/src/program/ops/opComparison/opcUnequal.js +17 -0
  195. package/src/program/ops/opConstant.js +13 -0
  196. package/src/program/ops/opConstantFalse.js +11 -0
  197. package/src/program/ops/opConstantNull.js +11 -0
  198. package/src/program/ops/opConstantTrue.js +11 -0
  199. package/src/program/ops/opFunctionDefinition.js +17 -0
  200. package/src/program/ops/opIf.js +44 -0
  201. package/src/program/ops/opInterpolatedString.js +27 -0
  202. package/src/program/ops/opNegation.js +26 -0
  203. package/src/program/ops/opNot.js +11 -0
  204. package/src/program/ops/opNullCoalescence.js +36 -0
  205. package/src/program/ops/opNumber.js +13 -0
  206. package/src/program/ops/opObjectConstructor.js +40 -0
  207. package/src/program/ops/opOr.js +32 -0
  208. package/src/program/ops/opOutputConcat.js +17 -0
  209. package/src/program/ops/opString.js +13 -0
  210. package/src/program/ops/opTry.js +38 -0
  211. package/src/program/ops/opVariable.js +17 -0
  212. package/src/program/ops/opVariableDefinition.js +18 -0
  213. package/src/program/ops/opVoid.js +11 -0
  214. package/src/program/ops/utils.js +19 -0
  215. package/src/runtime/index.js +309 -0
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Multiplex the specified array of arguments by calling cb for all possible combinations of arguments.
3
+ * If the specified `args` array is empty, `cb` is called once without any arguments.
4
+ *
5
+ * `mux([[1, 2], [3, 4]], cb)` for example calls:
6
+ * - `cb(1, 3)`
7
+ * - `cb(1, 4)`
8
+ * - `cb(2, 3)`
9
+ * - `cb(2, 4)`
10
+ */
11
+ function mux(args, cb) {
12
+ const argCount = args.length;
13
+ if (argCount === 1) {
14
+ args[0].forEach((arg) => cb(arg));
15
+ return;
16
+ }
17
+ let execCount = 1;
18
+ const indices = args.map((arg) => {
19
+ const argLength = arg?.length ?? 0;
20
+ execCount *= argLength;
21
+ return [argLength, 0, arg];
22
+ });
23
+ if (execCount === 0) return;
24
+ const buffer = args.map(([v]) => v);
25
+ let execIndex = 0;
26
+ for (;;) {
27
+ cb(...buffer);
28
+ execIndex += 1;
29
+ if (execIndex >= execCount) break;
30
+ // determine next combination
31
+ for (let argIndex = argCount - 1; argIndex >= 0; argIndex -= 1) {
32
+ const arg = indices[argIndex];
33
+ const [max, current, values] = arg;
34
+ const next = current + 1;
35
+ if (next < max) {
36
+ arg[1] = next;
37
+ buffer[argIndex] = values[next];
38
+ break;
39
+ }
40
+ arg[1] = 0;
41
+ [buffer[argIndex]] = values;
42
+ }
43
+ }
44
+ }
45
+
46
+ export default mux;
47
+
48
+ /** Multiplex the specified array of arguments and return the results produced by the callbacks */
49
+ export function muxOne(args, cb) {
50
+ const argCount = args.length;
51
+ if (argCount === 1) return args[0].map((arg) => cb(arg));
52
+ let execCount = 1;
53
+ const indices = args.map((arg) => {
54
+ const argLength = arg?.length ?? 0;
55
+ execCount *= argLength;
56
+ return [argLength, 0, arg];
57
+ });
58
+ if (execCount === 0) return [];
59
+ const outputs = new Array(execCount);
60
+ const buffer = args.map(([v]) => v);
61
+ let execIndex = 0;
62
+ for (;;) {
63
+ outputs[execIndex] = cb(...buffer);
64
+ execIndex += 1;
65
+ if (execIndex >= execCount) break;
66
+ // determine next combination
67
+ for (let argIndex = argCount - 1; argIndex >= 0; argIndex -= 1) {
68
+ const arg = indices[argIndex];
69
+ const [max, current, values] = arg;
70
+ const next = current + 1;
71
+ if (next < max) {
72
+ arg[1] = next;
73
+ buffer[argIndex] = values[next];
74
+ break;
75
+ }
76
+ arg[1] = 0;
77
+ [buffer[argIndex]] = values;
78
+ }
79
+ }
80
+ return outputs;
81
+ }
82
+
83
+ /** Multiplex the specified array of arguments asynchronously and return the results produced by the callbacks */
84
+ export function muxAsync(args, cb) {
85
+ return Promise.all(muxOne(args, cb));
86
+ }
87
+
88
+ /** Multiplex the specified array of arguments asynchronously and return a single array of all merged result arrays produced by the callbacks */
89
+ export async function muxAll(args, cb) {
90
+ return mergeSegments(await muxAsync(args, cb));
91
+ }
92
+
93
+ /** Create a single array from the specified array segments */
94
+ export function mergeSegments(segments) {
95
+ if (segments.length === 0) return segments;
96
+ if (segments.length === 1) return segments[0];
97
+ return segments.flat(1);
98
+ }
@@ -0,0 +1,5 @@
1
+ export * from './op';
2
+ export * from './opa';
3
+ export * from './opc';
4
+ export * from './opm';
5
+ export * from './opu';
@@ -0,0 +1,174 @@
1
+ /**
2
+ * { pipe: function, selectors: [opa] }
3
+ *
4
+ * { pipe: [op], selectors: [opa] }
5
+ */
6
+ export const OP_ACCESS = '$.';
7
+
8
+ /**
9
+ * { pipes: [function] }
10
+ *
11
+ * { pipes: [[op]] }
12
+ */
13
+ export const OP_AND = 'and';
14
+
15
+ /**
16
+ * { pipe: function }
17
+ *
18
+ * { pipe: [op] }
19
+ */
20
+ export const OP_ARRAY_CONSTRUCTOR = '[]';
21
+
22
+ /**
23
+ * { pipe: function, selectors: [opa], assignment: opu }
24
+ *
25
+ * { pipe: [op], selectors: [opa], assignment: [opu] }
26
+ */
27
+ export const OP_ASSIGNMENT = '$=';
28
+
29
+ /**
30
+ * { pipe: function, operations: [opm] }
31
+ *
32
+ * { pipe: [op], operations: [opm] }
33
+ */
34
+ export const OP_CALCULATION = 'clc';
35
+
36
+ /**
37
+ * { pipe: function, comparisons: [opc] }
38
+ *
39
+ * { pipe: [op], comparisons: [opc] }
40
+ */
41
+ export const OP_COMPARISON = 'cmp';
42
+
43
+ /**
44
+ * { value: any }
45
+ *
46
+ * { value: any }
47
+ */
48
+ export const OP_CONSTANT = 'cnt';
49
+
50
+ /**
51
+ * {}
52
+ *
53
+ * {}
54
+ */
55
+ export const OP_CONSTANT_FALSE = 'fls';
56
+
57
+ /**
58
+ * {}
59
+ *
60
+ * {}
61
+ */
62
+ export const OP_CONSTANT_NULL = 'nul';
63
+
64
+ /**
65
+ * {}
66
+ *
67
+ * {}
68
+ */
69
+ export const OP_CONSTANT_TRUE = 'tru';
70
+
71
+ /**
72
+ * { argNames: [string], pipe: function }
73
+ *
74
+ * { argNames: [string], pipe: [op] }
75
+ */
76
+ export const OP_FUNCTION_DEFINITION = 'fun';
77
+
78
+ /**
79
+ * { ifs: [{ if: function, then: function }], else: function }
80
+ *
81
+ * { ifs: [{ if: [op], then: [op] }], else: [op] }
82
+ */
83
+ export const OP_IF = 'if';
84
+
85
+ /**
86
+ * { interpolations: [{ before: string, pipe: function }], after: string }
87
+ *
88
+ * { interpolations: [{ before: string, pipe: [op] }], after: string }
89
+ */
90
+ export const OP_INTERPOLATED_STRING = '"$"';
91
+
92
+ /**
93
+ * {}
94
+ *
95
+ * {}
96
+ */
97
+ export const OP_NEGATION = 'neg';
98
+
99
+ /**
100
+ * {}
101
+ *
102
+ * {}
103
+ */
104
+ export const OP_NOT = 'not';
105
+
106
+ /**
107
+ * { pipes: [function] }
108
+ *
109
+ * { pipes: [[op]] }
110
+ */
111
+ export const OP_NULL_COALESCENCE = '??';
112
+
113
+ /**
114
+ * { number: number }
115
+ *
116
+ * { number: number }
117
+ */
118
+ export const OP_NUMBER = 'nbr';
119
+
120
+ /**
121
+ * { fields: [{ key: function, value: function, optional: boolean }] }
122
+ *
123
+ * { fields: [{ key: [op], value: [op], optional: boolean }] }
124
+ */
125
+ export const OP_OBJECT_CONSTRUCTOR = '{}';
126
+
127
+ /**
128
+ * { pipes: [function] }
129
+ *
130
+ * { pipes: [[op]] }
131
+ */
132
+ export const OP_OR = 'or';
133
+
134
+ /**
135
+ * { pipes: [function] }
136
+ *
137
+ * { pipes: [[op]] }
138
+ */
139
+ export const OP_OUTPUT_CONCAT = ',';
140
+
141
+ /**
142
+ * { string: string }
143
+ *
144
+ * { string: string }
145
+ */
146
+ export const OP_STRING = '""';
147
+
148
+ /**
149
+ * { try: function, catch: function }
150
+ *
151
+ * { try: [op], catch: [op] }
152
+ */
153
+ export const OP_TRY = 'try';
154
+
155
+ /**
156
+ * { name: string }
157
+ *
158
+ * { name: string }
159
+ */
160
+ export const OP_VARIABLE = 'var';
161
+
162
+ /**
163
+ * { name: string, pipe: function }
164
+ *
165
+ * { name: string, pipe: [op] }
166
+ */
167
+ export const OP_VARIABLE_DEFINITION = 'va=';
168
+
169
+ /**
170
+ * {}
171
+ *
172
+ * {}
173
+ */
174
+ export const OP_VOID = 'vod';
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Sub operator for OP_ACCESS
3
+ *
4
+ * { pipe: function, optional: boolean }
5
+ *
6
+ * { pipe: [op], optional: boolean }
7
+ */
8
+ export const OPA_FIELD = '$.$';
9
+
10
+ /**
11
+ * Sub operator for OP_ACCESS
12
+ *
13
+ * { args: [function], bound: boolean, optional: boolean }
14
+ *
15
+ * { args: [[op]], bound: boolean, optional: boolean }
16
+ */
17
+ export const OPA_FUNCTION = '$()';
18
+
19
+ /**
20
+ * Sub operator for OP_ACCESS
21
+ *
22
+ * { optional: boolean }
23
+ *
24
+ * { optional: boolean }
25
+ */
26
+ export const OPA_ITER = '$[]';
27
+
28
+ /**
29
+ * Sub operator for OP_ACCESS
30
+ *
31
+ * { from: function, to: function, optional: boolean }
32
+ *
33
+ * { from: [op], to: [op], optional: boolean }
34
+ */
35
+ export const OPA_SLICE = '[:]';
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Sub operator for OP_COMPARISON
3
+ *
4
+ * { by: function }
5
+ *
6
+ * { by: [op] }
7
+ */
8
+ export const OPC_EQUAL = '==';
9
+
10
+ /**
11
+ * Sub operator for OP_COMPARISON
12
+ *
13
+ * { by: function }
14
+ *
15
+ * { by: [op] }
16
+ */
17
+ export const OPC_GREATER = '>';
18
+
19
+ /**
20
+ * Sub operator for OP_COMPARISON
21
+ *
22
+ * { by: function }
23
+ *
24
+ * { by: [op] }
25
+ */
26
+ export const OPC_GREATEREQUAL = '>=';
27
+
28
+ /**
29
+ * Sub operator for OP_COMPARISON
30
+ *
31
+ * { by: function }
32
+ *
33
+ * { by: [op] }
34
+ */
35
+ export const OPC_LESS = '<';
36
+
37
+ /**
38
+ * Sub operator for OP_COMPARISON
39
+ *
40
+ * { by: function }
41
+ *
42
+ * { by: [op] }
43
+ */
44
+ export const OPC_LESSEQUAL = '<=';
45
+
46
+ /**
47
+ * Sub operator for OP_COMPARISON
48
+ *
49
+ * { by: function }
50
+ *
51
+ * { by: [op] }
52
+ */
53
+ export const OPC_UNEQUAL = '!=';
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Sub operator for OP_CALCULATION
3
+ *
4
+ * { by: function }
5
+ *
6
+ * { by: [op] }
7
+ */
8
+ export const OPM_ADDITION = '+';
9
+
10
+ /**
11
+ * Sub operator for OP_CALCULATION
12
+ *
13
+ * { by: function }
14
+ *
15
+ * { by: [op] }
16
+ */
17
+ export const OPM_DIVISION = '/';
18
+
19
+ /**
20
+ * Sub operator for OP_CALCULATION
21
+ *
22
+ * { by: function }
23
+ *
24
+ * { by: [op] }
25
+ */
26
+ export const OPM_MULTIPLICATION = '*';
27
+
28
+ /**
29
+ * Sub operator for OP_CALCULATION
30
+ *
31
+ * { by: function }
32
+ *
33
+ * { by: [op] }
34
+ */
35
+ export const OPM_REMAINDER = '%';
36
+
37
+ /**
38
+ * Sub operator for OP_CALCULATION
39
+ *
40
+ * { by: function }
41
+ *
42
+ * { by: [op] }
43
+ */
44
+ export const OPM_SUBTRACTION = '-';
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Sub operator for OP_ASSIGNMENT
3
+ *
4
+ * { pipe: function }
5
+ *
6
+ * { pipe: [op] }
7
+ */
8
+ export const OPU_ADDITION = '+=';
9
+
10
+ /**
11
+ * Sub operator for OP_ASSIGNMENT
12
+ *
13
+ * { pipe: function }
14
+ *
15
+ * { pipe: [op] }
16
+ */
17
+ export const OPU_DIVISION = '/=';
18
+
19
+ /**
20
+ * Sub operator for OP_ASSIGNMENT
21
+ *
22
+ * { pipe: function }
23
+ *
24
+ * { pipe: [op] }
25
+ */
26
+ export const OPU_MULTIPLICATION = '*=';
27
+
28
+ /**
29
+ * Sub operator for OP_ASSIGNMENT
30
+ *
31
+ * { pipe: function }
32
+ *
33
+ * { pipe: [op] }
34
+ */
35
+ export const OPU_NULL_COALESCENCE = '?=';
36
+
37
+ /**
38
+ * Sub operator for OP_ASSIGNMENT
39
+ *
40
+ * { pipe: function }
41
+ *
42
+ * { pipe: [op] }
43
+ */
44
+ export const OPU_REMAINDER = '%=';
45
+
46
+ /**
47
+ * Sub operator for OP_ASSIGNMENT
48
+ *
49
+ * { pipe: function }
50
+ *
51
+ * { pipe: [op] }
52
+ */
53
+ export const OPU_SET = '=';
54
+
55
+ /**
56
+ * Sub operator for OP_ASSIGNMENT
57
+ *
58
+ * { pipe: function }
59
+ *
60
+ * { pipe: [op] }
61
+ */
62
+ export const OPU_SUBTRACTION = '-=';
63
+
64
+ /**
65
+ * Sub operator for OP_ASSIGNMENT
66
+ *
67
+ * { pipe: function }
68
+ *
69
+ * { pipe: [op] }
70
+ */
71
+ export const OPU_UPDATE = '|=';
@@ -0,0 +1,35 @@
1
+ import { applyObject } from './apply';
2
+ import JPLRuntimeSignal from './runtimeSignal';
3
+
4
+ /**
5
+ * Runtime scope for managing contextual runtime information.
6
+ *
7
+ * Fields of the scope must not be modified directly.
8
+ * Instead, `JPLRuntimeScope.next` should be used to create a sub scope with the specified modifications.
9
+ */
10
+ class JPLRuntimeScope {
11
+ constructor(presets = {}) {
12
+ this._state = {
13
+ signal: presets.signal ?? new JPLRuntimeSignal(),
14
+ vars: presets.vars ?? {},
15
+ };
16
+ }
17
+
18
+ get signal() {
19
+ return this._state.signal;
20
+ }
21
+
22
+ get vars() {
23
+ return this._state.vars;
24
+ }
25
+
26
+ /** Inherit the next scope based on the specified modifications */
27
+ next(modifications) {
28
+ return new JPLRuntimeScope({
29
+ signal: modifications.signal ?? this.signal,
30
+ vars: applyObject(this.vars, Object.entries(modifications.vars ?? {})),
31
+ });
32
+ }
33
+ }
34
+
35
+ export default JPLRuntimeScope;
@@ -0,0 +1,67 @@
1
+ import JPLFatalError from './errors/fatal';
2
+
3
+ /** Runtime signal for managing execution lifecycle */
4
+ class JPLRuntimeSignal {
5
+ constructor(parent) {
6
+ this._parent = parent;
7
+ this._exited = false;
8
+ this._subscriptions = {};
9
+ this._nextSubscriptionKey = 0;
10
+ }
11
+
12
+ /** Return the signal's parent */
13
+ get parent() {
14
+ return this._parent;
15
+ }
16
+
17
+ /** Check whether the current runtime area has been requested to be exited */
18
+ get exited() {
19
+ this._exited ||= this.parent?.exited ?? false;
20
+ return this._exited;
21
+ }
22
+
23
+ /** Check that the current runtime area has not been requested to be exited and throw a fatal error otherwise */
24
+ checkHealth() {
25
+ if (this.exited) throw new JPLFatalError('execution aborted');
26
+ }
27
+
28
+ /**
29
+ * Request the current runtime area to be exited.
30
+ * This also involves all child areas (introduced using `JPLRuntimeSignal.next`).
31
+ */
32
+ exit = () => {
33
+ if (this.exited) return;
34
+ this._exited = true;
35
+ Object.values(this._subscriptions).forEach((subscription) => {
36
+ subscription();
37
+ });
38
+ this._subscriptions = {};
39
+ };
40
+
41
+ /**
42
+ * Subscribe for when the current runtime area is requested to be exited.
43
+ * This also involves all parent areas.
44
+ * The function returns an unsubscription hook which must be called when completed in order to prevent memory leaks.
45
+ * If the area has already been exited when subscribing, the callback is called immediately after the current event cycle.
46
+ */
47
+ subscribe = (cb) => {
48
+ if (this.exited) {
49
+ setTimeout(cb);
50
+ return () => {};
51
+ }
52
+ const key = this._nextSubscriptionKey;
53
+ this._nextSubscriptionKey += 1;
54
+ this._subscriptions[key] = cb;
55
+ const unsubscribeParent = (this.parent?.subscribe ?? (() => () => {}))(cb);
56
+ const unsubscribe = () => {
57
+ delete this._subscriptions[key];
58
+ unsubscribeParent();
59
+ };
60
+ return unsubscribe;
61
+ };
62
+
63
+ /** Inherit the next child area for the current one */
64
+ next = () => new JPLRuntimeSignal(this);
65
+ }
66
+
67
+ export default JPLRuntimeSignal;