3

I'm trying to adjust some mathematical code I've written to allow for arbitrary functions, but I only seem to be able to do so by pre-defining them at compile time, which seems clunky. I'm currently using function pointers, but as far as I can see the same problem would arise with functors. To provide a simplistic example, for forward-difference differentiation the code used is:

double xsquared(double x) {
    return x*x;
}

double expx(double x) {
    return exp(x);
}

double forward(double x, double h, double (*af)(double)) {
    double answer = (af(x+h)-af(x))/h;

    return answer;  
}

Where either of the first two functions can be passed as the third argument. What I would like to do, however, is pass user input (in valid C++) rather than having to set up the functions beforehand. Any help would be greatly appreciated!

Greg
  • 61
  • 1
  • 3
  • 1
    So you want to build a fully functional C++ compiler? Erm... that's a bit beyond the scope of Stack Overflow, I think. – Jonathan Grynspan Feb 09 '11 at 20:21
  • c++ is not a scripting language ... you could try to integrate with something like lua, tcl or python – Foo Bah Feb 09 '11 at 20:21
  • You're using the wrong language. (C++0x does have closures etc. but it's propably still clunky.) –  Feb 09 '11 at 20:22

8 Answers8

5

Historically the kind of functionality you're asking for has not been available in C++. The usual workaround is to embed an interpreter for a language other than C++ (Lua and Python for example are specifically designed for being integrated into C/C++ apps to allow scripting of them), or to create a new language specific to your application with your own parser, compiler, etc. However, that's changing.

Clang is a new open source compiler that's having its development by Apple that leverages LLVM. Clang is designed from the ground up to be usable not only as a compiler but also as a C++ library that you can embed into your applications. I haven't tried it myself, but you should be able to do what you want with Clang -- you'd link it as a library and ask it to compile code your users input into the application.

You might try checking out how the ClamAV team already did this, so that new virus definitions can be written in C.

As for other compilers, I know that GCC recently added support for plugins. It maybe possible to leverage that to bridge GCC and your app, but because GCC wasn't designed for being used as a library from the beginning it might be more difficult. I'm not aware of any other compilers that have a similar ability.

Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
3

As C++ is a fully compiled language, you cannot really transform user input into code unless you write your own compiler or interpreter. But in this example, it can be possible to build a simple interpreter for a Domain Specific Language which would be mathematical formulae. All depends on what you want to do.

wendazhou
  • 295
  • 2
  • 10
  • You can do this nowadays with at least one compiler, see my answer. – Joseph Garvin Feb 09 '11 at 20:36
  • Got it, thanks. Another user pointed me towards muParser, which is what I'll probably use in the final code, but I'd still be interested in understanding how I would go about passing the results of my own equation interpreter into the function - any chance of a brief explanation? – Greg Feb 09 '11 at 20:44
  • @Greg For example, you could create a class implementing an operator () (function call) which would then delegate the call to your interpreter passing in the abstract syntax tree you obtained from the parsing of the equation. – wendazhou Feb 09 '11 at 20:49
1

You could always take the user's input and run it through your compiler, then executing the resulting binary. This of course would have security risks as they could execute any arbitrary code.

Probably easier is to devise a minimalist language that lets users define simple functions, parsing them in C++ to execute the proper code.

Mark B
  • 95,107
  • 10
  • 109
  • 188
  • Thank you! I thought I was about to have to have to write a comment pointing out that executing user-supplied C++ code is a huge security risk, but it seems you've taken care of that. I'm very glad someone pointed it out. – Mac Feb 09 '11 at 20:53
1

The best solution is to use an embedded language like lua or python for this type of task. See e.g. Selecting An Embedded Language for suggestions.

Community
  • 1
  • 1
Rune Aamodt
  • 2,551
  • 2
  • 23
  • 27
1

You may use tiny C compiler as library (libtcc).

It allows you to compile arbitrary code in run-time and load it, but it is only works for C not C++.

Generally the only way is following:

  • Pass the code to compiler and create shared object or DLL
  • Load this Shared object or DLL
  • Use function from this shared object.
Artyom
  • 31,019
  • 21
  • 127
  • 215
0

While it seems like a blow off, there are a lot of people out there who have written equation parsers and interpreters for c++ and c, many commercial, many flawed, and all as different as faces in a crowd. One place to start is the college guys writing infix to postfix translators. Some of these systems use paranthetical grouping followed by putting the items on a stack like you would find in the old HP STL library. I spent 30 seconds and found this one:

http://www.speqmath.com/tutorials/expression_parser_cpp/index.html

possible search string:"gcc 'equation parser' infix to postfix"

0

C++, unlike some other languages like Perl, isn't capable of doing runtime interpretation of itself.

Your only option here would be to allow the user to compile small shared libraries that could be dynamically-loaded by your application at runtime.

Blrfl
  • 6,817
  • 1
  • 25
  • 25
0

Well, there are two things you can do:

  1. Take full advantage of boost/C++0x lambda's and to define functions at runtime.

  2. If only mathematical formula's are needed, libraries like muParser are designed to turn a string into bytecode, which can be seen as defining a function at runtime.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • muParser looks perfect for the job, thanks. I think I'll still try putting together my own rudimentary equation interpreter to help understand what's happening 'behind the scenes', though. – Greg Feb 09 '11 at 20:40