9

I am generating strings containing if else expressions. I was wondering if I can execute this expressions at run time? Here's an example:

string s = "if(x > 10) {Fly = true;} else {Fly = False;}";
Execute (s); 

Is it even possible to do this?

cigien
  • 57,834
  • 11
  • 73
  • 112
Hani Goc
  • 2,371
  • 5
  • 45
  • 89
  • What about https://root.cern.ch/cling ? Or have a look at: http://stackoverflow.com/questions/69539/have-you-used-any-of-the-c-interpreters-not-compilers – Jerry Jeremiah Oct 27 '16 at 01:02
  • Not unless you apply [Greenspun's 10th rule](http://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule). – Adam Burry Sep 12 '13 at 13:49

4 Answers4

4

It is possible to use TCC ( http://bellard.org/tcc/ ). It allows to compile and run code natively at runtime. Another approach is to use an interpreter, there are plenty out there (LUA, Python etc, see list wiki).

Raxvan
  • 6,257
  • 2
  • 25
  • 46
  • Ohh sounds interesting, yes if course it would be better. I actually Know pythong and i started to think how complicated it is. – Hani Goc Sep 12 '13 at 13:54
  • And One question Rax please. I can include it in my code right? Like for example in the for **Loop** I can use an **expression** to compile the string – Hani Goc Sep 12 '13 at 13:55
  • Yes, you can compile any c code this includes expressions and any c language feature. For your needs you could make a c functions that takes arguments and calculates the return the evaluated expression. TCC will compile that code and you can get a pointer to that function and evaluate it any time you want. I think all the code in TCC is bound by a context so as long as the context is initialized the compiled code can be executed. – Raxvan Sep 12 '13 at 13:59
  • However i do not encourage you to use it on a production project since i don't know if was used on a mass scale and it could have it's problems (memory leaks , crashes , etc). For a production project you should use a scripting language like LUA , you can even run PYTHON code if you want , check http://ironpython.net/ – Raxvan Sep 12 '13 at 14:02
  • Or http://www.boost.org/doc/libs/1_54_0/libs/python/doc/index.html if you don't want managed code. – Raxvan Sep 12 '13 at 14:05
4

One does not simply interpret C/C++ code... AFAIK you just can't.
(except if you compile another binary and run it from cmd line maybe...)

Note: You can write

fly = (x > 10);

instead of

if(x > 10){
    fly = true;
}else{
    fly = false;
}
Translunar
  • 3,739
  • 33
  • 55
johan d
  • 2,798
  • 18
  • 26
3

No. C++ is a compiled language and has no eval-function or the-like. You may want to include a scripting engine into your program, like Lua

Constantin Berhard
  • 1,271
  • 10
  • 12
-2

int n; cin >> n;

    for (int i = 0; i < n; i++) {
        cout << string((n - 1) - i, ' ') << string(2 * i + 1, '*') << endl;
    }

    for (int i = n - 2; i >= 0; i--) {
        cout << string((n - 1) - i, ' ') << string(2 * i + 1, '*') << endl;
    }

    return 0;
  • Question not understood. How does this turn a string into executable code? – Marcel Blanck Jan 03 '23 at 14:11
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 04 '23 at 01:16