nerdamer 2.0.0-rc.1 → 2.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 (236) hide show
  1. package/BREAKING_CHANGES.md +244 -0
  2. package/README.md +168 -229
  3. package/dist/bundle.js +1 -1
  4. package/dist/bundle.js.LICENSE.txt +6 -6
  5. package/output/algebra/adapters.d.ts +3 -33
  6. package/output/algebra/adapters.js +7 -105
  7. package/output/algebra/algorithms/arith.js +10 -9
  8. package/output/algebra/algorithms/groebnerBase.d.ts +24 -20
  9. package/output/algebra/algorithms/groebnerBase.js +486 -723
  10. package/output/algebra/dispatch.d.ts +1 -0
  11. package/output/algebra/dispatch.js +25 -0
  12. package/output/algebra/factor/factor.d.ts +15 -0
  13. package/output/algebra/factor/factor.js +99 -12
  14. package/output/algebra/gcd/gcd.d.ts +8 -7
  15. package/output/algebra/gcd/gcd.js +32 -78
  16. package/output/algebra/groebner.d.ts +2 -2
  17. package/output/algebra/groebner.js +6 -12
  18. package/output/algebra/partfrac.d.ts +3 -3
  19. package/output/algebra/partfrac.js +28 -61
  20. package/output/algebra/polynomial/ModularSparsePolynomial.d.ts +131 -0
  21. package/output/algebra/polynomial/ModularSparsePolynomial.js +378 -0
  22. package/output/algebra/polynomial/ModularSparsePolynomialFactor.d.ts +40 -0
  23. package/output/algebra/polynomial/ModularSparsePolynomialFactor.js +661 -0
  24. package/output/algebra/polynomial/SparsePolynomialFactor.d.ts +64 -0
  25. package/output/algebra/polynomial/SparsePolynomialFactor.js +492 -0
  26. package/output/algebra/polynomial/SparsePolynomialGcd.d.ts +29 -0
  27. package/output/algebra/polynomial/SparsePolynomialGcd.js +259 -0
  28. package/output/algebra/polynomial/SparsePolynomialMultivariateFactor.d.ts +82 -0
  29. package/output/algebra/polynomial/SparsePolynomialMultivariateFactor.js +604 -0
  30. package/output/algebra/polynomial/modularGcd.d.ts +40 -0
  31. package/output/algebra/polynomial/modularGcd.js +365 -0
  32. package/output/algebra/polynomialize.js +4 -2
  33. package/output/algebra/simplify/funcsimp.js +1 -1
  34. package/output/algebra/simplify/ratsimp.js +1 -1
  35. package/output/algebra/simplify/simplify.js +11 -2
  36. package/output/algebra/utils.d.ts +1 -1
  37. package/output/algebra/utils.js +3 -3
  38. package/output/api/advanced.d.ts +2 -1
  39. package/output/api/advanced.js +3 -3
  40. package/output/api/algebra.d.ts +21 -4
  41. package/output/api/algebra.js +22 -3
  42. package/output/api/assumptions.d.ts +1 -1
  43. package/output/api/assumptions.js +2 -1
  44. package/output/api/calculus.d.ts +2 -1
  45. package/output/api/calculus.js +2 -1
  46. package/output/api/core.d.ts +28 -7
  47. package/output/api/core.js +28 -2
  48. package/output/api/languages/deu.d.ts +3 -0
  49. package/output/api/languages/deu.js +266 -0
  50. package/output/api/languages/fra.d.ts +3 -0
  51. package/output/api/languages/fra.js +266 -0
  52. package/output/api/languages/ita.d.ts +3 -0
  53. package/output/api/languages/ita.js +266 -0
  54. package/output/api/languages/nld.d.ts +3 -0
  55. package/output/api/languages/nld.js +266 -0
  56. package/output/api/languages/por.d.ts +3 -0
  57. package/output/api/languages/por.js +266 -0
  58. package/output/api/languages/spa.d.ts +3 -0
  59. package/output/api/languages/spa.js +266 -0
  60. package/output/api/parser.d.ts +10 -2
  61. package/output/api/parser.js +2 -0
  62. package/output/api/solve.d.ts +1 -1
  63. package/output/api/solve.js +2 -1
  64. package/output/api/structures.d.ts +1 -1
  65. package/output/api/structures.js +2 -1
  66. package/output/calculus/adapters.d.ts +8 -0
  67. package/output/calculus/adapters.js +41 -0
  68. package/output/calculus/derivative/diff.js +6 -3
  69. package/output/calculus/dispatch.d.ts +1 -0
  70. package/output/calculus/dispatch.js +25 -0
  71. package/output/calculus/integrate/integrate.js +4 -4
  72. package/output/calculus/integrate/integrationTable.js +16 -16
  73. package/output/calculus/laplace/ilaplace.js +1 -1
  74. package/output/calculus/laplace/ilaplaceTable.js +18 -17
  75. package/output/calculus/laplace/laplaceTable.js +3 -2
  76. package/output/calculus/limit/limit.js +1 -1
  77. package/output/core/classes/assumption/Assumption.js +14 -10
  78. package/output/core/classes/assumption/assume.d.ts +6 -0
  79. package/output/core/classes/assumption/assume.js +9 -0
  80. package/output/core/classes/assumption/dispatch.d.ts +1 -0
  81. package/output/core/classes/assumption/dispatch.js +11 -0
  82. package/output/core/classes/collection/Collection.js +11 -2
  83. package/output/core/classes/complex/Complex.d.ts +10 -0
  84. package/output/core/classes/complex/Complex.js +19 -1
  85. package/output/core/classes/decimalSet/DecimalSet.js +5 -4
  86. package/output/core/classes/dictionary/Dictionary.js +4 -1
  87. package/output/core/classes/expression/CoeffObject.d.ts +1 -1
  88. package/output/core/classes/expression/CoeffObject.js +1 -2
  89. package/output/core/classes/expression/Expression.d.ts +35 -26
  90. package/output/core/classes/expression/Expression.js +77 -50
  91. package/output/core/classes/expression/analysis.d.ts +1 -1
  92. package/output/core/classes/expression/analysis.js +7 -7
  93. package/output/core/classes/expression/format.d.ts +9 -0
  94. package/output/core/classes/expression/format.js +130 -7
  95. package/output/core/classes/expression/utils.d.ts +9 -0
  96. package/output/core/classes/expression/utils.js +52 -6
  97. package/output/core/classes/matrix/Matrix.d.ts +2 -3
  98. package/output/core/classes/matrix/Matrix.js +26 -23
  99. package/output/core/classes/matrix/dispatch.d.ts +1 -0
  100. package/output/core/classes/matrix/dispatch.js +19 -0
  101. package/output/core/classes/matrix/functions.d.ts +2 -0
  102. package/output/core/classes/matrix/functions.js +5 -0
  103. package/output/core/classes/parser/Parser.d.ts +31 -5
  104. package/output/core/classes/parser/Parser.js +357 -444
  105. package/output/core/classes/parser/constants.d.ts +2 -0
  106. package/output/core/classes/parser/constants.js +4 -2
  107. package/output/core/classes/parser/controlFlowSignals.d.ts +38 -0
  108. package/output/core/classes/parser/controlFlowSignals.js +60 -0
  109. package/output/core/classes/parser/operations/add.js +14 -16
  110. package/output/core/classes/parser/operations/compare.js +13 -0
  111. package/output/core/classes/parser/operations/functions.d.ts +3 -4
  112. package/output/core/classes/parser/operations/functions.js +48 -102
  113. package/output/core/classes/parser/operations/multiply.js +22 -15
  114. package/output/core/classes/parser/operations/power.js +11 -11
  115. package/output/core/classes/parser/operations/subtract.js +2 -1
  116. package/output/core/classes/parser/preprocess.d.ts +0 -7
  117. package/output/core/classes/parser/preprocess.js +33 -39
  118. package/output/core/classes/parser/scripting/controlFlow.d.ts +4 -102
  119. package/output/core/classes/parser/scripting/controlFlow.js +134 -186
  120. package/output/core/classes/parser/scripting/deferred.d.ts +4 -0
  121. package/output/core/classes/parser/scripting/deferred.js +65 -0
  122. package/output/core/classes/parser/scripting/dispatch.d.ts +1 -0
  123. package/output/core/classes/parser/scripting/dispatch.js +62 -0
  124. package/output/core/classes/parser/scripting/evaluate.d.ts +13 -0
  125. package/output/core/classes/parser/scripting/evaluate.js +17 -1
  126. package/output/core/classes/parser/scripting/scope.d.ts +1 -28
  127. package/output/core/classes/parser/scripting/scope.js +36 -40
  128. package/output/core/classes/parser/types.d.ts +5 -5
  129. package/output/core/classes/parser/wrappers/IndexedReference.d.ts +4 -3
  130. package/output/core/classes/parser/wrappers/IndexedReference.js +8 -0
  131. package/output/core/classes/parser/wrappers/KeyValuePair.d.ts +2 -2
  132. package/output/core/classes/polynomial/Polynomial.d.ts +5 -1
  133. package/output/core/classes/polynomial/Polynomial.js +53 -7
  134. package/output/core/classes/polynomial/SparsePolynomial.d.ts +221 -0
  135. package/output/core/classes/polynomial/SparsePolynomial.js +824 -0
  136. package/output/core/classes/polynomial/SparsePolynomialAdapter.d.ts +56 -0
  137. package/output/core/classes/polynomial/SparsePolynomialAdapter.js +241 -0
  138. package/output/core/classes/polynomial/Term.js +2 -2
  139. package/output/core/{adapters.d.ts → classes/polynomial/adapters.d.ts} +2 -2
  140. package/output/core/classes/polynomial/adapters.js +33 -0
  141. package/output/core/classes/polynomial/dispatch.d.ts +1 -0
  142. package/output/core/classes/polynomial/dispatch.js +14 -0
  143. package/output/core/classes/polynomial/functions.d.ts +8 -0
  144. package/output/core/classes/polynomial/functions.js +71 -8
  145. package/output/core/classes/polynomial/utils.js +2 -1
  146. package/output/core/classes/rational/Rational.d.ts +8 -11
  147. package/output/core/classes/rational/Rational.js +70 -20
  148. package/output/core/classes/seq/SEQ.js +4 -4
  149. package/output/core/classes/valuesSet/ValuesSet.d.ts +9 -1
  150. package/output/core/classes/valuesSet/ValuesSet.js +22 -4
  151. package/output/core/classes/vector/Vector.d.ts +2 -3
  152. package/output/core/classes/vector/Vector.js +11 -15
  153. package/output/core/classes/vector/dispatch.d.ts +1 -0
  154. package/output/core/classes/vector/dispatch.js +11 -0
  155. package/output/core/common/classes/MathematicalAggregate.js +11 -2
  156. package/output/core/common/classes/StructuredEntity.d.ts +0 -17
  157. package/output/core/common/classes/StructuredEntity.js +3 -64
  158. package/output/core/common/common.d.ts +0 -1
  159. package/output/core/common/common.js +29 -234
  160. package/output/core/converters/BaseConverter.js +3 -2
  161. package/output/core/converters/Converter.js +9 -5
  162. package/output/core/converters/Pattern.js +1 -1
  163. package/output/core/dispatch.d.ts +18 -0
  164. package/output/core/dispatch.js +3 -281
  165. package/output/core/errors.d.ts +259 -474
  166. package/output/core/errors.js +270 -542
  167. package/output/core/fullFunctions.d.ts +7 -0
  168. package/output/core/fullFunctions.js +31 -0
  169. package/output/core/functions/bigint/bigint.d.ts +13 -1
  170. package/output/core/functions/bigint/bigint.js +56 -4
  171. package/output/core/functions/bigint/primeFactor.d.ts +2 -0
  172. package/output/core/functions/bigint/primeFactor.js +13 -13
  173. package/output/core/functions/build/definitions.js +6 -0
  174. package/output/core/functions/complex.d.ts +2 -2
  175. package/output/core/functions/complex.dispatch.d.ts +1 -0
  176. package/output/core/functions/complex.dispatch.js +16 -0
  177. package/output/core/functions/complex.js +7 -7
  178. package/output/core/functions/decimal.js +2 -1
  179. package/output/core/functions/expand/expand.js +2 -2
  180. package/output/core/functions/numeric.d.ts +1 -1
  181. package/output/core/functions/string.js +2 -1
  182. package/output/core/functions/subst.js +21 -13
  183. package/output/core/parserFunctions.d.ts +7 -0
  184. package/output/core/parserFunctions.js +25 -0
  185. package/output/index.d.ts +50 -30
  186. package/output/index.js +147 -19
  187. package/output/math/defint/defint.d.ts +18 -0
  188. package/output/math/defint/defint.js +60 -0
  189. package/output/math/defint/defintDecimal.js +6 -5
  190. package/output/math/defint/defintNative.js +7 -18
  191. package/output/math/dispatch.d.ts +1 -0
  192. package/output/math/dispatch.js +101 -0
  193. package/output/math/geometry.d.ts +4 -0
  194. package/output/math/geometry.js +23 -0
  195. package/output/math/math.d.ts +59 -19
  196. package/output/math/math.js +244 -100
  197. package/output/math/trig.js +4 -4
  198. package/output/math/utils.d.ts +6 -0
  199. package/output/math/utils.js +39 -0
  200. package/output/solve/classes/PolynomialSolver.d.ts +33 -14
  201. package/output/solve/classes/PolynomialSolver.js +267 -67
  202. package/output/solve/classes/SolutionSet.d.ts +20 -1
  203. package/output/solve/classes/SolutionSet.js +82 -5
  204. package/output/solve/dispatch.d.ts +1 -0
  205. package/output/solve/dispatch.js +13 -0
  206. package/output/solve/linsolve.d.ts +2 -0
  207. package/output/solve/linsolve.js +13 -5
  208. package/output/solve/solve.d.ts +10 -0
  209. package/output/solve/solve.js +128 -20
  210. package/output/solve/solveSystem.d.ts +1 -1
  211. package/output/solve/solveSystem.js +8 -32
  212. package/output/solve/utils.d.ts +2 -1
  213. package/output/solve/utils.js +18 -22
  214. package/output/utils/array.d.ts +1 -1
  215. package/output/utils/debug.js +2 -1
  216. package/package.json +22 -38
  217. package/dist/parser.js +0 -2
  218. package/dist/parser.js.LICENSE.txt +0 -7
  219. package/docs-data/parser-functions.json +0 -1960
  220. package/output/algebra/algorithms/factor.d.ts +0 -3
  221. package/output/algebra/algorithms/factor.js +0 -5
  222. package/output/algebra/algorithms/factorMultivariate.d.ts +0 -23
  223. package/output/algebra/algorithms/factorMultivariate.js +0 -2393
  224. package/output/algebra/algorithms/factorUnivariate.d.ts +0 -72
  225. package/output/algebra/algorithms/factorUnivariate.js +0 -1072
  226. package/output/algebra/algorithms/gcd.d.ts +0 -22
  227. package/output/algebra/algorithms/gcd.js +0 -690
  228. package/output/algebra/algorithms/multiPoly/MultiPoly.d.ts +0 -137
  229. package/output/algebra/algorithms/multiPoly/MultiPoly.js +0 -346
  230. package/output/algebra/algorithms/poly.d.ts +0 -228
  231. package/output/algebra/algorithms/poly.js +0 -1299
  232. package/output/algebra/algorithms/rational.d.ts +0 -17
  233. package/output/algebra/algorithms/rational.js +0 -115
  234. package/output/core/adapters.js +0 -40
  235. package/output/core/classes/parser/operations/comma.d.ts +0 -7
  236. package/output/core/classes/parser/operations/comma.js +0 -11
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ContinueSignal = exports.BreakSignal = exports.ReturnSignal = exports.ControlFlowSignal = exports.CONTINUE_SIGNAL = exports.BREAK_SIGNAL = exports.RETURN_SIGNAL = void 0;
3
+ exports.ReturnSignal = exports.NullSignal = exports.ControlFlowSignal = exports.ContinueSignal = exports.BreakSignal = exports.RETURN_SIGNAL = exports.NULL_SIGNAL = exports.CONTINUE_SIGNAL = exports.BREAK_SIGNAL = void 0;
4
+ exports.isInsideSymbolicFunction = isInsideSymbolicFunction;
5
+ exports.shouldConsumeLocalReturn = shouldConsumeLocalReturn;
6
+ exports.withReturnPropagation = withReturnPropagation;
4
7
  exports.functionBoundary = functionBoundary;
