Parser.js 4.5 KB

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