How can I convert string to code at run time
Asked Answered
G

4

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?

Glyceride answered 12/9, 2013 at 13:45 Comment(2)
Not unless you apply Greenspun's 10th rule.Memphis
What about root.cern.ch/cling ? Or have a look at: #70039Chari
E
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).

Edify answered 12/9, 2013 at 13:50 Comment(5)
Ohh sounds interesting, yes if course it would be better. I actually Know pythong and i started to think how complicated it is.Glyceride
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 stringGlyceride
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.Edify
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 ironpython.netEdify
Or boost.org/doc/libs/1_54_0/libs/python/doc/index.html if you don't want managed code.Edify
O
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;
}
Oeuvre answered 12/9, 2013 at 13:57 Comment(0)
S
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

Sarge answered 12/9, 2013 at 13:47 Comment(2)
Yes I saw that on Java they have an Eval-function, thought that it will be the same here.Glyceride
Even in Java they don't (because Java is also a somewhat compiled language). In JavaScript however they do. Don't confuse Java and JavaScript.Sarge
B
-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;
Barytone answered 3/1, 2023 at 14:2 Comment(2)
Question not understood. How does this turn a string into executable code?Pigheaded
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.Subclinical

© 2022 - 2024 — McMap. All rights reserved.