compileInlineExpression.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.compileInlineExpression = compileInlineExpression;
  6. var _is = require("../../../utils/is.js");
  7. var _scope = require("../../../utils/scope.js");
  8. /**
  9. * Compile an inline expression like "x > 0"
  10. * @param {Node} expression
  11. * @param {Object} math
  12. * @param {Object} scope
  13. * @return {function} Returns a function with one argument which fills in the
  14. * undefined variable (like "x") and evaluates the expression
  15. */
  16. function compileInlineExpression(expression, math, scope) {
  17. // find an undefined symbol
  18. var symbol = expression.filter(function (node) {
  19. return (0, _is.isSymbolNode)(node) && !(node.name in math) && !scope.has(node.name);
  20. })[0];
  21. if (!symbol) {
  22. throw new Error('No undefined variable found in inline expression "' + expression + '"');
  23. }
  24. // create a test function for this equation
  25. var name = symbol.name; // variable name
  26. var subScope = (0, _scope.createSubScope)(scope);
  27. var eq = expression.compile();
  28. return function inlineExpression(x) {
  29. subScope.set(name, x);
  30. return eq.evaluate(subScope);
  31. };
  32. }