simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

compileInlineExpression.js (1160B)


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