string - Making a C++ app scriptable -


I have several functions in my program that look like this:

  void foo ( Int x, int y)  

Now I want my program to take a string that looks:

  foo (3, 5) < / Code> 

and execute the same function.

When I say straightforward, I mean that it is great and elegant, but it should not take too much time to code up.

Edit:

When using a real scripting language, my problem will be solved, I still need to know whether Pure C ++ There is a quick way to implement it.

I also go to the scripting language answer.

By using Pure C ++, I would probably use a parser generator, which will get the token and grammar rules, and give me the c code which can indeed parse the given function call language , And I can be used to chop any input of the syntax tree of that call, and can be used to parse the tokens and convert them into a syntax tree. Alternatively, for that approach, the function can also be used to parse the call language, I've never used any of these devices, but have worked on programs that use them, thus I know what I will use to solve that problem.

For very simple cases, you can change your syntax on:

  func_name arg1, arg2  

then you You can use:

  std :: istringstream str (line); Std :: string fun_name; String & gt; & Gt; Fun_name; Map [fun_name] (tokenize_args (STR));  

Map a

  std :: map & lt; Std :: string, boost :: function & lt; Zero (std :: vector & lt; std:: string & gt;) & gt; & Gt; Map;  

which will be populated with the function at the beginning of your program. Only the logic will separate tokenize_args , and a vector will return them as a string. Of course, this is very primitive, but I think it's advisable if you want to have a way to call a function (i.e., if you really want to support the script, this method will not be enough).


Comments