1 | import { deepMap } from '../../utils/collection';
|
2 | import { factory } from '../../utils/factory';
|
3 | var name = 'compile';
|
4 | var dependencies = ['typed', 'parse'];
|
5 | export var createCompile = /* #__PURE__ */factory(name, dependencies, function (_ref) {
|
6 | var typed = _ref.typed,
|
7 | parse = _ref.parse;
|
8 |
|
9 | /**
|
10 | * Parse and compile an expression.
|
11 | * Returns a an object with a function `evaluate([scope])` to evaluate the
|
12 | * compiled expression.
|
13 | *
|
14 | * Syntax:
|
15 | *
|
16 | * math.compile(expr) // returns one node
|
17 | * math.compile([expr1, expr2, expr3, ...]) // returns an array with nodes
|
18 | *
|
19 | * Examples:
|
20 | *
|
21 | * const code1 = math.compile('sqrt(3^2 + 4^2)')
|
22 | * code1.evaluate() // 5
|
23 | *
|
24 | * let scope = {a: 3, b: 4}
|
25 | * const code2 = math.compile('a * b') // 12
|
26 | * code2.evaluate(scope) // 12
|
27 | * scope.a = 5
|
28 | * code2.evaluate(scope) // 20
|
29 | *
|
30 | * const nodes = math.compile(['a = 3', 'b = 4', 'a * b'])
|
31 | * nodes[2].evaluate() // 12
|
32 | *
|
33 | * See also:
|
34 | *
|
35 | * parse, evaluate
|
36 | *
|
37 | * @param {string | string[] | Array | Matrix} expr
|
38 | * The expression to be compiled
|
39 | * @return {{evaluate: Function} | Array.<{evaluate: Function}>} code
|
40 | * An object with the compiled expression
|
41 | * @throws {Error}
|
42 | */
|
43 | return typed(name, {
|
44 | string: function string(expr) {
|
45 | return parse(expr).compile();
|
46 | },
|
47 | 'Array | Matrix': function ArrayMatrix(expr) {
|
48 | return deepMap(expr, function (entry) {
|
49 | return parse(entry).compile();
|
50 | });
|
51 | }
|
52 | });
|
53 | }); |
\ | No newline at end of file |