Parser.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { factory } from '../utils/factory.js';
  2. import { createEmptyMap, toObject } from '../utils/map.js';
  3. var name = 'Parser';
  4. var dependencies = ['evaluate'];
  5. export var createParserClass = /* #__PURE__ */factory(name, dependencies, _ref => {
  6. var {
  7. evaluate
  8. } = _ref;
  9. /**
  10. * @constructor Parser
  11. * Parser contains methods to evaluate or parse expressions, and has a number
  12. * of convenience methods to get, set, and remove variables from memory. Parser
  13. * keeps a scope containing variables in memory, which is used for all
  14. * evaluations.
  15. *
  16. * Methods:
  17. * const result = parser.evaluate(expr) // evaluate an expression
  18. * const value = parser.get(name) // retrieve a variable from the parser
  19. * const values = parser.getAll() // retrieve all defined variables
  20. * parser.set(name, value) // set a variable in the parser
  21. * parser.remove(name) // clear a variable from the
  22. * // parsers scope
  23. * parser.clear() // clear the parsers scope
  24. *
  25. * Example usage:
  26. * const parser = new Parser()
  27. * // Note: there is a convenience method which can be used instead:
  28. * // const parser = new math.parser()
  29. *
  30. * // evaluate expressions
  31. * parser.evaluate('sqrt(3^2 + 4^2)') // 5
  32. * parser.evaluate('sqrt(-4)') // 2i
  33. * parser.evaluate('2 inch in cm') // 5.08 cm
  34. * parser.evaluate('cos(45 deg)') // 0.7071067811865476
  35. *
  36. * // define variables and functions
  37. * parser.evaluate('x = 7 / 2') // 3.5
  38. * parser.evaluate('x + 3') // 6.5
  39. * parser.evaluate('f(x, y) = x^y') // f(x, y)
  40. * parser.evaluate('f(2, 3)') // 8
  41. *
  42. * // get and set variables and functions
  43. * const x = parser.get('x') // 7
  44. * const f = parser.get('f') // function
  45. * const g = f(3, 2) // 9
  46. * parser.set('h', 500)
  47. * const i = parser.evaluate('h / 2') // 250
  48. * parser.set('hello', function (name) {
  49. * return 'hello, ' + name + '!'
  50. * })
  51. * parser.evaluate('hello("user")') // "hello, user!"
  52. *
  53. * // clear defined functions and variables
  54. * parser.clear()
  55. *
  56. */
  57. function Parser() {
  58. if (!(this instanceof Parser)) {
  59. throw new SyntaxError('Constructor must be called with the new operator');
  60. }
  61. Object.defineProperty(this, 'scope', {
  62. value: createEmptyMap(),
  63. writable: false
  64. });
  65. }
  66. /**
  67. * Attach type information
  68. */
  69. Parser.prototype.type = 'Parser';
  70. Parser.prototype.isParser = true;
  71. /**
  72. * Parse and evaluate the given expression
  73. * @param {string | string[]} expr A string containing an expression,
  74. * for example "2+3", or a list with expressions
  75. * @return {*} result The result, or undefined when the expression was empty
  76. * @throws {Error}
  77. */
  78. Parser.prototype.evaluate = function (expr) {
  79. // TODO: validate arguments
  80. return evaluate(expr, this.scope);
  81. };
  82. /**
  83. * Get a variable (a function or variable) by name from the parsers scope.
  84. * Returns undefined when not found
  85. * @param {string} name
  86. * @return {* | undefined} value
  87. */
  88. Parser.prototype.get = function (name) {
  89. // TODO: validate arguments
  90. if (this.scope.has(name)) {
  91. return this.scope.get(name);
  92. }
  93. };
  94. /**
  95. * Get a map with all defined variables
  96. * @return {Object} values
  97. */
  98. Parser.prototype.getAll = function () {
  99. return toObject(this.scope);
  100. };
  101. /**
  102. * Get a map with all defined variables
  103. * @return {Map} values
  104. */
  105. Parser.prototype.getAllAsMap = function () {
  106. return this.scope;
  107. };
  108. /**
  109. * Set a symbol (a function or variable) by name from the parsers scope.
  110. * @param {string} name
  111. * @param {* | undefined} value
  112. */
  113. Parser.prototype.set = function (name, value) {
  114. this.scope.set(name, value);
  115. return value;
  116. };
  117. /**
  118. * Remove a variable from the parsers scope
  119. * @param {string} name
  120. */
  121. Parser.prototype.remove = function (name) {
  122. this.scope.delete(name);
  123. };
  124. /**
  125. * Clear the scope with variables and functions
  126. */
  127. Parser.prototype.clear = function () {
  128. this.scope.clear();
  129. };
  130. return Parser;
  131. }, {
  132. isClass: true
  133. });