[Issue 1255] operator overloading

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jun 3 17:18:32 PDT 2007


http://d.puremagic.com/issues/show_bug.cgi?id=1255





------- Comment #1 from wbaxter at gmail.com  2007-06-03 19:18 -------
As Jarrett pointed out in the NG follow-up, porting Spirit line-by-line is
undoutedly the wrong way to go about it.

With spirit you can have the syntax:
    boost::spirit::rule<ScannerT> factor, term;
    term = factor >> *( ( '*' >> factor) | ( '/' >> factor ) );

Which admittedly looks pretty similar to BNF.
But with the compile time text processing stuff that's been going into D you
can just use the BNF:
    mixin(Rule!(
        "term ::= factor ( ( '*' factor ) | ( '/' factor ) )*"
        ));
And maybe eventually the 'mixin' part won't be necessary either, but for now it
is.

Operator overloading abuse is cute, but it's just working around the lack of
ability in C++ to do more generic compile-time processing.  The biggest issue
with operator overloading for this kind of thing is that you're stuck with the
built-in precedence rules and operators (*prefix* kleen star?)

One approach might be to build a standard API-based syntax for a
parser-generator first (like C# ports of boost -
http://www.codeproject.com/csharp/spart.asp).  Then use the compile time string
facilities in D to translate BNF strings to that API.

Just a thought.
But in any event, I can tell you for sure that "I want to use unary * as a
Kleen star operator so D needs to have opDeref()" is not an argument that will
gain any traction with Walter.  He's a firm believer in operator overloads not
changing the semantics of the underlying operator.


-- 



More information about the Digitalmars-d-bugs mailing list