Assume I allow the user to input a arbitrary numerical expression that allows usage of the explicit set of functions and predefined variables.
Interpreting this is pretty straight forward but what if performance is a critical factor?
I need a way to compile (in runtime) a string with a expression into something that can be executed 20K times with different values on the variables.
So really you want to parse the string into (eventually) a typed delegate.
One option, then it to parse the string into an
Expression, which you can then compile to a lambda. This is quite complex, but I have some code that will do much of this – but there may be existing parsers that already do the job in full.As a trivial
Expressionexample:The problem is the rest of the parsing code ;-p
I wrote a
stringtoExpressionparser last week, but it is more code than I’d normally post here… over 300 lines (not including recognition of bespoke external functions, or named arguments (it currently uses anonymous ‘?’ place-holders)).but if (as you say) interpreting is easy, then perhaps use similar code to write an
Expressionthat you can compile?