问题
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