www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - The X Macro

reply Walter Bright <newshound2 digitalmars.com> writes:
http://www.drdobbs.com/blog/archives/2010/06/the_x_macro.html

Even though D doesn't have a text macro preprocessor, this can be done using 
string mixins and a bit of CTFE.
Jun 24 2010
next sibling parent reply "Yao G." <nospamyao gmail.com> writes:
http://www.reddit.com/r/programming/comments/ciq5w/the_x_macro/

I hope I don't screw up this time  :D

On Thu, 24 Jun 2010 17:10:35 -0500, Walter Bright  
<newshound2 digitalmars.com> wrote:

 http://www.drdobbs.com/blog/archives/2010/06/the_x_macro.html

 Even though D doesn't have a text macro preprocessor, this can be done  
 using string mixins and a bit of CTFE.
-- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jun 24 2010
next sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
Yao G. wrote:
 http://www.reddit.com/r/programming/comments/ciq5w/the_x_macro/
 
 I hope I don't screw up this time  :D
Autobanned again, sigh.
Jun 24 2010
parent reply "Yao G." <nospamyao gmail.com> writes:
  :'(

On Thu, 24 Jun 2010 18:55:52 -0500, Walter Bright  
<newshound2 digitalmars.com> wrote:

 Yao G. wrote:
 http://www.reddit.com/r/programming/comments/ciq5w/the_x_macro/
  I hope I don't screw up this time  :D
Autobanned again, sigh.
-- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jun 24 2010
parent reply Walter Bright <newshound2 digitalmars.com> writes:
Yao G. wrote:
  :'(
 
 On Thu, 24 Jun 2010 18:55:52 -0500, Walter Bright 
 <newshound2 digitalmars.com> wrote:
 
 Yao G. wrote:
 http://www.reddit.com/r/programming/comments/ciq5w/the_x_macro/
  I hope I don't screw up this time  :D
Autobanned again, sigh.
Why don't we just give up on reddit and go with http://news.ycombinator.com/ ?
Jun 24 2010
parent reply Bernard Helyer <b.helyer gmail.com> writes:
On Thu, 24 Jun 2010 19:09:36 -0700, Walter Bright wrote:

 Yao G. wrote:
  :'(
 
 On Thu, 24 Jun 2010 18:55:52 -0500, Walter Bright
 <newshound2 digitalmars.com> wrote:
 
 Yao G. wrote:
 http://www.reddit.com/r/programming/comments/ciq5w/the_x_macro/
  I hope I don't screw up this time  :D
Autobanned again, sigh.
Why don't we just give up on reddit and go with http://news.ycombinator.com/ ?
I prefer the reddit community, and the way reddit handles conversation threading. Perhaps you could email the Reddit guys and see why they keep getting autobanned?
Jun 24 2010
parent Walter Bright <newshound2 digitalmars.com> writes:
Bernard Helyer wrote:
 On Thu, 24 Jun 2010 19:09:36 -0700, Walter Bright wrote:
 Why don't we just give up on reddit and go with
 http://news.ycombinator.com/ ?
I prefer the reddit community, and the way reddit handles conversation threading. Perhaps you could email the Reddit guys and see why they keep getting autobanned?
I did - no reply.
Jun 24 2010
prev sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
Yao G. wrote:
 http://www.reddit.com/r/programming/comments/ciq5w/the_x_macro/
Even with it banned, it has gotten 5 downvotes, so it must be lurkers here. Any downvoters care to say what's wrong with it?
Jun 24 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Walter Bright:
 Even with it banned, it has gotten 5 downvotes, so it must be lurkers here.
Any 
 downvoters care to say what's wrong with it?
I don't know. Maybe they think C is not "cool". There is also this, they can be more interested: http://www.reddit.com/r/C_Programming/ Bye, bearophile
Jun 25 2010
prev sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Walter Bright Wrote:

 Yao G. wrote:
 http://www.reddit.com/r/programming/comments/ciq5w/the_x_macro/
