mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
83 lines
2.3 KiB
JavaScript
83 lines
2.3 KiB
JavaScript
var Node = require('./Node');
|
|
|
|
/**
|
|
* @constructor FunctionNode
|
|
* @extends {Node}
|
|
* Function assignment
|
|
*
|
|
* @param {String} name Function name
|
|
* @param {String[]} args Function arguments
|
|
* @param {Node} expr The function expression
|
|
*/
|
|
function FunctionNode(name, args, expr) {
|
|
this.name = name;
|
|
this.args = args;
|
|
this.expr = expr;
|
|
}
|
|
|
|
FunctionNode.prototype = new Node();
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
FunctionNode.prototype._compile = function (defs) {
|
|
|
|
// TODO: validate whether name and all arguments are strings
|
|
|
|
return 'scope["' + this.name + '"] = ' +
|
|
' (function (scope) {' +
|
|
' scope = Object.create(scope); ' +
|
|
' var fn = function ' + this.name + '(' + this.args.join(',') + ') {' +
|
|
' if (arguments.length != ' + this.args.length + ') {' +
|
|
// TODO: use math.error.ArgumentsError here
|
|
// TODO: test arguments error
|
|
' throw new SyntaxError("Wrong number of arguments in function ' + this.name + ' (" + arguments.length + " provided, ' + this.args.length + ' expected)");' +
|
|
' }' +
|
|
this.args.map(function (variable, index) {
|
|
return 'scope["' + variable + '"] = arguments[' + index + '];';
|
|
}).join('') +
|
|
' return ' + this.expr._compile(defs) + '' +
|
|
' };' +
|
|
' fn.syntax = "' + this.name + '(' + this.args.join(', ') + ')";' +
|
|
' return fn;' +
|
|
' })(scope);';
|
|
};
|
|
|
|
/**
|
|
* Find all nodes matching given filter
|
|
* @param {Object} filter See Node.find for a description of the filter settings
|
|
* @returns {Node[]} nodes
|
|
*/
|
|
FunctionNode.prototype.find = function (filter) {
|
|
var nodes = [];
|
|
|
|
// check itself
|
|
if (this.match(filter)) {
|
|
nodes.push(this);
|
|
}
|
|
|
|
// search in expression
|
|
if (this.expr) {
|
|
nodes = nodes.concat(this.expr.find(filter));
|
|
}
|
|
|
|
return nodes;
|
|
};
|
|
|
|
/**
|
|
* get string representation
|
|
* @return {String} str
|
|
*/
|
|
FunctionNode.prototype.toString = function() {
|
|
return 'function ' + this.name +
|
|
'(' + this.args.join(', ') + ') = ' +
|
|
this.expr.toString();
|
|
};
|
|
|
|
module.exports = FunctionNode;
|