Executing C++ Code at Runtime

This is a discussion on Executing C++ Code at Runtime within the C++ Programming forums, part of the General Programming Boards category; Hi everybody, I'm looking for a way to let the user enter their own bit of code into the program, ...

  1. #1
    jdm
    jdm is offline
    Registered User
    Join Date
    Apr 2004
    Posts
    14

    Executing C++ Code at Runtime

    Hi everybody,
    I'm looking for a way to let the user enter their own bit of code into the program, and then have the program evaluate that line. As a web designer I know that JavaScript has such a function, which follows this syntax:

    Code:
    var string = "xyz = 1 + 3";
    eval(string);
    That code will assign the value 4 to the variable xyz.

    Is there any equivalent way to do this in C++? Ultimately, I want to create a program that lets the user enter a function, and then the program will find the derivative of that function (but, of course, there are many other uses as well).

    Any help is appreciated!
    // Visual C++ 6.0... newbie with many questions!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there any equivalent way to do this in C++?
    Sure, but it involves writing your own C++ interpreter. Somehow I get the feeling you don't want to do that.
    My best code is written with the delete key.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Echo Prelude. Sure it can be done, but you'd have to write all the code for it yourself.

    With all the different languages that use .NET, they've made something pretty similar to compiler classes, where you can have your own little VB compiler embedded in your program. There might be a C++ one. I don't know much about it, but I'll have a closer look and give you a better idea of what's there.

    You could also search Google and see if someone's already done it. Just because it's not standard doesn't mean it isn't existant.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Just because it's not standard doesn't mean it isn't existant.
    *Flips through the C++ standard*

    I can't find that clause anywhere.

    My best code is written with the delete key.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    It's on page 783...

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >It's on page 783...
    When referring to the standard it's customary to quote the relevant part that makes your point. So let's see it.
    My best code is written with the delete key.

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Oh... you were being serious... I thought you were just kidding...

    No, that's not written in the standard anywhere, but take a look at SDL. SDL is not standard, but it's there. That's all I was saying. Just because there isn't a standard function that executes the code contained in a string, doesn't mean someone, somewhere hasn't written one.

    edit: And for anyone that doesn't get the joke about Page 783 - There are only 782 pages in the standard. Also the reason for a quick PM to Prelude to find that out.
    Last edited by sean; 10-17-2004 at 07:19 PM.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Oh... you were being serious... I thought you were just kidding...
    I'm never serious, but I can't resist shooting down a good joke.
    My best code is written with the delete key.

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    http://www.softintegration.com/

    C++ would be my last choice as a script language.

    If your using Windows, you can make use of the MS Script Control to run your script in either VBScript or JScript. Check out DispHelper, especially the script control sample.

    Here is a sample of executing javascript:
    Code:
    int nResult;
    RunScript(L"%d", &nResult, TEXT("Math.round(Math.pow(5, 2) * Math.PI)"), TEXT("JScript"));
    cout << "Result = " << nResult << endl;
    You could execute your script:
    Code:
    int nResult;
    RunScript(L"%d", &nResult, TEXT("1 + 3"), TEXT("JScript"));
    cout << "Result = " << nResult << endl;
    Last edited by anonytmouse; 10-18-2004 at 03:11 PM.

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    If your on windows, there is no need to write your own interpreter. You could feed the code that the user writes into a file and run a compiler on it to create a dll and then link to that dll and execute the code inside it. Of course, this requires that the user coordinate naming conventions (which method in the dll are you going to call) and all the includes they will need and all that junk. Its a tall task that will take a long time to properly debug, but its possible.

    And don't take my word for it...its always easier to talk about doing something...its quite different to actually do it

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    that would of course require the user to have a compiler
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You might be more interested in looking at some interpreted language such as Scheme (for which interpreters do exist), or something of your own design. It would be far too much hassle to try to make C++ into a scripting language, in my opinion (especially for a math program like that).

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  13. #13
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I'm never serious
    Hey, me too!

  14. #14
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    eval(string)
    isn't there a bunch of security risks associated with this also? having the end user excute arbitarty code like that. Is there some sandbox in place?

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I'm not sure that Javascript can get into anything terribly vital, but I do know that such a thing would be rather unwise in something like Perl. Similar to one of the arguments against using the system( ) command in C++.

    jdm, check this thread: The saga continues: Algebraic simplification
    It is pertinent to the problem you are trying to solve.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Giving instruction to an executing C code
    By darkwalk in forum C Programming
    Replies: 2
    Last Post: 10-24-2006, 01:01 PM
  2. Replies: 40
    Last Post: 09-01-2006, 01:09 AM
  3. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 01:48 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21