www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - mixins and compile-time coding take 2

reply janderson <askme me.com> writes:
Although not my favorite idea (when compared to my static idea) here's 
another crazy compile-time idea.

What about a subset of compile time code (kinda like a scripting 
language, DMDScript could be used I guess, although D is just fine for 
me)? Execute would compile and run the code given.  You would also be 
able to pass in parameters into execute which:

Maybe the syntax needs a little work.

mixin(execute(T ...)(" return T[0]", "int foo = 5;"));  //Results in 
"int foo = 5;"

or this way so you can alias the execute:

mixin(execute(T ...)(" return T[0]")( "int foo = 5;") ~
	execute(T ...)(" return T[0]")( "int foo2 = 15;")
);

would translate to:

alias execute(T ...)(" return T[0]") fooExecute;

mixin(fooExecute("int foo = 5;") ~ fooExecute("int foo2 = 15;"));

An extension to this syntax since it really is a form of template:

template fooExecute(T...)
{
	"return T[0]"
}

Maybe values could be passed by name so that you could for instance 
write some something like:

template fooExecute(T...)
{
   //ie T[0] could be "return" and T[1] could be "int foo = 5"
	"T[0] \"T[1]\""
}

//Ok I guess that's only one change away from my static suggestion
template execute(T...)
{
	char [] foo(T t) //Detect at compile time that this uses the minimum 
set of allowable commands
	{
		return T[0];
	}
}


-Joel
Feb 07 2007
parent janderson <askme me.com> writes:
janderson wrote:
 Although not my favorite idea (when compared to my static idea) here's 
 another crazy compile-time idea.
 
 What about a subset of compile time code (kinda like a scripting 
 language, DMDScript could be used I guess, although D is just fine for 
 me)? Execute would compile and run the code given.  You would also be 
 able to pass in parameters into execute which:
 
 Maybe the syntax needs a little work.
 
 mixin(execute(T ...)(" return T[0]", "int foo = 5;"));  //Results in 
 "int foo = 5;"
 
 or this way so you can alias the execute:
 
 mixin(execute(T ...)(" return T[0]")( "int foo = 5;") ~
     execute(T ...)(" return T[0]")( "int foo2 = 15;")
 );
 
 would translate to:
 
 alias execute(T ...)(" return T[0]") fooExecute;
 
 mixin(fooExecute("int foo = 5;") ~ fooExecute("int foo2 = 15;"));
 
 An extension to this syntax since it really is a form of template:
 
 template fooExecute(T...)
 {
     "return T[0]"
 }
 
 Maybe values could be passed by name so that you could for instance 
 write some something like:
 
 template fooExecute(T...)
 {
   //ie T[0] could be "return" and T[1] could be "int foo = 5"
     "T[0] \"T[1]\""
 }
 
 //Ok I guess that's only one change away from my static suggestion
 template execute(T...)
 {
     char [] foo(T t) //Detect at compile time that this uses the minimum 
 set of allowable commands
     {
         return T[0];
     }
 }
 
 
 -Joel

Ok maybe scratch the named arguments part as it means that the code can't be cached very easily and it'll have to be compiled for every reference. Pass by value is still feasible though. -JOel
Feb 07 2007