mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
88 lines
2.3 KiB
JavaScript
88 lines
2.3 KiB
JavaScript
'use strict';
|
|
|
|
var Node = require('./Node'),
|
|
ArrayNode = require('./ArrayNode'),
|
|
|
|
keywords = require('../keywords'),
|
|
|
|
latex = require('../../util/latex'),
|
|
isString = require('../../util/string').isString;
|
|
|
|
/**
|
|
* @constructor AssignmentNode
|
|
* @extends {Node}
|
|
* Define a symbol, like "a = 3.2"
|
|
*
|
|
* @param {String} name Symbol name
|
|
* @param {Node} expr The expression defining the symbol
|
|
*/
|
|
function AssignmentNode(name, expr) {
|
|
if (!(this instanceof AssignmentNode)) {
|
|
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 (!(expr instanceof Node)) throw new TypeError('Node expected for parameter "expr"');
|
|
if (name in keywords) throw new Error('Illegal symbol name, "' + name + '" is a reserved keyword');
|
|
|
|
this.name = name;
|
|
this.expr = expr;
|
|
}
|
|
|
|
AssignmentNode.prototype = new Node();
|
|
|
|
AssignmentNode.prototype.type = 'AssignmentNode';
|
|
|
|
/**
|
|
* 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
|
|
* @private
|
|
*/
|
|
AssignmentNode.prototype._compile = function (defs) {
|
|
return 'scope["' + this.name + '"] = ' + this.expr._compile(defs) + '';
|
|
};
|
|
|
|
|
|
/**
|
|
* Recursively execute a callback for each of the child nodes of this node
|
|
* @param {function(Node, string, Node)} callback
|
|
* @private
|
|
*/
|
|
AssignmentNode.prototype._traverse = function (callback) {
|
|
callback(this.expr, 'expr', this);
|
|
this.expr._traverse(callback);
|
|
};
|
|
|
|
/**
|
|
* Create a clone of this node
|
|
* @return {AssignmentNode}
|
|
*/
|
|
AssignmentNode.prototype.clone = function() {
|
|
return new AssignmentNode(this.name, this.expr.clone());
|
|
};
|
|
|
|
/**
|
|
* Get string representation
|
|
* @return {String}
|
|
*/
|
|
AssignmentNode.prototype.toString = function() {
|
|
return this.name + ' = ' + this.expr.toString();
|
|
};
|
|
|
|
/**
|
|
* Get LaTeX representation
|
|
* @return {String}
|
|
*/
|
|
AssignmentNode.prototype.toTex = function() {
|
|
var brace;
|
|
if (this.expr instanceof ArrayNode) {
|
|
brace = ['\\mathbf{', '}'];
|
|
}
|
|
return latex.addBraces(latex.toSymbol(this.name), brace) + '=' +
|
|
latex.addBraces(this.expr.toTex());
|
|
};
|
|
|
|
module.exports = AssignmentNode; |