Even with it banned, it has gotten 5 downvotes, so it must be lurkers here. Any downvoters care to say what's wrong with it?
I've upvoted. But I'm pretty sure you're supossed to use curly brackets when initializing that static char array there. ;)
Jun 25 2010
parent Walter Bright <newshound2 digitalmars.com> writes:
Andrej Mitrovic wrote:
 But I'm pretty sure you're supossed to use curly brackets when initializing
that static char array there. ;)
Gaaah, how embarrassing! Fixed.
Jun 25 2010
prev sibling next sibling parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
On 06/24/2010 05:10 PM, Walter Bright wrote:
 Even though D doesn't have a text macro preprocessor, this can be done
 using string mixins and a bit of CTFE.
an example, taken to horrible extremes: http://www.dsource.org/projects/dexcelapi/browser/trunk/src/dxl/biff/Enums.d
Jun 24 2010
prev sibling next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Walter Bright:
 Even though D doesn't have a text macro preprocessor, this can be done using 
 string mixins and a bit of CTFE.
Static introspection to the rescue! :-) Bye, bearophile
Jun 24 2010
prev sibling next sibling parent reply MIURA Masahiro <echochamber gmail.com> writes:
On 06/25/2010 07:10 AM, Walter Bright wrote:
 http://www.drdobbs.com/blog/archives/2010/06/the_x_macro.html
Interesting technique that I haven't heard of. However one of my friends, a lead programmer at Sega, says they use similar technique extensively in C++; actually they avoid overusing it, as it slows down compilation considerably. And for this particular Color case, enum in D can have a base type: enum Color: string { RED = "red", GREEN = "green", BLUE = "blue", } writeln(Color.RED); // gives "red" so we don't have that maintainance problem :-)
Jun 24 2010
parent Walter Bright <newshound2 digitalmars.com> writes:
MIURA Masahiro wrote:
 On 06/25/2010 07:10 AM, Walter Bright wrote:
 http://www.drdobbs.com/blog/archives/2010/06/the_x_macro.html
Interesting technique that I haven't heard of. However one of my friends, a lead programmer at Sega, says they use similar technique extensively in C++; actually they avoid overusing it, as it slows down compilation considerably.
It shouldn't. I wonder what they are doing, or if perhaps they just have a miserably slow macro processor.
Jun 24 2010
prev sibling next sibling parent Jeff Merrick <merrick.jd gmail.com> writes:
At the company I work for we call these "macro maps" and we always pass the X
macro as a parameter.
Macro maps are common knowledge here because they are used fairly extensively;
in the filesystem alone I
found 16 instances of the technique.

The token pasting that's often used in conjunction with macro maps often makes
it difficult to find the
definition of the constructed token, but as far as keeping different tables and
declarations in sync with one
another, I've yet to find a good alternative in C.
Jun 25 2010
prev sibling next sibling parent reply Justin Johansson <no spam.com> writes:
Walter Bright wrote:
 http://www.drdobbs.com/blog/archives/2010/06/the_x_macro.html
 
 Even though D doesn't have a text macro preprocessor, this can be done 
 using string mixins and a bit of CTFE.
Very cool article. Thanks Walter, I'll try to remember to pass that on to my grandkids :-) Also on that DDJ page I saw the ad for the Samsung Bada developer challenge. http://developer.bada.com/challenge/index.do There is some decent $prize money on offer here. Of course you have to use the Samsung Bada SDK which I understand is C++. Naturally we would all delight in one of the winning entires being some Bada app written in D. Now thinking about a killer app for D, me thinks it doesn't really matter what that app really does or if even it is really lame. The real kill would come from a D app winning a prize in the Bada Developer Challenge. So wadda you reckon?
Jun 25 2010
parent reply "Rory McGuire" <rmcguire neonova.co.za> writes:
On Fri, 25 Jun 2010 15:11:53 +0200, Justin Johansson <no spam.com> wrote:

 Walter Bright wrote:
 http://www.drdobbs.com/blog/archives/2010/06/the_x_macro.html
  Even though D doesn't have a text macro preprocessor, this can be done  
 using string mixins and a bit of CTFE.
