@motionscript/math 0.1.0 → 0.1.1-alpha.1
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.
- package/CHANGELOG.md +21 -0
- package/dist/annotate/angle-mark.d.ts +4 -14
- package/dist/annotate/angle-mark.d.ts.map +1 -1
- package/dist/annotate/angle-mark.js +2 -1
- package/dist/annotate/angle-mark.js.map +1 -1
- package/dist/annotate/curly-brace.d.ts +4 -16
- package/dist/annotate/curly-brace.d.ts.map +1 -1
- package/dist/annotate/curly-brace.js.map +1 -1
- package/dist/annotate/emphasis-mark.d.ts +0 -5
- package/dist/annotate/emphasis-mark.d.ts.map +1 -1
- package/dist/annotate/emphasis-mark.js.map +1 -1
- package/dist/browser/chunks/chunk-FZ2YBTPL.js +2 -0
- package/dist/browser/chunks/chunk-FZ2YBTPL.js.map +7 -0
- package/dist/browser/chunks/chunk-T42HWKBC.js +2 -0
- package/dist/browser/chunks/chunk-T42HWKBC.js.map +7 -0
- package/dist/browser/chunks/{mathjax-WV55AC2P.js → mathjax-SOM6RSPV.js} +1 -1
- package/dist/browser/chunks/{mathjax-WV55AC2P.js.map → mathjax-SOM6RSPV.js.map} +2 -2
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +3 -3
- package/dist/browser/kit.js +1 -1
- package/dist/browser/manifest.json +1 -1
- package/dist/browser/tex.js +1 -1
- package/dist/engine.d.ts +3 -4
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +3 -4
- package/dist/engine.js.map +1 -1
- package/dist/kit/cache.d.ts +10 -21
- package/dist/kit/cache.d.ts.map +1 -1
- package/dist/kit/cache.js +33 -24
- package/dist/kit/cache.js.map +1 -1
- package/dist/kit/component.d.ts +5 -14
- package/dist/kit/component.d.ts.map +1 -1
- package/dist/kit/component.js +1 -8
- package/dist/kit/component.js.map +1 -1
- package/dist/kit/coords.d.ts +1 -1
- package/dist/kit/coords.d.ts.map +1 -1
- package/dist/kit/coords.js +2 -1
- package/dist/kit/coords.js.map +1 -1
- package/dist/kit/curve.d.ts +1 -5
- package/dist/kit/curve.d.ts.map +1 -1
- package/dist/kit/curve.js.map +1 -1
- package/dist/kit/equation.d.ts +10 -30
- package/dist/kit/equation.d.ts.map +1 -1
- package/dist/kit/equation.js +9 -44
- package/dist/kit/equation.js.map +1 -1
- package/dist/kit/equation3d.d.ts +3 -3
- package/dist/kit/equation3d.d.ts.map +1 -1
- package/dist/kit/equation3d.js +27 -59
- package/dist/kit/equation3d.js.map +1 -1
- package/dist/kit/expression.d.ts +7 -7
- package/dist/kit/expression.d.ts.map +1 -1
- package/dist/kit/expression.js +18 -17
- package/dist/kit/expression.js.map +1 -1
- package/dist/kit/field-mark.d.ts +0 -7
- package/dist/kit/field-mark.d.ts.map +1 -1
- package/dist/kit/field-mark.js.map +1 -1
- package/dist/kit/field.d.ts +2 -2
- package/dist/kit/field.d.ts.map +1 -1
- package/dist/kit/field.js +11 -31
- package/dist/kit/field.js.map +1 -1
- package/dist/kit/follow.d.ts +3 -10
- package/dist/kit/follow.d.ts.map +1 -1
- package/dist/kit/follow.js +5 -11
- package/dist/kit/follow.js.map +1 -1
- package/dist/kit/furniture.d.ts +2 -1
- package/dist/kit/furniture.d.ts.map +1 -1
- package/dist/kit/furniture.js +6 -3
- package/dist/kit/furniture.js.map +1 -1
- package/dist/kit/geometry.d.ts +1 -0
- package/dist/kit/geometry.d.ts.map +1 -1
- package/dist/kit/geometry.js +3 -0
- package/dist/kit/geometry.js.map +1 -1
- package/dist/kit/mark.d.ts +4 -10
- package/dist/kit/mark.d.ts.map +1 -1
- package/dist/kit/mark.js.map +1 -1
- package/dist/kit/place.d.ts +3 -5
- package/dist/kit/place.d.ts.map +1 -1
- package/dist/kit/place.js +3 -5
- package/dist/kit/place.js.map +1 -1
- package/dist/kit/sampler.d.ts +0 -5
- package/dist/kit/sampler.d.ts.map +1 -1
- package/dist/kit/sampler.js.map +1 -1
- package/dist/kit/system.d.ts +0 -1
- package/dist/kit/system.d.ts.map +1 -1
- package/dist/kit/system.js +3 -10
- package/dist/kit/system.js.map +1 -1
- package/dist/marks/curve-area.d.ts +2 -12
- package/dist/marks/curve-area.d.ts.map +1 -1
- package/dist/marks/curve-area.js +4 -9
- package/dist/marks/curve-area.js.map +1 -1
- package/dist/marks/drop-lines.d.ts +1 -5
- package/dist/marks/drop-lines.d.ts.map +1 -1
- package/dist/marks/drop-lines.js.map +1 -1
- package/dist/marks/plot-point.d.ts +4 -13
- package/dist/marks/plot-point.d.ts.map +1 -1
- package/dist/marks/plot-point.js.map +1 -1
- package/dist/marks/riemann-sum.d.ts +1 -7
- package/dist/marks/riemann-sum.d.ts.map +1 -1
- package/dist/marks/riemann-sum.js +2 -2
- package/dist/marks/riemann-sum.js.map +1 -1
- package/dist/marks/tangent-line.d.ts +2 -12
- package/dist/marks/tangent-line.d.ts.map +1 -1
- package/dist/marks/tangent-line.js +1 -5
- package/dist/marks/tangent-line.js.map +1 -1
- package/dist/marks/vector-arrow.d.ts +2 -11
- package/dist/marks/vector-arrow.d.ts.map +1 -1
- package/dist/marks/vector-arrow.js +3 -6
- package/dist/marks/vector-arrow.js.map +1 -1
- package/dist/nodes.d.ts +1 -12
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js.map +1 -1
- package/dist/plots/function-plot.d.ts +1 -6
- package/dist/plots/function-plot.d.ts.map +1 -1
- package/dist/plots/function-plot.js +1 -5
- package/dist/plots/function-plot.js.map +1 -1
- package/dist/plots/points-plot.d.ts +1 -6
- package/dist/plots/points-plot.d.ts.map +1 -1
- package/dist/plots/points-plot.js.map +1 -1
- package/dist/plots/stream-lines.d.ts +0 -7
- package/dist/plots/stream-lines.d.ts.map +1 -1
- package/dist/plots/stream-lines.js.map +1 -1
- package/dist/plots/traced-path.d.ts +1 -12
- package/dist/plots/traced-path.d.ts.map +1 -1
- package/dist/plots/traced-path.js +1 -6
- package/dist/plots/traced-path.js.map +1 -1
- package/dist/plots/vector-field.d.ts +1 -10
- package/dist/plots/vector-field.d.ts.map +1 -1
- package/dist/plots/vector-field.js +3 -6
- package/dist/plots/vector-field.js.map +1 -1
- package/dist/structures/network-graph.d.ts +15 -34
- package/dist/structures/network-graph.d.ts.map +1 -1
- package/dist/structures/network-graph.js +2 -5
- package/dist/structures/network-graph.js.map +1 -1
- package/dist/structures/sample-space.d.ts +8 -22
- package/dist/structures/sample-space.d.ts.map +1 -1
- package/dist/structures/sample-space.js +3 -5
- package/dist/structures/sample-space.js.map +1 -1
- package/dist/systems/axes2d.d.ts +0 -1
- package/dist/systems/axes2d.d.ts.map +1 -1
- package/dist/systems/axes2d.js +3 -4
- package/dist/systems/axes2d.js.map +1 -1
- package/dist/systems/number-line.d.ts +7 -22
- package/dist/systems/number-line.d.ts.map +1 -1
- package/dist/systems/number-line.js +2 -2
- package/dist/systems/number-line.js.map +1 -1
- package/dist/systems/number-plane.d.ts +12 -27
- package/dist/systems/number-plane.d.ts.map +1 -1
- package/dist/systems/number-plane.js +1 -7
- package/dist/systems/number-plane.js.map +1 -1
- package/dist/systems/polar-plane.d.ts +11 -26
- package/dist/systems/polar-plane.d.ts.map +1 -1
- package/dist/systems/polar-plane.js +7 -10
- package/dist/systems/polar-plane.js.map +1 -1
- package/dist/tex/geometry.d.ts +5 -24
- package/dist/tex/geometry.d.ts.map +1 -1
- package/dist/tex/geometry.js +19 -90
- package/dist/tex/geometry.js.map +1 -1
- package/dist/tex/mathjax.d.ts +0 -13
- package/dist/tex/mathjax.d.ts.map +1 -1
- package/dist/tex/mathjax.js +4 -24
- package/dist/tex/mathjax.js.map +1 -1
- package/dist/tex/matrix.d.ts +1 -10
- package/dist/tex/matrix.d.ts.map +1 -1
- package/dist/tex/matrix.js +1 -5
- package/dist/tex/matrix.js.map +1 -1
- package/dist/tex/tween.d.ts +9 -30
- package/dist/tex/tween.d.ts.map +1 -1
- package/dist/tex/tween.js +19 -67
- package/dist/tex/tween.js.map +1 -1
- package/dist/tex/typeset.d.ts +2 -23
- package/dist/tex/typeset.d.ts.map +1 -1
- package/dist/tex/typeset.js +4 -30
- package/dist/tex/typeset.js.map +1 -1
- package/dist/three/axes3d.d.ts +32 -62
- package/dist/three/axes3d.d.ts.map +1 -1
- package/dist/three/axes3d.js +5 -13
- package/dist/three/axes3d.js.map +1 -1
- package/dist/three/curve-plot3d.d.ts +4 -15
- package/dist/three/curve-plot3d.d.ts.map +1 -1
- package/dist/three/curve-plot3d.js.map +1 -1
- package/dist/three/plot-point3d.d.ts +7 -14
- package/dist/three/plot-point3d.d.ts.map +1 -1
- package/dist/three/plot-point3d.js.map +1 -1
- package/dist/three/space.d.ts +3 -8
- package/dist/three/space.d.ts.map +1 -1
- package/dist/three/space.js +1 -8
- package/dist/three/space.js.map +1 -1
- package/dist/three/surface-plot3d.d.ts +9 -23
- package/dist/three/surface-plot3d.d.ts.map +1 -1
- package/dist/three/surface-plot3d.js.map +1 -1
- package/dist/three/vector-arrow3d.d.ts +1 -7
- package/dist/three/vector-arrow3d.d.ts.map +1 -1
- package/dist/three/vector-arrow3d.js.map +1 -1
- package/package.json +4 -4
- package/src/annotate/angle-mark.ts +4 -14
- package/src/annotate/curly-brace.ts +2 -14
- package/src/annotate/emphasis-mark.ts +1 -6
- package/src/engine.ts +3 -4
- package/src/kit/cache.ts +33 -25
- package/src/kit/component.ts +5 -19
- package/src/kit/coords.ts +2 -2
- package/src/kit/curve.ts +1 -5
- package/src/kit/equation.ts +12 -50
- package/src/kit/equation3d.ts +23 -59
- package/src/kit/expression.ts +18 -23
- package/src/kit/field-mark.ts +1 -7
- package/src/kit/field.ts +10 -26
- package/src/kit/follow.ts +5 -13
- package/src/kit/furniture.ts +7 -4
- package/src/kit/geometry.ts +4 -0
- package/src/kit/mark.ts +4 -11
- package/src/kit/place.ts +3 -5
- package/src/kit/sampler.ts +0 -6
- package/src/kit/system.ts +3 -12
- package/src/marks/curve-area.ts +3 -16
- package/src/marks/drop-lines.ts +0 -6
- package/src/marks/plot-point.ts +2 -12
- package/src/marks/riemann-sum.ts +2 -10
- package/src/marks/tangent-line.ts +1 -13
- package/src/marks/vector-arrow.ts +3 -13
- package/src/nodes.ts +1 -12
- package/src/plots/function-plot.ts +1 -6
- package/src/plots/points-plot.ts +0 -7
- package/src/plots/stream-lines.ts +1 -8
- package/src/plots/traced-path.ts +2 -13
- package/src/plots/vector-field.ts +4 -12
- package/src/structures/network-graph.ts +6 -25
- package/src/structures/sample-space.ts +5 -20
- package/src/systems/axes2d.ts +3 -6
- package/src/systems/number-line.ts +4 -20
- package/src/systems/number-plane.ts +4 -19
- package/src/systems/polar-plane.ts +11 -25
- package/src/tex/geometry.ts +24 -126
- package/src/tex/mathjax.ts +4 -26
- package/src/tex/matrix.ts +1 -11
- package/src/tex/tween.ts +21 -75
- package/src/tex/typeset.ts +4 -37
- package/src/three/axes3d.ts +7 -42
- package/src/three/curve-plot3d.ts +0 -12
- package/src/three/plot-point3d.ts +0 -10
- package/src/three/space.ts +2 -11
- package/src/three/surface-plot3d.ts +0 -15
- package/src/three/vector-arrow3d.ts +0 -8
- package/dist/browser/chunks/chunk-IRZCG6PM.js +0 -2
- package/dist/browser/chunks/chunk-IRZCG6PM.js.map +0 -7
- package/dist/browser/chunks/chunk-WMVW6ZIV.js +0 -2
- package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +0 -7
- package/dist/kit/withdrawn.d.ts +0 -9
- package/dist/kit/withdrawn.d.ts.map +0 -1
- package/dist/kit/withdrawn.js +0 -9
- package/dist/kit/withdrawn.js.map +0 -1
- package/src/kit/withdrawn.ts +0 -8
package/dist/kit/equation.d.ts
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
import { type CompiledExpression } from "./expression.js";
|
|
2
2
|
/**
|
|
3
3
|
* A plotted equation, read the way it is written rather than by a `kind` prop:
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* | `sin(x)`, `y = sin(x)` | a function of `x` |
|
|
8
|
-
* | `x = y^2` | a function of `y` |
|
|
9
|
-
* | `r = 1 + cos(theta)` | polar, in `theta` |
|
|
10
|
-
* | `(cos(t), sin(t))` | parametric, in `t` |
|
|
11
|
-
* | `x^2 + y^2 = 4` | implicit, in `x` and `y` |
|
|
4
|
+
* `sin(x)` or `y = sin(x)` is a function of `x`, `x = y^2` of `y`,
|
|
5
|
+
* `r = 1 + cos(theta)` polar, `(cos(t), sin(t))` parametric, and
|
|
6
|
+
* `x^2 + y^2 = 4` implicit.
|
|
12
7
|
*/
|
|
13
8
|
export type Equation = {
|
|
14
9
|
kind: "function";
|
|
@@ -21,39 +16,24 @@ export type Equation = {
|
|
|
21
16
|
kind: "parametric";
|
|
22
17
|
x: CompiledExpression;
|
|
23
18
|
y: CompiledExpression;
|
|
24
|
-
}
|
|
25
|
-
/** Drawn where `f(x, y)` is zero. */
|
|
26
|
-
| {
|
|
19
|
+
} | {
|
|
27
20
|
kind: "implicit";
|
|
28
21
|
f: CompiledExpression;
|
|
29
22
|
};
|
|
30
|
-
/** The free variable each kind is sampled over, for a host's help
|
|
23
|
+
/** The free variable each kind is sampled over, for a host's help. */
|
|
31
24
|
export declare const EQUATION_VARIABLES: {
|
|
32
25
|
readonly function: readonly ["x"];
|
|
33
26
|
readonly polar: readonly ["theta"];
|
|
34
27
|
readonly parametric: readonly ["t"];
|
|
35
28
|
readonly implicit: readonly ["x", "y"];
|
|
36
29
|
};
|
|
37
|
-
/**
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
*/
|
|
41
|
-
export declare function parseEquation(source: string, params?: readonly string[]): Equation | Error;
|
|
42
|
-
/**
|
|
43
|
-
* Why `source` will not plot, or `null` when it will. An empty one is not an
|
|
44
|
-
* error (nobody has written it yet), and neither is a name it does not know:
|
|
45
|
-
* that is a named value, see {@link equationParams}.
|
|
46
|
-
*/
|
|
30
|
+
/** `source` as an {@link Equation}, or the reason it is not one. */
|
|
31
|
+
export declare const parseEquation: (source: string, params?: readonly string[]) => Error | Equation;
|
|
32
|
+
/** Why `source` will not plot, or `null`. Empty text is no error, and neither is an unknown name: that is a named value ({@link equationParams}). */
|
|
47
33
|
export declare function equationError(source: string): string | null;
|
|
48
|
-
/**
|
|
49
|
-
* Every name `source` reads, in the order it first names them, variables
|
|
50
|
-
* included. Empty for text that does not parse.
|
|
51
|
-
*/
|
|
34
|
+
/** Every name `source` reads, in the order first named, variables included; empty for text that does not parse. */
|
|
52
35
|
export declare function equationNames(source: string): readonly string[];
|
|
53
|
-
/**
|
|
54
|
-
* The named values `source` reads: every name that is not a variable, a
|
|
55
|
-
* function or a constant.
|
|
56
|
-
*/
|
|
36
|
+
/** The named values `source` reads: every name that is not a variable, function or constant. */
|
|
57
37
|
export declare function equationParams(source: string): readonly string[];
|
|
58
38
|
/** `(a, b, …)` as its parts, or `null` when `source` is not one parenthesised list of two or more. */
|
|
59
39
|
export declare function splitTuple(source: string): string[] | null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"equation.d.ts","sourceRoot":"","sources":["../../src/kit/equation.ts"],"names":[],"mappings":"AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"equation.d.ts","sourceRoot":"","sources":["../../src/kit/equation.ts"],"names":[],"mappings":"AAEA,OAAO,EAA6B,KAAK,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElF;;;;;GAKG;AACH,MAAM,MAAM,QAAQ,GACd;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,EAAE,EAAE,GAAG,GAAG,GAAG,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAA;CAAE,GAC1D;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAA;CAAE,GACxC;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAA;CAAE,GACpE;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAA;CAAE,CAAC;AAElD,sEAAsE;AACtE,eAAO,MAAM,kBAAkB;;;;;CAKiC,CAAC;AAEjE,oEAAoE;AACpE,eAAO,MAAM,aAAa,kEAAiC,CAAC;AAE5D,qJAAqJ;AACrJ,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAI3D;AAOD,mHAAmH;AACnH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAe/D;AAED,gGAAgG;AAChG,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAEhE;AAuDD,sGAAsG;AACtG,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,CAS1D;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,CAezE"}
|
package/dist/kit/equation.js
CHANGED
|
@@ -1,38 +1,16 @@
|
|
|
1
1
|
import { expressionOps } from "@motionscript/sdk";
|
|
2
|
-
import { createBoundedCache } from "./cache.js";
|
|
3
|
-
import {
|
|
4
|
-
/** The free variable each kind is sampled over, for a host's help
|
|
2
|
+
import { createBoundedCache, memoParse } from "./cache.js";
|
|
3
|
+
import { compilerFor, MATH_DIALECT } from "./expression.js";
|
|
4
|
+
/** The free variable each kind is sampled over, for a host's help. */
|
|
5
5
|
export const EQUATION_VARIABLES = {
|
|
6
6
|
function: ["x"],
|
|
7
7
|
polar: ["theta"],
|
|
8
8
|
parametric: ["t"],
|
|
9
9
|
implicit: ["x", "y"],
|
|
10
10
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
* unchanged equation is the identical object and a geometry cache can key on it.
|
|
15
|
-
*/
|
|
16
|
-
export function parseEquation(source, params = []) {
|
|
17
|
-
const key = `${params.join(",")}|${source}`;
|
|
18
|
-
const hit = parsed.get(key);
|
|
19
|
-
if (hit !== undefined)
|
|
20
|
-
return hit;
|
|
21
|
-
let result;
|
|
22
|
-
try {
|
|
23
|
-
result = read(source.trim(), params);
|
|
24
|
-
}
|
|
25
|
-
catch (error) {
|
|
26
|
-
result = error instanceof Error ? error : new Error(String(error));
|
|
27
|
-
}
|
|
28
|
-
parsed.set(key, result);
|
|
29
|
-
return result;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Why `source` will not plot, or `null` when it will. An empty one is not an
|
|
33
|
-
* error (nobody has written it yet), and neither is a name it does not know:
|
|
34
|
-
* that is a named value, see {@link equationParams}.
|
|
35
|
-
*/
|
|
11
|
+
/** `source` as an {@link Equation}, or the reason it is not one. */
|
|
12
|
+
export const parseEquation = memoParse(256, read);
|
|
13
|
+
/** Why `source` will not plot, or `null`. Empty text is no error, and neither is an unknown name: that is a named value ({@link equationParams}). */
|
|
36
14
|
export function equationError(source) {
|
|
37
15
|
if (source.trim() === "")
|
|
38
16
|
return null;
|
|
@@ -42,10 +20,7 @@ export function equationError(source) {
|
|
|
42
20
|
/** A name that is always a variable of some kind of equation, so never a named value. */
|
|
43
21
|
const VARIABLE_NAMES = new Set(["r", ...Object.values(EQUATION_VARIABLES).flat()]);
|
|
44
22
|
const named = createBoundedCache(256);
|
|
45
|
-
/**
|
|
46
|
-
* Every name `source` reads, in the order it first names them, variables
|
|
47
|
-
* included. Empty for text that does not parse.
|
|
48
|
-
*/
|
|
23
|
+
/** Every name `source` reads, in the order first named, variables included; empty for text that does not parse. */
|
|
49
24
|
export function equationNames(source) {
|
|
50
25
|
const hit = named.get(source);
|
|
51
26
|
if (hit !== undefined)
|
|
@@ -64,10 +39,7 @@ export function equationNames(source) {
|
|
|
64
39
|
named.set(source, result);
|
|
65
40
|
return result;
|
|
66
41
|
}
|
|
67
|
-
/**
|
|
68
|
-
* The named values `source` reads: every name that is not a variable, a
|
|
69
|
-
* function or a constant.
|
|
70
|
-
*/
|
|
42
|
+
/** The named values `source` reads: every name that is not a variable, function or constant. */
|
|
71
43
|
export function equationParams(source) {
|
|
72
44
|
return equationNames(source).filter((name) => !VARIABLE_NAMES.has(name));
|
|
73
45
|
}
|
|
@@ -91,14 +63,7 @@ function collectNames(node, into) {
|
|
|
91
63
|
}
|
|
92
64
|
}
|
|
93
65
|
function read(source, params) {
|
|
94
|
-
|
|
95
|
-
throw new Error("Empty equation");
|
|
96
|
-
const compile = (text, variables) => {
|
|
97
|
-
const result = compileExpressionCached(text.trim(), variables, params);
|
|
98
|
-
if (result instanceof Error)
|
|
99
|
-
throw result;
|
|
100
|
-
return result;
|
|
101
|
-
};
|
|
66
|
+
const compile = compilerFor(params);
|
|
102
67
|
const pair = splitTuple(source);
|
|
103
68
|
if (pair?.length === 2)
|
|
104
69
|
return { kind: "parametric", x: compile(pair[0], ["t"]), y: compile(pair[1], ["t"]) };
|
package/dist/kit/equation.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"equation.js","sourceRoot":"","sources":["../../src/kit/equation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAuB,MAAM,mBAAmB,CAAC;AACvE,OAAO,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAC7C,OAAO,EAAE,uBAAuB,EAAE,YAAY,EAA2B,MAAM,cAAc,CAAC;AAoB9F,2EAA2E;AAC3E,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAC9B,QAAQ,EAAE,CAAC,GAAG,CAAC;IACf,KAAK,EAAE,CAAC,OAAO,CAAC;IAChB,UAAU,EAAE,CAAC,GAAG,CAAC;IACjB,QAAQ,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC;CACwC,CAAC;AAEjE,MAAM,MAAM,GAAG,kBAAkB,CAAmB,GAAG,CAAC,CAAC;AAEzD;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,MAAc,EAAE,SAA4B,EAAE;IACxE,MAAM,GAAG,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,MAAM,EAAE,CAAC;IAC5C,MAAM,GAAG,GAA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E,CAAC,CAAC,CAAC,CAAC;YAClC,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;QACjB,CAAC;IACL,CAAC;IACD,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IAC/B,OAAO,KAAK,CAAC;AACjB,CAAC","sourcesContent":["import { expressionOps, type ExpressionNode } from \"@motionscript/sdk\";\nimport { createBoundedCache } from \"./cache\";\nimport { compileExpressionCached, MATH_DIALECT, type CompiledExpression } from \"./expression\";\n\n/**\n * A plotted equation, read the way it is written rather than by a `kind` prop:\n *\n * | Written as | Is |\n * |---|---|\n * | `sin(x)`, `y = sin(x)` | a function of `x` |\n * | `x = y^2` | a function of `y` |\n * | `r = 1 + cos(theta)` | polar, in `theta` |\n * | `(cos(t), sin(t))` | parametric, in `t` |\n * | `x^2 + y^2 = 4` | implicit, in `x` and `y` |\n */\nexport type Equation =\n | { kind: \"function\"; of: \"x\" | \"y\"; f: CompiledExpression }\n | { kind: \"polar\"; f: CompiledExpression }\n | { kind: \"parametric\"; x: CompiledExpression; y: CompiledExpression }\n /** Drawn where `f(x, y)` is zero. */\n | { kind: \"implicit\"; f: CompiledExpression };\n\n/** The free variable each kind is sampled over, for a host's help text. */\nexport const EQUATION_VARIABLES = {\n function: [\"x\"],\n polar: [\"theta\"],\n parametric: [\"t\"],\n implicit: [\"x\", \"y\"],\n} as const satisfies Record<Equation[\"kind\"], readonly string[]>;\n\nconst parsed = createBoundedCache<Equation | Error>(256);\n\n/**\n * `source` as an {@link Equation}, or the reason it is not one. Memoised, so an\n * unchanged equation is the identical object and a geometry cache can key on it.\n */\nexport function parseEquation(source: string, params: readonly string[] = []): Equation | Error {\n const key = `${params.join(\",\")}|${source}`;\n const hit = parsed.get(key);\n if (hit !== undefined) return hit;\n let result: Equation | Error;\n try {\n result = read(source.trim(), params);\n } catch (error) {\n result = error instanceof Error ? error : new Error(String(error));\n }\n parsed.set(key, result);\n return result;\n}\n\n/**\n * Why `source` will not plot, or `null` when it will. An empty one is not an\n * error (nobody has written it yet), and neither is a name it does not know:\n * that is a named value, see {@link equationParams}.\n */\nexport function equationError(source: string): string | null {\n if (source.trim() === \"\") return null;\n const equation = parseEquation(source, equationParams(source));\n return equation instanceof Error ? equation.message : null;\n}\n\n/** A name that is always a variable of some kind of equation, so never a named value. */\nconst VARIABLE_NAMES: ReadonlySet<string> = new Set([\"r\", ...Object.values(EQUATION_VARIABLES).flat()]);\n\nconst named = createBoundedCache<readonly string[]>(256);\n\n/**\n * Every name `source` reads, in the order it first names them, variables\n * included. Empty for text that does not parse.\n */\nexport function equationNames(source: string): readonly string[] {\n const hit = named.get(source);\n if (hit !== undefined) return hit;\n const names = new Set<string>();\n const text = source.trim();\n try {\n for (const part of splitTuple(text) ?? splitTopLevel(text, \"=\")) {\n collectNames(expressionOps.parse(part.trim(), MATH_DIALECT).root, names);\n }\n } catch {\n names.clear();\n }\n const result = [...names];\n named.set(source, result);\n return result;\n}\n\n/**\n * The named values `source` reads: every name that is not a variable, a\n * function or a constant.\n */\nexport function equationParams(source: string): readonly string[] {\n return equationNames(source).filter((name) => !VARIABLE_NAMES.has(name));\n}\n\nfunction collectNames(node: ExpressionNode, into: Set<string>): void {\n switch (node.kind) {\n case \"ref\":\n into.add(node.name);\n return;\n case \"unary\":\n collectNames(node.operand, into);\n return;\n case \"binary\":\n collectNames(node.left, into);\n collectNames(node.right, into);\n return;\n case \"call\":\n for (const arg of node.args) collectNames(arg, into);\n return;\n default:\n }\n}\n\nfunction read(source: string, params: readonly string[]): Equation {\n if (source === \"\") throw new Error(\"Empty equation\");\n const compile = (text: string, variables: readonly string[]): CompiledExpression => {\n const result = compileExpressionCached(text.trim(), variables, params);\n if (result instanceof Error) throw result;\n return result;\n };\n\n const pair = splitTuple(source);\n if (pair?.length === 2) return { kind: \"parametric\", x: compile(pair[0], [\"t\"]), y: compile(pair[1], [\"t\"]) };\n\n const sides = splitTopLevel(source, \"=\");\n if (sides.length > 2) throw new Error(\"An equation has one '='\");\n if (sides.length === 1) return { kind: \"function\", of: \"x\", f: compile(source, [\"x\"]) };\n\n const [left, right] = sides;\n const subject = left.trim();\n const implicit = (): Equation => {\n const l = compile(left, [\"x\", \"y\"]);\n const r = compile(right, [\"x\", \"y\"]);\n return { kind: \"implicit\", f: (x, y, scope) => l(x, y, scope) - r(x, y, scope) };\n };\n if (subject === \"r\") return { kind: \"polar\", f: compile(right, [\"theta\"]) };\n if (subject === \"y\" || subject === \"x\") {\n const of = subject === \"y\" ? \"x\" : \"y\";\n // `y = x + y` names its own subject on the right, so it is not a function of one variable.\n try {\n return { kind: \"function\", of, f: compile(right, [of]) };\n } catch (error) {\n try {\n return implicit();\n } catch {\n throw error;\n }\n }\n }\n return implicit();\n}\n\n/** `(a, b, …)` as its parts, or `null` when `source` is not one parenthesised list of two or more. */\nexport function splitTuple(source: string): string[] | null {\n if (!source.startsWith(\"(\") || !source.endsWith(\")\")) return null;\n let depth = 0;\n for (let i = 0; i < source.length - 1; i++) {\n if (source[i] === \"(\") depth++;\n else if (source[i] === \")\" && --depth === 0) return null;\n }\n const parts = splitTopLevel(source.slice(1, -1), \",\");\n return parts.length >= 2 ? parts : null;\n}\n\nexport function splitTopLevel(source: string, separator: string): string[] {\n const parts: string[] = [];\n let depth = 0;\n let from = 0;\n for (let i = 0; i < source.length; i++) {\n const c = source[i];\n if (c === \"(\") depth++;\n else if (c === \")\") depth--;\n else if (c === separator && depth === 0) {\n parts.push(source.slice(from, i));\n from = i + 1;\n }\n }\n parts.push(source.slice(from));\n return parts;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"equation.js","sourceRoot":"","sources":["../../src/kit/equation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAuB,MAAM,mBAAmB,CAAC;AACvE,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,YAAY,EAA2B,MAAM,cAAc,CAAC;AAclF,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,KAAK,GAAG,aAAa,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IACtD,OAAO,KAAK,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;AAC5C,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,MAAc,EAAE,SAAiB;IAC3D,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;QACpB,IAAI,CAAC,KAAK,GAAG;YAAE,KAAK,EAAE,CAAC;aAClB,IAAI,CAAC,KAAK,GAAG;YAAE,KAAK,EAAE,CAAC;aACvB,IAAI,CAAC,KAAK,SAAS,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;YACtC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;YAClC,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;QACjB,CAAC;IACL,CAAC;IACD,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IAC/B,OAAO,KAAK,CAAC;AACjB,CAAC","sourcesContent":["import { expressionOps, type ExpressionNode } from \"@motionscript/sdk\";\nimport { createBoundedCache, memoParse } from \"./cache\";\nimport { compilerFor, MATH_DIALECT, type CompiledExpression } from \"./expression\";\n\n/**\n * A plotted equation, read the way it is written rather than by a `kind` prop:\n * `sin(x)` or `y = sin(x)` is a function of `x`, `x = y^2` of `y`,\n * `r = 1 + cos(theta)` polar, `(cos(t), sin(t))` parametric, and\n * `x^2 + y^2 = 4` implicit.\n */\nexport type Equation =\n | { kind: \"function\"; of: \"x\" | \"y\"; f: CompiledExpression }\n | { kind: \"polar\"; f: CompiledExpression }\n | { kind: \"parametric\"; x: CompiledExpression; y: CompiledExpression }\n | { kind: \"implicit\"; f: CompiledExpression };\n\n/** The free variable each kind is sampled over, for a host's help. */\nexport const EQUATION_VARIABLES = {\n function: [\"x\"],\n polar: [\"theta\"],\n parametric: [\"t\"],\n implicit: [\"x\", \"y\"],\n} as const satisfies Record<Equation[\"kind\"], readonly string[]>;\n\n/** `source` as an {@link Equation}, or the reason it is not one. */\nexport const parseEquation = memoParse<Equation>(256, read);\n\n/** Why `source` will not plot, or `null`. Empty text is no error, and neither is an unknown name: that is a named value ({@link equationParams}). */\nexport function equationError(source: string): string | null {\n if (source.trim() === \"\") return null;\n const equation = parseEquation(source, equationParams(source));\n return equation instanceof Error ? equation.message : null;\n}\n\n/** A name that is always a variable of some kind of equation, so never a named value. */\nconst VARIABLE_NAMES: ReadonlySet<string> = new Set([\"r\", ...Object.values(EQUATION_VARIABLES).flat()]);\n\nconst named = createBoundedCache<readonly string[]>(256);\n\n/** Every name `source` reads, in the order first named, variables included; empty for text that does not parse. */\nexport function equationNames(source: string): readonly string[] {\n const hit = named.get(source);\n if (hit !== undefined) return hit;\n const names = new Set<string>();\n const text = source.trim();\n try {\n for (const part of splitTuple(text) ?? splitTopLevel(text, \"=\")) {\n collectNames(expressionOps.parse(part.trim(), MATH_DIALECT).root, names);\n }\n } catch {\n names.clear();\n }\n const result = [...names];\n named.set(source, result);\n return result;\n}\n\n/** The named values `source` reads: every name that is not a variable, function or constant. */\nexport function equationParams(source: string): readonly string[] {\n return equationNames(source).filter((name) => !VARIABLE_NAMES.has(name));\n}\n\nfunction collectNames(node: ExpressionNode, into: Set<string>): void {\n switch (node.kind) {\n case \"ref\":\n into.add(node.name);\n return;\n case \"unary\":\n collectNames(node.operand, into);\n return;\n case \"binary\":\n collectNames(node.left, into);\n collectNames(node.right, into);\n return;\n case \"call\":\n for (const arg of node.args) collectNames(arg, into);\n return;\n default:\n }\n}\n\nfunction read(source: string, params: readonly string[]): Equation {\n const compile = compilerFor(params);\n\n const pair = splitTuple(source);\n if (pair?.length === 2) return { kind: \"parametric\", x: compile(pair[0], [\"t\"]), y: compile(pair[1], [\"t\"]) };\n\n const sides = splitTopLevel(source, \"=\");\n if (sides.length > 2) throw new Error(\"An equation has one '='\");\n if (sides.length === 1) return { kind: \"function\", of: \"x\", f: compile(source, [\"x\"]) };\n\n const [left, right] = sides;\n const subject = left.trim();\n const implicit = (): Equation => {\n const l = compile(left, [\"x\", \"y\"]);\n const r = compile(right, [\"x\", \"y\"]);\n return { kind: \"implicit\", f: (x, y, scope) => l(x, y, scope) - r(x, y, scope) };\n };\n if (subject === \"r\") return { kind: \"polar\", f: compile(right, [\"theta\"]) };\n if (subject === \"y\" || subject === \"x\") {\n const of = subject === \"y\" ? \"x\" : \"y\";\n // `y = x + y` names its own subject on the right, so it is not a function of one variable.\n try {\n return { kind: \"function\", of, f: compile(right, [of]) };\n } catch (error) {\n try {\n return implicit();\n } catch {\n throw error;\n }\n }\n }\n return implicit();\n}\n\n/** `(a, b, …)` as its parts, or `null` when `source` is not one parenthesised list of two or more. */\nexport function splitTuple(source: string): string[] | null {\n if (!source.startsWith(\"(\") || !source.endsWith(\")\")) return null;\n let depth = 0;\n for (let i = 0; i < source.length - 1; i++) {\n if (source[i] === \"(\") depth++;\n else if (source[i] === \")\" && --depth === 0) return null;\n }\n const parts = splitTopLevel(source.slice(1, -1), \",\");\n return parts.length >= 2 ? parts : null;\n}\n\nexport function splitTopLevel(source: string, separator: string): string[] {\n const parts: string[] = [];\n let depth = 0;\n let from = 0;\n for (let i = 0; i < source.length; i++) {\n const c = source[i];\n if (c === \"(\") depth++;\n else if (c === \")\") depth--;\n else if (c === separator && depth === 0) {\n parts.push(source.slice(from, i));\n from = i + 1;\n }\n }\n parts.push(source.slice(from));\n return parts;\n}\n"]}
|
package/dist/kit/equation3d.d.ts
CHANGED
|
@@ -32,10 +32,10 @@ export declare const EQUATION_VARIABLES_3D: {
|
|
|
32
32
|
export declare function surfaceParams(source: string): readonly string[];
|
|
33
33
|
/** The named values a space curve's equation reads. */
|
|
34
34
|
export declare function spaceCurveParams(source: string): readonly string[];
|
|
35
|
-
/** `source` as a surface, or the reason it is not one.
|
|
36
|
-
export declare
|
|
35
|
+
/** `source` as a surface, or the reason it is not one. */
|
|
36
|
+
export declare const parseSurface: (source: string, params?: readonly string[]) => Error | SurfaceEquation;
|
|
37
37
|
/** `source` as a space curve, or the reason it is not one. */
|
|
38
|
-
export declare
|
|
38
|
+
export declare const parseSpaceCurve: (source: string, params?: readonly string[]) => Error | SpaceCurveEquation;
|
|
39
39
|
/** Why `source` will not draw as a surface, or `null`; empty text is not an error. */
|
|
40
40
|
export declare function surfaceError(source: string): string | null;
|
|
41
41
|
/** Why `source` will not draw as a space curve, or `null`. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"equation3d.d.ts","sourceRoot":"","sources":["../../src/kit/equation3d.ts"],"names":[],"mappings":"AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"equation3d.d.ts","sourceRoot":"","sources":["../../src/kit/equation3d.ts"],"names":[],"mappings":"AAEA,OAAO,EAAe,KAAK,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAGpE;;;;;;;GAOG;AACH,MAAM,MAAM,eAAe,GACrB;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAA;CAAE,GACzC;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAA;CAAE,CAAC;AAElG,4EAA4E;AAC5E,MAAM,WAAW,kBAAkB;IAC/B,CAAC,EAAE,kBAAkB,CAAC;IACtB,CAAC,EAAE,kBAAkB,CAAC;IACtB,CAAC,EAAE,kBAAkB,CAAC;CACzB;AAED,iEAAiE;AACjE,eAAO,MAAM,qBAAqB;;;;CAIxB,CAAC;AAKX,gGAAgG;AAChG,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAE/D;AAED,uDAAuD;AACvD,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAElE;AAED,0DAA0D;AAC1D,eAAO,MAAM,YAAY,yEAYvB,CAAC;AAEH,8DAA8D;AAC9D,eAAO,MAAM,eAAe,4EAM1B,CAAC;AAEH,sFAAsF;AACtF,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAI1D;AAED,8DAA8D;AAC9D,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAI7D;AAED,qHAAqH;AACrH,MAAM,WAAW,gBAAgB;IAC7B,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IACrC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAAC;IAC1C,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;CAChC;AAED,eAAO,MAAM,cAAc,EAAE,QAAQ,CACjC,MAAM,CAAC,UAAU,GAAG,iBAAiB,GAAG,oBAAoB,GAAG,eAAe,EAAE,gBAAgB,CAAC,CAUpG,CAAC"}
|
package/dist/kit/equation3d.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { memoParse } from "./cache.js";
|
|
2
2
|
import { equationError, equationNames, equationParams, EQUATION_VARIABLES, splitTopLevel, splitTuple } from "./equation.js";
|
|
3
|
-
import {
|
|
3
|
+
import { compilerFor } from "./expression.js";
|
|
4
4
|
import { fieldError, fieldParams, FIELD_VARIABLES } from "./field.js";
|
|
5
5
|
/** What each 3D form is written over, for a host's help text. */
|
|
6
6
|
export const EQUATION_VARIABLES_3D = {
|
|
@@ -10,8 +10,6 @@ export const EQUATION_VARIABLES_3D = {
|
|
|
10
10
|
};
|
|
11
11
|
const SURFACE_NAMES = new Set(["x", "y", "z", "u", "v"]);
|
|
12
12
|
const SPACE_CURVE_NAMES = new Set(["t"]);
|
|
13
|
-
const surfaces = createBoundedCache(128);
|
|
14
|
-
const curves = createBoundedCache(128);
|
|
15
13
|
/** The named values a surface's equation reads: every name that is not one of its variables. */
|
|
16
14
|
export function surfaceParams(source) {
|
|
17
15
|
return equationNames(source).filter((name) => !SURFACE_NAMES.has(name));
|
|
@@ -20,36 +18,32 @@ export function surfaceParams(source) {
|
|
|
20
18
|
export function spaceCurveParams(source) {
|
|
21
19
|
return equationNames(source).filter((name) => !SPACE_CURVE_NAMES.has(name));
|
|
22
20
|
}
|
|
23
|
-
/** `source` as a surface, or the reason it is not one.
|
|
24
|
-
export
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (parts)
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
});
|
|
41
|
-
}
|
|
21
|
+
/** `source` as a surface, or the reason it is not one. */
|
|
22
|
+
export const parseSurface = memoParse(128, (text, params) => {
|
|
23
|
+
const compile = compilerFor(params);
|
|
24
|
+
const parts = splitTuple(text);
|
|
25
|
+
if (parts) {
|
|
26
|
+
if (parts.length !== 3)
|
|
27
|
+
throw new Error("A parametric surface has three parts: (x, y, z)");
|
|
28
|
+
const [x, y, z] = parts.map((part) => compile(part, EQUATION_VARIABLES_3D.surface));
|
|
29
|
+
return { kind: "parametric", x, y, z };
|
|
30
|
+
}
|
|
31
|
+
const sides = splitTopLevel(text, "=");
|
|
32
|
+
if (sides.length > 2)
|
|
33
|
+
throw new Error("An equation has one '='");
|
|
34
|
+
if (sides.length === 2 && sides[0].trim() !== "z")
|
|
35
|
+
throw new Error("A surface is written z = …, or as three parts (x, y, z)");
|
|
36
|
+
return { kind: "height", f: compile(sides[sides.length - 1], EQUATION_VARIABLES_3D.height) };
|
|
37
|
+
});
|
|
42
38
|
/** `source` as a space curve, or the reason it is not one. */
|
|
43
|
-
export
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
});
|
|
52
|
-
}
|
|
39
|
+
export const parseSpaceCurve = memoParse(128, (text, params) => {
|
|
40
|
+
const compile = compilerFor(params);
|
|
41
|
+
const parts = splitTuple(text);
|
|
42
|
+
if (parts?.length !== 3)
|
|
43
|
+
throw new Error("A space curve has three parts: (x, y, z)");
|
|
44
|
+
const [x, y, z] = parts.map((part) => compile(part, EQUATION_VARIABLES_3D.spaceCurve));
|
|
45
|
+
return { x, y, z };
|
|
46
|
+
});
|
|
53
47
|
/** Why `source` will not draw as a surface, or `null`; empty text is not an error. */
|
|
54
48
|
export function surfaceError(source) {
|
|
55
49
|
if (source.trim() === "")
|
|
@@ -64,32 +58,6 @@ export function spaceCurveError(source) {
|
|
|
64
58
|
const equation = parseSpaceCurve(source, spaceCurveParams(source));
|
|
65
59
|
return equation instanceof Error ? equation.message : null;
|
|
66
60
|
}
|
|
67
|
-
function compiler(params) {
|
|
68
|
-
return (text, variables) => {
|
|
69
|
-
const result = compileExpressionCached(text.trim(), variables, params);
|
|
70
|
-
if (result instanceof Error)
|
|
71
|
-
throw result;
|
|
72
|
-
return result;
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
function memo(cache, source, params, read) {
|
|
76
|
-
const key = `${params.join(",")}|${source}`;
|
|
77
|
-
const hit = cache.get(key);
|
|
78
|
-
if (hit !== undefined)
|
|
79
|
-
return hit;
|
|
80
|
-
let result;
|
|
81
|
-
try {
|
|
82
|
-
const text = source.trim();
|
|
83
|
-
if (text === "")
|
|
84
|
-
throw new Error("Empty equation");
|
|
85
|
-
result = read(text);
|
|
86
|
-
}
|
|
87
|
-
catch (error) {
|
|
88
|
-
result = error instanceof Error ? error : new Error(String(error));
|
|
89
|
-
}
|
|
90
|
-
cache.set(key, result);
|
|
91
|
-
return result;
|
|
92
|
-
}
|
|
93
61
|
export const EQUATION_KINDS = {
|
|
94
62
|
equation: { error: equationError, params: equationParams, variables: [...new Set(Object.values(EQUATION_VARIABLES).flat())] },
|
|
95
63
|
surfaceEquation: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"equation3d.js","sourceRoot":"","sources":["../../src/kit/equation3d.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,cAAc,EAAE,kBAAkB,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACzH,OAAO,EAAE,uBAAuB,EAA2B,MAAM,cAAc,CAAC;AAChF,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAqBnE,iEAAiE;AACjE,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACjC,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC;IAClB,OAAO,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC;IACnB,UAAU,EAAE,CAAC,GAAG,CAAC;CACX,CAAC;AAEX,MAAM,aAAa,GAAwB,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;AAC9E,MAAM,iBAAiB,GAAwB,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AAE9D,MAAM,QAAQ,GAAG,kBAAkB,CAA0B,GAAG,CAAC,CAAC;AAClE,MAAM,MAAM,GAAG,kBAAkB,CAA6B,GAAG,CAAC,CAAC;AAEnE,gGAAgG;AAChG,MAAM,UAAU,aAAa,CAAC,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C,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IACvE,CAAC;IACD,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IACvB,OAAO,MAAM,CAAC;AAClB,CAAC;AASD,MAAM,CAAC,MAAM,cAAc,GAEvB;IACA,QAAQ,EAAE,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,EAAE,cAAc,EAAE,SAAS,EAAE,CAAC,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,kBAAkB,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE;IAC7H,eAAe,EAAE;QACb,KAAK,EAAE,YAAY;QACnB,MAAM,EAAE,aAAa;QACrB,SAAS,EAAE,CAAC,GAAG,qBAAqB,CAAC,MAAM,EAAE,GAAG,qBAAqB,CAAC,OAAO,CAAC;KACjF;IACD,kBAAkB,EAAE,EAAE,KAAK,EAAE,eAAe,EAAE,MAAM,EAAE,gBAAgB,EAAE,SAAS,EAAE,qBAAqB,CAAC,UAAU,EAAE;IACrH,aAAa,EAAE,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,eAAe,EAAE;CACxF,CAAC","sourcesContent":["import { createBoundedCache } from \"./cache\";\nimport { equationError, equationNames, equationParams, EQUATION_VARIABLES, splitTopLevel, splitTuple } from \"./equation\";\nimport { compileExpressionCached, type CompiledExpression } from \"./expression\";\nimport { fieldError, fieldParams, FIELD_VARIABLES } from \"./field\";\n\n/**\n * A surface's equation, read the way it is written:\n *\n * | Written as | Is |\n * |---|---|\n * | `sin(x) * cos(y)`, `z = x^2 - y^2` | a height over `x` and `y` |\n * | `(cos(u) sin(v), sin(u) sin(v), cos(v))` | parametric, in `u` and `v` |\n */\nexport type SurfaceEquation =\n | { kind: \"height\"; f: CompiledExpression }\n | { kind: \"parametric\"; x: CompiledExpression; y: CompiledExpression; z: CompiledExpression };\n\n/** A curve through space, three parts in `t`: `(cos(t), sin(t), t / 4)`. */\nexport interface SpaceCurveEquation {\n x: CompiledExpression;\n y: CompiledExpression;\n z: CompiledExpression;\n}\n\n/** What each 3D form is written over, for a host's help text. */\nexport const EQUATION_VARIABLES_3D = {\n height: [\"x\", \"y\"],\n surface: [\"u\", \"v\"],\n spaceCurve: [\"t\"],\n} as const;\n\nconst SURFACE_NAMES: ReadonlySet<string> = new Set([\"x\", \"y\", \"z\", \"u\", \"v\"]);\nconst SPACE_CURVE_NAMES: ReadonlySet<string> = new Set([\"t\"]);\n\nconst surfaces = createBoundedCache<SurfaceEquation | Error>(128);\nconst curves = createBoundedCache<SpaceCurveEquation | Error>(128);\n\n/** The named values a surface's equation reads: every name that is not one of its variables. */\nexport function surfaceParams(source: string): readonly string[] {\n return equationNames(source).filter((name) => !SURFACE_NAMES.has(name));\n}\n\n/** The named values a space curve's equation reads. */\nexport function spaceCurveParams(source: string): readonly string[] {\n return equationNames(source).filter((name) => !SPACE_CURVE_NAMES.has(name));\n}\n\n/** `source` as a surface, or the reason it is not one. Memoised, so an unchanged equation is the identical object. */\nexport function parseSurface(source: string, params: readonly string[] = []): SurfaceEquation | Error {\n return memo(surfaces, source, params, (text) => {\n const compile = compiler(params);\n const parts = splitTuple(text);\n if (parts) {\n if (parts.length !== 3) throw new Error(\"A parametric surface has three parts: (x, y, z)\");\n const [x, y, z] = parts.map((part) => compile(part, EQUATION_VARIABLES_3D.surface));\n return { kind: \"parametric\", x, y, z };\n }\n const sides = splitTopLevel(text, \"=\");\n if (sides.length > 2) throw new Error(\"An equation has one '='\");\n if (sides.length === 2 && sides[0].trim() !== \"z\") throw new Error(\"A surface is written z = …, or as three parts (x, y, z)\");\n return { kind: \"height\", f: compile(sides[sides.length - 1], EQUATION_VARIABLES_3D.height) };\n });\n}\n\n/** `source` as a space curve, or the reason it is not one. */\nexport function parseSpaceCurve(source: string, params: readonly string[] = []): SpaceCurveEquation | Error {\n return memo(curves, source, params, (text) => {\n const compile = compiler(params);\n const parts = splitTuple(text);\n if (parts?.length !== 3) throw new Error(\"A space curve has three parts: (x, y, z)\");\n const [x, y, z] = parts.map((part) => compile(part, EQUATION_VARIABLES_3D.spaceCurve));\n return { x, y, z };\n });\n}\n\n/** Why `source` will not draw as a surface, or `null`; empty text is not an error. */\nexport function surfaceError(source: string): string | null {\n if (source.trim() === \"\") return null;\n const equation = parseSurface(source, surfaceParams(source));\n return equation instanceof Error ? equation.message : null;\n}\n\n/** Why `source` will not draw as a space curve, or `null`. */\nexport function spaceCurveError(source: string): string | null {\n if (source.trim() === \"\") return null;\n const equation = parseSpaceCurve(source, spaceCurveParams(source));\n return equation instanceof Error ? equation.message : null;\n}\n\nfunction compiler(params: readonly string[]) {\n return (text: string, variables: readonly string[]): CompiledExpression => {\n const result = compileExpressionCached(text.trim(), variables, params);\n if (result instanceof Error) throw result;\n return result;\n };\n}\n\nfunction memo<T extends object>(\n cache: ReturnType<typeof createBoundedCache<T | Error>>,\n source: string,\n params: readonly string[],\n read: (text: string) => T,\n): T | Error {\n const key = `${params.join(\",\")}|${source}`;\n const hit = cache.get(key);\n if (hit !== undefined) return hit;\n let result: T | Error;\n try {\n const text = source.trim();\n if (text === \"\") throw new Error(\"Empty equation\");\n result = read(text);\n } catch (error) {\n result = error instanceof Error ? error : new Error(String(error));\n }\n cache.set(key, result);\n return result;\n}\n\n/** How a host checks an equation as it is typed, and finds its named values, by the `kind` its property declares. */\nexport interface EquationLanguage {\n error(source: string): string | null;\n params(source: string): readonly string[];\n variables: readonly string[];\n}\n\nexport const EQUATION_KINDS: Readonly<\n Record<\"equation\" | \"surfaceEquation\" | \"spaceCurveEquation\" | \"fieldEquation\", EquationLanguage>\n> = {\n equation: { error: equationError, params: equationParams, variables: [...new Set(Object.values(EQUATION_VARIABLES).flat())] },\n surfaceEquation: {\n error: surfaceError,\n params: surfaceParams,\n variables: [...EQUATION_VARIABLES_3D.height, ...EQUATION_VARIABLES_3D.surface],\n },\n spaceCurveEquation: { error: spaceCurveError, params: spaceCurveParams, variables: EQUATION_VARIABLES_3D.spaceCurve },\n fieldEquation: { error: fieldError, params: fieldParams, variables: FIELD_VARIABLES },\n};\n"]}
|
|
1
|
+
{"version":3,"file":"equation3d.js","sourceRoot":"","sources":["../../src/kit/equation3d.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,cAAc,EAAE,kBAAkB,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACzH,OAAO,EAAE,WAAW,EAA2B,MAAM,cAAc,CAAC;AACpE,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAqBnE,iEAAiE;AACjE,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACjC,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC;IAClB,OAAO,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC;IACnB,UAAU,EAAE,CAAC,GAAG,CAAC;CACX,CAAC;AAEX,MAAM,aAAa,GAAwB,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;AAC9E,MAAM,iBAAiB,GAAwB,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AAE9D,gGAAgG;AAChG,MAAM,UAAU,aAAa,CAAC,MAAc;IACxC,OAAO,aAAa,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;AAC5E,CAAC;AAED,uDAAuD;AACvD,MAAM,UAAU,gBAAgB,CAAC,MAAc;IAC3C,OAAO,aAAa,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,iBA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AE,aAAa,EAAE,MAAM,EAAE,cAAc,EAAE,SAAS,EAAE,CAAC,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,kBAAkB,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE;IAC7H,eAAe,EAAE;QACb,KAAK,EAAE,YAAY;QACnB,MAAM,EAAE,aAAa;QACrB,SAAS,EAAE,CAAC,GAAG,qBAAqB,CAAC,MAAM,EAAE,GAAG,qBAAqB,CAAC,OAAO,CAAC;KACjF;IACD,kBAAkB,EAAE,EAAE,KAAK,EAAE,eAAe,EAAE,MAAM,EAAE,gBAAgB,EAAE,SAAS,EAAE,qBAAqB,CAAC,UAAU,EAAE;IACrH,aAAa,EAAE,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,eAAe,EAAE;CACxF,CAAC","sourcesContent":["import { memoParse } from \"./cache\";\nimport { equationError, equationNames, equationParams, EQUATION_VARIABLES, splitTopLevel, splitTuple } from \"./equation\";\nimport { compilerFor, type CompiledExpression } from \"./expression\";\nimport { fieldError, fieldParams, FIELD_VARIABLES } from \"./field\";\n\n/**\n * A surface's equation, read the way it is written:\n *\n * | Written as | Is |\n * |---|---|\n * | `sin(x) * cos(y)`, `z = x^2 - y^2` | a height over `x` and `y` |\n * | `(cos(u) sin(v), sin(u) sin(v), cos(v))` | parametric, in `u` and `v` |\n */\nexport type SurfaceEquation =\n | { kind: \"height\"; f: CompiledExpression }\n | { kind: \"parametric\"; x: CompiledExpression; y: CompiledExpression; z: CompiledExpression };\n\n/** A curve through space, three parts in `t`: `(cos(t), sin(t), t / 4)`. */\nexport interface SpaceCurveEquation {\n x: CompiledExpression;\n y: CompiledExpression;\n z: CompiledExpression;\n}\n\n/** What each 3D form is written over, for a host's help text. */\nexport const EQUATION_VARIABLES_3D = {\n height: [\"x\", \"y\"],\n surface: [\"u\", \"v\"],\n spaceCurve: [\"t\"],\n} as const;\n\nconst SURFACE_NAMES: ReadonlySet<string> = new Set([\"x\", \"y\", \"z\", \"u\", \"v\"]);\nconst SPACE_CURVE_NAMES: ReadonlySet<string> = new Set([\"t\"]);\n\n/** The named values a surface's equation reads: every name that is not one of its variables. */\nexport function surfaceParams(source: string): readonly string[] {\n return equationNames(source).filter((name) => !SURFACE_NAMES.has(name));\n}\n\n/** The named values a space curve's equation reads. */\nexport function spaceCurveParams(source: string): readonly string[] {\n return equationNames(source).filter((name) => !SPACE_CURVE_NAMES.has(name));\n}\n\n/** `source` as a surface, or the reason it is not one. */\nexport const parseSurface = memoParse<SurfaceEquation>(128, (text, params) => {\n const compile = compilerFor(params);\n const parts = splitTuple(text);\n if (parts) {\n if (parts.length !== 3) throw new Error(\"A parametric surface has three parts: (x, y, z)\");\n const [x, y, z] = parts.map((part) => compile(part, EQUATION_VARIABLES_3D.surface));\n return { kind: \"parametric\", x, y, z };\n }\n const sides = splitTopLevel(text, \"=\");\n if (sides.length > 2) throw new Error(\"An equation has one '='\");\n if (sides.length === 2 && sides[0].trim() !== \"z\") throw new Error(\"A surface is written z = …, or as three parts (x, y, z)\");\n return { kind: \"height\", f: compile(sides[sides.length - 1], EQUATION_VARIABLES_3D.height) };\n});\n\n/** `source` as a space curve, or the reason it is not one. */\nexport const parseSpaceCurve = memoParse<SpaceCurveEquation>(128, (text, params) => {\n const compile = compilerFor(params);\n const parts = splitTuple(text);\n if (parts?.length !== 3) throw new Error(\"A space curve has three parts: (x, y, z)\");\n const [x, y, z] = parts.map((part) => compile(part, EQUATION_VARIABLES_3D.spaceCurve));\n return { x, y, z };\n});\n\n/** Why `source` will not draw as a surface, or `null`; empty text is not an error. */\nexport function surfaceError(source: string): string | null {\n if (source.trim() === \"\") return null;\n const equation = parseSurface(source, surfaceParams(source));\n return equation instanceof Error ? equation.message : null;\n}\n\n/** Why `source` will not draw as a space curve, or `null`. */\nexport function spaceCurveError(source: string): string | null {\n if (source.trim() === \"\") return null;\n const equation = parseSpaceCurve(source, spaceCurveParams(source));\n return equation instanceof Error ? equation.message : null;\n}\n\n/** How a host checks an equation as it is typed, and finds its named values, by the `kind` its property declares. */\nexport interface EquationLanguage {\n error(source: string): string | null;\n params(source: string): readonly string[];\n variables: readonly string[];\n}\n\nexport const EQUATION_KINDS: Readonly<\n Record<\"equation\" | \"surfaceEquation\" | \"spaceCurveEquation\" | \"fieldEquation\", EquationLanguage>\n> = {\n equation: { error: equationError, params: equationParams, variables: [...new Set(Object.values(EQUATION_VARIABLES).flat())] },\n surfaceEquation: {\n error: surfaceError,\n params: surfaceParams,\n variables: [...EQUATION_VARIABLES_3D.height, ...EQUATION_VARIABLES_3D.surface],\n },\n spaceCurveEquation: { error: spaceCurveError, params: spaceCurveParams, variables: EQUATION_VARIABLES_3D.spaceCurve },\n fieldEquation: { error: fieldError, params: fieldParams, variables: FIELD_VARIABLES },\n};\n"]}
|
package/dist/kit/expression.d.ts
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
1
|
import type { ExpressionDialect } from "@motionscript/sdk";
|
|
2
|
-
/**
|
|
3
|
-
* The language a plot's equation is written in: the engine's own expression
|
|
4
|
-
* grammar (`expressionOps.parse`) under a dialect of its own, where trig is in
|
|
5
|
-
* radians, names are bare identifiers, and the logs and hyperbolics a plot
|
|
6
|
-
* wants are present. Never `new Function`: an equation is text stored in a
|
|
7
|
-
* document, and the shared parser can only produce arithmetic.
|
|
8
|
-
*/
|
|
9
2
|
/** Named values an expression reads at call time, beside its two variables. */
|
|
10
3
|
export type ExpressionScope = Readonly<Record<string, number>>;
|
|
11
4
|
/**
|
|
@@ -13,6 +6,11 @@ export type ExpressionScope = Readonly<Record<string, number>>;
|
|
|
13
6
|
* hot path of every sampled curve; a one-variable expression never reads `b`.
|
|
14
7
|
*/
|
|
15
8
|
export type CompiledExpression = (a: number, b: number, scope?: ExpressionScope) => number;
|
|
9
|
+
/**
|
|
10
|
+
* The language a plot's equation is written in: the engine's expression grammar
|
|
11
|
+
* under a dialect of its own (trig in radians, bare identifiers, logs and
|
|
12
|
+
* hyperbolics). Never `new Function`: an equation is stored text.
|
|
13
|
+
*/
|
|
16
14
|
export declare const MATH_DIALECT: ExpressionDialect;
|
|
17
15
|
/** What an expression may name, for a host's "what can I write here" help. */
|
|
18
16
|
export interface ExpressionVocabulary {
|
|
@@ -32,4 +30,6 @@ export declare function compileExpression(source: string, variables: readonly st
|
|
|
32
30
|
* is remembered rather than re-parsed every frame.
|
|
33
31
|
*/
|
|
34
32
|
export declare function compileExpressionCached(source: string, variables: readonly string[], params?: readonly string[]): CompiledExpression | Error;
|
|
33
|
+
/** A compiler that throws a failure instead of returning it, for a reader running inside `memoParse`. */
|
|
34
|
+
export declare function compilerFor(params: readonly string[]): (text: string, variables: readonly string[]) => CompiledExpression;
|
|
35
35
|
//# sourceMappingURL=expression.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"expression.d.ts","sourceRoot":"","sources":["../../src/kit/expression.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAkB,MAAM,mBAAmB,CAAC;AAG3E
|
|
1
|
+
{"version":3,"file":"expression.d.ts","sourceRoot":"","sources":["../../src/kit/expression.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAkB,MAAM,mBAAmB,CAAC;AAG3E,+EAA+E;AAC/E,MAAM,MAAM,eAAe,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAE/D;;;GAGG;AACH,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,eAAe,KAAK,MAAM,CAAC;AAwB3F;;;;GAIG;AACH,eAAO,MAAM,YAAY,EAAE,iBAQ1B,CAAC;AAEF,8EAA8E;AAC9E,MAAM,WAAW,oBAAoB;IACjC,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;IAC7B,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;IAC7B,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;CAChC;AAED,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,GAAG,oBAAoB,CAMvF;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC7B,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,SAAS,MAAM,EAAE,EAC5B,MAAM,GAAE,SAAS,MAAM,EAAO,GAC/B,kBAAkB,CAMpB;AAsDD;;;;GAIG;AACH,wBAAgB,uBAAuB,CACnC,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,SAAS,MAAM,EAAE,EAC5B,MAAM,GAAE,SAAS,MAAM,EAAO,GAC/B,kBAAkB,GAAG,KAAK,CAE5B;AAED,yGAAyG;AACzG,wBAAgB,WAAW,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,IACzC,MAAM,MAAM,EAAE,WAAW,SAAS,MAAM,EAAE,KAAG,kBAAkB,CAK1E"}
|
package/dist/kit/expression.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { expressionOps } from "@motionscript/sdk";
|
|
2
|
-
import { createBoundedCache } from "./cache.js";
|
|
2
|
+
import { createBoundedCache, remember } from "./cache.js";
|
|
3
3
|
const UNARY = {
|
|
4
4
|
sin: Math.sin, cos: Math.cos, tan: Math.tan,
|
|
5
5
|
asin: Math.asin, acos: Math.acos, atan: Math.atan,
|
|
@@ -19,6 +19,11 @@ const NARY = {
|
|
|
19
19
|
const CONSTANTS = {
|
|
20
20
|
pi: Math.PI, PI: Math.PI, e: Math.E, E: Math.E, tau: Math.PI * 2, phi: (1 + Math.sqrt(5)) / 2,
|
|
21
21
|
};
|
|
22
|
+
/**
|
|
23
|
+
* The language a plot's equation is written in: the engine's expression grammar
|
|
24
|
+
* under a dialect of its own (trig in radians, bare identifiers, logs and
|
|
25
|
+
* hyperbolics). Never `new Function`: an equation is stored text.
|
|
26
|
+
*/
|
|
22
27
|
export const MATH_DIALECT = {
|
|
23
28
|
functions: {
|
|
24
29
|
...Object.fromEntries(Object.keys(UNARY).map((name) => [name, { arity: [1, 1] }])),
|
|
@@ -32,7 +37,7 @@ export function expressionVocabulary(variables) {
|
|
|
32
37
|
return {
|
|
33
38
|
variables,
|
|
34
39
|
functions: [...Object.keys(UNARY), ...Object.keys(NARY)].sort(),
|
|
35
|
-
constants:
|
|
40
|
+
constants: Object.keys(CONSTANTS).filter((name) => name === name.toLowerCase()),
|
|
36
41
|
};
|
|
37
42
|
}
|
|
38
43
|
/**
|
|
@@ -40,8 +45,7 @@ export function expressionVocabulary(variables) {
|
|
|
40
45
|
* it may read from the scope. Throws on a syntax error or an unknown name.
|
|
41
46
|
*/
|
|
42
47
|
export function compileExpression(source, variables, params = []) {
|
|
43
|
-
// The parser folds a constant before it yields a reference, so a name that
|
|
44
|
-
// is both would silently stop being the variable it was passed as.
|
|
48
|
+
// The parser folds a constant before it yields a reference, so a name that is both stops being the variable.
|
|
45
49
|
for (const name of [...variables, ...params]) {
|
|
46
50
|
if (Object.hasOwn(CONSTANTS, name))
|
|
47
51
|
throw new Error(`'${name}' is a constant, so it cannot be a variable`);
|
|
@@ -107,18 +111,15 @@ const compiled = createBoundedCache(256);
|
|
|
107
111
|
* is remembered rather than re-parsed every frame.
|
|
108
112
|
*/
|
|
109
113
|
export function compileExpressionCached(source, variables, params = []) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
compiled.set(key, result);
|
|
122
|
-
return result;
|
|
114
|
+
return remember(compiled, `${variables.join(",")}|${params.join(",")}|${source}`, () => compileExpression(source, variables, params));
|
|
115
|
+
}
|
|
116
|
+
/** A compiler that throws a failure instead of returning it, for a reader running inside `memoParse`. */
|
|
117
|
+
export function compilerFor(params) {
|
|
118
|
+
return (text, variables) => {
|
|
119
|
+
const result = compileExpressionCached(text.trim(), variables, params);
|
|
120
|
+
if (result instanceof Error)
|
|
121
|
+
throw result;
|
|
122
|
+
return result;
|
|
123
|
+
};
|
|
123
124
|
}
|
|
124
125
|
//# sourceMappingURL=expression.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"expression.js","sourceRoot":"","sources":["../../src/kit/expression.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAElD,OAAO,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAmB7C,MAAM,KAAK,GAA0C;IACjD,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG;IAC3C,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI;IACjD,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI;IACjD,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG;IAC9D,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK;IAC/D,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK;CAC5F,CAAC;AAEF,MAAM,IAAI,GAAmF;IACzF,KAAK,EAAE,EAAE,EAAE,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C;AAC9C,CAAC;AAED,MAAM,QAAQ,GAAG,kBAAkB,CAA6B,GAAG,CAAC,CAAC;AAErE;;;;GAIG;AACH,MAAM,UAAU,uBAAuB,CACnC,MAAc,EACd,SAA4B,EAC5B,SAA4B,EAAE;IAE9B,MAAM,GAAG,GAAG,GAAG,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,MAAM,EAAE,CAAC;IACnE,MAAM,GAAG,GAAG,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC9B,IAAI,GAAG,KAAK,SAAS;QAAE,OAAO,GAAG,CAAC;IAClC,IAAI,MAAkC,CAAC;IACvC,IAAI,CAAC;QACD,MAAM,GAAG,iBAAiB,CAAC,MAAM,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;IAC1D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,MAAM,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IACvE,CAAC;IACD,QAAQ,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IAC1B,OAAO,MAAM,CAAC;AAClB,CAAC","sourcesContent":["import { expressionOps } from \"@motionscript/sdk\";\nimport type { ExpressionDialect, ExpressionNode } from \"@motionscript/sdk\";\nimport { createBoundedCache } from \"./cache\";\n\n/**\n * The language a plot's equation is written in: the engine's own expression\n * grammar (`expressionOps.parse`) under a dialect of its own, where trig is in\n * radians, names are bare identifiers, and the logs and hyperbolics a plot\n * wants are present. Never `new Function`: an equation is text stored in a\n * document, and the shared parser can only produce arithmetic.\n */\n\n/** Named values an expression reads at call time, beside its two variables. */\nexport type ExpressionScope = Readonly<Record<string, number>>;\n\n/**\n * A compiled expression. The two variables are fixed slots because this is the\n * hot path of every sampled curve; a one-variable expression never reads `b`.\n */\nexport type CompiledExpression = (a: number, b: number, scope?: ExpressionScope) => number;\n\nconst UNARY: Record<string, (v: number) => number> = {\n sin: Math.sin, cos: Math.cos, tan: Math.tan,\n asin: Math.asin, acos: Math.acos, atan: Math.atan,\n sinh: Math.sinh, cosh: Math.cosh, tanh: Math.tanh,\n sqrt: Math.sqrt, cbrt: Math.cbrt, abs: Math.abs, exp: Math.exp,\n log: Math.log, ln: Math.log, log2: Math.log2, log10: Math.log10,\n floor: Math.floor, ceil: Math.ceil, round: Math.round, sign: Math.sign, trunc: Math.trunc,\n};\n\nconst NARY: Record<string, { fn: (...args: number[]) => number; arity: [number, number] }> = {\n atan2: { fn: Math.atan2, arity: [2, 2] },\n pow: { fn: Math.pow, arity: [2, 2] },\n mod: { fn: (a, b) => a % b, arity: [2, 2] },\n hypot: { fn: Math.hypot, arity: [1, Infinity] },\n min: { fn: Math.min, arity: [1, Infinity] },\n max: { fn: Math.max, arity: [1, Infinity] },\n};\n\nconst CONSTANTS: Record<string, number> = {\n pi: Math.PI, PI: Math.PI, e: Math.E, E: Math.E, tau: Math.PI * 2, phi: (1 + Math.sqrt(5)) / 2,\n};\n\nexport const MATH_DIALECT: ExpressionDialect = {\n functions: {\n ...Object.fromEntries(Object.keys(UNARY).map((name) => [name, { arity: [1, 1] as [number, number] }])),\n ...Object.fromEntries(Object.entries(NARY).map(([name, { arity }]) => [name, { arity }])),\n },\n constants: CONSTANTS,\n refs: \"bare\",\n unknownName: (name) => `Unknown name '${name}'`,\n};\n\n/** What an expression may name, for a host's \"what can I write here\" help. */\nexport interface ExpressionVocabulary {\n variables: readonly string[];\n functions: readonly string[];\n constants: readonly string[];\n}\n\nexport function expressionVocabulary(variables: readonly string[]): ExpressionVocabulary {\n return {\n variables,\n functions: [...Object.keys(UNARY), ...Object.keys(NARY)].sort(),\n constants: [\"pi\", \"e\", \"tau\", \"phi\"],\n };\n}\n\n/**\n * Compile `source` over up to two `variables`, in slot order, and any `params`\n * it may read from the scope. Throws on a syntax error or an unknown name.\n */\nexport function compileExpression(\n source: string,\n variables: readonly string[],\n params: readonly string[] = [],\n): CompiledExpression {\n // The parser folds a constant before it yields a reference, so a name that\n // is both would silently stop being the variable it was passed as.\n for (const name of [...variables, ...params]) {\n if (Object.hasOwn(CONSTANTS, name)) throw new Error(`'${name}' is a constant, so it cannot be a variable`);\n }\n return emit(expressionOps.parse(source, MATH_DIALECT).root, variables, params);\n}\n\nfunction emit(node: ExpressionNode, variables: readonly string[], params: readonly string[]): CompiledExpression {\n switch (node.kind) {\n case \"const\": {\n const { value } = node;\n return () => value;\n }\n case \"ref\": {\n const { name } = node;\n const slot = variables.indexOf(name);\n if (slot === 0) return (a) => a;\n if (slot === 1) return (_a, b) => b;\n if (params.includes(name)) return (_a, _b, scope) => scope?.[name] ?? NaN;\n throw new Error(MATH_DIALECT.unknownName(name));\n }\n case \"unary\": {\n const operand = emit(node.operand, variables, params);\n return (a, b, s) => -operand(a, b, s);\n }\n case \"binary\": {\n const l = emit(node.left, variables, params);\n const r = emit(node.right, variables, params);\n switch (node.op) {\n case \"+\": return (a, b, s) => l(a, b, s) + r(a, b, s);\n case \"-\": return (a, b, s) => l(a, b, s) - r(a, b, s);\n case \"*\": return (a, b, s) => l(a, b, s) * r(a, b, s);\n case \"/\": return (a, b, s) => l(a, b, s) / r(a, b, s);\n case \"%\": return (a, b, s) => l(a, b, s) % r(a, b, s);\n case \"^\": return (a, b, s) => Math.pow(l(a, b, s), r(a, b, s));\n }\n break;\n }\n case \"call\": {\n const args = node.args.map((arg) => emit(arg, variables, params));\n const unary = UNARY[node.name];\n if (unary) {\n const [x] = args;\n return (a, b, s) => unary(x(a, b, s));\n }\n const { fn } = NARY[node.name];\n // The common arity, so the hot path allocates no array per sample.\n if (args.length === 2) {\n const [x, y] = args;\n return (a, b, s) => fn(x(a, b, s), y(a, b, s));\n }\n return (a, b, s) => fn(...args.map((arg) => arg(a, b, s)));\n }\n }\n throw new Error(\"Unsupported expression\");\n}\n\nconst compiled = createBoundedCache<CompiledExpression | Error>(256);\n\n/**\n * {@link compileExpression}, memoised: equal source under equal names is the\n * identical closure, so \"did this expression change\" is an `===`, and a failure\n * is remembered rather than re-parsed every frame.\n */\nexport function compileExpressionCached(\n source: string,\n variables: readonly string[],\n params: readonly string[] = [],\n): CompiledExpression | Error {\n const key = `${variables.join(\",\")}|${params.join(\",\")}|${source}`;\n const hit = compiled.get(key);\n if (hit !== undefined) return hit;\n let result: CompiledExpression | Error;\n try {\n result = compileExpression(source, variables, params);\n } catch (error) {\n result = error instanceof Error ? error : new Error(String(error));\n }\n compiled.set(key, result);\n return result;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"expression.js","sourceRoot":"","sources":["../../src/kit/expression.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAElD,OAAO,EAAE,kBAAkB,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAWvD,MAAM,KAAK,GAA0C;IACjD,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG;IAC3C,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI;IACjD,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI;IACjD,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG;IAC9D,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK;IAC/D,KAAK,EAAE,IA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C,CAAC,CAAC;YACnD,CAAC;YACD,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QAC/D,CAAC;IACL,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;AAC9C,CAAC;AAED,MAAM,QAAQ,GAAG,kBAAkB,CAA6B,GAAG,CAAC,CAAC;AAErE;;;;GAIG;AACH,MAAM,UAAU,uBAAuB,CACnC,MAAc,EACd,SAA4B,EAC5B,SAA4B,EAAE;IAE9B,OAAO,QAAQ,CAAC,QAAQ,EAAE,GAAG,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,MAAM,EAAE,EAAE,GAAG,EAAE,CAAC,iBAAiB,CAAC,MAAM,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC;AAC1I,CAAC;AAED,yGAAyG;AACzG,MAAM,UAAU,WAAW,CAAC,MAAyB;IACjD,OAAO,CAAC,IAAY,EAAE,SAA4B,EAAsB,EAAE;QACtE,MAAM,MAAM,GAAG,uBAAuB,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;QACvE,IAAI,MAAM,YAAY,KAAK;YAAE,MAAM,MAAM,CAAC;QAC1C,OAAO,MAAM,CAAC;IAClB,CAAC,CAAC;AACN,CAAC","sourcesContent":["import { expressionOps } from \"@motionscript/sdk\";\nimport type { ExpressionDialect, ExpressionNode } from \"@motionscript/sdk\";\nimport { createBoundedCache, remember } from \"./cache\";\n\n/** Named values an expression reads at call time, beside its two variables. */\nexport type ExpressionScope = Readonly<Record<string, number>>;\n\n/**\n * A compiled expression. The two variables are fixed slots because this is the\n * hot path of every sampled curve; a one-variable expression never reads `b`.\n */\nexport type CompiledExpression = (a: number, b: number, scope?: ExpressionScope) => number;\n\nconst UNARY: Record<string, (v: number) => number> = {\n sin: Math.sin, cos: Math.cos, tan: Math.tan,\n asin: Math.asin, acos: Math.acos, atan: Math.atan,\n sinh: Math.sinh, cosh: Math.cosh, tanh: Math.tanh,\n sqrt: Math.sqrt, cbrt: Math.cbrt, abs: Math.abs, exp: Math.exp,\n log: Math.log, ln: Math.log, log2: Math.log2, log10: Math.log10,\n floor: Math.floor, ceil: Math.ceil, round: Math.round, sign: Math.sign, trunc: Math.trunc,\n};\n\nconst NARY: Record<string, { fn: (...args: number[]) => number; arity: [number, number] }> = {\n atan2: { fn: Math.atan2, arity: [2, 2] },\n pow: { fn: Math.pow, arity: [2, 2] },\n mod: { fn: (a, b) => a % b, arity: [2, 2] },\n hypot: { fn: Math.hypot, arity: [1, Infinity] },\n min: { fn: Math.min, arity: [1, Infinity] },\n max: { fn: Math.max, arity: [1, Infinity] },\n};\n\nconst CONSTANTS: Record<string, number> = {\n pi: Math.PI, PI: Math.PI, e: Math.E, E: Math.E, tau: Math.PI * 2, phi: (1 + Math.sqrt(5)) / 2,\n};\n\n/**\n * The language a plot's equation is written in: the engine's expression grammar\n * under a dialect of its own (trig in radians, bare identifiers, logs and\n * hyperbolics). Never `new Function`: an equation is stored text.\n */\nexport const MATH_DIALECT: ExpressionDialect = {\n functions: {\n ...Object.fromEntries(Object.keys(UNARY).map((name) => [name, { arity: [1, 1] as [number, number] }])),\n ...Object.fromEntries(Object.entries(NARY).map(([name, { arity }]) => [name, { arity }])),\n },\n constants: CONSTANTS,\n refs: \"bare\",\n unknownName: (name) => `Unknown name '${name}'`,\n};\n\n/** What an expression may name, for a host's \"what can I write here\" help. */\nexport interface ExpressionVocabulary {\n variables: readonly string[];\n functions: readonly string[];\n constants: readonly string[];\n}\n\nexport function expressionVocabulary(variables: readonly string[]): ExpressionVocabulary {\n return {\n variables,\n functions: [...Object.keys(UNARY), ...Object.keys(NARY)].sort(),\n constants: Object.keys(CONSTANTS).filter((name) => name === name.toLowerCase()),\n };\n}\n\n/**\n * Compile `source` over up to two `variables`, in slot order, and any `params`\n * it may read from the scope. Throws on a syntax error or an unknown name.\n */\nexport function compileExpression(\n source: string,\n variables: readonly string[],\n params: readonly string[] = [],\n): CompiledExpression {\n // The parser folds a constant before it yields a reference, so a name that is both stops being the variable.\n for (const name of [...variables, ...params]) {\n if (Object.hasOwn(CONSTANTS, name)) throw new Error(`'${name}' is a constant, so it cannot be a variable`);\n }\n return emit(expressionOps.parse(source, MATH_DIALECT).root, variables, params);\n}\n\nfunction emit(node: ExpressionNode, variables: readonly string[], params: readonly string[]): CompiledExpression {\n switch (node.kind) {\n case \"const\": {\n const { value } = node;\n return () => value;\n }\n case \"ref\": {\n const { name } = node;\n const slot = variables.indexOf(name);\n if (slot === 0) return (a) => a;\n if (slot === 1) return (_a, b) => b;\n if (params.includes(name)) return (_a, _b, scope) => scope?.[name] ?? NaN;\n throw new Error(MATH_DIALECT.unknownName(name));\n }\n case \"unary\": {\n const operand = emit(node.operand, variables, params);\n return (a, b, s) => -operand(a, b, s);\n }\n case \"binary\": {\n const l = emit(node.left, variables, params);\n const r = emit(node.right, variables, params);\n switch (node.op) {\n case \"+\": return (a, b, s) => l(a, b, s) + r(a, b, s);\n case \"-\": return (a, b, s) => l(a, b, s) - r(a, b, s);\n case \"*\": return (a, b, s) => l(a, b, s) * r(a, b, s);\n case \"/\": return (a, b, s) => l(a, b, s) / r(a, b, s);\n case \"%\": return (a, b, s) => l(a, b, s) % r(a, b, s);\n case \"^\": return (a, b, s) => Math.pow(l(a, b, s), r(a, b, s));\n }\n break;\n }\n case \"call\": {\n const args = node.args.map((arg) => emit(arg, variables, params));\n const unary = UNARY[node.name];\n if (unary) {\n const [x] = args;\n return (a, b, s) => unary(x(a, b, s));\n }\n const { fn } = NARY[node.name];\n // The common arity, so the hot path allocates no array per sample.\n if (args.length === 2) {\n const [x, y] = args;\n return (a, b, s) => fn(x(a, b, s), y(a, b, s));\n }\n return (a, b, s) => fn(...args.map((arg) => arg(a, b, s)));\n }\n }\n throw new Error(\"Unsupported expression\");\n}\n\nconst compiled = createBoundedCache<CompiledExpression | Error>(256);\n\n/**\n * {@link compileExpression}, memoised: equal source under equal names is the\n * identical closure, so \"did this expression change\" is an `===`, and a failure\n * is remembered rather than re-parsed every frame.\n */\nexport function compileExpressionCached(\n source: string,\n variables: readonly string[],\n params: readonly string[] = [],\n): CompiledExpression | Error {\n return remember(compiled, `${variables.join(\",\")}|${params.join(\",\")}|${source}`, () => compileExpression(source, variables, params));\n}\n\n/** A compiler that throws a failure instead of returning it, for a reader running inside `memoParse`. */\nexport function compilerFor(params: readonly string[]) {\n return (text: string, variables: readonly string[]): CompiledExpression => {\n const result = compileExpressionCached(text.trim(), variables, params);\n if (result instanceof Error) throw result;\n return result;\n };\n}\n"]}
|
package/dist/kit/field-mark.d.ts
CHANGED
|
@@ -3,12 +3,6 @@ import type { CoordinateMap } from "./coords.js";
|
|
|
3
3
|
import type { ExpressionScope } from "./expression.js";
|
|
4
4
|
import { type MarkPaint, type MarkReading } from "./mark.js";
|
|
5
5
|
import { type PlotSource } from "./source.js";
|
|
6
|
-
/**
|
|
7
|
-
* What a mark drawn from a vector field, `(P(x, y), Q(x, y))`, shares: arrows,
|
|
8
|
-
* or the lines that follow it, all in one stroke. Without strength fills it is
|
|
9
|
-
* one path in the stroke's paint; with them, what it draws is sorted by the
|
|
10
|
-
* field's strength into a few shades.
|
|
11
|
-
*/
|
|
12
6
|
export interface FieldProps extends PaintProps {
|
|
13
7
|
expression?: string | PlotSource;
|
|
14
8
|
params?: ExpressionScope;
|
|
@@ -53,7 +47,6 @@ export declare const FIELD_COMMANDS: {
|
|
|
53
47
|
readonly run?: undefined;
|
|
54
48
|
};
|
|
55
49
|
};
|
|
56
|
-
/** What a field mark is drawn from. */
|
|
57
50
|
export interface FieldValues {
|
|
58
51
|
readonly expression: PlotSource;
|
|
59
52
|
readonly params: ExpressionScope;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"field-mark.d.ts","sourceRoot":"","sources":["../../src/kit/field-mark.ts"],"names":[],"mappings":"AAAA,OAAO,EAGH,KAAK,IAAI,EACT,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,OAAO,EAEZ,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,cAAc,IAAI,eAAe,EACtC,KAAK,MAAM,IAAI,OAAO,EACzB,MAAM,mBAAmB,CAAC;AAG3B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAC9C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAIpD,OAAO,EAAY,KAAK,SAAS,EAAE,KAAK,WAAW,EAAE,MAAM,QAAQ,CAAC;AAEpE,OAAO,EAAgD,KAAK,UAAU,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"field-mark.d.ts","sourceRoot":"","sources":["../../src/kit/field-mark.ts"],"names":[],"mappings":"AAAA,OAAO,EAGH,KAAK,IAAI,EACT,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,OAAO,EAEZ,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,cAAc,IAAI,eAAe,EACtC,KAAK,MAAM,IAAI,OAAO,EACzB,MAAM,mBAAmB,CAAC;AAG3B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAC9C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAIpD,OAAO,EAAY,KAAK,SAAS,EAAE,KAAK,WAAW,EAAE,MAAM,QAAQ,CAAC;AAEpE,OAAO,EAAgD,KAAK,UAAU,EAAE,MAAM,UAAU,CAAC;AAIzF,MAAM,WAAW,UAAW,SAAQ,UAAU;IAC1C,UAAU,CAAC,EAAE,MAAM,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