parser.js 1.7 KB

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