I'm looking for a formula interpreter that I can use in a C# application. It needs to be able to interpret a string like this:
max(1+2, 4) * x
I found Writing a fast formula interpreter (codeproject.com) which almost does what I need but it doesn't allow for functions with multiple parameters. I could probably add that functionality to it but I was just wondering if something like this already exists.
Thanks
A couple I've used in the past with no problems:
You can actually build a very effective interpreter by parsing and replacing certain functional keywords such as
max
with Math.Max and then dynamically building and executing the formula as a C# class and method. So actually you would be parsing and wrapping the formula and allowing the C# compiler to interpret and execute it.So, taking your sample formula of
max(1+2, 4) * x
would turn into:Which you would compile on the fly and then execute per the linked article. You still have to parse for and pass the x value of course.
A long time ago in one project i had to create some booking with formulas, and i used VsaEngine. To use this engine you need to add reference to
Microsoft.JScript
. Here is example:Code for usage is very simple, just replace formula parameters like:
And here is core method, CalculateFormula:
With this you can create your custom formula interpreter engine.