mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
57 lines
1.4 KiB
JavaScript
57 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
var deepMap = require('../../utils/collection/deepMap');
|
|
|
|
function factory (type, config, load, typed) {
|
|
var parse = load(require('../parse'));
|
|
|
|
/**
|
|
* Parse and compile an expression.
|
|
* Returns a an object with a function `eval([scope])` to evaluate the
|
|
* compiled expression.
|
|
*
|
|
* Syntax:
|
|
*
|
|
* math.compile(expr) // returns one node
|
|
* math.compile([expr1, expr2, expr3, ...]) // returns an array with nodes
|
|
*
|
|
* Examples:
|
|
*
|
|
* var code = math.compile('sqrt(3^2 + 4^2)');
|
|
* code.eval(); // 5
|
|
*
|
|
* var scope = {a: 3, b: 4}
|
|
* var code = math.compile('a * b'); // 12
|
|
* code.eval(scope); // 12
|
|
* scope.a = 5;
|
|
* code.eval(scope); // 20
|
|
*
|
|
* var nodes = math.compile(['a = 3', 'b = 4', 'a * b']);
|
|
* nodes[2].eval(); // 12
|
|
*
|
|
* See also:
|
|
*
|
|
* parse, eval
|
|
*
|
|
* @param {string | string[] | Array | Matrix} expr
|
|
* The expression to be compiled
|
|
* @return {{eval: Function} | Array.<{eval: Function}>} code
|
|
* An object with the compiled expression
|
|
* @throws {Error}
|
|
*/
|
|
return typed('compile', {
|
|
'string': function (expr) {
|
|
return parse(expr).compile();
|
|
},
|
|
|
|
'Array | Matrix': function (expr) {
|
|
return deepMap(expr, function (entry) {
|
|
return parse(entry).compile();
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
exports.name = 'compile';
|
|
exports.factory = factory;
|