
How to parse text for a DSL at compile time? (C++) - Cieplak
https://stackoverflow.com/questions/17783393/how-to-parse-text-for-a-dsl-at-compile-time
======
dom96
It seems like doing this in C++ is overly complex. In language's like Nim
where you can just executed ordinary code at compile-time it would be far
simpler.

~~~
ndh2
That's what the OP meant with "Yes. That's right." In C++ circles, this
loosely translates to "Let's use this ridiculously unwieldy if not downright
stupid mechanism to do something just because we can".

~~~
dman
I know it is fashionable to bash on C++ lately but I dont fully understand the
motivation behind it. Could you please elaborate what drove you to write a
post expressing your derision about C++? Does the frustration come from trying
to use the language in the past and running into its limitations?

~~~
0xFFC
I don't know about the original comment writer. But my frustration comes
because of overly complex, while there would be simpler way to do that.

~~~
jcelerier
what would be the simpler way to translate any string in a given DSL in an
expression at compile time in C++ ?

~~~
Arelius
'const char* transformSrc_MyExpr(const char* src);'

We're the compiler to invoke some function like that at compile time, it would
be much more simple.

I'm not suggesting this exactally, or even something very similar, but serves
as an example.

------
kazinator
In practice, what you would do is write a preprocessing/code-generating _tool_
that runs in the build environment, as part of the toolchain.

~~~
Const-me
+1.

In Visual Studio, a good built-in mechanism for that is T4 text templates.

------
Doxin
D has this use case solved in a really nice way. All you have to do is write a
function that converts the DSL into D code and then do something like
`mixin(my_dsl_converter('foobar'))`.

------
stargrazer
This may not be quite what the original poster is looking for, but
boost::spirit allows one to build DSLs directly in the code. The DSL is then
used to parse constructs and execute the results.

