March 31st, 2003, 01:11 PM
dynamically defined functions
Does anyone know a way to store a function which could be defined at runtime and altered or called later?
What im trying to do is something like MATLAB's inline function. it would take a string like "f(x,y)=x+2*y*y" for instance and turn it into the c++ equivalent, which may then be called when needed. The string would be input by the user. Is there any good way to do this. Id like to not have to parse the string every time the function is called. Any advice would be great. Thanks
March 31st, 2003, 02:16 PM
You're essentially looking for a run-time scripting language, and a corresponding interpreter. Definitely not a 'quick and easy' project.
The jist of it though is you define all the functions that will be supported. Then your interpreter scans the input string, chopping it up and passing each piece onto the proper pre-defined function.
March 31st, 2003, 02:36 PM
You don't necessarily need to develop your own embedded script interpreter. There are already a number of them out there.
One that immediately comes to mind is Lua:
March 31st, 2003, 03:22 PM
Thanks for your replies!
Actually i was planning to build an interpreter. I have a command line interpreter in place. I was going to adapt that to parse the function string each time the function needed to be evaluated. Basically any user-defined functions would be stored as strings. When a function needed to be evaluated the string associated with it would be parsed, replacing variable names with the functions arguments, and it would be evaluated. I was just wondering if there was a faster way to do this. Id like to not have to traverse a string, create substrings, make many for loops, etc. each time i call a function to , say add its two arguments. It would be much faster to parse the function once when its defined, figure out what it wants to do and store it in a way in which, provided some arguments, i could ONLY do what it wanted to do, not waste time interpreting it again.
March 31st, 2003, 03:33 PM
what about putting your on the fly source code together using your code, writing it to a file and invoking the compiler, then invoking your just compiled code? maybe you wouldn't have to write it to a file? - pass it to the compiler while in memory somehow? just a thought.
March 31st, 2003, 05:14 PM
well that would be a good idea but every user of the software would need a compiler, the same compiler, and really all the features of a fullblown compiler aren't really needed. i don't need any looping or case evaluations. nothing fancy really, just order of operations and the ability to call other functions. thanks for the reply
March 31st, 2003, 05:18 PM
Hmmm... this is a problem I had run into a LONG time ago, progamming in BASIC. I think I may have thought of a solution just now, though. Basically, you wish to have something fast, because you want to compute the function over and over again for different values of x, y, etc., such as for the purposes of drawing a graph, correct?
Basically, functions are either unary or binary, meaning that they take in one operand or two. You could define a pointer to a function that is of both of these types. You could then parse the input string, and create an array of function pointers so that they are each executed in sequence, as the functions are required (when you execute them in order of precedence). Also, just like the functions are called from an array of function pointers, you can have the arguments and results stored into pointers that are pre-computed before, as well. This should result in a VERY quick result. Besides the fact that you are dereferencing pointers all of the time (which is really not that slow), you need not do any computations after the initial parsing, and should be almost as fast as doing the scripting code yourself.
Let me know if you follow what I have said. I almost feel like programming this myself now that I just thought about it! :)
March 31st, 2003, 05:26 PM
that sounds like a sound approach. i had heard of function pointers but i just learned c++ (im a physicist not a comp scientist!). i wanna make a matlab clone which will keep track of units and unit conversion. speed is definitely an issue as you said. ill look into this approach and let you know how it goes thanks a lot
March 31st, 2003, 05:30 PM
I am just getting into programming for windows. If I knew enough about it right now, I'd make this program tonight. I WILL make this program, because this is something I have been wanting to do forever. I discovered a wicked way to draw very detailed graphs in which x and y are both variable (i.e. cos(x)+y^2=sin(y+x^2)-y^x) in BASIC quite some time ago (actually, I think I originall programmed it for my TI-85, now that I think about it, and reprogrammed it into pascal). It was a pain to recompile for a new function everytime. I can't believe I just came up with the answer. heh :) I will post results here when I get it done, probably in a week or so. Let me know how your work turns out...
March 31st, 2003, 09:17 PM
well i might have changed my mind. i dont think a list of functions is quite flexible enough for my purpose. id like users to be able to create complex functions and i think any list large enough to provide sufficient coverage and nesting would be too lengthy to traverse practically. I suppose I might look into some kind of dynamic code generation scheme. I think that is what this application really calls for but i also realize its a whopper of a project. ill keep ya posted
April 1st, 2003, 07:42 AM
compiling on the fly - truly flexible functions. if your on the fly functions don't require full compiler features all the more better - only a simple minimal compiler installation is required. also more likely to work with various compilers - using a simple standard c or whichever language you're using. free compilers are available. requiring users to install a compiler is a disadvantage but there are ways to make that easier i think - maybe easier to work out an easy way to install compiler than what you mentioned above though? if the functions need to be generated on a regular basis during the running of the code then speed would be not good i guess. - but once generation has taken place, speed would be fine. in fact the resulting code could be very good - as you could eliminate a lot of the if's and other decision making parts. ask and answer those questions prior to generation.
the reason i'm harping on about this technique is i plan to do it myself. speed not being my number one priority, but being able to do what i want to do being my main priority (without too much effort). also once generated code has been generated then the speed could actually be better (decisions eliminated), so it depends just how much generation and how often generation is required. the environment needs to be able to load functions dynamically though obviously, but i don't think that's a problem. not for me it's not in any case.