Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    May 2004
    Location
    New Hampshire, America
    Posts
    246
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Execute code from a text file? (C/C++/Assembly)

    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:
    Code:
    class InterpretMe
    {
    public:
       void SomeFunction();
    };
    I want the user to be able to enter something like:
    Code:
    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.

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    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:
    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.

  • #3
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    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.
    OracleGuy

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •