UNPKG

1.57 kBJavaScriptView Raw
1'use strict'
2
3function factory (type, config, load, typed) {
4 const parse = load(require('../parse'))
5
6 /**
7 * Parse an expression. Returns a node tree, which can be evaluated by
8 * invoking node.eval().
9 *
10 * Note the evaluating arbitrary expressions may involve security risks,
11 * see [http://mathjs.org/docs/expressions/security.html](http://mathjs.org/docs/expressions/security.html) for more information.
12 *
13 * Syntax:
14 *
15 * math.parse(expr)
16 * math.parse(expr, options)
17 * math.parse([expr1, expr2, expr3, ...])
18 * math.parse([expr1, expr2, expr3, ...], options)
19 *
20 * Example:
21 *
22 * const node1 = math.parse('sqrt(3^2 + 4^2)')
23 * node1.compile().eval() // 5
24 *
25 * let scope = {a:3, b:4}
26 * const node2 = math.parse('a * b') // 12
27 * const code2 = node2.compile()
28 * code2.eval(scope) // 12
29 * scope.a = 5
30 * code2.eval(scope) // 20
31 *
32 * const nodes = math.parse(['a = 3', 'b = 4', 'a * b'])
33 * nodes[2].compile().eval() // 12
34 *
35 * See also:
36 *
37 * eval, compile
38 *
39 * @param {string | string[] | Matrix} expr Expression to be parsed
40 * @param {{nodes: Object<string, Node>}} [options] Available options:
41 * - `nodes` a set of custom nodes
42 * @return {Node | Node[]} node
43 * @throws {Error}
44 */
45 return typed('parse', {
46 'string | Array | Matrix': parse,
47 'string | Array | Matrix, Object': parse
48 })
49}
50
51exports.name = 'parse'
52exports.factory = factory