mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
111 lines
2.7 KiB
JavaScript
111 lines
2.7 KiB
JavaScript
var Node = require('./Node');
|
|
|
|
/**
|
|
* @constructor OperatorNode
|
|
* @extends {Node}
|
|
* An operator with two arguments, like 2+3
|
|
*
|
|
* @param {String} name Operator name, for example '+'
|
|
* @param {String} fnName Function name, for example 'add'
|
|
* @param {function} fn Function, for example math.add
|
|
* @param {Node[]} params Parameters
|
|
*/
|
|
function OperatorNode (name, fnName, fn, params) {
|
|
this.name = name;
|
|
this.fnName = fnName;
|
|
this.fn = fn; // TODO: remove link to function instance
|
|
this.params = params;
|
|
}
|
|
|
|
OperatorNode.prototype = new Node();
|
|
|
|
/**
|
|
* Evaluate the parameters
|
|
* @return {*} result
|
|
*/
|
|
// TODO: cleanup
|
|
OperatorNode.prototype._eval = function() {
|
|
return this.fn.apply(this, this.params.map(function (param) {
|
|
return param.eval();
|
|
}));
|
|
};
|
|
|
|
/**
|
|
* Compile the node to javascript code
|
|
* @param {Object} defs Object which can be used to define functions
|
|
* or constants globally available for the compiled
|
|
* expression
|
|
* @return {String} js
|
|
* @private
|
|
*/
|
|
OperatorNode.prototype._compile = function (defs) {
|
|
if (!(this.fnName in defs.math)) {
|
|
throw new Error('Function ' + this.fnName + ' missing in provided namespace "math"');
|
|
}
|
|
|
|
var params = this.params.map(function (param) {
|
|
return param._compile(defs);
|
|
});
|
|
return 'math.' + this.fnName + '(' + params.join(', ') + ')';
|
|
};
|
|
|
|
/**
|
|
* Find all nodes matching given filter
|
|
* @param {Object} filter See Node.find for a description of the filter settings
|
|
* @returns {Node[]} nodes
|
|
*/
|
|
OperatorNode.prototype.find = function (filter) {
|
|
var nodes = [];
|
|
|
|
// check itself
|
|
if (this.match(filter)) {
|
|
nodes.push(this);
|
|
}
|
|
|
|
// search in parameters
|
|
var params = this.params;
|
|
if (params) {
|
|
for (var i = 0, len = params.length; i < len; i++) {
|
|
nodes = nodes.concat(params[i].find(filter));
|
|
}
|
|
}
|
|
|
|
return nodes;
|
|
};
|
|
|
|
/**
|
|
* Get string representation
|
|
* @return {String} str
|
|
*/
|
|
OperatorNode.prototype.toString = function() {
|
|
var params = this.params;
|
|
|
|
switch (params.length) {
|
|
case 1:
|
|
if (this.name == '-') {
|
|
// special case: unary minus
|
|
return '-' + params[0].toString();
|
|
}
|
|
else {
|
|
// for example '5!'
|
|
return params[0].toString() + this.name;
|
|
}
|
|
|
|
case 2: // for example '2+3'
|
|
var lhs = params[0].toString();
|
|
if (params[0] instanceof OperatorNode) {
|
|
lhs = '(' + lhs + ')';
|
|
}
|
|
var rhs = params[1].toString();
|
|
if (params[1] instanceof OperatorNode) {
|
|
rhs = '(' + rhs + ')';
|
|
}
|
|
return lhs + ' ' + this.name + ' ' + rhs;
|
|
|
|
default: // this should occur. format as a function call
|
|
return this.name + '(' + this.params.join(', ') + ')';
|
|
}
|
|
};
|
|
|
|
module.exports = OperatorNode;
|