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