compile.js (1779B)
1 "use strict"; 2 3 Object.defineProperty(exports, "__esModule", { 4 value: true 5 }); 6 exports.createCompile = void 0; 7 8 var _collection = require("../../utils/collection.js"); 9 10 var _factory = require("../../utils/factory.js"); 11 12 var name = 'compile'; 13 var dependencies = ['typed', 'parse']; 14 var createCompile = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) { 15 var typed = _ref.typed, 16 parse = _ref.parse; 17 18 /** 19 * Parse and compile an expression. 20 * Returns a an object with a function `evaluate([scope])` to evaluate the 21 * compiled expression. 22 * 23 * Syntax: 24 * 25 * math.compile(expr) // returns one node 26 * math.compile([expr1, expr2, expr3, ...]) // returns an array with nodes 27 * 28 * Examples: 29 * 30 * const code1 = math.compile('sqrt(3^2 + 4^2)') 31 * code1.evaluate() // 5 32 * 33 * let scope = {a: 3, b: 4} 34 * const code2 = math.compile('a * b') // 12 35 * code2.evaluate(scope) // 12 36 * scope.a = 5 37 * code2.evaluate(scope) // 20 38 * 39 * const nodes = math.compile(['a = 3', 'b = 4', 'a * b']) 40 * nodes[2].evaluate() // 12 41 * 42 * See also: 43 * 44 * parse, evaluate 45 * 46 * @param {string | string[] | Array | Matrix} expr 47 * The expression to be compiled 48 * @return {{evaluate: Function} | Array.<{evaluate: Function}>} code 49 * An object with the compiled expression 50 * @throws {Error} 51 */ 52 return typed(name, { 53 string: function string(expr) { 54 return parse(expr).compile(); 55 }, 56 'Array | Matrix': function ArrayMatrix(expr) { 57 return (0, _collection.deepMap)(expr, function (entry) { 58 return parse(entry).compile(); 59 }); 60 } 61 }); 62 }); 63 exports.createCompile = createCompile;