Re: How can I evaluate code?

Chris Angelico <[email protected]> Fri, 26 Apr 2019 02:27:51 +1000
Newsgroups gmane.comp.lang.pike.user
Message-ID <CAPTjJmpa1a0KDHGnLoWZ1BhReKFkGDW_7z0MPy7BcvJCwWDLSA@mail.gmail.com>
On Thu, Apr 25, 2019 at 11:54 PM MineMoreGames <[email protected]> wrote:
>
> I’ve got the Pike code listening to some input from a program, I want to evaluate code based on the input,
>
> (Assume security is unnecessary)
>
> How can I do that? And of course I want the code that the program has access to where it gets evaluated.
>
>

The normal way would be one of the compile() functions - for instance,
compile_string(). You give it a string of text equivalent to what
you'd have in a .pike file, and you get back a program, which you can
instantiate to an object. That can then contain whatever functions you
like. Simple example:

program p = compile_string(sprintf(#"mixed foo() {
    return %s;
}", your_code));
mixed result = p()->foo();

ChrisA