Scripting again.

Daniel Gibson metalcaedes at gmail.com
Thu Dec 9 15:47:45 PST 2010


%u schrieb:
> == Quote from Daniel Gibson (metalcaedes at gmail.com)'s article
>> %u schrieb:
>>> == Quote from so (so at so.do)'s article
>>>>> Doesn't this work??
>>>>>
>>>>> import std.file;
>>>>> import std.script;
>>>>>
>>>>> Script s = new Script(cast(ubyte[]) read("script1.d"));
>>>>> s.compile();
>>>>> s.execute();
>>>> What is this supposed to mean?
>>> This should be a bit more clear (untested code).
>>>
>>> --scrip1.d
>>> import std.stdio;
>>> writefln(i);
>>> ----
>>>
>>> --main.d
>>> import std.file;
>>> import std.script;
>>>
>>> void main(){
>>>   Script s = new Script(cast(ubyte[]) read("script1.d"));
>>>   assert(!s.validate);
>>>   int i = 1;
>>>   assert(s.validate);
>>>   s.compile();
>>>   s.execute(); // output 1
>>> }
>>> ----
>> That wouldn't be just a script but a runtime-string-mixin (or something like that).
> Yes, I forgot to change the s.execute to s.mixin :)
> 
>> I don't think something like that is feasible in a compiled language.
> how about something like this:
> 
> s.execute(i);
> Where in the script i will be the first arg in main?
> That way the compiler knows which variables to export.

I guess something like that is possible.

I'd prefer something like
s.call("main", i);
and also
s.call("foo", 42, x);
though - so you can define multiple functions in a script and call them.

Or maybe, with more magic and support within the D compiler
s.main(i);
s.foo(42,x);
But that means that the compiler needs to know the Script type, create a class 
for each script that contains the functions called on the script ("main", 
"foo"), create stubs for them that either do something like call("foo",i) or, if 
  no function foo with an appropriate argument is found in the script, throws an 
exception..
Probably possible, but certainly not easy.
The first suggestion however (s.call("foo", i)) should be possible without 
support in the D compiler, all the work would be done in the Script class. Still 
not trivial, of course, especially with arguments for called functions and their 
type etc.


More information about the Digitalmars-d mailing list