#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    17
    Rep Power
    0

    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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    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.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    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:
    http://www.lua.org/
    http://lua-users.org/
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    17
    Rep Power
    0
    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.
  8. #5
  9. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    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.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    17
    Rep Power
    0
    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
  12. #7
  13. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    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! :)
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    17
    Rep Power
    0
    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
  16. #9
  17. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    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...
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    17
    Rep Power
    0
    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
  20. #11
  21. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    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.

IMN logo majestic logo threadwatch logo seochat tools logo