AST like coding syntax. Easy upgrade!

Prudence via Digitalmars-d digitalmars-d at puremagic.com
Sun Sep 6 12:32:57 PDT 2015


template X(Y)
{
	string X = Y.stringof;
}

auto s = X({int 3;})

Of course, doesn't work!!

But having the ability to pass code that isn't contained in a 
string is very useful!!


1. A new code keyword, similar to alias. Can only be used as 
template parameters. If you are worried about backwards 
compatibility, call it _code, or __code, or __code__... I don't 
care, only sticks and stones hurt me.

2. The argument is only checked for syntaxical errors as the 
argument. It obviously will be checked. If we allow for other 
types of code besides D, then this won't work. We could extend 
code to allow for other languages(which would require their 
syntax checking algorithms): code(python).

e.g.,

template PythonParser X(code(python) pythoncode)
{
     // Parses pythoncode and coverts it into D or passes it to 
the python interpreter or whatever...
}

In this case, D will know it is python code, check for a python 
code parser and deal with it(use an external lib to check it or 
have built in grammars for such things).

No code is ever actually executed by this extension in the 
compiler, so it's somewhat of a trivial addition. All the 
processing is added by the programmer.

3. stringof returns the code as a string. e.g. 
pythoncode.stringof is simply the string representation of the 
block of code passed.


This complements D's mixin mechanisms by getting out of having to 
put code in strings, which are nearly a clusterfuck for complex 
code.



4. This opens the door to allow for one to migrate code easier to 
D. Suppose you have to transfer a large code base in, say, 
javascript. Instead of having to convert it all by hand, you 
could have something like


template IncludeJS(code(javscript))
{
    magiccookie(javscript.stringof);
}

... converts and integrates the javascript code into D or simply 
interprets it and returns the error code, or whatever. It's not 
unfeasible to think that someone could write the magiccookie that 
brings in all the objects, functions, and such into D to be 
consumed in D code. I've seen this done for several scripting 
languages such as lua and js.




More information about the Digitalmars-d mailing list