6 Replies - 3012 Views - Last Post: 14 June 2009 - 01:43 PM Rate Topic: -----

#1 cam888  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 20-February 09

calling a function from a string

Posted 14 June 2009 - 09:20 AM

I am writing an interpreter for a custom language (in C++), and it is all going well, but one part.

I have a string, which contains the function name, and then a string array containing the parameters.

I cannot think of any way I can call the appropriate function, any ideas?

Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: calling a function from a string

#2 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: calling a function from a string

Posted 14 June 2009 - 09:38 AM

have to see example to visualize problem
Was This Post Helpful? 0
  • +
  • -

#3 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: calling a function from a string

Posted 14 June 2009 - 09:44 AM

If you're asking what I think you're asking, you can't do it. C++ doesn't do that kind of binding.Specifically, the symbol information simply isn't there once the program is compiled.
But if you have a finite number of functions to call you could just do it in a big switch statement?
Was This Post Helpful? 0
  • +
  • -

#4 thepeoplescoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 97
  • Joined: 02-February 09

Re: calling a function from a string

Posted 14 June 2009 - 09:52 AM

Wait, are you saying you're trying to do this?

// Assume files are included
// Assume using namespace std

// Prototypes
int someFunction(int x, int y, int z);

// Main
int main(void)
{
   string theFunction = "someFunction";
   string parms[] = {"1", "2", "3"};

   // Are you trying to do something like this?
   theFunction(parms);

   return 0;
}

int someFunction(int x, int y, int z)
{
   // Whatever you want done here, etc.
}



If that's what you're trying to do, the answer is you can't do that.

What I should be saying, is anything that's even slightly equivalent to that, you can't do it. Here's why: variable and function names only have significance to us as HUMANS. When your program is compiled and linked into an executable, guess what. All of those function and variable names go away, and are replaced with numeric addresses.

For the most part, computers are really elaborate number crunchers. If we could tell our program the name of a function we wrote, and told it to call it, the computer would have no clue of what we were talking about. The only reason why function NAMES have meaning is because the PEOPLE that wrote the compilers and linkers gave them meaning for those specific purposes. When your program is an executable, it no longer needs the names.

If you wanted a function to be "bound" to your program dynamically (which is essentially what it looks like you're doing, and yes, that's what it's called), then you have to use function pointers. Even then, you, as the programmer, still have to KNOW what function you want to call.

However, if you still want to do what you're doing, you would have to make a table of strings containing the names of your function, and have them associated with the pointers of the appropriate functions.

The last base to touch on would be that everything I told you had nothing to do with your question, since you mentioned that you're writing an interpreter, and you really could be asking how do you implement a feature like that in your INTERPRETER.

I honestly haven't written any interpreters, but I believe your question should be much easier to answer, since when you read in a source file for the language you are interpreting, you DO have access to the names, so trying to get that functionality would simply be a matter of properly working it into what you already have set in place.

Hope this helps.

This post has been edited by thepeoplescoder: 14 June 2009 - 10:01 AM

Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: calling a function from a string

Posted 14 June 2009 - 10:02 AM

What you can do, is embed some Lua in C/C++ code and then you can do it to a certain extent as Lua allows certain functional programming things like that: you can assign a variable to a function name and call appropriately.
Was This Post Helpful? 0
  • +
  • -

#6 cam888  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 20-February 09

Re: calling a function from a string

Posted 14 June 2009 - 01:33 PM

Thanks for the fast replies, I think the best method will be using a big swich statement. I had hoped there would be something, but never mind.

Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: calling a function from a string

Posted 14 June 2009 - 01:43 PM

You can only switch on an integral type (a string is not an integral type). But I assume you'll have some kind of lookup table, right?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1