compile.js 1.7 KB

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