Very cool article. Thanks Walter, I'll try to remember to pass that on to my grandkids :-) Also on that DDJ page I saw the ad for the Samsung Bada developer challenge. http://developer.bada.com/challenge/index.do There is some decent $prize money on offer here. Of course you have to use the Samsung Bada SDK which I understand is C++. Naturally we would all delight in one of the winning entires being some Bada app written in D. Now thinking about a killer app for D, me thinks it doesn't really matter what that app really does or if even it is really lame. The real kill would come from a D app winning a prize in the Bada Developer Challenge. So wadda you reckon?
what processor/s are we talking about? I'm guessing ARM. GDC is a bit out dated isn't it? (GDC being the only D compiler that might support ARM processors) -Rory
Jun 25 2010
parent reply Justin Johansson <no spam.com> writes:
Rory McGuire wrote:
 On Fri, 25 Jun 2010 15:11:53 +0200, Justin Johansson <no spam.com> wrote:
 
 Walter Bright wrote:
 http://www.drdobbs.com/blog/archives/2010/06/the_x_macro.html
  Even though D doesn't have a text macro preprocessor, this can be 
 done using string mixins and a bit of CTFE.
Very cool article. Thanks Walter, I'll try to remember to pass that on to my grandkids :-) Also on that DDJ page I saw the ad for the Samsung Bada developer challenge. http://developer.bada.com/challenge/index.do There is some decent $prize money on offer here. Of course you have to use the Samsung Bada SDK which I understand is C++. Naturally we would all delight in one of the winning entires being some Bada app written in D. Now thinking about a killer app for D, me thinks it doesn't really matter what that app really does or if even it is really lame. The real kill would come from a D app winning a prize in the Bada Developer Challenge. So wadda you reckon?
what processor/s are we talking about? I'm guessing ARM. GDC is a bit out dated isn't it? (GDC being the only D compiler that might support ARM processors) -Rory
ARM is probably right. ARM, handhelds, mobile etc is where all the action is at the moment. You are dead right about GDC; it's dead and nowhere near D2. D (D2) needs a marketing and rollout plan. One good such plan would embrace ARM, handhelds, mobile etc. Hint, hint. Golden (marketing) opportunities like this do not come around very often.
Jun 25 2010
parent reply "Rory McGuire" <rmcguire neonova.co.za> writes:
On Fri, 25 Jun 2010 15:35:30 +0200, Justin Johansson <no spam.com> wrote:

 Rory McGuire wrote:
 On Fri, 25 Jun 2010 15:11:53 +0200, Justin Johansson <no spam.com>  
 wrote:

 Walter Bright wrote:
 http://www.drdobbs.com/blog/archives/2010/06/the_x_macro.html
  Even though D doesn't have a text macro preprocessor, this can be  
 done using string mixins and a bit of CTFE.
Very cool article. Thanks Walter, I'll try to remember to pass that on to my grandkids :-) Also on that DDJ page I saw the ad for the Samsung Bada developer challenge. http://developer.bada.com/challenge/index.do There is some decent $prize money on offer here. Of course you have to use the Samsung Bada SDK which I understand is C++. Naturally we would all delight in one of the winning entires being some Bada app written in D. Now thinking about a killer app for D, me thinks it doesn't really matter what that app really does or if even it is really lame. The real kill would come from a D app winning a prize in the Bada Developer Challenge. So wadda you reckon?
what processor/s are we talking about? I'm guessing ARM. GDC is a bit out dated isn't it? (GDC being the only D compiler that might support ARM processors) -Rory
ARM is probably right. ARM, handhelds, mobile etc is where all the action is at the moment. You are dead right about GDC; it's dead and nowhere near D2. D (D2) needs a marketing and rollout plan. One good such plan would embrace ARM, handhelds, mobile etc. Hint, hint. Golden (marketing) opportunities like this do not come around very often.
found this about LDC. Do you know if there are plans for D2 on LLVM. http://comments.gmane.org/gmane.comp.compilers.llvm.ldc/1 -Rory
Jun 26 2010
parent Justin Johansson <no spam.com> writes:
Rory McGuire wrote:
 On Fri, 25 Jun 2010 15:35:30 +0200, Justin Johansson <no spam.com> wrote:
 
 Rory McGuire wrote:
 On Fri, 25 Jun 2010 15:11:53 +0200, Justin Johansson <no spam.com> 
 wrote:

 Walter Bright wrote:
 http://www.drdobbs.com/blog/archives/2010/06/the_x_macro.html
  Even though D doesn't have a text macro preprocessor, this can be 
 done using string mixins and a bit of CTFE.
