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?
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).
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;
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With