Changeset 215 for trunk/meta/demo

Show
Ignore:
Timestamp:
07/11/06 04:02:33 (2 years ago)
Author:
Don Clugston
Message:

Simpler beer!

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/meta/demo/beer.d

    r126 r215  
    22// The "99 bottles of beer" song, using _only_ the template 
    33// metaprograming facilities of D. No executable is generated. 
    4 // No libraries are used. 
     4// No libraries are used (though the itoa!() metafunction is part of the 
     5// 'meta' metaprogramming library). 
    56// Compile with dmd -c -o- beer.d 
    67// Illustrates template default values, template string value parameters, 
    78// compile-time concatentation of constant strings, static if. 
    89 
    9 import meta.conv; 
     10template decimaldigit(int n) { const char [] decimaldigit = "0123456789"[n..n+1]; } 
    1011 
    11 const bool CAPITAL = true; 
     12template itoa(ulong n) 
     13
     14    static if ( n < 10L ) 
     15        const char [] itoa = decimaldigit!(n); 
     16    else 
     17        const char [] itoa = itoa!( n / 10L ) ~ decimaldigit!( n % 10L ); 
     18
    1219 
    13 // Print the number of objects. 
    14 template showHowMany(int n, char [] objectname, bool needcapital=false) 
     20template showHowMany(int n, char [] where, bool needcapital = false) 
    1521{ 
    16   static if (n>1)  
    17       const char [] showHowMany = itoa!(n) ~ " " ~ objectname ~ "s"
    18   else static if (n==1) // don't add an "s". 
    19       const char [] showHowMany = "1 " ~ objectname
    20   else static if (needcapital
    21       const char [] showHowMany = "No more " ~ objectname ~ "s"
     22  static if ( n > 1 )  
     23      const char [] showHowMany = itoa!(n) ~ " bottles of beer" ~ where ~ \n
     24  else static if ( n == 1 ) 
     25      const char [] showHowMany = "1 bottle of beer" ~ where ~ \n
     26  else static if ( needcapital
     27      const char [] showHowMany = "No more bottles of beer" ~ where ~ \n
    2228  else  
    23       const char [] showHowMany = "no more " ~ objectname ~ "s"
     29      const char [] showHowMany = "no more bottles of beer" ~ where ~ \n
    2430} 
    2531 
    2632template beer(int maxbeers, int n = maxbeers) 
    2733{ 
    28   pragma(msg, showHowMany!(n, "bottle", CAPITAL) ~ " of beer on the wall,"); 
    29   pragma(msg, showHowMany!(n, "bottle") ~ " of beer."); 
    30    
    31  const char [] beersleft = showHowMany!( (maxbeers + n ) % (maxbeers+1), "bottle", CAPITAL)  
    32             ~ " of beer on the wall." \n; 
    33  
    34   static if (n>0) { 
    35     pragma(msg, "Take one down and pass it around, " \n ~ beersleft); 
    36     const bool drunk = beer!(maxbeers, n-1).drunk; 
    37   } else { 
    38    pragma(msg, "Go to the store and buy some more, " \n ~ beersleft); 
    39    const bool drunk = true; 
    40   } 
     34  static if ( n > 0 ) 
     35    const char [] beer = showHowMany!(n, " on the wall,", true) 
     36        ~ showHowMany!(n, ".") 
     37        ~ "Take one down and pass it around, " \n  
     38        ~ showHowMany!( n - 1 , " on the wall.")  
     39        ~ \n ~ beer!(maxbeers, n - 1); // recurse for subsequent verses. 
     40  else 
     41    const char [] beer = showHowMany!(n, " on the wall,", true) 
     42        ~ showHowMany!(n, ".") 
     43        ~ "Go to the store and buy some more, " \n 
     44        ~ showHowMany!( maxbeers, " on the wall."); 
    4145} 
    4246 
    43 static assert(beer!(9).drunk); 
     47pragma(msg, beer!(99));