UNPKG

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