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