Debian Codegraph @

gcc/testsuite/gdc.test/compilable/99bottles.d

raw
1 // written by Don Clugston:
2 // https://www.digitalmars.com/d/archives/digitalmars/D/announce/4374.html
3 // http://www.99-bottles-of-beer.net/language-d-1212.html
4
5 // Generates the "99 bottles of beer" song at compile time,
6 // using the template metaprograming facilities of D.
7 // No executable is generated. No libraries are used.
8 // Illustrates template default values, template string value parameters,
9 // compile-time concatenation of constant strings, static if.
10
11 module bottles99;
12
13 template decimaldigit(int n) {
14 const string decimaldigit = "0123456789"[n..n+1];
15 }
16
17 template itoa(ulong n)
18 {
19 static if ( n < 10L )
20 const string itoa = decimaldigit!(n);
21 else
22 const string itoa = itoa!( n / 10L ) ~ decimaldigit!( n % 10L );
23 }
24
25 template showHowMany(int n, string where, bool needcapital = false)
26 {
27 static if ( n > 1 )
28 const string showHowMany = itoa!(n) ~ " bottles of beer" ~ where ~ "\n";
29 else static if ( n == 1 )
30 const string showHowMany = "1 bottle of beer" ~ where ~ "\n";
31 else static if ( needcapital )
32 const string showHowMany = "No more bottles of beer" ~ where ~ "\n";
33 else
34 const string showHowMany = "no more bottles of beer" ~ where ~ "\n";
35 }
36
37 template beer(int maxbeers, int n = maxbeers)
38 {
39 static if ( n > 0 )
40 const string beer = showHowMany!(n, " on the wall,", true)
41 ~ showHowMany!(n, ".")
42 ~ "Take one down and pass it around," ~ "\n"
43 ~ showHowMany!( n - 1 , " on the wall.")
44 ~ "\n" ~ beer!(maxbeers, n - 1); // recurse for subsequent verses.
45 else
46 const string beer = showHowMany!(n, " on the wall,", true)
47 ~ showHowMany!(n, ".")
48 ~ "Go to the store and buy some more," ~ "\n"
49 ~ showHowMany!( maxbeers, " on the wall.");
50 }
51
52 enum expected = `99 bottles of beer on the wall,
53 99 bottles of beer.
54 Take one down and pass it around,
55 98 bottles of beer on the wall.
56
57 98 bottles of beer on the wall,
58 98 bottles of beer.
59 Take one down and pass it around,
60 97 bottles of beer on the wall.
61
62 97 bottles of beer on the wall,
63 97 bottles of beer.
64 Take one down and pass it around,
65 96 bottles of beer on the wall.
66
67 96 bottles of beer on the wall,
68 96 bottles of beer.
69 Take one down and pass it around,
70 95 bottles of beer on the wall.
71
72 95 bottles of beer on the wall,
73 95 bottles of beer.
74 Take one down and pass it around,
75 94 bottles of beer on the wall.
76
77 94 bottles of beer on the wall,
78 94 bottles of beer.
79 Take one down and pass it around,
80 93 bottles of beer on the wall.
81
82 93 bottles of beer on the wall,
83 93 bottles of beer.
84 Take one down and pass it around,
85 92 bottles of beer on the wall.
86
87 92 bottles of beer on the wall,
88 92 bottles of beer.
89 Take one down and pass it around,
90 91 bottles of beer on the wall.
91
92 91 bottles of beer on the wall,
93 91 bottles of beer.
94 Take one down and pass it around,
95 90 bottles of beer on the wall.
96
97 90 bottles of beer on the wall,
98 90 bottles of beer.
99 Take one down and pass it around,
100 89 bottles of beer on the wall.
101
102 89 bottles of beer on the wall,
103 89 bottles of beer.
104 Take one down and pass it around,
105 88 bottles of beer on the wall.
106
107 88 bottles of beer on the wall,
108 88 bottles of beer.
109 Take one down and pass it around,
110 87 bottles of beer on the wall.
111
112 87 bottles of beer on the wall,
113 87 bottles of beer.
114 Take one down and pass it around,
115 86 bottles of beer on the wall.
116
117 86 bottles of beer on the wall,
118 86 bottles of beer.
119 Take one down and pass it around,
120 85 bottles of beer on the wall.
121
122 85 bottles of beer on the wall,
123 85 bottles of beer.
124 Take one down and pass it around,
125 84 bottles of beer on the wall.
126
127 84 bottles of beer on the wall,
128 84 bottles of beer.
129 Take one down and pass it around,
130 83 bottles of beer on the wall.
131
132 83 bottles of beer on the wall,
133 83 bottles of beer.
134 Take one down and pass it around,
135 82 bottles of beer on the wall.
136
137 82 bottles of beer on the wall,
138 82 bottles of beer.
139 Take one down and pass it around,
140 81 bottles of beer on the wall.
141
142 81 bottles of beer on the wall,
143 81 bottles of beer.
144 Take one down and pass it around,
145 80 bottles of beer on the wall.
146
147 80 bottles of beer on the wall,
148 80 bottles of beer.
149 Take one down and pass it around,
150 79 bottles of beer on the wall.
151
152 79 bottles of beer on the wall,
153 79 bottles of beer.
154 Take one down and pass it around,
155 78 bottles of beer on the wall.
156
157 78 bottles of beer on the wall,
158 78 bottles of beer.
159 Take one down and pass it around,
160 77 bottles of beer on the wall.
161
162 77 bottles of beer on the wall,
163 77 bottles of beer.
164 Take one down and pass it around,
165 76 bottles of beer on the wall.
166
167 76 bottles of beer on the wall,
168 76 bottles of beer.
169 Take one down and pass it around,
170 75 bottles of beer on the wall.
171
172 75 bottles of beer on the wall,
173 75 bottles of beer.
174 Take one down and pass it around,
175 74 bottles of beer on the wall.
176
177 74 bottles of beer on the wall,
178 74 bottles of beer.
179 Take one down and pass it around,
180 73 bottles of beer on the wall.
181
182 73 bottles of beer on the wall,
183 73 bottles of beer.
184 Take one down and pass it around,
185 72 bottles of beer on the wall.
186
187 72 bottles of beer on the wall,
188 72 bottles of beer.
189 Take one down and pass it around,
190 71 bottles of beer on the wall.
191
192 71 bottles of beer on the wall,
193 71 bottles of beer.
194 Take one down and pass it around,
195 70 bottles of beer on the wall.
196
197 70 bottles of beer on the wall,
198 70 bottles of beer.
199 Take one down and pass it around,
200 69 bottles of beer on the wall.
201
202 69 bottles of beer on the wall,
203 69 bottles of beer.
204 Take one down and pass it around,
205 68 bottles of beer on the wall.
206
207 68 bottles of beer on the wall,
208 68 bottles of beer.
209 Take one down and pass it around,
210 67 bottles of beer on the wall.
211
212 67 bottles of beer on the wall,
213 67 bottles of beer.
214 Take one down and pass it around,
215 66 bottles of beer on the wall.
216
217 66 bottles of beer on the wall,
218 66 bottles of beer.
219 Take one down and pass it around,
220 65 bottles of beer on the wall.
221
222 65 bottles of beer on the wall,
223 65 bottles of beer.
224 Take one down and pass it around,
225 64 bottles of beer on the wall.
226
227 64 bottles of beer on the wall,
228 64 bottles of beer.
229 Take one down and pass it around,
230 63 bottles of beer on the wall.
231
232 63 bottles of beer on the wall,
233 63 bottles of beer.
234 Take one down and pass it around,
235 62 bottles of beer on the wall.
236
237 62 bottles of beer on the wall,
238 62 bottles of beer.
239 Take one down and pass it around,
240 61 bottles of beer on the wall.
241
242 61 bottles of beer on the wall,
243 61 bottles of beer.
244 Take one down and pass it around,
245 60 bottles of beer on the wall.
246
247 60 bottles of beer on the wall,
248 60 bottles of beer.
249 Take one down and pass it around,
250 59 bottles of beer on the wall.
251
252 59 bottles of beer on the wall,
253 59 bottles of beer.
254 Take one down and pass it around,
255 58 bottles of beer on the wall.
256
257 58 bottles of beer on the wall,
258 58 bottles of beer.
259 Take one down and pass it around,
260 57 bottles of beer on the wall.
261
262 57 bottles of beer on the wall,
263 57 bottles of beer.
264 Take one down and pass it around,
265 56 bottles of beer on the wall.
266
267 56 bottles of beer on the wall,
268 56 bottles of beer.
269 Take one down and pass it around,
270 55 bottles of beer on the wall.
271
272 55 bottles of beer on the wall,
273 55 bottles of beer.
274 Take one down and pass it around,
275 54 bottles of beer on the wall.
276
277 54 bottles of beer on the wall,
278 54 bottles of beer.
279 Take one down and pass it around,
280 53 bottles of beer on the wall.
281
282 53 bottles of beer on the wall,
283 53 bottles of beer.
284 Take one down and pass it around,
285 52 bottles of beer on the wall.
286
287 52 bottles of beer on the wall,
288 52 bottles of beer.
289 Take one down and pass it around,
290 51 bottles of beer on the wall.
291
292 51 bottles of beer on the wall,
293 51 bottles of beer.
294 Take one down and pass it around,
295 50 bottles of beer on the wall.
296
297 50 bottles of beer on the wall,
298 50 bottles of beer.
299 Take one down and pass it around,
300 49 bottles of beer on the wall.
301
302 49 bottles of beer on the wall,
303 49 bottles of beer.
304 Take one down and pass it around,
305 48 bottles of beer on the wall.
306
307 48 bottles of beer on the wall,
308 48 bottles of beer.
309 Take one down and pass it around,
310 47 bottles of beer on the wall.
311
312 47 bottles of beer on the wall,
313 47 bottles of beer.
314 Take one down and pass it around,
315 46 bottles of beer on the wall.
316
317 46 bottles of beer on the wall,
318 46 bottles of beer.
319 Take one down and pass it around,
320 45 bottles of beer on the wall.
321
322 45 bottles of beer on the wall,
323 45 bottles of beer.
324 Take one down and pass it around,
325 44 bottles of beer on the wall.
326
327 44 bottles of beer on the wall,
328 44 bottles of beer.
329 Take one down and pass it around,
330 43 bottles of beer on the wall.
331
332 43 bottles of beer on the wall,
333 43 bottles of beer.
334 Take one down and pass it around,
335 42 bottles of beer on the wall.
336
337 42 bottles of beer on the wall,
338 42 bottles of beer.
339 Take one down and pass it around,
340 41 bottles of beer on the wall.
341
342 41 bottles of beer on the wall,
343 41 bottles of beer.
344 Take one down and pass it around,
345 40 bottles of beer on the wall.
346
347 40 bottles of beer on the wall,
348 40 bottles of beer.
349 Take one down and pass it around,
350 39 bottles of beer on the wall.
351
352 39 bottles of beer on the wall,
353 39 bottles of beer.
354 Take one down and pass it around,
355 38 bottles of beer on the wall.
356
357 38 bottles of beer on the wall,
358 38 bottles of beer.
359 Take one down and pass it around,
360 37 bottles of beer on the wall.
361
362 37 bottles of beer on the wall,
363 37 bottles of beer.
364 Take one down and pass it around,
365 36 bottles of beer on the wall.
366
367 36 bottles of beer on the wall,
368 36 bottles of beer.
369 Take one down and pass it around,
370 35 bottles of beer on the wall.
371
372 35 bottles of beer on the wall,
373 35 bottles of beer.
374 Take one down and pass it around,
375 34 bottles of beer on the wall.
376
377 34 bottles of beer on the wall,
378 34 bottles of beer.
379 Take one down and pass it around,
380 33 bottles of beer on the wall.
381
382 33 bottles of beer on the wall,
383 33 bottles of beer.
384 Take one down and pass it around,
385 32 bottles of beer on the wall.
386
387 32 bottles of beer on the wall,
388 32 bottles of beer.
389 Take one down and pass it around,
390 31 bottles of beer on the wall.
391
392 31 bottles of beer on the wall,
393 31 bottles of beer.
394 Take one down and pass it around,
395 30 bottles of beer on the wall.
396
397 30 bottles of beer on the wall,
398 30 bottles of beer.
399 Take one down and pass it around,
400 29 bottles of beer on the wall.
401
402 29 bottles of beer on the wall,
403 29 bottles of beer.
404 Take one down and pass it around,
405 28 bottles of beer on the wall.
406
407 28 bottles of beer on the wall,
408 28 bottles of beer.
409 Take one down and pass it around,
410 27 bottles of beer on the wall.
411
412 27 bottles of beer on the wall,
413 27 bottles of beer.
414 Take one down and pass it around,
415 26 bottles of beer on the wall.
416
417 26 bottles of beer on the wall,
418 26 bottles of beer.
419 Take one down and pass it around,
420 25 bottles of beer on the wall.
421
422 25 bottles of beer on the wall,
423 25 bottles of beer.
424 Take one down and pass it around,
425 24 bottles of beer on the wall.
426
427 24 bottles of beer on the wall,
428 24 bottles of beer.
429 Take one down and pass it around,
430 23 bottles of beer on the wall.
431
432 23 bottles of beer on the wall,
433 23 bottles of beer.
434 Take one down and pass it around,
435 22 bottles of beer on the wall.
436
437 22 bottles of beer on the wall,
438 22 bottles of beer.
439 Take one down and pass it around,
440 21 bottles of beer on the wall.
441
442 21 bottles of beer on the wall,
443 21 bottles of beer.
444 Take one down and pass it around,
445 20 bottles of beer on the wall.
446
447 20 bottles of beer on the wall,
448 20 bottles of beer.
449 Take one down and pass it around,
450 19 bottles of beer on the wall.
451
452 19 bottles of beer on the wall,
453 19 bottles of beer.
454 Take one down and pass it around,
455 18 bottles of beer on the wall.
456
457 18 bottles of beer on the wall,
458 18 bottles of beer.
459 Take one down and pass it around,
460 17 bottles of beer on the wall.
461
462 17 bottles of beer on the wall,
463 17 bottles of beer.
464 Take one down and pass it around,
465 16 bottles of beer on the wall.
466
467 16 bottles of beer on the wall,
468 16 bottles of beer.
469 Take one down and pass it around,
470 15 bottles of beer on the wall.
471
472 15 bottles of beer on the wall,
473 15 bottles of beer.
474 Take one down and pass it around,
475 14 bottles of beer on the wall.
476
477 14 bottles of beer on the wall,
478 14 bottles of beer.
479 Take one down and pass it around,
480 13 bottles of beer on the wall.
481
482 13 bottles of beer on the wall,
483 13 bottles of beer.
484 Take one down and pass it around,
485 12 bottles of beer on the wall.
486
487 12 bottles of beer on the wall,
488 12 bottles of beer.
489 Take one down and pass it around,
490 11 bottles of beer on the wall.
491
492 11 bottles of beer on the wall,
493 11 bottles of beer.
494 Take one down and pass it around,
495 10 bottles of beer on the wall.
496
497 10 bottles of beer on the wall,
498 10 bottles of beer.
499 Take one down and pass it around,
500 9 bottles of beer on the wall.
501
502 9 bottles of beer on the wall,
503 9 bottles of beer.
504 Take one down and pass it around,
505 8 bottles of beer on the wall.
506
507 8 bottles of beer on the wall,
508 8 bottles of beer.
509 Take one down and pass it around,
510 7 bottles of beer on the wall.
511
512 7 bottles of beer on the wall,
513 7 bottles of beer.
514 Take one down and pass it around,
515 6 bottles of beer on the wall.
516
517 6 bottles of beer on the wall,
518 6 bottles of beer.
519 Take one down and pass it around,
520 5 bottles of beer on the wall.
521
522 5 bottles of beer on the wall,
523 5 bottles of beer.
524 Take one down and pass it around,
525 4 bottles of beer on the wall.
526
527 4 bottles of beer on the wall,
528 4 bottles of beer.
529 Take one down and pass it around,
530 3 bottles of beer on the wall.
531
532 3 bottles of beer on the wall,
533 3 bottles of beer.
534 Take one down and pass it around,
535 2 bottles of beer on the wall.
536
537 2 bottles of beer on the wall,
538 2 bottles of beer.
539 Take one down and pass it around,
540 1 bottle of beer on the wall.
541
542 1 bottle of beer on the wall,
543 1 bottle of beer.
544 Take one down and pass it around,
545 no more bottles of beer on the wall.
546
547 No more bottles of beer on the wall,
548 no more bottles of beer.
549 Go to the store and buy some more,
550 99 bottles of beer on the wall.
551 `;
552
553 static assert(beer!(99) == expected);