Converting strings to functions

Nibbie
Posts: 1
Joined: 2010.11
Post: #1
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
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
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.
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2010.11
Post: #3
ok... hash? I'm fairly new to C, what exactly is this? (yes, I'm trying to make a simple scripting language)
Quote this message in a reply
Moderator
Posts: 437
Joined: 2002.09
Post: #4
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.

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #5
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. Wink 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.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #6
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.
Quote this message in a reply
Member
Posts: 198
Joined: 2005.01
Post: #7
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. Wink

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 Grin

Cryptic Allusion Games / Cryptic Allusion, LLC
http://www.cagames.com/
Quote this message in a reply
sh4ggy87
Unregistered
 
Post: #8
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.
Quote this message in a reply
Member
Posts: 41
Joined: 2005.02
Post: #9
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 Grin

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.
Quote this message in a reply
Member
Posts: 116
Joined: 2002.04
Post: #10
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
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #11
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Functions not being initialised onKeyDown Yendall 1 1,363 Apr 15, 2014 01:31 PM
Last Post: OneSadCookie
  generate MD5 strings with Objective-C? fernandovalente 3 4,398 Jan 27, 2010 09:29 AM
Last Post: Hog
  C strings mikey 24 8,683 May 18, 2009 04:52 AM
Last Post: Oddity007
  Some quick help getting started with certain Carbon functions zmwworm 12 8,296 Jan 10, 2008 01:14 AM
Last Post: zmwworm
  Where are strings? Tekkan 2 2,776 Jun 17, 2007 02:44 AM
Last Post: Fenris