Strange issue with the B83 expression parser

I’m using the wonderful Expression Parser created by @Bunny83 as-is with no modifications (yet):

http://wiki.unity3d.com/index.php/ExpressionParser

What I’m trying to do is set it up so that the parser can take variables from within my code, for example, the power of a weapon or ability. This should be possible with the AddConst() function. However, I must be misunderstanding something here. See below:

            ExpressionParser fParser = new ExpressionParser();
            fParser.AddConst("effectPower", () => 42);
            fParser.AddConst("meaningOfLife", () => 42);
            string test = "meaningOfLife + rnd(1,4)";
            float value = (float)fParser.Evaluate(test);

In the test case shown above, “value” evaluates to 42 plus 1-4. Correct, and makes sense. But if I change the evaluation string to use “effectPower” instead…

            string test = "effectPower + rnd(1,4)";

The effectPower constant is ignored / evaluated as 0. I don’t understand why or how this could happen. Is there something with constants that I’m missing?

Of course the end goal is to actually set the value of the constant “effectPower” to something else entirely, but for now, for the sake of testing, I set it to an actual simple constant… and it still evaluates as 0!

Any advice appreciated… thanks in advance!

:smiley: yes, I just realized what the error is just because of 2.718. As I said I’m not home and I don’t have a PC at hand (just my tablet). Though the problem is the way how function and constant names are recognized. I use the string function StartsWith. The problem is that there is the default constant “e” (Euler’s number) so everything that starts with “e” is currently recognized as Euler’s number.

As a quick fix just for constants you can change this:

        foreach (var C in m_Consts)
        {
            if (aExpression.StartsWith(C.Key))
            {
                return new CustomFunction(C.Key,(p)=>C.Value(),null);
            }
        }

to this

        foreach (var C in m_Consts)
        {
            if (aExpression == C.Key)
            {
                return new CustomFunction(C.Key,(p)=>C.Value(),null);
            }
        }

This won’t work for functions since each function name is immediately followed by a bracket substitution string. It’s also possible but a bit more work. I’ll fix it when I’m back home.