Commit d5608efb authored by Carsten Schlote's avatar Carsten Schlote

Initial commit

parents
Pipeline #172 failed with stages
in 19 seconds
.dub
docs.json
__dummy.html
docs/
/99beers-d-template
99beers-d-template.so
99beers-d-template.dylib
99beers-d-template.dll
99beers-d-template.a
99beers-d-template.lib
99beers-d-template-test-*
*.exe
*.o
*.obj
*.lst
Copy of some example on D language templates.
{
"authors": [
"Don Clugston"
],
"copyright": "Copyright © 2006, Don Clugston",
"description": "An example for template usage with D language",
"license": "public domain",
"name": "99beers-d-template"
}
import std.stdio;
import test_ctfe_template;
void main()
{
writeln("Templated songtext:\n", beer!(99));
}
// Displays the "99 bottles of beer" song at compile time,
// using the template metaprograming facilities of D.
// No executable is generated. No libraries are used.
// Illustrates template default values, template string value parameters,
// compile-time concatenation of constant strings, static if.
template decimaldigit(int n) {
const char [] decimaldigit = "0123456789"[n..n+1];
}
template itoa(ulong n)
{
static if ( n < 10L )
const char [] itoa = decimaldigit!(n);
else
const char [] itoa = itoa!( n / 10L ) ~ decimaldigit!( n % 10L );
}
template showHowMany(int n, string where, bool needcapital = false)
{
static if ( n > 1 )
const char [] showHowMany = itoa!(n) ~ " bottles of beer" ~ where ~ "\n";
else static if ( n == 1 )
const char [] showHowMany = "1 bottle of beer" ~ where ~ "\n";
else static if ( needcapital )
const char [] showHowMany = "No more bottles of beer" ~ where ~ "\n";
else
const char [] showHowMany = "no more bottles of beer" ~ where ~ "\n";
}
template beer(int maxbeers, int n = maxbeers)
{
static if ( n > 0 )
const char [] beer =
showHowMany!(n, " on the wall,", true)
~ showHowMany!(n, ".\n")
~ "Take one down and pass it around,"
~ showHowMany!( n - 1 , " on the wall.\n")
~ beer!(maxbeers, n - 1); // recurse for subsequent verses.
else
const char [] beer =
showHowMany!(n, " on the wall,", true)
~ showHowMany!(n, ".\n")
~ "Go to the store and buy some more, "
~ showHowMany!( maxbeers, " on the wall.\n");
}
pragma(msg, beer!(99));
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment