Evaluating code passed by argument?
In a small DLL I'm working on, I need a way for the user to enter a functional definition, which is then passed to my function as an argument (PCHAR), and have this evaluated.
For example, if my function is defined thus:
double eval(PCHAR func,PCHAR vari,double val) { }
And the user calls it thus:
eval("3x+5","x",5)
It will return 20, as 3x+5=20 when x=5.
This is similar to Javascript's eval() function, but as C++ is nothing like Javascript, I doubt it'll be so easy.
Is there any way to do this? I need the user to be able to pass their custom functions to the DLL for evaluation.
If it matters, due to compatibility constraints, only PCHARs and doubles may be passed as arguments or returned, though anything goes within a function.
-IMP ;) :)
Re: Evaluating code passed by argument?
It can be done, but you will need to write code to interpret the data (I would look at using stringstream to do the coversions from string to double.
Re: Evaluating code passed by argument?
You can have a look at this example for math parser.
It's not C++, but you can easily rewrite it in C++.
Regards,
Zachm