C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 31st, 2003, 01:11 PM
antarctic antarctic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 17 antarctic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #2  
Old March 31st, 2003, 02:16 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 13
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.

Reply With Quote
  #3  
Old March 31st, 2003, 02:36 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,134 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 49 m 18 sec
Reputation Power: 1974
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/

Reply With Quote
  #4  
Old March 31st, 2003, 03:22 PM
antarctic antarctic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 17 antarctic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #5  
Old March 31st, 2003, 03:33 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
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.

Reply With Quote
  #6  
Old March 31st, 2003, 05:14 PM
antarctic antarctic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 17 antarctic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #7  
Old March 31st, 2003, 05:18 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
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!

Reply With Quote
  #8  
Old March 31st, 2003, 05:26 PM
antarctic antarctic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 17 antarctic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #9  
Old March 31st, 2003, 05:30 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
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...

Reply With Quote
  #10  
Old March 31st, 2003, 09:17 PM
antarctic antarctic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 17 antarctic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #11  
Old April 1st, 2003, 07:42 AM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > dynamically defined functions

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap