@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
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # [JPL - JSON Processing Language](../docs/index.md) - JavaScript implementation
2
+
3
+ ## Code example
4
+
5
+ ```js
6
+ import jpl from '@jplorg/jpl';
7
+
8
+ (async () => {
9
+ const inputs = [null];
10
+
11
+ const results = await jpl.run('"Hello, 🌎!"', inputs);
12
+
13
+ console.log(...results);
14
+ })();
15
+ ```
16
+
17
+ ## REPL
18
+
19
+ The package provides a CLI REPL, which can be used as a language playground.
20
+
21
+ ```sh
22
+ > jpl-repl # or `npm --prefix js start`
23
+
24
+ Welcome to JPL v1.0.0.
25
+ Type ":h" for more information.
26
+
27
+ > "Hello, \('🌎', 'JPL')!"
28
+ "Hello, 🌎!", "Hello, JPL!"
29
+ >
30
+ ```
31
+
32
+ ## Extending JPL
33
+
34
+ TODO: inform about the runtime API, functions, JPLTypes and different error types
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ /** Apply the specified defaults to the config */
8
+ function applyDefaults(config, defaults, ...strictPaths) {
9
+ const iter = (c, d, s) => {
10
+ if (Array.isArray(c)) return c;
11
+ if (typeof c === 'object') {
12
+ if (c === null || Array.isArray(d) || typeof d !== 'object' || d === null) return c;
13
+ const result = {
14
+ ...c
15
+ };
16
+ const isStrict = s.some(entry => entry.length === 0);
17
+ Object.keys({
18
+ ...c,
19
+ ...d
20
+ }).forEach(key => {
21
+ if (isStrict) {
22
+ if (!Object.hasOwn(result, key)) result[key] = d[key];
23
+ } else {
24
+ result[key] = iter(result[key], d[key], s.filter(entry => entry[0] === key).map(entry => entry.slice(1)));
25
+ }
26
+ if (result[key] === undefined) delete result[key];
27
+ });
28
+ return result;
29
+ }
30
+ return c === undefined ? d : c;
31
+ };
32
+ return iter(config, defaults, strictPaths.map(path => typeof path === 'string' ? path.split('.') : path));
33
+ }
34
+ var _default = exports.default = applyDefaults;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _library = require("../library");
8
+ function builtin(runtime, signal, next, input, arg0) {
9
+ const value = runtime.unwrapValue(input);
10
+ const t = runtime.type(value);
11
+ const token = runtime.unwrapValue(arg0 ?? null);
12
+ const tt = runtime.type(token);
13
+ switch (t) {
14
+ case 'array':
15
+ return next(value.some(item => runtime.equals(item, token)));
16
+ case 'string':
17
+ if (tt === 'string') return next(value.includes(token));
18
+ break;
19
+ case 'object':
20
+ return next(Object.values(value).some(item => runtime.equals(item, token)));
21
+ default:
22
+ }
23
+ throw new _library.JPLTypeError('%s (%*<100v) cannot contain %s (%*<100v)', t, value, tt, token);
24
+ }
25
+ var _default = exports.default = builtin;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _library = require("../library");
8
+ function builtin(runtime, signal, next, input, arg0) {
9
+ const value = runtime.unwrapValue(input);
10
+ const t = runtime.type(value);
11
+ const token = runtime.unwrapValue(arg0 ?? null);
12
+ const tt = runtime.type(token);
13
+ switch (t) {
14
+ case 'string':
15
+ if (tt === 'string') return next(value.endsWith(token));
16
+ break;
17
+ default:
18
+ }
19
+ throw new _library.JPLTypeError('%s (%*<100v) cannot end with %s (%*<100v)', t, value, tt, token);
20
+ }
21
+ var _default = exports.default = builtin;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _library = require("../library");
8
+ function builtin(runtime, signal, next, input) {
9
+ throw new _library.JPLRuntimeError(input);
10
+ }
11
+ var _default = exports.default = builtin;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _library = require("../library");
8
+ function builtin(runtime, signal, next, input) {
9
+ const value = runtime.unwrapValue(input);
10
+ const t = runtime.type(value);
11
+ switch (t) {
12
+ case 'string':
13
+ {
14
+ let result;
15
+ try {
16
+ result = JSON.parse(value);
17
+ } catch (err) {
18
+ throw new _library.JPLRuntimeError(err.message);
19
+ }
20
+ return next(result);
21
+ }
22
+ default:
23
+ }
24
+ throw new _library.JPLTypeError('%s (%*<100v) cannot be parsed as JSON', t, value);
25
+ }
26
+ var _default = exports.default = builtin;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _library = require("../library");
8
+ function builtin(runtime, signal, next, input, arg0) {
9
+ const value = runtime.unwrapValue(input);
10
+ const tv = runtime.type(value);
11
+ const key = runtime.unwrapValue(arg0 ?? null);
12
+ const tk = runtime.type(key);
13
+ switch (tv) {
14
+ case 'array':
15
+ if (tk === 'number') return next(key >= 0 && key < value.length);
16
+ break;
17
+ case 'object':
18
+ if (tk === 'string') return next(Object.hasOwn(value, key));
19
+ break;
20
+ default:
21
+ }
22
+ throw new _library.JPLTypeError('%s (%*<100v) cannot have %s (%*<100v) as key', tv, value, tk, key);
23
+ }
24
+ var _default = exports.default = builtin;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _library = require("../library");
8
+ function builtin(runtime, signal, next, input, arg0) {
9
+ const key = runtime.unwrapValue(input);
10
+ const tk = runtime.type(key);
11
+ const value = runtime.unwrapValue(arg0 ?? null);
12
+ const tv = runtime.type(value);
13
+ switch (tv) {
14
+ case 'array':
15
+ if (tk === 'number') return next(key >= 0 && key < value.length);
16
+ break;
17
+ case 'object':
18
+ if (tk === 'string') return next(Object.hasOwn(value, key));
19
+ break;
20
+ default:
21
+ }
22
+ throw new _library.JPLTypeError('%s (%*<100v) cannot have %s (%*<100v) as key', tv, value, tk, key);
23
+ }
24
+ var _default = exports.default = builtin;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _library = require("../library");
8
+ function builtin(runtime, signal, next, input) {
9
+ const value = runtime.unwrapValue(input);
10
+ const t = runtime.type(value);
11
+ switch (t) {
12
+ case 'array':
13
+ return next(value.map((_, i) => i));
14
+ case 'object':
15
+ return next(Object.keys(value));
16
+ default:
17
+ }
18
+ throw new _library.JPLTypeError('%s (%*<100v) has no keys', t, value);
19
+ }
20
+ var _default = exports.default = builtin;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _library = require("../library");
8
+ function builtin(runtime, signal, next, input) {
9
+ const value = runtime.unwrapValue(input);
10
+ const t = runtime.type(value);
11
+ switch (t) {
12
+ case 'null':
13
+ return next(0);
14
+ case 'array':
15
+ return next(value.length);
16
+ case 'string':
17
+ return next([...value].length);
18
+ case 'object':
19
+ return next(Object.keys(value).length);
20
+ default:
21
+ }
22
+ throw new _library.JPLTypeError('%s (%*<100v) has no length', t, value);
23
+ }
24
+ var _default = exports.default = builtin;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ function builtin(runtime, signal, next) {
8
+ return next(Date.now());
9
+ }
10
+ var _default = exports.default = builtin;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _library = require("../library");
8
+ function builtin(runtime, signal, next, input, arg0) {
9
+ const value = runtime.unwrapValue(input);
10
+ const t = runtime.type(value);
11
+ const token = runtime.unwrapValue(arg0 ?? null);
12
+ const tt = runtime.type(token);
13
+ switch (t) {
14
+ case 'string':
15
+ if (tt === 'string') return next(value.startsWith(token));
16
+ break;
17
+ default:
18
+ }
19
+ throw new _library.JPLTypeError('%s (%*<100v) cannot start with %s (%*<100v)', t, value, tt, token);
20
+ }
21
+ var _default = exports.default = builtin;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ function builtin(runtime, signal, next, input) {
8
+ return next(runtime.stringifyJSON(input, false));
9
+ }
10
+ var _default = exports.default = builtin;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _library = require("../library");
8
+ function builtin(runtime, signal, next, input) {
9
+ const value = runtime.unwrapValue(input);
10
+ const t = runtime.type(value);
11
+ switch (t) {
12
+ case 'number':
13
+ return next(value);
14
+ case 'string':
15
+ {
16
+ const result = runtime.normalizeValue(+value);
17
+ if (value === '' || runtime.type(result) !== 'number') {
18
+ throw new _library.JPLTypeConversionError('%s (%*<100v) does not contain a valid number', t, value);
19
+ }
20
+ return next(result);
21
+ }
22
+ default:
23
+ }
24
+ throw new _library.JPLTypeError('%s (%*<100v) cannot be parsed as a number', t, value);
25
+ }
26
+ var _default = exports.default = builtin;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ function builtin(runtime, signal, next, input) {
8
+ return next(runtime.stringifyJSON(input, true));
9
+ }
10
+ var _default = exports.default = builtin;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _library = require("../library");
8
+ async function builtin(runtime, signal, next, input) {
9
+ const t = runtime.type(input);
10
+ switch (t) {
11
+ case 'string':
12
+ return next(await runtime.alterValue(input, value => value.trim()));
13
+ default:
14
+ }
15
+ throw new _library.JPLTypeError('%s (%*<100v) cannot be trimmed', t, runtime.unwrapValue(input));
16
+ }
17
+ var _default = exports.default = builtin;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _library = require("../library");
8
+ async function builtin(runtime, signal, next, input) {
9
+ const t = runtime.type(input);
10
+ switch (t) {
11
+ case 'string':
12
+ return next(await runtime.alterValue(input, value => value.trimEnd()));
13
+ default:
14
+ }
15
+ throw new _library.JPLTypeError('%s (%*<100v) cannot be trimmed', t, runtime.unwrapValue(input));
16
+ }
17
+ var _default = exports.default = builtin;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _library = require("../library");
8
+ async function builtin(runtime, signal, next, input) {
9
+ const t = runtime.type(input);
10
+ switch (t) {
11
+ case 'string':
12
+ return next(await runtime.alterValue(input, value => value.trimStart()));
13
+ default:
14
+ }
15
+ throw new _library.JPLTypeError('%s (%*<100v) cannot be trimmed', t, runtime.unwrapValue(input));
16
+ }
17
+ var _default = exports.default = builtin;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ function builtin(runtime, signal, next, input) {
8
+ return next(runtime.type(input));
9
+ }
10
+ var _default = exports.default = builtin;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ function builtin() {
8
+ return [];
9
+ }
10
+ var _default = exports.default = builtin;
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _createSingleton = _interopRequireDefault(require("../createSingleton"));
8
+ var _system = _interopRequireDefault(require("../interpreter/system"));
9
+ var _library = require("../library");
10
+ var internals = _interopRequireWildcard(require("./internals"));
11
+ var native = _interopRequireWildcard(require("./native"));
12
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
13
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+ const builtins = `
16
+ # There are additional builtins that are implemented directly in the corresponding languages.
17
+ # Also the functions in the "internals" namespace are not exposed to the public and are only to be used by the builtins below.
18
+
19
+ func map(f): ([.[] | f()])
20
+ | func mapValues(f): (.[] |= f())
21
+ | func select(f): (if f() then . else void() end)
22
+ | func reduce(f, s): (
23
+ l = length()
24
+ | func i(i, n, c): (if n < l then i(i, n + 1, f->(.[n], c, n)) else c end)
25
+ | i(i, 0, s)
26
+ )
27
+ | func while(cond, f): (
28
+ func i(i): (if cond() then ., i->(f(), i) else void() end)
29
+ | i(i)
30
+ )
31
+ | func until(cond, f): (
32
+ func i(i): (if cond() then . else i->(f(), i) end)
33
+ | i(i)
34
+ )
35
+ | func range(from, to, step): (
36
+ t = type->(from) | if t != "number" then error->("cannot use \\(t) as a number") end
37
+ | t = type->(to) | if t != "number" then error->("cannot use \\(t) as a number") end
38
+ | s = (step | if . != 0 and . != null then abs() else 1 end)
39
+ | while->(
40
+ from,
41
+ if from <= to then func (): (. < to) else func (): (. > to) end,
42
+ if from <= to then func (): (. + s) else func (): (. - s) end
43
+ )
44
+ )
45
+ | func hasContent(): (not contains->(["null", "function"], type()) and not contains->([[], {}, ""], .))
46
+
47
+
48
+ # type selectors:
49
+
50
+ | func arrays(): (select(func (): (type() == "array")))
51
+ | func objects(): (select(func (): (type() == "object")))
52
+ | func iterables(): (select(func (): (contains->(["array", "object"], type()))))
53
+ | func scalars(): (select(func (): (not contains->(["array", "object"], type()))))
54
+ | func booleans(): (select(func (): (type() == "boolean")))
55
+ | func numbers(): (select(func (): (type() == "number")))
56
+ | func strings(): (select(func (): (type() == "string")))
57
+ | func nulls(): (select(func (): (type() == "null")))
58
+ | func functions(): (select(func (): (type() == "function")))
59
+ | func nullLikes(): (select(func (): (contains->(["null", "function"], type()))))
60
+ | func values(): (select(func (): (not contains->(["null", "function"], type()))))
61
+ | func contents(): (select(hasContent))
62
+
63
+
64
+ | func toEntries(): (o = . | keys() | map(func (): ({ key: ., value: o[.] })))
65
+ | func fromEntries(): (
66
+ reduce(
67
+ func (s): (s + { (.key ?? .Key ?? .name ?? .Name): if has("value") then .value else .Value end }),
68
+ {}
69
+ )
70
+ )
71
+ | func withEntries(f): (toEntries() | map(f) | fromEntries())
72
+
73
+ | func add(): (reduce(func (sum): (sum + .)))
74
+ | func join(sep): (
75
+ reduce(
76
+ func (sum): (
77
+ if sum == null then "" else sum + sep end
78
+ + (. | toString())
79
+ )
80
+ ) ?? ""
81
+ )
82
+ | func sortBy(f): ([.[] | [[f()], .]] | internals.sortEntries() | [.[][1]])
83
+ | func sort(): (sortBy(func (): (.)))
84
+ | func groupBy(f): (
85
+ key = 0 | value = 1
86
+ | [.[] | [[f()], [.]]] | internals.sortEntries()
87
+ | if . == []
88
+ then .
89
+ else
90
+ reduce->(
91
+ .[1:],
92
+ func (sum): (if sum[-1][key] == .[key] then (sum)[-1][value] += .[value] else sum + [.] end),
93
+ .[:1]
94
+ )
95
+ | [.[][value]]
96
+ end
97
+ )
98
+ | func group(): (groupBy(func (): (.)))
99
+ | func uniqueBy(f): (groupBy(f) | map(func (): (.[0])))
100
+ | func unique(): (group() | map(func (): (.[0])))
101
+ | func recurseBy(f, cond): (
102
+ c = cond ?? func (): (. != null)
103
+ | func r(r): (., (f() | select(c) | r(r)))
104
+ | r(r)
105
+ )
106
+ | func recurse(cond): (recurseBy(func (): ((arrays(), objects()) | .[]), cond))
107
+ | func reverse(): ([.[length() - 1 - range(0, length())]])
108
+ | func minBy(f): (
109
+ key = 0 | value = 1
110
+ | [.[] | [[f()], .]]
111
+ | reduce->(.[1:], func (sum): (if .[key] < sum[key] then . else sum end), .[0])
112
+ | .[value]
113
+ )
114
+ | func min(): (reduce->(.[1:], func (sum): (if . < sum then . else sum end), .[0]))
115
+ | func maxBy(f): (
116
+ key = 0 | value = 1
117
+ | [.[] | [[f()], .]]
118
+ | reduce->(.[1:], func (sum): (if .[key] > sum[key] then . else sum end), .[0])
119
+ | .[value]
120
+ )
121
+ | func max(): (reduce->(.[1:], func (sum): (if . > sum then . else sum end), .[0]))
122
+ | func first(f): ([f()][0])
123
+ | func nth(which, f): (
124
+ [f()]
125
+ | .[if which | type() == "number" then which else length() | which() end]
126
+ )
127
+ | func last(f): ([f()][-1])
128
+ | func isEmpty(f): (first(func (): ((f() | false), true)))
129
+ | func allBy(f, cond): (isEmpty(func (): (f() | cond() and void())))
130
+ | func all(cond): (allBy(func (): (.[]), cond))
131
+ | func anyBy(f, cond): (not isEmpty(func (): (f() | cond() or void())))
132
+ | func any(cond): (anyBy(func (): (.[]), cond))
133
+ `;
134
+ const getBuiltins = (0, _createSingleton.default)(async () => {
135
+ await undefined;
136
+ let result = {
137
+ ...native
138
+ };
139
+ const program = await _system.default.parse(builtins, {
140
+ runtime: {
141
+ vars: {
142
+ internals,
143
+ ...native
144
+ },
145
+ adjustResult: (value, scope) => {
146
+ const {
147
+ internals: _,
148
+ ...vars
149
+ } = scope.vars;
150
+ result = (0, _library.applyObject)(result, Object.entries(vars));
151
+ return [];
152
+ }
153
+ }
154
+ });
155
+ await program.run([null]);
156
+ return result;
157
+ });
158
+ var _default = exports.default = getBuiltins;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.sortEntries = void 0;
7
+ var _library = require("../library");
8
+ const sortEntries = exports.sortEntries = (0, _library.nativeFunction)(async (runtime, input) => [await runtime.alterValue(input, value => [...value].sort((a, b) => runtime.compareArrays(runtime.unwrapValue(a)[0], runtime.unwrapValue(b)[0])))]);
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.trunc = exports.tan = exports.sqrt = exports.sin = exports.round = exports.pow = exports.log10 = exports.log = exports.floor = exports.exp = exports.cos = exports.ceil = exports.atan = exports.asin = exports.acos = exports.abs = void 0;
7
+ var _library = require("../library");
8
+ function unwrapNumber(runtime, v) {
9
+ const t = runtime.type(v);
10
+ const u = runtime.unwrapValue(v);
11
+ if (t !== 'number') {
12
+ throw new _library.JPLTypeError('%s (%*<100v) cannot be used for mathematical operations', t, u);
13
+ }
14
+ return u;
15
+ }
16
+ function funcMath(alter) {
17
+ return async function builtin(runtime, signal, next, input, ...args) {
18
+ const t = runtime.type(input);
19
+ if (t !== 'number') {
20
+ throw new _library.JPLTypeError('%s (%*<100v) cannot be used for mathematical operations', t, runtime.unwrapValue(input));
21
+ }
22
+ return next(await runtime.alterValue(input, value => alter(runtime, value, ...args)));
23
+ };
24
+ }
25
+ const pow = exports.pow = funcMath((runtime, value, arg0) => value ** unwrapNumber(runtime, arg0 ?? null));
26
+ const sqrt = exports.sqrt = funcMath((runtime, value) => Math.sqrt(value));
27
+ const exp = exports.exp = funcMath((runtime, value) => Math.exp(value));
28
+ const log = exports.log = funcMath((runtime, value) => Math.log(value));
29
+ const log10 = exports.log10 = funcMath((runtime, value) => Math.log10(value));
30
+ const sin = exports.sin = funcMath((runtime, value) => Math.sin(value));
31
+ const cos = exports.cos = funcMath((runtime, value) => Math.cos(value));
32
+ const tan = exports.tan = funcMath((runtime, value) => Math.tan(value));
33
+ const asin = exports.asin = funcMath((runtime, value) => Math.asin(value));
34
+ const acos = exports.acos = funcMath((runtime, value) => Math.acos(value));
35
+ const atan = exports.atan = funcMath((runtime, value) => Math.atan(value));
36
+ const ceil = exports.ceil = funcMath((runtime, value) => Math.ceil(value));
37
+ const floor = exports.floor = funcMath((runtime, value) => Math.floor(value));
38
+ const round = exports.round = funcMath((runtime, value) => Math.round(value));
39
+ const trunc = exports.trunc = funcMath((runtime, value) => Math.trunc(value));
40
+ const abs = exports.abs = funcMath((runtime, value) => Math.abs(value));