Convert string to code

M

muffinman

Hello,
I try to convert a string (char[] or string or CString, etc) into
an executable function.

For example :

char sample[20] = "cout << \"sample\"";
char setColor[30] = "glColor3f(1.0f, 1.0f, 0.0f);"

execute(sample); // my wish is to do this!
execute(setColor); // and also this!

then I can run the string.

I am trying to write a game console without directInput. I want to
execute any strings, without switch or if comparisons...


Thank you
 
V

Victor Bazarov

muffinman said:
I try to convert a string (char[] or string or CString, etc) into
an executable function.

For example :

char sample[20] = "cout << \"sample\"";
char setColor[30] = "glColor3f(1.0f, 1.0f, 0.0f);"

execute(sample); // my wish is to do this!
execute(setColor); // and also this!

then I can run the string.

I am trying to write a game console without directInput. I want to
execute any strings, without switch or if comparisons...

Not possible. Search the web for "interpreter c++".

V
 
M

Mogens Heller Jensen

muffinman said:
Hello,
I try to convert a string (char[] or string or CString, etc) into
an executable function.

For example :

char sample[20] = "cout << \"sample\"";
char setColor[30] = "glColor3f(1.0f, 1.0f, 0.0f);"

execute(sample); // my wish is to do this!
execute(setColor); // and also this!

then I can run the string.

I am trying to write a game console without directInput. I want to
execute any strings, without switch or if comparisons...


Thank you

This is actually very high level, and C++ does not support this type of
programming. It's called reflection, since it's a feature to let a
programming language sort of "see itself". Also called meta programming for
the same reason.

You can do those things in C# (and Java I think), since the run time code
("MS intermediate language" or "Java bytecode") contains information on the
names of classes and their methods and so on.

When you compile a C++ program you end up with assembly, and all that sort
of information will not be available at run time.

Actually, to some degree, you can use a few of the features of reflection in
C++ if you compile your program with the RTTI switch turned on, but your
compiler must support this. And it does not allow full reflection, it just
allows you to do type checking at run time (Run Time Type Information).

-M
 
M

Marcin Kalicinski

Hello,
I try to convert a string (char[] or string or CString, etc) into
an executable function.

For example :

char sample[20] = "cout << \"sample\"";
char setColor[30] = "glColor3f(1.0f, 1.0f, 0.0f);"

execute(sample); // my wish is to do this!
execute(setColor); // and also this!

What you need is just to write your own C++ compiler to compile the string
into executable code. Good luck!

cheers,
Marcin
 
J

Jim Langston

muffinman said:
Hello,
I try to convert a string (char[] or string or CString, etc) into
an executable function.

For example :

char sample[20] = "cout << \"sample\"";
char setColor[30] = "glColor3f(1.0f, 1.0f, 0.0f);"

execute(sample); // my wish is to do this!
execute(setColor); // and also this!

then I can run the string.

I am trying to write a game console without directInput. I want to
execute any strings, without switch or if comparisons...


Thank you

C++ is a compiled language. Meaning tokens (such as cout, glColor3f, 1.0f,
etc...) are converted to something the computer understands then converted
to assembly calls. Which means at the time your program is run there is
nothing to convert from cout to the assembly without compiling.

If you want to run arbitrary code at run time you'll need to interpret it.
An interpreted langauge (such as some implementations of java) don't convert
tokens to machine executable instructions until run time.

There are a number of interpreted langauges that will do what you want
without you writing an interpreter, but c++ isn't one of them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top