Very cool article. Thanks Walter, I'll try to remember to pass that on to my grandkids :-) Also on that DDJ page I saw the ad for the Samsung Bada developer challenge. http://developer.bada.com/challenge/index.do There is some decent $prize money on offer here. Of course you have to use the Samsung Bada SDK which I understand is C++. Naturally we would all delight in one of the winning entires being some Bada app written in D. Now thinking about a killer app for D, me thinks it doesn't really matter what that app really does or if even it is really lame. The real kill would come from a D app winning a prize in the Bada Developer Challenge. So wadda you reckon?
what processor/s are we talking about? I'm guessing ARM. GDC is a bit out dated isn't it? (GDC being the only D compiler that might support ARM processors) -Rory
ARM is probably right. ARM, handhelds, mobile etc is where all the action is at the moment. You are dead right about GDC; it's dead and nowhere near D2. D (D2) needs a marketing and rollout plan. One good such plan would embrace ARM, handhelds, mobile etc. Hint, hint. Golden (marketing) opportunities like this do not come around very often.
found this about LDC. Do you know if there are plans for D2 on LLVM. http://comments.gmane.org/gmane.comp.compilers.llvm.ldc/1 -Rory
"Do you know if there are plans for D2 on LLVM?" I think others would know better than myself though I have previously surmised from this ng that D implementation for LLVM is waning at the moment.
Jun 26 2010
prev sibling next sibling parent BCS <none anon.com> writes:
Hello Walter,

 http://www.drdobbs.com/blog/archives/2010/06/the_x_macro.html
 
 Even though D doesn't have a text macro preprocessor, this can be done
 using string mixins and a bit of CTFE.
 
I did more or less the same thing to expand vector equations to there scalar components. But for some reason I needed two passes through cpp and a run through grep/sed (I think cpp was refusing to expand a macro on the correct side of another macro. -- ... <IXOYE><
Jun 26 2010
prev sibling parent KlausO <oberhofer users.sf.net> writes:
I did something similar some time ago:


import std.stdio;

struct EnumDescription
{
   char[] name;
   char[] description;
};

//
// generates an enum, an associated string table and a "save" lookup
// function which determines the string which belongs to an given enum
//
char[] generate_enum(char[] enumname, EnumDescription[] list)
{
   // create enum declaration
   char result[] = "enum " ~ enumname ~ "{";
   foreach(entry; list)
   {
     result ~= entry.name ~ ",";
   }
   result ~= "};";

   // create description array
   result ~= `private static char[][` ~ enumname ~ `.max + 1] s_` ~ 
enumname ~ `ToString =
[`;
   foreach(entry; list)
   {
     result ~= enumname ~ "." ~ entry.name ~ ` : "` ~ entry.description 
~ `",`;
   }
   result ~= "];";

   // create save "toString" function
   result ~= `static char[] ` ~ enumname ~ `ToString(` ~ enumname ~ ` type)
{
   if (type <= ` ~ enumname ~ `.max)
   {
     return s_` ~ enumname ~ `ToString[type];
   }
   return "<invalid enum, no description available>";
}`;

   return result;
}

const EnumDescription[] ErrorList =
[
   { "INTERNAL_ERROR"       , "internal error" },
   { "ALLOCATION_ERROR"     , "allocation error" },
   { "INTEGER_EXPECTED"     , "integer expected" },
];

mixin(generate_enum("ErrorType", ErrorList));


void main()
{
   writefln(ErrorType.ALLOCATION_ERROR, " : ", 
ErrorTypeToString(ErrorType.ALLOCATION_ERROR));
}


Greets

KlausO



Walter Bright schrieb:
 http://www.drdobbs.com/blog/archives/2010/06/the_x_macro.html
 
 Even though D doesn't have a text macro preprocessor, this can be done 
 using string mixins and a bit of CTFE.
Jun 28 2010