UNPKG

1.73 kBJavaScriptView Raw
1import { factory } from '../../utils/factory'
2
3const name = 'parser'
4const dependencies = ['typed', 'Parser']
5
6export const createParser = /* #__PURE__ */ factory(name, dependencies, ({ typed, Parser }) => {
7 /**
8 * Create a parser. The function creates a new `math.Parser` object.
9 *
10 * Syntax:
11 *
12 * math.parser()
13 *
14 * Examples:
15 *
16 * const parser = new math.parser()
17 *
18 * // evaluate expressions
19 * const a = parser.evaluate('sqrt(3^2 + 4^2)') // 5
20 * const b = parser.evaluate('sqrt(-4)') // 2i
21 * const c = parser.evaluate('2 inch in cm') // 5.08 cm
22 * const d = parser.evaluate('cos(45 deg)') // 0.7071067811865476
23 *
24 * // define variables and functions
25 * parser.evaluate('x = 7 / 2') // 3.5
26 * parser.evaluate('x + 3') // 6.5
27 * parser.evaluate('function f(x, y) = x^y') // f(x, y)
28 * parser.evaluate('f(2, 3)') // 8
29 *
30 * // get and set variables and functions
31 * const x = parser.get('x') // 7
32 * const f = parser.get('f') // function
33 * const g = f(3, 2) // 9
34 * parser.set('h', 500)
35 * const i = parser.evaluate('h / 2') // 250
36 * parser.set('hello', function (name) {
37 * return 'hello, ' + name + '!'
38 * })
39 * parser.evaluate('hello("user")') // "hello, user!"
40 *
41 * // clear defined functions and variables
42 * parser.clear()
43 *
44 * See also:
45 *
46 * evaluate, compile, parse
47 *
48 * @return {Parser} Parser
49 */
50 return typed(name, {
51 '': function () {
52 return new Parser()
53 }
54 })
55})