UNPKG

11.5 kBJavaScriptView Raw
1import { isAccessorNode, isIndexNode, isNode, isSymbolNode } from '../../utils/is.js';
2import { getSafeProperty, setSafeProperty } from '../../utils/customs.js';
3import { factory } from '../../utils/factory.js';
4import { accessFactory } from './utils/access.js';
5import { assignFactory } from './utils/assign.js';
6import { getPrecedence } from '../operators.js';
7var name = 'AssignmentNode';
8var dependencies = ['subset', '?matrix', // FIXME: should not be needed at all, should be handled by subset
9'Node'];
10export var createAssignmentNode = /* #__PURE__ */factory(name, dependencies, (_ref) => {
11 var {
12 subset,
13 matrix,
14 Node
15 } = _ref;
16 var access = accessFactory({
17 subset
18 });
19 var assign = assignFactory({
20 subset,
21 matrix
22 });
23 /**
24 * @constructor AssignmentNode
25 * @extends {Node}
26 *
27 * Define a symbol, like `a=3.2`, update a property like `a.b=3.2`, or
28 * replace a subset of a matrix like `A[2,2]=42`.
29 *
30 * Syntax:
31 *
32 * new AssignmentNode(symbol, value)
33 * new AssignmentNode(object, index, value)
34 *
35 * Usage:
36 *
37 * new AssignmentNode(new SymbolNode('a'), new ConstantNode(2)) // a=2
38 * new AssignmentNode(new SymbolNode('a'), new IndexNode('b'), new ConstantNode(2)) // a.b=2
39 * new AssignmentNode(new SymbolNode('a'), new IndexNode(1, 2), new ConstantNode(3)) // a[1,2]=3
40 *
41 * @param {SymbolNode | AccessorNode} object Object on which to assign a value
42 * @param {IndexNode} [index=null] Index, property name or matrix
43 * index. Optional. If not provided
44 * and `object` is a SymbolNode,
45 * the property is assigned to the
46 * global scope.
47 * @param {Node} value The value to be assigned
48 */
49
50 function AssignmentNode(object, index, value) {
51 if (!(this instanceof AssignmentNode)) {
52 throw new SyntaxError('Constructor must be called with the new operator');
53 }
54
55 this.object = object;
56 this.index = value ? index : null;
57 this.value = value || index; // validate input
58
59 if (!isSymbolNode(object) && !isAccessorNode(object)) {
60 throw new TypeError('SymbolNode or AccessorNode expected as "object"');
61 }
62
63 if (isSymbolNode(object) && object.name === 'end') {
64 throw new Error('Cannot assign to symbol "end"');
65 }
66
67 if (this.index && !isIndexNode(this.index)) {
68 // index is optional
69 throw new TypeError('IndexNode expected as "index"');
70 }
71
72 if (!isNode(this.value)) {
73 throw new TypeError('Node expected as "value"');
74 } // readonly property name
75
76
77 Object.defineProperty(this, 'name', {
78 get: function () {
79 if (this.index) {
80 return this.index.isObjectProperty() ? this.index.getObjectProperty() : '';
81 } else {
82 return this.object.name || '';
83 }
84 }.bind(this),
85 set: function set() {
86 throw new Error('Cannot assign a new name, name is read-only');
87 }
88 });
89 }
90
91 AssignmentNode.prototype = new Node();
92 AssignmentNode.prototype.type = 'AssignmentNode';
93 AssignmentNode.prototype.isAssignmentNode = true;
94 /**
95 * Compile a node into a JavaScript function.
96 * This basically pre-calculates as much as possible and only leaves open
97 * calculations which depend on a dynamic scope with variables.
98 * @param {Object} math Math.js namespace with functions and constants.
99 * @param {Object} argNames An object with argument names as key and `true`
100 * as value. Used in the SymbolNode to optimize
101 * for arguments from user assigned functions
102 * (see FunctionAssignmentNode) or special symbols
103 * like `end` (see IndexNode).
104 * @return {function} Returns a function which can be called like:
105 * evalNode(scope: Object, args: Object, context: *)
106 */
107
108 AssignmentNode.prototype._compile = function (math, argNames) {
109 var evalObject = this.object._compile(math, argNames);
110
111 var evalIndex = this.index ? this.index._compile(math, argNames) : null;
112
113 var evalValue = this.value._compile(math, argNames);
114
115 var name = this.object.name;
116
117 if (!this.index) {
118 // apply a variable to the scope, for example `a=2`
119 if (!isSymbolNode(this.object)) {
120 throw new TypeError('SymbolNode expected as object');
121 }
122
123 return function evalAssignmentNode(scope, args, context) {
124 return setSafeProperty(scope, name, evalValue(scope, args, context));
125 };
126 } else if (this.index.isObjectProperty()) {
127 // apply an object property for example `a.b=2`
128 var prop = this.index.getObjectProperty();
129 return function evalAssignmentNode(scope, args, context) {
130 var object = evalObject(scope, args, context);
131 var value = evalValue(scope, args, context);
132 return setSafeProperty(object, prop, value);
133 };
134 } else if (isSymbolNode(this.object)) {
135 // update a matrix subset, for example `a[2]=3`
136 return function evalAssignmentNode(scope, args, context) {
137 var childObject = evalObject(scope, args, context);
138 var value = evalValue(scope, args, context);
139 var index = evalIndex(scope, args, childObject); // Important: we pass childObject instead of context
140
141 setSafeProperty(scope, name, assign(childObject, index, value));
142 return value;
143 };
144 } else {
145 // isAccessorNode(node.object) === true
146 // update a matrix subset, for example `a.b[2]=3`
147 // we will not use the compile function of the AccessorNode, but compile it
148 // ourselves here as we need the parent object of the AccessorNode:
149 // wee need to apply the updated object to parent object
150 var evalParentObject = this.object.object._compile(math, argNames);
151
152 if (this.object.index.isObjectProperty()) {
153 var parentProp = this.object.index.getObjectProperty();
154 return function evalAssignmentNode(scope, args, context) {
155 var parent = evalParentObject(scope, args, context);
156 var childObject = getSafeProperty(parent, parentProp);
157 var index = evalIndex(scope, args, childObject); // Important: we pass childObject instead of context
158
159 var value = evalValue(scope, args, context);
160 setSafeProperty(parent, parentProp, assign(childObject, index, value));
161 return value;
162 };
163 } else {
164 // if some parameters use the 'end' parameter, we need to calculate the size
165 var evalParentIndex = this.object.index._compile(math, argNames);
166
167 return function evalAssignmentNode(scope, args, context) {
168 var parent = evalParentObject(scope, args, context);
169 var parentIndex = evalParentIndex(scope, args, parent); // Important: we pass parent instead of context
170
171 var childObject = access(parent, parentIndex);
172 var index = evalIndex(scope, args, childObject); // Important: we pass childObject instead of context
173
174 var value = evalValue(scope, args, context);
175 assign(parent, parentIndex, assign(childObject, index, value));
176 return value;
177 };
178 }
179 }
180 };
181 /**
182 * Execute a callback for each of the child nodes of this node
183 * @param {function(child: Node, path: string, parent: Node)} callback
184 */
185
186
187 AssignmentNode.prototype.forEach = function (callback) {
188 callback(this.object, 'object', this);
189
190 if (this.index) {
191 callback(this.index, 'index', this);
192 }
193
194 callback(this.value, 'value', this);
195 };
196 /**
197 * Create a new AssignmentNode having it's childs be the results of calling
198 * the provided callback function for each of the childs of the original node.
199 * @param {function(child: Node, path: string, parent: Node): Node} callback
200 * @returns {AssignmentNode} Returns a transformed copy of the node
201 */
202
203
204 AssignmentNode.prototype.map = function (callback) {
205 var object = this._ifNode(callback(this.object, 'object', this));
206
207 var index = this.index ? this._ifNode(callback(this.index, 'index', this)) : null;
208
209 var value = this._ifNode(callback(this.value, 'value', this));
210
211 return new AssignmentNode(object, index, value);
212 };
213 /**
214 * Create a clone of this node, a shallow copy
215 * @return {AssignmentNode}
216 */
217
218
219 AssignmentNode.prototype.clone = function () {
220 return new AssignmentNode(this.object, this.index, this.value);
221 };
222 /*
223 * Is parenthesis needed?
224 * @param {node} node
225 * @param {string} [parenthesis='keep']
226 * @private
227 */
228
229
230 function needParenthesis(node, parenthesis) {
231 if (!parenthesis) {
232 parenthesis = 'keep';
233 }
234
235 var precedence = getPrecedence(node, parenthesis);
236 var exprPrecedence = getPrecedence(node.value, parenthesis);
237 return parenthesis === 'all' || exprPrecedence !== null && exprPrecedence <= precedence;
238 }
239 /**
240 * Get string representation
241 * @param {Object} options
242 * @return {string}
243 */
244
245
246 AssignmentNode.prototype._toString = function (options) {
247 var object = this.object.toString(options);
248 var index = this.index ? this.index.toString(options) : '';
249 var value = this.value.toString(options);
250
251 if (needParenthesis(this, options && options.parenthesis)) {
252 value = '(' + value + ')';
253 }
254
255 return object + index + ' = ' + value;
256 };
257 /**
258 * Get a JSON representation of the node
259 * @returns {Object}
260 */
261
262
263 AssignmentNode.prototype.toJSON = function () {
264 return {
265 mathjs: 'AssignmentNode',
266 object: this.object,
267 index: this.index,
268 value: this.value
269 };
270 };
271 /**
272 * Instantiate an AssignmentNode from its JSON representation
273 * @param {Object} json An object structured like
274 * `{"mathjs": "AssignmentNode", object: ..., index: ..., value: ...}`,
275 * where mathjs is optional
276 * @returns {AssignmentNode}
277 */
278
279
280 AssignmentNode.fromJSON = function (json) {
281 return new AssignmentNode(json.object, json.index, json.value);
282 };
283 /**
284 * Get HTML representation
285 * @param {Object} options
286 * @return {string}
287 */
288
289
290 AssignmentNode.prototype.toHTML = function (options) {
291 var object = this.object.toHTML(options);
292 var index = this.index ? this.index.toHTML(options) : '';
293 var value = this.value.toHTML(options);
294
295 if (needParenthesis(this, options && options.parenthesis)) {
296 value = '<span class="math-paranthesis math-round-parenthesis">(</span>' + value + '<span class="math-paranthesis math-round-parenthesis">)</span>';
297 }
298
299 return object + index + '<span class="math-operator math-assignment-operator math-variable-assignment-operator math-binary-operator">=</span>' + value;
300 };
301 /**
302 * Get LaTeX representation
303 * @param {Object} options
304 * @return {string}
305 */
306
307
308 AssignmentNode.prototype._toTex = function (options) {
309 var object = this.object.toTex(options);
310 var index = this.index ? this.index.toTex(options) : '';
311 var value = this.value.toTex(options);
312
313 if (needParenthesis(this, options && options.parenthesis)) {
314 value = "\\left(".concat(value, "\\right)");
315 }
316
317 return object + index + ':=' + value;
318 };
319
320 return AssignmentNode;
321}, {
322 isClass: true,
323 isNode: true
324});
\No newline at end of file