How to best implement a DSL?

Jacob Carlborg doob at me.com
Tue Jul 31 09:42:38 UTC 2018


On 2018-07-28 16:59, Robert M. Münch wrote:
> Hi, I'm seeking for ideas/comments/experiences how to best implement a
> DSL in D.
>
> What I would like to do is something like this:
>
>      ... my D code ...
>
>      my-dsl {
>          ... my multi-line DSL code ...
>          trade 100 shares(x) when (time < 20:00) and timingisright()
>      }
>
>
>      ... my D code ...
>
>
> Some things that circle in my head:
> * Can the D parser somehow be missued for a DSL? So I can skip all the
> generic features for types etc.?

Yes. But it would require your DSL to be syntactically valid D code (but 
not semantically). DMD can be used as a library but is lacking quite a 
bit i this regard. For example, you cannot get the original source code 
from an AST node because the AST node only stores where in the source 
code the node starts, not where it ends. Some AST nodes doesn't contain 
location information at all. Also, unless you want to write the DSL in a 
string literal you need to build some kind of pre-processor that 
converts the DSL to valid D code.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list