digitalmars.D.learn - Embedding a DSL into D source
- Justin Johansson (23/23) Oct 12 2009 I'm working on a mini domain specific language (called say, MyDSL) which...
- Bill Baxter (18/26) Oct 12 2009 compiles to
I'm working on a mini domain specific language (called say, MyDSL) which compiles to (generates) D code and am thinking that it might be nice to be able to embed MyDSL statements directly in a D source file in manner similar to string mixins. It's use might look something like this: void foo() { MyDSLResult res = mixin( mydsl( `some-mydsl-statement-here`); now_do_something_with( res); } There was a question similar to this somewhere around DM newsgroups a while back (regarding embedding Scheme I think?), but as I recall there wasn't any definitive feedback on the idea. Also, being still relatively new to D, there are a number of possibly related threads on the NGs that have gone over my head, so please bear with me if this has been dealt with before. My specific question is: What would be the best way of embedding some DSL code into D source code? Is there some cool feature of D (that I don't yet know about) that lets you employ string mixins for this purpose, or would one need to write a preprocessor to grok D source code in order to compile in the embedded DSL code? The alternative is just to compile/translate MyDSL code into a standalone D source module and just run with that .. though the idea of actually embedding directly into D does have some real appeal (at least to me). Hope this makes sense and, as usual, thanks for all help, -- Justin Johansson
Oct 12 2009
On Mon, Oct 12, 2009 at 8:24 AM, Justin Johansson <see-body-of-message-for-name adam.com.au> wrote:I'm working on a mini domain specific language (called say, MyDSL) which =compiles to(generates) D code and am thinking that it might be nice to be able to em=bed MyDSL statementsdirectly in a D source file in manner similar to string mixins. =A0It's u=se might look something like this:void foo() { =A0 =A0MyDSLResult res =3D mixin( mydsl( `some-mydsl-statement-here`); =A0 now_do_something_with( res); }In theory that can work, and I think there have been some proofs of concept along those lines. But the problem is that you have to manage to write a translator for your DSL using only the subset of D that works at compile time. And I believe there are still memory leaks in D's CTFE engine. So if CTFE expressions get too complex the compiler will slow to a crawl and eventually crash. Ah yes, here it is: http://d.puremagic.com/issues/show_bug.cgi?id=3D1382 I don't know of any other way of implementing a DSL in D, other than using a standard run-time parser tool, which is probably the more sane way to go here. Unless your DSL is really simple or you just love a challenge. --bb
Oct 12 2009