...

View Full Version : Execute code from a text file? (C/C++/Assembly)



Dunna
09-07-2008, 07:56 PM
Hi, I'm wondering if anyone knows if it's possible to execute live lines of code from a text file (or user input) during program execution without an interpreter. In other words, say I had the following class:


class InterpretMe
{
public:
void SomeFunction();
};


I want the user to be able to enter something like:


InterpretMe Hello;
Hello.SomeFunction();


But I don't want to use an interpreter. The reason is I want the user to be able to execute any API, in realtime. If this cannot be accomplished in C++ I am willing to do it in assembly. Does anyone know of a function that will take some string as a parameter and convert it to live code? Or know how to write one? Thanks.

ralph l mayo
09-07-2008, 10:06 PM
You keep saying "without an interpreter" but


Does anyone know of a function that will take some string as a parameter and convert it to live code?

is a fairly workable definition of 'interpreter', and you certainly can't execute code without at some point 'interpreting' its meaning, can you?

If your aim is simply to get inline execution working, consider the Visual Studio mechanism of handing it off to the compiler and executing it asynchronously from the GUI components of the program.

I do something similar in vim (albeit synchronously) by mapping C-F5 to run C++ code:


map <C-F5> :w !g++ -x c++ -Wall -Wextra -o ~/.vimrun - && ~/.vimrun<CR>


One problem with this approach is linking... normal C++ code needs some linker directives and possibly a specific include path. Visual Studio uses the project configuration to add "references" (IIRC) that are linked during the inline compile stage, and I just add my libraries to the macro directly since I generally work with the same small group of tools.

If you wanted to automatically enable API access you'd have to either link with everything by default (probably a bad idea) or do your own pre-processing with a large database of supported APIs, looking for things like #include <boost/filesystem/operations.hpp> to map to -lboost_filesystem in the compiler switches.

oracleguy
09-07-2008, 10:16 PM
If you don't want to use an interpreter you are going to have to run a compiler (or assembler) when the user wants to run the code they typed in. Then your program could call the generated executable OR what you could is this:

Build a a generic framework for a DLL (or SO in Unix) and then the user types their code in, when they hit run your program runs the compiler and compiles the library. Then your program could dynamically link to the DLL at runtime, where it would load it when called and you could call some function inside the DLL that would run the user code.

Frankly it would be kind of complicated but it could certainly be done. However using an interpreter might be a better idea.

liorean
09-08-2008, 03:10 PM
You could check out the TCC compiler at http://bellard.org/tcc/ . It's available both as a stand alone program and as a library, and can compile and optimise C at really fast speeds.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum