Nice Inline Syntax for DSLs
Kristian Kilpi
kjkilpi at gmail.com
Tue Feb 20 05:47:04 PST 2007
On Sat, 17 Feb 2007 03:08:47 +0200, Tyler Knott <tywebmail at mailcity.com>
wrote:
> Knud Soerensen wrote:
>> On Fri, 16 Feb 2007 16:35:46 -0700, Russell Lewis wrote:
>> Well, we already have asm as a dsl.
>> Why not use a similar syntax like:
>> dslname {
>> ...
>> }
>> We just need a way to tell the compiler which passer to use for
>> dslname.
>
> I was thinking of a similar syntax as well, except it would look like
> this:
>
> # dsl(compilerFunc[, ...])
> # {
> # DSL GOES HERE
> # }
>
> With three restrictions:
> 1. All curly braces within the DSL must be balanced.
> 2. compilerFunc must be evaluable at compile time.
> 3. compilerFunc must use the signature char[] function(char[][, ...]).
>
> The code between the braces would be passed in to compilerFunc as the
> first argument, any arguments to the DSL statement after the complier
> function would be passed as the remaining arguments to the compiler
> function (like how opApply overloading works), and the return value of
> the function would be implicitly mixed in where the dsl declaration
> occurs. (This of course depends on Walter fixing compile-time
> evaluation of functions for const initialization, which should be done
> in the next release of DMD.)
I have proposed earlier an alternate way to mark string literals that
allows nesting etc
(http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=48307).
That is, I suggested that some special marks could be used to mark a part
of a source file as a string literal, just like /* */ marks a part of code
as a comment. I used the @{ }@ marks, but some other (unambiguous) ones
could also be used, of course (for instance, </ /> was proposed).
The benefit of using the @{ }@ syntax is you can get rid of the
restriction #1, and the syntax can be used anywhere where strings can be
used.
The 'dsl() {...}' syntax is nice, and that could be combined with the
@{ }@ syntax; that is, when you need to use unbalanced curly braces with
it. For example:
dsl(MyDSL) @{
a = 10;
...
}@
->
mixin(MyDSL!( "
a = 10;
...
"));
If my 'extension' (added later) to the @{ }@ syntax
(http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=49042)
would be possible, then DSLs will nicely be the part of the language, just
like the asm is. The $ character will cause a constant, an alias, a
(constant) function call, etc to be evaluated and inserted to the string.
For example:
int getVal() {return 200;}
dsl(MyDSL) {
a = $getVal();
...
}
->
mixin(MyDSL!( "
a = 200;
...
"));
So, in short, all the following cases:
dsl(X) {...}
dsl(X) @{...}@
dsl(X) "..."
would be an alternate way to write:
mixin(X!("..."));
(Optional arguments for X are left out for simplicity.)
More information about the Digitalmars-d
mailing list