Converting strings to functions
Hi, I'm wondering if there is any effecient way to convert a C string such as "foo(x,y)" into a function call of foo(x,y)? Do I have to create a lookup table for all the functions in the code?
Thanks
Thanks
You're best bet is to put all the functions in a hash table and look up your functions that way...
...but, I'm guessing that what you are doing is something that would benefit from an embeded scripting language.
...but, I'm guessing that what you are doing is something that would benefit from an embeded scripting language.
ok... hash? I'm fairly new to C, what exactly is this? (yes, I'm trying to make a simple scripting language)
How about, use Python as your scripting language, and use the SWIG extension which exposes C and C++ functions to Python. Thus the python (text) script can actually call your C code! This was used to good effect by the uDG winner from a few years back, Kiko the Nanobot. See the winner's post-mortem for info.
SWIG home: http://www.swig.org
Apparently it's very easy to embed the Python interpreter in your program. As a bonus, Python is probably going to be more fully-featured than a scripting language of your own invention.
SWIG home: http://www.swig.org
Apparently it's very easy to embed the Python interpreter in your program. As a bonus, Python is probably going to be more fully-featured than a scripting language of your own invention.
Measure twice, cut once, curse three or four times.
If you're not too skittish about digging around in large projects, you might consider checking out the Quake2 source for a good example of how to do this. Specifically the two files qcommon.h and cmd.c might help you better understand the process.
Or, you could do like MattDiamond suggested and do like the rest of us: Embed an existing scripting language. I personally love working with Lua, but Python, Ruby, etc., would be just as acceptable. With an embedded scripting language you get many, many added benefits, not to mention saving you a whole truckload of extra work reinventing yet another wheel. Plus, they usually don't add much size to your final executable. I think Lua might have added around 200 KB to mine. I was able to implement Lua as a robust command processor for my command console in less than 40 lines! Although, admittedly, it took me a few days to figure out what those lines were supposed to be.
I've been toying with the possibility of writing a small tutorial on how to embed Lua in a small game, but I'm really not a Lua expert by any stretch of the imagination so I'm hesitant of passing along mis-information.
Or, you could do like MattDiamond suggested and do like the rest of us: Embed an existing scripting language. I personally love working with Lua, but Python, Ruby, etc., would be just as acceptable. With an embedded scripting language you get many, many added benefits, not to mention saving you a whole truckload of extra work reinventing yet another wheel. Plus, they usually don't add much size to your final executable. I think Lua might have added around 200 KB to mine. I was able to implement Lua as a robust command processor for my command console in less than 40 lines! Although, admittedly, it took me a few days to figure out what those lines were supposed to be.
I've been toying with the possibility of writing a small tutorial on how to embed Lua in a small game, but I'm really not a Lua expert by any stretch of the imagination so I'm hesitant of passing along mis-information.
Lua works great as a an embedded scripting language, where your C code occasionally calls a script written in another language. Ruby and Python aren't as well suited towards embedding. However, if you want to write the high level stuff in Ruby/Python and "embed" your C code for graphics, etc. that works very well. That's what I'm doing right now, Ruby controls the execution and calls classes written in C for drawing objects, collision detection, and so on. This makes it very easy to play around.
My advice for SWIG is to stay away unless you want to use a library that's already been written. It will save you a lot of time and effort, but be warned that the bindings that it generates are just as procedural as the code that it comes from. So if you're making an extension to do something specifically for the language, do yourself a favor and design it like a real class.
My advice for SWIG is to stay away unless you want to use a library that's already been written. It will save you a lot of time and effort, but be warned that the bindings that it generates are just as procedural as the code that it comes from. So if you're making an extension to do something specifically for the language, do yourself a favor and design it like a real class.
I've tried Lua in a couple of Dreamcast things I worked on and haven't had a lot of practical luck with it due to its extreme simplicity (for example, there is only one type of numbers, and you have to choose between int/float/double at compile time). Seems like it'd be good for a deeply embedded system though, especially the older versions which used ref counting instead of garbage collection. Or for one of the things they suggest in the "what's it good for" docs, data modeling.
Ruby's the nicest of the scripting languages I've looked into but I'm too stuck on Python to do anything else these days.
To not be 100% off-topic, I think it's a worthwhile project to write your own scripting language at least once. It's a great learning experience. Once it's done, throw it away and get a pre-built one
Ruby's the nicest of the scripting languages I've looked into but I'm too stuck on Python to do anything else these days.

To not be 100% off-topic, I think it's a worthwhile project to write your own scripting language at least once. It's a great learning experience. Once it's done, throw it away and get a pre-built one
Joseph Duchesne Wrote:ok... hash? I'm fairly new to C, what exactly is this? (yes, I'm trying to make a simple scripting language)
Hash tables are a type of data structure and not C specific. Read more here.
Dan Potter Wrote:I think it's a worthwhile project to write your own scripting language at least once. It's a great learning experience. Once it's done, throw it away and get a pre-built one
Agreed. I made one myself many years ago. I also made myself an interpreted language. I think the interpreted language was easier to write the interpreter for, yet harder to write programs to run on. I haven't touched them in years, and likely never will again - it was a good experience though.
Yep, no two ways about it - Lua is the way you want to go. It's extremely well designed and would take a TON of work to duplicate yourself.
Wade
Wade
I guess it could be good practice, and if it sounds interesting to you, go for it. Otherwise if you just want the results, use Lua which will ultimately be a much more powerful solution.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| New Space Simulator - light speed functions | gooncorp | 3 | 5,030 |
Jan 2, 2013 12:52 AM Last Post: NikG |
|
| generate MD5 strings with Objective-C? | fernandovalente | 3 | 3,632 |
Jan 27, 2010 09:29 AM Last Post: Hog |
|
| C strings | mikey | 24 | 7,644 |
May 18, 2009 04:52 AM Last Post: Oddity007 |
|
| Some quick help getting started with certain Carbon functions | zmwworm | 12 | 7,025 |
Jan 10, 2008 01:14 AM Last Post: zmwworm |
|
| Where are strings? | Tekkan | 2 | 2,453 |
Jun 17, 2007 02:44 AM Last Post: Fenris |
|

