Execute commands in a C++ string

Asked

Viewed 139 times

1

I have commands in a string something like:

string comm = "int x; cout << \"Digite um número\"; cin << x;";

My idea is to execute the commands that are in this string, having the result as if I had typed:

int x
cout << "Digite um número";
cin << x;

Is there a command that does this?

I thought of using reader that identifies patterns and performs functions, but this would be very complicated, beyond what, do not know how to create variables.

1 answer

1


Generally speaking you don’t have to, you have to practically create a compiler that will do it for you. Of course you can use what you have ready as a basis to adapt.

Of course, if you restrict what you can use, it can be easier to make something simpler than a full compiler, because a C++ compiler is too complicated, C++ is considered the language mainstream more complicated to interpret.

If you think Pattern matching complicated, so forget it, because it’s much more complicated than that.

  • I thought to write a file . cpp with the contents of the string and have the compiler run, this is possible?

  • Of course, of course, in practice this has no use in this simple way, and it has enormous disadvantages, so it gets kind of in the theoretical field.

  • And what would work? As I said in the question, the biggest problem is declaring variables.

  • No, that’s not the biggest problem, what you’re wondering is a comma in a whole book, give up this idea.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.