How to implement data execution (as if it were script source code) [closed]

[亡魂溺海] 提交于 2020-01-06 14:10:46

问题


Suppose there is a script language called ScriptCode with the capabilities of execute code in same language.

//This ficticius program executes a simple constant code.

main()
{
    ScriptCode sc=new ScriptCode ( "print \"Hello\"");

    execute(sc); 

    print (" world");
}

//This ficticius program would read 10 programs from stdin and then execute them all.

main()
{

     ScriptCode programs[10] 

     String input;

     for(int i=0;i<10; i++)   
      {
          input =readInput();
          program[i] = new ScriptCode(input);
      }


   for( SriptCode p : programs)
       execute( p );

}

The question is:

Which existent program language could "ScriptCode" be ? or How could this kind of software be implemented?

(I hope it's not LISP the only choice, but if so.. I would have to face it.)

Thanks


回答1:


your execute routine is traditionally called eval. the wikipedia article on eval lists many languages that have an "eval", including JavaScript, ActionScript, Lisp, Perl, PHP, Lua, PostScript, Python, D, ColdFusion, REALbasic, Ruby, Forth, VBScript, Visual Basic for Applications, and Smalltalk. SQL is also mentioned in comments below (thanks).

typically, the implementation uses the same code as the language itself (to reduce duplication). so interpreted languages invoke the interpreter and compiled languages invoke the compiler. since the interpreter must be included with interpreted programs, but a compiler is often not included with compiled ones, this functionality is more common in interpreted languages.




回答2:


something like:

#!/bin/bash 
# ohai, I'm bash, this is test
i=3
while ((i--)); do
    read -ra prog
    ${prog[@]}
done

get a command, run it. test case

$ bash test
echo foo
foo
mpc play
65daysofstatic - The Conspiracy of Seeds
printf %s\n heh
heh

I guess, on some extent, most interpreted languages would fit.

now if input is a file, you can even compile it and run the executable, even with simple C programs.

What are the rules of this game ?




回答3:


Smalltalk answer:

Compiler evaluate: '3 + 4'

==> 7



来源:https://stackoverflow.com/questions/10253115/how-to-implement-data-execution-as-if-it-were-script-source-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!