Evaluating a math expression with variables. (java 8)
<p>I would like additional help on a answer to this question,   <a href="https://stackoverflow.com/a/26227947/4825796">Evaluating a math expression given in string form</a>.  The user @Boann answered the question with a very interesting algorithm that he also points out can be altered to accept variables.  I've managed to alter it and get it to work, <strong>but dont know how he separates the compilation and evaluation</strong>.  Here's my code:</p>

<pre><code>import java.util.HashMap;
import java.util.Map;

public class EvaluateExpressionWithVariabels {

@FunctionalInterface
interface Expression {
    double eval();
}

public static void main(String[] args){
    Map&lt;String,Double&gt; variables = new HashMap&lt;&gt;();     
    for (double x = 100; x &lt;= +120; x++) {
        variables.put("x", x);
        System.out.println(x + " =&gt; " + eval("x+(sqrt(x))",variables).eval());
    }
}

public static Expression eval(final String str,Map&lt;String,Double&gt; variables) {
    return new Object() {
        int pos = -1, ch;

        //if check pos+1 is smaller than string length ch is char at new pos
        void nextChar() {
            ch = (++pos &lt; str.length()) ? str.charAt(pos) : -1;
        }

        //skips 'spaces' and if current char is what was searched, if true move to next char return true
        //else return false
        boolean eat(int charToEat) {
            while (ch == ' ') nextChar();
            if (ch == charToEat) {
                nextChar();
                return true;
            }
            return false;
        }


        Expression parse() {
            nextChar();
            Expression x = parseExpression();
            if (pos &lt; str.length()) throw new RuntimeException("Unexpected: " + (char)ch);
            return x;
        }

        // Grammar:
        // expression = term | expression `+` term | expression `-` term
        // term = factor | term `*` factor | term `/` factor
        // factor = `+` factor | `-` factor | `(` expression `)`
        //        | number | functionName factor | factor `^` factor

        Expression parseExpression() {
            Expression x = parseTerm();
            for (;;) {
                if (eat('+')) { // addition
                    Expression a = x, b = parseTerm();                      
                    x = (() -&gt; a.eval() + b.eval());
                } else if (eat('-')) { // subtraction
                    Expression a = x, b = parseTerm();
                    x = (() -&gt; a.eval() - b.eval());
                } else {
                    return x;
                }
            }
        }

        Expression parseTerm() {
            Expression x = parseFactor();
            for (;;) {
                if (eat('*')){
                     Expression a = x, b = parseFactor(); // multiplication
                     x = (() -&gt; a.eval() * b.eval());
                }
                else if(eat('/')){
                     Expression a = x, b = parseFactor(); // division
                     x = (() -&gt; a.eval() / b.eval());
                }
                else return x;
            }
        }

        Expression parseFactor() {
            if (eat('+')) return parseFactor(); // unary plus
            if (eat('-')){
                Expression b = parseFactor(); // unary minus
                return (() -&gt; -1 * b.eval());
            }

            Expression x;
            int startPos = this.pos;
            if (eat('(')) { // parentheses
                x = parseExpression();
                eat(')');
            } else if ((ch &gt;= '0' &amp;&amp; ch &lt;= '9') || ch == '.') { // numbers
                while ((ch &gt;= '0' &amp;&amp; ch &lt;= '9') || ch == '.'){                     
                    nextChar();
                }
                double xx = Double.parseDouble(str.substring(startPos, this.pos));
                x = () -&gt; xx;
            } else if (ch &gt;= 'a' &amp;&amp; ch &lt;= 'z') { // functions
                while (ch &gt;= 'a' &amp;&amp; ch &lt;= 'z') nextChar();
                String func = str.substring(startPos, this.pos);

                if ( variables.containsKey(func)){
                    x = () -&gt; variables.get(func);
                }else{
                    double xx = parseFactor().eval();
                    if (func.equals("sqrt")) x = () -&gt; Math.sqrt(xx);
                    else if (func.equals("sin")) x = () -&gt; Math.sin(Math.toRadians(xx));
                    else if (func.equals("cos")) x = () -&gt; Math.cos(Math.toRadians(xx));
                    else if (func.equals("tan")) x = () -&gt; Math.tan(Math.toRadians(xx));                    
                    else throw new RuntimeException("Unknown function: " + func);
                }
            } else {
                throw new RuntimeException("Unexpected: " + (char)ch);
            }

            if (eat('^')){ 
                x = () -&gt; {
                    double d =  parseFactor().eval();
                    return Math.pow(d,d); // exponentiation
                };
            }

            return  x;
        }
    }.parse();
}
}
</code></pre>

<p>If you take a look at his answer his main</p>

<pre><code>public static void main(String[] args) {
    Map&lt;String,Double&gt; variables = new HashMap&lt;&gt;();
    Expression exp = parse("x^2 - x + 2", variables);
    for (double x = -20; x &lt;= +20; x++) {
        variables.put("x", x);
        System.out.println(x + " =&gt; " + exp.eval());
    }
}
</code></pre>

<p>He calls function <code>parse</code> on this line <code>Expression exp = parse("x^2 - x + 2", variables);</code> to compile the expression once and uses the for to evaluate it multiple times with a unique x values.  What does the <code>parse</code> function refer to.</p>

<p>ps: I have commented on the users question with no reply.</p>