5
8
  exports.RETURN = RETURN;
6
9
  exports.BREAK = BREAK;
@@ -17,77 +20,52 @@ exports.FOR = FOR;
17
20
  exports.BLOCK = BLOCK;
18
21
  exports.parserReturnBoundary = parserReturnBoundary;
19
22
  exports.parserBlock = parserBlock;
20
- const common_1 = require("../../../common/common");
21
23
  const errors_1 = require("../../../errors");
22
24
  const Settings_1 = require("../../../Settings");
23
25
  const Expression_1 = require("../../expression/Expression");
24
26
  const shortcuts_1 = require("../../expression/shortcuts");
25
- const Vector_1 = require("../../vector/Vector");
26
27
  const constants_1 = require("../constants");
27
- // Internal runtime tags for parser control-flow signals. These are not ParserEntity data types.
28
- exports.RETURN_SIGNAL = 'RETURN_SIGNAL';
29
- exports.BREAK_SIGNAL = 'BREAK_SIGNAL';
30
- exports.CONTINUE_SIGNAL = 'CONTINUE_SIGNAL';
31
- /** Base class for internal parser control transfers. These signals are not parser errors. */
32
- class ControlFlowSignal extends Error {
33
- constructor() {
34
- super();
35
- this.name = new.target.name;
36
- }
28
+ const controlFlowSignals_1 = require("../controlFlowSignals");
29
+ var controlFlowSignals_2 = require("../controlFlowSignals");
30
+ Object.defineProperty(exports, "BREAK_SIGNAL", { enumerable: true, get: function () { return controlFlowSignals_2.BREAK_SIGNAL; } });
31
+ Object.defineProperty(exports, "CONTINUE_SIGNAL", { enumerable: true, get: function () { return controlFlowSignals_2.CONTINUE_SIGNAL; } });
32
+ Object.defineProperty(exports, "NULL_SIGNAL", { enumerable: true, get: function () { return controlFlowSignals_2.NULL_SIGNAL; } });
33
+ Object.defineProperty(exports, "RETURN_SIGNAL", { enumerable: true, get: function () { return controlFlowSignals_2.RETURN_SIGNAL; } });
34
+ Object.defineProperty(exports, "BreakSignal", { enumerable: true, get: function () { return controlFlowSignals_2.BreakSignal; } });
35
+ Object.defineProperty(exports, "ContinueSignal", { enumerable: true, get: function () { return controlFlowSignals_2.ContinueSignal; } });
36
+ Object.defineProperty(exports, "ControlFlowSignal", { enumerable: true, get: function () { return controlFlowSignals_2.ControlFlowSignal; } });
37
+ Object.defineProperty(exports, "NullSignal", { enumerable: true, get: function () { return controlFlowSignals_2.NullSignal; } });
38
+ Object.defineProperty(exports, "ReturnSignal", { enumerable: true, get: function () { return controlFlowSignals_2.ReturnSignal; } });
39
+ const BREAK_SIGNAL_INSTANCE = new controlFlowSignals_1.BreakSignal();
40
+ const CONTINUE_SIGNAL_INSTANCE = new controlFlowSignals_1.ContinueSignal();
41
+ let loopBodyDepth = 0;
42
+ let parserReturnBoundaryDepth = 0;
43
+ let returnPropagationDepth = 0;
44
+ let symbolicFunctionDepth = 0;
45
+ function throwNullSignal() {
46
+ throw new controlFlowSignals_1.NullSignal();
37
47
  }
38
- exports.ControlFlowSignal = ControlFlowSignal;
39
- /**
40
- * Internal control-flow signal used to carry a symbolic function's return value to its
41
- * function boundary. It is not a mathematical parser result.
42
- */
43
- class ReturnSignal extends ControlFlowSignal {
44
- value;
45
- dataType = exports.RETURN_SIGNAL;
46
- constructor(value) {
47
- super();
48
- this.value = value;
49
- }
50
- static isReturnSignal(obj) {
51
- return (0, common_1.isNerdamerNativeType)(obj, exports.RETURN_SIGNAL);
52
- }
48
+ function isInsideSymbolicFunction() {
49
+ return symbolicFunctionDepth > 0;
53
50
  }
54
- exports.ReturnSignal = ReturnSignal;
55
- /** Internal control-flow signal consumed by the nearest parser loop. */
56
- class BreakSignal extends ControlFlowSignal {
57
- dataType = exports.BREAK_SIGNAL;
58
- static isBreakSignal(obj) {
59
- return (0, common_1.isNerdamerNativeType)(obj, exports.BREAK_SIGNAL);
60
- }
51
+ function shouldConsumeLocalReturn() {
52
+ return symbolicFunctionDepth === 0 && returnPropagationDepth === 0;
61
53
  }
62
- exports.BreakSignal = BreakSignal;
63
- /** Internal control-flow signal consumed by the nearest parser loop. */
64
- class ContinueSignal extends ControlFlowSignal {
65
- dataType = exports.CONTINUE_SIGNAL;
66
- static isContinueSignal(obj) {
67
- return (0, common_1.isNerdamerNativeType)(obj, exports.CONTINUE_SIGNAL);
54
+ function withReturnPropagation(evaluate) {
55
+ returnPropagationDepth++;
56
+ try {
57
+ return evaluate();
58
+ }
59
+ finally {
60
+ returnPropagationDepth--;
68
61
  }
69
62
  }
70
- exports.ContinueSignal = ContinueSignal;
71
- // BREAK and CONTINUE carry no data, so reuse their signals instead of allocating an Error
72
- // on every control transfer. RETURN carries a value and therefore remains per invocation.
73
- const BREAK_SIGNAL_INSTANCE = new BreakSignal();
74
- const CONTINUE_SIGNAL_INSTANCE = new ContinueSignal();
75
- // Parser control flow is synchronous. Track active execution boundaries so internal signals
76
- // are consumed by the correct parser construct without leaking across function calls.
77
- let loopBodyDepth = 0;
78
- let parserReturnBoundaryDepth = 0;
79
- let symbolicFunctionDepth = 0;
80
- /** Returns the parser's boolean interpretation of one evaluated condition. */
81
63
  function isTruthy(value) {
82
64
  if (!Expression_1.Expression.isExpression(value)) {
83
65
  throw new errors_1.UnexpectedDataType((0, errors_1.message)('expressionExpected', { type: constants_1.dataTypes[value.dataType] }));
84
66
  }
85
67
  return !value.isZero();
86
68
  }
87
- /**
88
- * Evaluates one symbolic-function body and consumes control flow at the function boundary.
89
- * RETURN becomes the function result, while BREAK and CONTINUE cannot escape a function call.
90
- */
91
69
  function functionBoundary(evaluate) {
92
70
  let retval;
93
71
  symbolicFunctionDepth++;
@@ -95,13 +73,13 @@ function functionBoundary(evaluate) {
95
73
  retval = evaluate();
96
74
  }
97
75
  catch (error) {
98
- if (ReturnSignal.isReturnSignal(error)) {
76
+ if (controlFlowSignals_1.ReturnSignal.isReturnSignal(error)) {
99
77
  retval = error.value;
100
78
  }
101
- else if (BreakSignal.isBreakSignal(error)) {
79
+ else if (controlFlowSignals_1.BreakSignal.isBreakSignal(error)) {
102
80
  throw new errors_1.ParserError((0, errors_1.message)('breakOutsideLoop'));
103
81
  }
104
- else if (ContinueSignal.isContinueSignal(error)) {
82
+ else if (controlFlowSignals_1.ContinueSignal.isContinueSignal(error)) {
105
83
  throw new errors_1.ParserError((0, errors_1.message)('continueOutsideLoop'));
106
84
  }
107
85
  else {
@@ -113,32 +91,21 @@ function functionBoundary(evaluate) {
113
91
  }
114
92
  return retval;
115
93
  }
116
- /**
117
- * Returns a value from the current symbolic function.
118
- * @param x
119
- * @returns
120
- */
121
94
  function RETURN(x) {
122
- throw new ReturnSignal(x);
95
+ throw new controlFlowSignals_1.ReturnSignal(x);
123
96
  }
124
- /** Exits the nearest parser control-flow loop. */
125
97
  function BREAK() {
126
98
  if (loopBodyDepth === 0) {
127
99
  throw new errors_1.ParserError((0, errors_1.message)('breakOutsideLoop'));
128
100
  }
129
101
  throw BREAK_SIGNAL_INSTANCE;
130
102
  }
131
- /** Skips to the next iteration of the nearest parser control-flow loop. */
132
103
  function CONTINUE() {
133
104
  if (loopBodyDepth === 0) {
134
105
  throw new errors_1.ParserError((0, errors_1.message)('continueOutsideLoop'));
135
106
  }
136
107
  throw CONTINUE_SIGNAL_INSTANCE;
137
108
  }
138
- /**
139
- * Returns 1 when every deferred argument is nonzero, otherwise 0.
140
- * Evaluation stops as soon as a false argument is found.
141
- */
142
109
  function AND(...args) {
143
110
  let retval = (0, shortcuts_1.one)();
144
111
  for (const arg of args) {
@@ -149,10 +116,6 @@ function AND(...args) {
149
116
  }
150
117
  return retval;
151
118
  }
152
- /**
153
- * Returns 1 when at least one deferred argument is nonzero, otherwise 0.
154
- * Evaluation stops as soon as a true argument is found.
155
- */
156
119
  function OR(...args) {
157
120
  let retval = (0, shortcuts_1.zero)();
158
121
  for (const arg of args) {
@@ -163,12 +126,9 @@ function OR(...args) {
163
126
  }
164
127
  return retval;
165
128
  }
166
- /** Returns 1 when the argument is zero and 0 otherwise. */
167
129
  function NOT(value) {
168
- const retval = isTruthy(value) ? (0, shortcuts_1.zero)() : (0, shortcuts_1.one)();
169
- return retval;
130
+ return isTruthy(value) ? (0, shortcuts_1.zero)() : (0, shortcuts_1.one)();
170
131
  }
171
- /** Returns 1 when an odd number of arguments are nonzero, otherwise 0. */
172
132
  function XOR(...args) {
173
133
  let odd = false;
174
134
  for (const arg of args) {
@@ -176,159 +136,148 @@ function XOR(...args) {
176
136
  odd = !odd;
177
137
  }
178
138
  }
179
- const retval = odd ? (0, shortcuts_1.one)() : (0, shortcuts_1.zero)();
180
- return retval;
139
+ return odd ? (0, shortcuts_1.one)() : (0, shortcuts_1.zero)();
181
140
  }
182
- /**
183
- * Evaluates an expression and returns its result unless evaluation throws an ordinary error.
184
- * The fallback is evaluated only after an error. Internal parser control-flow signals continue
185
- * unwinding to their enclosing function or loop boundary.
186
- */
187
141
  function IFERROR(expression, fallback) {
188
142
  let retval;
189
143
  try {
190
144
  retval = expression();
191
145
  }
192
146
  catch (error) {
193
- if (error instanceof ControlFlowSignal) {
147
+ if (error instanceof controlFlowSignals_1.ControlFlowSignal) {
194
148
  throw error;
195
149
  }
196
150
  retval = fallback();
197
151
  }
198
152
  return retval;
199
153
  }
200
- /**
201
- * Returns 1 when deferred evaluation throws an ordinary error and 0 otherwise.
202
- * Internal parser control-flow signals are not errors and continue unwinding normally.
203
- */
204
154
  function ISERROR(expression) {
205
155
  let retval = (0, shortcuts_1.zero)();
206
156
  try {
207
157
  expression();
208
158
  }
209
159
  catch (error) {
210
- if (error instanceof ControlFlowSignal) {
160
+ if (error instanceof controlFlowSignals_1.ControlFlowSignal) {
211
161
  throw error;
212
162
  }
213
163
  retval = (0, shortcuts_1.one)();
214
164
  }
215
165
  return retval;
216
166
  }
217
- /**
218
- * A branching function. Its arguments are deferred so only the selected branch is evaluated.
219
- * When the false branch is omitted, a false condition produces the parser's empty result.
220
- * @param condition
221
- * @param branch1
222
- * @param branch2
223
- * @returns
224
- */
225
- function IF(condition, branch1, branch2) {
226
- const evaluatedCondition = condition();
167
+ function controlFlowReturnBoundary(evaluate) {
227
168
  let retval;
228
- if (isTruthy(evaluatedCondition)) {
229
- retval = branch1();
169
+ try {
170
+ retval = evaluate();
230
171
  }
231
- else {
232
- retval = branch2 ? branch2() : new Vector_1.Vector();
172
+ catch (error) {
173
+ if (parserReturnBoundaryDepth > 0 &&
174
+ shouldConsumeLocalReturn() &&
175
+ controlFlowSignals_1.ReturnSignal.isReturnSignal(error)) {
176
+ retval = error.value;
177
+ }
178
+ else {
179
+ throw error;
180
+ }
233
181
  }
234
182
  return retval;
235
183
  }
236
- /**
237
- * Repeatedly evaluates a deferred body while a deferred condition is nonzero.
238
- * The condition is reevaluated before every iteration. BREAK and CONTINUE are consumed here so
239
- * they apply only to the nearest loop; other control-flow signals continue to unwind outward.
240
- * The final completed body result is returned; if the body never completes, an empty Vector is
241
- * returned.
242
- * @param condition
243
- * @param body
244
- * @returns
245
- */
246
- function WHILE(condition, body) {
247
- let retval = new Vector_1.Vector();
248
- let continueLoop = true;
249
- let iterations = 0;
250
- while (continueLoop) {
184
+ function IF(condition, branch1, branch2) {
185
+ return controlFlowReturnBoundary(() => {
251
186
  const evaluatedCondition = condition();
252
- if (!isTruthy(evaluatedCondition)) {
253
- continueLoop = false;
187
+ let retval;
188
+ if (isTruthy(evaluatedCondition)) {
189
+ retval = branch1();
190
+ }
191
+ else if (branch2) {
192
+ retval = branch2();
254
193
  }
255
194
  else {
256
- if (iterations >= Settings_1.Settings.MAX_LOOP_ITERATIONS) {
257
- throw new errors_1.ParserError((0, errors_1.message)('loopIterationLimitExceeded', {
258
- max: String(Settings_1.Settings.MAX_LOOP_ITERATIONS),
259
- }));
260
- }
261
- loopBodyDepth++;
262
- try {
263
- retval = body();
195
+ return throwNullSignal();
196
+ }
197
+ return retval;
198
+ });
199
+ }
200
+ function WHILE(condition, body) {
201
+ return controlFlowReturnBoundary(() => {
202
+ let retval;
203
+ let continueLoop = true;
204
+ let iterations = 0;
205
+ while (continueLoop) {
206
+ const evaluatedCondition = condition();
207
+ if (!isTruthy(evaluatedCondition)) {
208
+ continueLoop = false;
264
209
  }
265
- catch (error) {
266
- if (BreakSignal.isBreakSignal(error)) {
267
- continueLoop = false;
210
+ else {
211
+ if (iterations >= Settings_1.Settings.MAX_LOOP_ITERATIONS) {
212
+ throw new errors_1.ParserError((0, errors_1.message)('loopIterationLimitExceeded', { max: String(Settings_1.Settings.MAX_LOOP_ITERATIONS) }));
268
213
  }
269
- else if (!ContinueSignal.isContinueSignal(error)) {
270
- throw error;
214
+ loopBodyDepth++;
215
+ try {
216
+ retval = body();
271
217
  }
218
+ catch (error) {
219
+ if (controlFlowSignals_1.NullSignal.isNullSignal(error)) {
220
+ // A loop body may legitimately complete without producing a value.
221
+ }
222
+ else if (controlFlowSignals_1.BreakSignal.isBreakSignal(error)) {
223
+ continueLoop = false;
224
+ }
225
+ else if (!controlFlowSignals_1.ContinueSignal.isContinueSignal(error)) {
226
+ throw error;
227
+ }
228
+ }
229
+ finally {
230
+ loopBodyDepth--;
231
+ }
232
+ iterations++;
272
233
  }
273
- finally {
274
- loopBodyDepth--;
275
- }
276
- iterations++;
277
234
  }
278
- }
279
- return retval;
235
+ if (retval === undefined) {
236
+ return throwNullSignal();
237
+ }
238
+ return retval;
239
+ });
280
240
  }
281
- /**
282
- * Evaluates a deferred initializer once, then delegates repeated condition and body evaluation
283
- * to WHILE. The update runs after each completed body evaluation and also before a CONTINUE
284
- * advances to the next condition check. BREAK and RETURN skip the pending update. The final
285
- * completed body result is returned; if the body never completes, WHILE's empty-Vector result
286
- * is preserved.
287
- * @param initializer
288
- * @param condition
289
- * @param update
290
- * @param body
291
- * @returns
292
- */
293
241
  function FOR(initializer, condition, update, body) {
294
- initializer();
295
- const retval = WHILE(condition, () => {
296
- let bodyResult;
297
- try {
298
- bodyResult = body();
299
- }
300
- catch (error) {
301
- if (ContinueSignal.isContinueSignal(error)) {
302
- update();
242
+ return controlFlowReturnBoundary(() => {
243
+ initializer();
244
+ return WHILE(condition, () => {
245
+ let bodyResult;
246
+ try {
247
+ bodyResult = body();
303
248
  }
304
- throw error;
305
- }
306
- update();
307
- return bodyResult;
249
+ catch (error) {
250
+ if (controlFlowSignals_1.ContinueSignal.isContinueSignal(error) ||
251
+ controlFlowSignals_1.NullSignal.isNullSignal(error)) {
252
+ update();
253
+ }
254
+ throw error;
255
+ }
256
+ update();
257
+ return bodyResult;
258
+ });
308
259
  });
309
- return retval;
310
260
  }
311
- /**
312
- * Evaluates a sequence of deferred parser commands in order.
313
- * The final command's result is returned. Control-flow signals are not consumed here, allowing
314
- * RETURN, BREAK, and CONTINUE to unwind to their enclosing function or loop boundary. An empty
315
- * block retains the historical empty-vector result.
316
- * @param args
317
- * @returns
318
- */
319
261
  function BLOCK(...args) {
320
- let retval = new Vector_1.Vector();
262
+ if (args.length === 0) {
263
+ return throwNullSignal();
264
+ }
265
+ let retval;
321
266
  for (const arg of args) {
322
- retval = arg();
267
+ try {
268
+ retval = arg();
269
+ }
270
+ catch (error) {
271
+ if (!controlFlowSignals_1.NullSignal.isNullSignal(error)) {
272
+ throw error;
273
+ }
274
+ }
275
+ }
276
+ if (retval === undefined) {
277
+ return throwNullSignal();
323
278
  }
324
279
  return retval;
325
280
  }
326
- /**
327
- * Evaluates one parser-dispatched function through the outer return boundary.
328
- *
329
- * Nested parser calls keep propagating RETURN. The outermost parser call consumes it when
330
- * execution is not already inside a symbolic function.
331
- */
332
281
  function parserReturnBoundary(evaluate) {
333
282
  const consumesReturn = symbolicFunctionDepth === 0 && parserReturnBoundaryDepth === 0;
334
283
  let retval;
@@ -337,7 +286,7 @@ function parserReturnBoundary(evaluate) {
337
286
  retval = evaluate();
338
287
  }
339
288
  catch (error) {
340
- if (consumesReturn && ReturnSignal.isReturnSignal(error)) {
289
+ if (consumesReturn && controlFlowSignals_1.ReturnSignal.isReturnSignal(error)) {
341
290
  retval = error.value;
342
291
  }
343
292
  else {
@@ -349,7 +298,6 @@ function parserReturnBoundary(evaluate) {
349
298
  }
350
299
  return retval;
351
300
  }
352
- /** Evaluates BLOCK through the parser return boundary. */
353
301
  function parserBlock(...args) {
354
302
  return parserReturnBoundary(() => BLOCK(...args));
355
303
  }
@@ -0,0 +1,4 @@
1
+ import type { ParserEntity } from '../../../types';
2
+ import type { DeferredFunctionArgument } from '../types';
3
+ /** Iterates parser containers while keeping iteration variables local to each body call. */
4
+ export declare function each(collection: DeferredFunctionArgument, variable: DeferredFunctionArgument, bodyOrValueVariable: DeferredFunctionArgument, dictionaryBody?: DeferredFunctionArgument): ParserEntity;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.each = each;
4
+ const errors_1 = require("../../../errors");
5
+ const Collection_1 = require("../../collection/Collection");
6
+ const Dictionary_1 = require("../../dictionary/Dictionary");
7
+ const Expression_1 = require("../../expression/Expression");
8
+ const shortcuts_1 = require("../../expression/shortcuts");
9
+ const Matrix_1 = require("../../matrix/Matrix");
10
+ const ValuesSet_1 = require("../../valuesSet/ValuesSet");
11
+ const Vector_1 = require("../../vector/Vector");
12
+ const constants_1 = require("../constants");
13
+ const controlFlow_1 = require("./controlFlow");
14
+ const scope_1 = require("./scope");
15
+ /** Iterates parser containers while keeping iteration variables local to each body call. */
16
+ function each(collection, variable, bodyOrValueVariable, dictionaryBody) {
17
+ const target = collection();
18
+ const values = [];
19
+ const keys = [];
20
+ if (dictionaryBody) {
21
+ if (!Dictionary_1.Dictionary.isDictionary(target)) {
22
+ throw new errors_1.UnexpectedDataType((0, errors_1.message)('unsupportedType', { type: constants_1.dataTypes[target.dataType] }));
23
+ }
24
+ for (const [key, value] of target.entries()) {
25
+ keys.push(Expression_1.Expression.create(key));
26
+ values.push(value.copy());
27
+ }
28
+ }
29
+ else if (Vector_1.Vector.isVector(target) ||
30
+ Collection_1.Collection.isCollection(target) ||
31
+ ValuesSet_1.ValuesSet.isValuesSet(target)) {
32
+ target.forEach(value => values.push(value.copy()));
33
+ }
34
+ else if (Matrix_1.Matrix.isMatrix(target)) {
35
+ target.forEach(value => values.push(value.copy()));
36
+ }
37
+ else if (Dictionary_1.Dictionary.isDictionary(target)) {
38
+ target.forEach(value => values.push(value.copy()));
39
+ }
40
+ else {
41
+ throw new errors_1.UnexpectedDataType((0, errors_1.message)('unsupportedType', { type: constants_1.dataTypes[target.dataType] }));
42
+ }
43
+ let index = 0;
44
+ let retval;
45
+ try {
46
+ retval = (0, controlFlow_1.withReturnPropagation)(() => (0, controlFlow_1.FOR)(() => (0, shortcuts_1.zero)(), () => (index < values.length ? (0, shortcuts_1.one)() : (0, shortcuts_1.zero)()), () => {
47
+ index++;
48
+ return (0, shortcuts_1.zero)();
49
+ }, () => {
50
+ if (dictionaryBody) {
51
+ return (0, scope_1.LET)(variable, () => keys[index], () => (0, scope_1.LET)(bodyOrValueVariable, () => values[index], dictionaryBody));
52
+ }
53
+ return (0, scope_1.LET)(variable, () => values[index], bodyOrValueVariable);
54
+ }));
55
+ }
56
+ catch (error) {
57
+ if (!(0, controlFlow_1.isInsideSymbolicFunction)() && controlFlow_1.ReturnSignal.isReturnSignal(error)) {
58
+ retval = error.value;
59
+ }
60
+ else {
61
+ throw error;
62
+ }
63
+ }
64
+ return retval;
65
+ }
@@ -0,0 +1 @@
1
+ export declare function loadScriptingFunctions(): void;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadScriptingFunctions = loadScriptingFunctions;
4
+ const dispatch_1 = require("../../../dispatch");
5
+ const controlFlow_1 = require("./controlFlow");
6
+ const deferred_1 = require("./deferred");
7
+ const evaluate_1 = require("./evaluate");
8
+ const scope_1 = require("./scope");
9
+ function loadScriptingFunctions() {
10
+ Object.assign(dispatch_1.mathFunctionRegistry, {
11
+ unassign: (0, dispatch_1.systemFunction)({ fn: scope_1.unassign, minArgs: 1, maxArgs: 1, distElWise: false, deferArguments: true }),
12
+ evaluate: (0, dispatch_1.systemFunction)({
13
+ fn: evaluate_1.evaluate,
14
+ minArgs: 1,
15
+ maxArgs: 3,
16
+ equationArgs: [0],
17
+ distElWise: false,
18
+ deferArguments: true,
19
+ usage: 'scripting',
20
+ }),
21
+ return: (0, dispatch_1.systemFunction)({
22
+ fn: controlFlow_1.RETURN,
23
+ minArgs: 1,
24
+ maxArgs: 1,
25
+ equationArgs: [0],
26
+ distElWise: false,
27
+ bracketlessStatement: true,
28
+ usage: 'scripting',
29
+ }),
30
+ break: (0, dispatch_1.systemFunction)({ fn: controlFlow_1.BREAK, minArgs: 0, maxArgs: 0, distElWise: false, usage: 'scripting' }),
31
+ continue: (0, dispatch_1.systemFunction)({ fn: controlFlow_1.CONTINUE, minArgs: 0, maxArgs: 0, distElWise: false, usage: 'scripting' }),
32
+ and: (0, dispatch_1.systemFunction)({ fn: controlFlow_1.AND, minArgs: 2, maxArgs: -1, distElWise: false, deferArguments: true, usage: 'scripting' }),
33
+ or: (0, dispatch_1.systemFunction)({ fn: controlFlow_1.OR, minArgs: 2, maxArgs: -1, distElWise: false, deferArguments: true, usage: 'scripting' }),
34
+ not: (0, dispatch_1.systemFunction)({ fn: controlFlow_1.NOT, minArgs: 1, maxArgs: 1, distElWise: false, usage: 'scripting' }),
35
+ xor: (0, dispatch_1.systemFunction)({ fn: controlFlow_1.XOR, minArgs: 2, maxArgs: -1, distElWise: false, usage: 'scripting' }),
36
+ iferror: (0, dispatch_1.systemFunction)({ fn: controlFlow_1.IFERROR, minArgs: 2, maxArgs: 2, distElWise: false, deferArguments: true, usage: 'scripting' }),
37
+ iserror: (0, dispatch_1.systemFunction)({ fn: controlFlow_1.ISERROR, minArgs: 1, maxArgs: 1, distElWise: false, deferArguments: true, usage: 'scripting' }),
38
+ if: (0, dispatch_1.systemFunction)({ fn: controlFlow_1.IF, minArgs: 2, maxArgs: 3, distElWise: false, deferArguments: true, returnBoundary: true, usage: 'scripting' }),
39
+ while: (0, dispatch_1.systemFunction)({ fn: controlFlow_1.WHILE, minArgs: 2, maxArgs: 2, distElWise: false, deferArguments: true, returnBoundary: true, usage: 'scripting' }),
40
+ for: (0, dispatch_1.systemFunction)({ fn: controlFlow_1.FOR, minArgs: 4, maxArgs: 4, distElWise: false, deferArguments: true, returnBoundary: true, usage: 'scripting' }),
41
+ each: (0, dispatch_1.systemFunction)({ fn: deferred_1.each, minArgs: 3, maxArgs: 4, distElWise: false, deferArguments: true, usage: 'scripting' }),
42
+ let: (0, dispatch_1.systemFunction)({
43
+ fn: scope_1.LET,
44
+ minArgs: 3,
45
+ maxArgs: -1,
46
+ distElWise: false,
47
+ deferArguments: true,
48
+ normalizeDeferredArguments: scope_1.normalizeLetArguments,
49
+ returnBoundary: true,
50
+ usage: 'scripting',
51
+ }),
52
+ block: (0, dispatch_1.systemFunction)({
53
+ fn: controlFlow_1.BLOCK,
54
+ minArgs: -1,
55
+ maxArgs: -1,
56
+ distElWise: false,
57
+ deferArguments: true,
58
+ returnBoundary: true,
59
+ usage: 'scripting',
60
+ }),
61
+ });
62
+ }
@@ -2,6 +2,19 @@ import type { ParserEntity } from '../../../types';
2
2
  import type { DeferredFunctionArgument } from '../types';
3
3
  /**
4
4
  * Evaluates a deferred parser value and returns the resolved input if evaluation cannot complete.
5
+ *
6
+ * @remarks
7
+ * A Dictionary supplied as the second argument provides substitutions for this evaluation only.
8
+ * Those names are passed through the same call-scoped parser-value mechanism used by
9
+ * `Parser.parse(..., values)`; they are not written to the parser's known-value table.
10
+ *
11
+ * This distinction matters for reserved names. The solver index `_n` cannot be assigned
12
+ * globally with `_n: value`, but it can be supplied locally when evaluating a parametric
13
+ * `SolutionSet`. For example, `evaluate(solve(sin(x),x),{_n=>0})` returns `{0}`.
14
+ *
15
+ * When a solver result is stored first in Nerdamer Scripting, use `:` for assignment:
16
+ * `sol: solve(sin(x),x); evaluate(sol,{_n=>0})`. The `=` operator denotes an equation;
17
+ * it does not store a parser value in a variable.
5
18
  */
6
19
  export declare function evaluate(value: DeferredFunctionArgument, substitutions?: DeferredFunctionArgument, point?: DeferredFunctionArgument): ParserEntity;
7
20
  /**
@@ -16,6 +16,19 @@ const constants_1 = require("../constants");
16
16
  const EVALUATE_FUNCTION = 'evaluate';
17
17
  /**
18
18
  * Evaluates a deferred parser value and returns the resolved input if evaluation cannot complete.
19
+ *
20
+ * @remarks
21
+ * A Dictionary supplied as the second argument provides substitutions for this evaluation only.
22
+ * Those names are passed through the same call-scoped parser-value mechanism used by
23
+ * `Parser.parse(..., values)`; they are not written to the parser's known-value table.
24
+ *
25
+ * This distinction matters for reserved names. The solver index `_n` cannot be assigned
26
+ * globally with `_n: value`, but it can be supplied locally when evaluating a parametric
27
+ * `SolutionSet`. For example, `evaluate(solve(sin(x),x),{_n=>0})` returns `{0}`.
28
+ *
29
+ * When a solver result is stored first in Nerdamer Scripting, use `:` for assignment:
30
+ * `sol: solve(sin(x),x); evaluate(sol,{_n=>0})`. The `=` operator denotes an equation;
31
+ * it does not store a parser value in a variable.
19
32
  */
20
33
  function evaluate(value, substitutions, point) {
21
34
  const input = value();
@@ -54,7 +67,10 @@ function evaluate(value, substitutions, point) {
54
67
  let retval = input;
55
68
  try {
56
69
  if (values &&
57
- (Expression_1.Expression.isExpression(input) || Vector_1.Vector.isVector(input) || Matrix_1.Matrix.isMatrix(input))) {
70
+ (Expression_1.Expression.isExpression(input) ||
71
+ Vector_1.Vector.isVector(input) ||
72
+ Matrix_1.Matrix.isMatrix(input) ||
73
+ ValuesSet_1.ValuesSet.isValuesSet(input))) {
58
74
  retval = input.evaluate(values);
59
75
  }
60
76
  else {