Am I evil for this?

Max H maxhaton at gmail.com
Fri Oct 14 20:49:35 UTC 2022


On Friday, 14 October 2022 at 06:59:43 UTC, Walter Bright wrote:
> On 10/13/2022 7:44 PM, H. S. Teoh wrote:
>>> and also a project he saw in the past that overloaded C++ 
>>> operators to
>>> make a regex DSL.
>> [...]
>> 
>> That would be Boost.Xpressive, I think.
>
> Here it is:
>
> #include <boost/spirit.hpp>
> using namespace boost;
> int main() {
>     spirit::rule<> group, fact, term, expr;
>     group   = '(' >> expr >> ')';
>     fact    = spirit::int_p   | group;
>     term    = fact >> *(('*' >> fact) | ('/' >> fact));
>     expr    = term >> *(('+' >> term) | ('-' >> term));
>     assert( spirit::parse("2*(3+4)", expr).full );
>     assert( ! spirit::parse("2*(3+4", expr).full );
> }
>
> https://studylib.net/doc/10029968/text-processing-with-boost---northwest-c---users--group
>
> slide 40
>
> It's a technical marvel, but the embedded DSL looks like C++ 
> expressions yet does something completely different. One cannot 
> distinguish the C++ code from the DSL code. It's in the same 
> category as macros, and I can't recommend it.

Potentially controversial response: Who cares? The alternative in 
D is basically just the same code with some quotes around it (a 
DSL like pegged).

You can do real sin with operating overloading, or just badly 
chosen operators, but this particular one really isn't that bad. 
The alternative is preprocessor macros, which are considerably 
worse.


More information about the Digitalmars-d mailing list