custom AST library

Alex_Dovhal alex_dovhal at yahoo.com
Tue Dec 21 13:29:12 PST 2010


"Andrej Mitrovic" <andrej.mitrovich at gmail.com> wrote:
> That's why I think it might be a better idea to simply use "DSL
> blocks" throughout your code. Instead of having to mixin each
> template, you would only have one template where you have all the
> rules for your DSL:
>
> float a = 1 ;
> int n = 10 ;
> // more D code..
>
> mixin(MyDSL!(q{
>    // Here you use the syntax defined by the "MyDSL" language
>    float x = sum!(i=0:10; a*i) ;
>    float y = sum!(i=1:n; i*prod!(j=0:i ; i+j);
>    // More MyDSL code..
> }));
>
> // Here you use D syntax again, and D will know about float x and float y.
>
> Not to mention that you could potentially have some control over
> syntax highlighting (if you have a capable editor :p). For example you
> could write a little script in VIM which detects the use of "MyDSL!()"
> template, and highlights the code inside the block differently than
> the one outside the block.

Very interesting idea :), one note - sum!(...) in DSL is not template any 
more, so one can change a design as he wants, say:

mixin(MyDSL_Impl(q{
    float x = sum {i=0:10 ; a * i};
    float y = sum {i=1:n; i*prod{j=0:i; i+j}} ;
}));

this MyDSL_Impl only scans for "name" "{" text "}" and replaces it with 
mixin(nameImpl(q{text})) ;

User can put entire function body or even almost full module (if don't care 
about compile time) into that DSL in order not to change from DSL to normal 
code and back. One more thing to think about - how to show correct error 
line number in there? 




More information about the Digitalmars-d mailing list