@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,145 @@
1
+ import createSingleton from '../createSingleton';
2
+ import systemInterpreter from '../interpreter/system';
3
+ import { applyObject } from '../library';
4
+ import * as internals from './internals';
5
+ import * as native from './native';
6
+
7
+ const builtins = `
8
+ # There are additional builtins that are implemented directly in the corresponding languages.
9
+ # Also the functions in the "internals" namespace are not exposed to the public and are only to be used by the builtins below.
10
+
11
+ func map(f): ([.[] | f()])
12
+ | func mapValues(f): (.[] |= f())
13
+ | func select(f): (if f() then . else void() end)
14
+ | func reduce(f, s): (
15
+ l = length()
16
+ | func i(i, n, c): (if n < l then i(i, n + 1, f->(.[n], c, n)) else c end)
17
+ | i(i, 0, s)
18
+ )
19
+ | func while(cond, f): (
20
+ func i(i): (if cond() then ., i->(f(), i) else void() end)
21
+ | i(i)
22
+ )
23
+ | func until(cond, f): (
24
+ func i(i): (if cond() then . else i->(f(), i) end)
25
+ | i(i)
26
+ )
27
+ | func range(from, to, step): (
28
+ t = type->(from) | if t != "number" then error->("cannot use \\(t) as a number") end
29
+ | t = type->(to) | if t != "number" then error->("cannot use \\(t) as a number") end
30
+ | s = (step | if . != 0 and . != null then abs() else 1 end)
31
+ | while->(
32
+ from,
33
+ if from <= to then func (): (. < to) else func (): (. > to) end,
34
+ if from <= to then func (): (. + s) else func (): (. - s) end
35
+ )
36
+ )
37
+ | func hasContent(): (not contains->(["null", "function"], type()) and not contains->([[], {}, ""], .))
38
+
39
+
40
+ # type selectors:
41
+
42
+ | func arrays(): (select(func (): (type() == "array")))
43
+ | func objects(): (select(func (): (type() == "object")))
44
+ | func iterables(): (select(func (): (contains->(["array", "object"], type()))))
45
+ | func scalars(): (select(func (): (not contains->(["array", "object"], type()))))
46
+ | func booleans(): (select(func (): (type() == "boolean")))
47
+ | func numbers(): (select(func (): (type() == "number")))
48
+ | func strings(): (select(func (): (type() == "string")))
49
+ | func nulls(): (select(func (): (type() == "null")))
50
+ | func functions(): (select(func (): (type() == "function")))
51
+ | func nullLikes(): (select(func (): (contains->(["null", "function"], type()))))
52
+ | func values(): (select(func (): (not contains->(["null", "function"], type()))))
53
+ | func contents(): (select(hasContent))
54
+
55
+
56
+ | func toEntries(): (o = . | keys() | map(func (): ({ key: ., value: o[.] })))
57
+ | func fromEntries(): (
58
+ reduce(
59
+ func (s): (s + { (.key ?? .Key ?? .name ?? .Name): if has("value") then .value else .Value end }),
60
+ {}
61
+ )
62
+ )
63
+ | func withEntries(f): (toEntries() | map(f) | fromEntries())
64
+
65
+ | func add(): (reduce(func (sum): (sum + .)))
66
+ | func join(sep): (
67
+ reduce(
68
+ func (sum): (
69
+ if sum == null then "" else sum + sep end
70
+ + (. | toString())
71
+ )
72
+ ) ?? ""
73
+ )
74
+ | func sortBy(f): ([.[] | [[f()], .]] | internals.sortEntries() | [.[][1]])
75
+ | func sort(): (sortBy(func (): (.)))
76
+ | func groupBy(f): (
77
+ key = 0 | value = 1
78
+ | [.[] | [[f()], [.]]] | internals.sortEntries()
79
+ | if . == []
80
+ then .
81
+ else
82
+ reduce->(
83
+ .[1:],
84
+ func (sum): (if sum[-1][key] == .[key] then (sum)[-1][value] += .[value] else sum + [.] end),
85
+ .[:1]
86
+ )
87
+ | [.[][value]]
88
+ end
89
+ )
90
+ | func group(): (groupBy(func (): (.)))
91
+ | func uniqueBy(f): (groupBy(f) | map(func (): (.[0])))
92
+ | func unique(): (group() | map(func (): (.[0])))
93
+ | func recurseBy(f, cond): (
94
+ c = cond ?? func (): (. != null)
95
+ | func r(r): (., (f() | select(c) | r(r)))
96
+ | r(r)
97
+ )
98
+ | func recurse(cond): (recurseBy(func (): ((arrays(), objects()) | .[]), cond))
99
+ | func reverse(): ([.[length() - 1 - range(0, length())]])
100
+ | func minBy(f): (
101
+ key = 0 | value = 1
102
+ | [.[] | [[f()], .]]
103
+ | reduce->(.[1:], func (sum): (if .[key] < sum[key] then . else sum end), .[0])
104
+ | .[value]
105
+ )
106
+ | func min(): (reduce->(.[1:], func (sum): (if . < sum then . else sum end), .[0]))
107
+ | func maxBy(f): (
108
+ key = 0 | value = 1
109
+ | [.[] | [[f()], .]]
110
+ | reduce->(.[1:], func (sum): (if .[key] > sum[key] then . else sum end), .[0])
111
+ | .[value]
112
+ )
113
+ | func max(): (reduce->(.[1:], func (sum): (if . > sum then . else sum end), .[0]))
114
+ | func first(f): ([f()][0])
115
+ | func nth(which, f): (
116
+ [f()]
117
+ | .[if which | type() == "number" then which else length() | which() end]
118
+ )
119
+ | func last(f): ([f()][-1])
120
+ | func isEmpty(f): (first(func (): ((f() | false), true)))
121
+ | func allBy(f, cond): (isEmpty(func (): (f() | cond() and void())))
122
+ | func all(cond): (allBy(func (): (.[]), cond))
123
+ | func anyBy(f, cond): (not isEmpty(func (): (f() | cond() or void())))
124
+ | func any(cond): (anyBy(func (): (.[]), cond))
125
+ `;
126
+
127
+ const getBuiltins = createSingleton(async () => {
128
+ await undefined;
129
+
130
+ let result = { ...native };
131
+ const program = await systemInterpreter.parse(builtins, {
132
+ runtime: {
133
+ vars: { internals, ...native },
134
+ adjustResult: (value, scope) => {
135
+ const { internals: _, ...vars } = scope.vars;
136
+ result = applyObject(result, Object.entries(vars));
137
+ return [];
138
+ },
139
+ },
140
+ });
141
+ await program.run([null]);
142
+ return result;
143
+ });
144
+
145
+ export default getBuiltins;
@@ -0,0 +1,9 @@
1
+ import { nativeFunction } from '../library';
2
+
3
+ export const sortEntries = nativeFunction(async (runtime, input) => [
4
+ await runtime.alterValue(input, (value) =>
5
+ [...value].sort((a, b) =>
6
+ runtime.compareArrays(runtime.unwrapValue(a)[0], runtime.unwrapValue(b)[0]),
7
+ ),
8
+ ),
9
+ ]);
@@ -0,0 +1,44 @@
1
+ import { JPLTypeError } from '../library';
2
+
3
+ function unwrapNumber(runtime, v) {
4
+ const t = runtime.type(v);
5
+ const u = runtime.unwrapValue(v);
6
+ if (t !== 'number') {
7
+ throw new JPLTypeError('%s (%*<100v) cannot be used for mathematical operations', t, u);
8
+ }
9
+ return u;
10
+ }
11
+
12
+ function funcMath(alter) {
13
+ return async function builtin(runtime, signal, next, input, ...args) {
14
+ const t = runtime.type(input);
15
+ if (t !== 'number') {
16
+ throw new JPLTypeError(
17
+ '%s (%*<100v) cannot be used for mathematical operations',
18
+ t,
19
+ runtime.unwrapValue(input),
20
+ );
21
+ }
22
+ return next(await runtime.alterValue(input, (value) => alter(runtime, value, ...args)));
23
+ };
24
+ }
25
+
26
+ export const pow = funcMath((runtime, value, arg0) => value ** unwrapNumber(runtime, arg0 ?? null));
27
+ export const sqrt = funcMath((runtime, value) => Math.sqrt(value));
28
+ export const exp = funcMath((runtime, value) => Math.exp(value));
29
+ export const log = funcMath((runtime, value) => Math.log(value));
30
+ export const log10 = funcMath((runtime, value) => Math.log10(value));
31
+
32
+ export const sin = funcMath((runtime, value) => Math.sin(value));
33
+ export const cos = funcMath((runtime, value) => Math.cos(value));
34
+ export const tan = funcMath((runtime, value) => Math.tan(value));
35
+ export const asin = funcMath((runtime, value) => Math.asin(value));
36
+ export const acos = funcMath((runtime, value) => Math.acos(value));
37
+ export const atan = funcMath((runtime, value) => Math.atan(value));
38
+
39
+ export const ceil = funcMath((runtime, value) => Math.ceil(value));
40
+ export const floor = funcMath((runtime, value) => Math.floor(value));
41
+ export const round = funcMath((runtime, value) => Math.round(value));
42
+ export const trunc = funcMath((runtime, value) => Math.trunc(value));
43
+
44
+ export const abs = funcMath((runtime, value) => Math.abs(value));
@@ -0,0 +1,19 @@
1
+ export { default as contains } from './funcContains';
2
+ export { default as endsWith } from './funcEndsWith';
3
+ export { default as error } from './funcError';
4
+ export { default as fromJSON } from './funcFromJSON';
5
+ export { default as has } from './funcHas';
6
+ export { default as in } from './funcIn';
7
+ export { default as keys } from './funcKeys';
8
+ export { default as length } from './funcLength';
9
+ export { default as now } from './funcNow';
10
+ export { default as startsWith } from './funcStartsWith';
11
+ export { default as toJSON } from './funcToJSON';
12
+ export { default as toNumber } from './funcToNumber';
13
+ export { default as toString } from './funcToString';
14
+ export { default as trim } from './funcTrim';
15
+ export { default as trimEnd } from './funcTrimEnd';
16
+ export { default as trimStart } from './funcTrimStart';
17
+ export { default as type } from './funcType';
18
+ export { default as void } from './funcVoid';
19
+ export * from './math';
@@ -0,0 +1,9 @@
1
+ function createSingleton(generatorFn) {
2
+ let result;
3
+ return () => {
4
+ if (result == null) result = generatorFn();
5
+ return result;
6
+ };
7
+ }
8
+
9
+ export default createSingleton;
package/src/index.js ADDED
@@ -0,0 +1,44 @@
1
+ import getBuiltins from './builtins';
2
+ import createSingleton from './createSingleton';
3
+ import JPLInterpreter, { applyInterpreterDefaults } from './interpreter';
4
+ import parseFunction from './interpreter/parseFunction';
5
+ import * as library from './library';
6
+ import { applyProgramDefaults } from './program';
7
+ import { applyRuntimeDefaults } from './runtime';
8
+
9
+ const getGlobalOptions = createSingleton(async () => ({
10
+ interpreter: {},
11
+
12
+ program: {},
13
+
14
+ runtime: {
15
+ vars: { ...(await getBuiltins()) },
16
+ },
17
+ }));
18
+
19
+ const parse = async (source, options) => {
20
+ const globalOptions = await getGlobalOptions();
21
+ const interpreter = new JPLInterpreter({
22
+ interpreter: applyInterpreterDefaults(options?.interpreter, globalOptions.interpreter),
23
+ program: applyProgramDefaults(options?.program, globalOptions.program),
24
+ runtime: applyRuntimeDefaults(options?.runtime, globalOptions.runtime),
25
+ });
26
+ return interpreter.parse(source);
27
+ };
28
+
29
+ const run = async (source, inputs, options) => {
30
+ const program = await parse(source, options);
31
+ return program.run(inputs);
32
+ };
33
+
34
+ export default {
35
+ ...library,
36
+ run,
37
+ parse,
38
+ parseFunction,
39
+ getBuiltins,
40
+ getOptions: getGlobalOptions,
41
+ };
42
+
43
+ export * from './library';
44
+ export { getBuiltins, getGlobalOptions as getOptions, parse, parseFunction, run };
@@ -0,0 +1,48 @@
1
+ import applyDefaults from '../applyDefaults';
2
+ import { DEFINITION_VERSION } from '../library';
3
+ import JPLProgram, { applyProgramDefaults } from '../program';
4
+ import { applyRuntimeDefaults } from '../runtime';
5
+ import { parseEntrypoint } from './parse';
6
+
7
+ const defaultOptions = {};
8
+
9
+ export function applyInterpreterDefaults(options = {}, defaults = {}) {
10
+ return applyDefaults(options, defaults);
11
+ }
12
+
13
+ /** JPL interpreter */
14
+ class JPLInterpreter {
15
+ constructor(options) {
16
+ this._options = applyInterpreterDefaults(options?.interpreter, defaultOptions);
17
+ this._programOptions = options?.program;
18
+ this._runtimeOptions = options?.runtime;
19
+ }
20
+
21
+ /** Return the interpreter's options */
22
+ get options() {
23
+ return this._options;
24
+ }
25
+
26
+ /** Parse the specified source program string into a reusable JPLProgram instance */
27
+ parse = async (source, options) => {
28
+ const instructions = await this.parseInstructions(source);
29
+
30
+ const programDefinition = {
31
+ version: DEFINITION_VERSION,
32
+ instructions,
33
+ };
34
+
35
+ return new JPLProgram(programDefinition, {
36
+ program: applyProgramDefaults(options?.program, this._programOptions),
37
+ runtime: applyRuntimeDefaults(options?.runtime, this._runtimeOptions),
38
+ });
39
+ };
40
+
41
+ /** Parse the specified source program string */
42
+ parseInstructions = async (source) => {
43
+ const { ops: instructions } = await parseEntrypoint(source, 0, { interpreter: this });
44
+ return instructions;
45
+ };
46
+ }
47
+
48
+ export default JPLInterpreter;