eval() (Was: One more update on d-programming-language.org)
Nick Sabalausky
a at a.a
Wed Sep 15 13:19:03 PDT 2010
"Philippe Sigaud" <philippe.sigaud at gmail.com> wrote in message
news:mailman.214.1284496545.858.digitalmars-d at puremagic.com...
> On Tue, Sep 14, 2010 at 17:01, Andrei Alexandrescu <
> SeeWebsiteForEmail at erdani.org> wrote:
>>
>> I think we'll move forward with that one. I'll start working on the
> content. Ideas for good tutorial examples?
>>
>
> Maybe more examples of what's interesting me right now than tutorials, but
> who knows?
>
> * executing command-lines instructions from a D program. Theme: file I/O,
> OS
> interaction.
> An interesting example is having a script that loads a D file, modifies
> its
> source, asks for DMD to compile it and then runs it and get its result. It
> could be the first brick to get a REPL / idmd (interactive dmd). Maybe
> more
> a [challenge] subject than a tutorial example?
>
One thing that could be used for that is the eval() function I've recently
added to my SemiTwist D Tools library:
http://www.dsource.org/projects/semitwist/browser/trunk/src/semitwist/util/process.d
Example:
-----------------------------
import semitwist.util.all;
void main()
{
auto x = eval!int(q{ return 21 * 2; }); // String can be
runtime-generated
assert(x == 42);
eval!void( q{ writeln("Hello World"); }, q{ import std.stdio; } );
}
-----------------------------
It does require dmd and rdmd to be on the path.
I hadn't posted anything about it before because it's still
rough-around-the-edges and needs polish. For instance, most of the useful
phobos modules aren't imported by default, and it doesn't yet support
returning string/wstring/dstring - you have to return char[]/wchar[]/dchar[]
instead and then convert back to string/wstring/dstring (but that shouldn't
be too hard to fix). Also, on Windows it requires a patched version of rdmd,
which is included with the library (
http://www.dsource.org/projects/semitwist/browser/trunk/rdmdAlt.d ) but eval
doesn't yet compile it if it isn't already compiled, and it assumes it's on
the path (I've already solved both of these in the included stbuild program,
I just need to move the solution over into the general library). My ultimate
goal with this is to hack up DMD just enough to make it work at compile-time
(so that *any* arbitrary code can be run at compile-time, albiet more
awkwardly and with much more overhead than ordinary CTFE).
More information about the Digitalmars-d
mailing list