mathjs
Version:
Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser and offers an integrated solution to work with numbers, big numbers, complex numbers, units, and matrices.
89 lines (77 loc) • 2.8 kB
JavaScript
var Node = require('./Node'),
isString = require('../../util/string').isString;
isArray = Array.isArray;
/**
* @constructor FunctionNode
* @extends {Node}
* Function assignment
*
* @param {String} name Function name
* @param {String[]} args Function argument names
* @param {Node} expr The function expression
*/
function FunctionNode(name, args, expr) {
if (!(this instanceof FunctionNode)) {
throw new SyntaxError('Constructor must be called with the new operator');
}
// validate input
if (!isString(name)) throw new TypeError('String expected for parameter "name"');
if (!isArray(args) || !args.every(isString)) throw new TypeError('Array containing strings expected for parameter "args"');
if (!(expr instanceof Node)) throw new TypeError('Node expected for parameter "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) {
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 util.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 options
* @returns {Node[]} nodes
*/
FunctionNode.prototype.find = function (filter) {
var nodes = [];
// check itself
if (this.match(filter)) {
nodes.push(this);
}
// search in expression
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;