UNPKG

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