Compile time executable calling?

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Jul 15 11:21:52 PDT 2013


On Mon, Jul 15, 2013 at 07:54:56PM +0200, Tofu Ninja wrote:
> On Monday, 15 July 2013 at 17:49:04 UTC, H. S. Teoh wrote:
> >On Mon, Jul 15, 2013 at 07:35:44PM +0200, Tofu Ninja wrote:
> >[...]
> >>The use cases I am more interested in are not possible with make.
> >>Having the ability to pass the arguments from within the language
> >>itself allows you to define your use cases inline instead of having
> >>to separately define the use case outside. Something where you would
> >>have many different points in the code that needed different input
> >>from what ever executable you were calling. With out ctec you would
> >>have to maintain a list of every time you needed something generated
> >>from the outside and add that to your make which is error prone.  I
> >>think it is in these cases where the ctec would be the most useful
> >>as it would remove a lot of the burden of keeping track of all the
> >>pregenerated code.
> >
> >You mean, you want to do something like this?
> >
> >	static inputValues = [
> >		"data1", "data2", "data3"
> >	];
> >
> >	static foreach (val; inputValues) {
> >		import exec("prog", val);
> >	}
> >
> >I can see why that could be useful.
> >
> >
> >T
> Or something more complex where a template generates its code based
> on the output of an executable, so in that case the template args
> would be passed into the executable call.

Hmm. Let's see:

	module dmd;
	static grammar = import exec("wget", "http://dlang.org/spec");
	static c_code = import exec("yacc", grammar);
	void main() {
		import exec("d-ify", c_code);
	}

Hey look, we just solved the problem of rewriting DMD in D, *and* the
problem of the spec being out-of-sync with the compiler! :-P

On a more serious note, though, if I ever needed a template of the sort
you're describing, I'd prefer to use CTFE to generate it (and
fix/improve CTFE if for whatever reason it can't yet do what I want it
to do).

The problem with invoking an external program to produce template code
is that you have to somehow serialize the template arguments so that you
can pass them to the program, and the program has to somehow deserialize
them and reconstruct what the compiler has already constructed. Whereas
if you did everything in CTFE, you have access to whatever the compiler
has internally built to describe the types and values.  Serializing /
deserializing these structures generally are a big hiding place for
bugs; I'd imagine that CTFE, being part of the compiler, is far more
reliable at interpreting these structures correctly.

Besides, I'd hate for my D code to have opaque blocks of import exec()
where I have to examine another program to figure out exactly what's
being compiled, instead of just reading the source code.

So I'm still kinda on the fence about this whole thing (ignoring, of
course, the major security concerns involving having import exec() in
the first place).

What I really want to see, is for CTFE to improve to the point where you
can essentially write an arbitrary program that runs in CTFE to produce
whatever code you wish to produce for the executable, thus making import
exec() redundant.


T

-- 
MASM = Mana Ada Sistem, Man!


More information about the Digitalmars-d mailing list