1
+ /* -*- mode:rust;indent-tabs-mode:nil -*-
2
+ * Implementation of 99 Bottles of Beer
3
+ * http://99-bottles-of-beer.net/
4
+ */
5
+ use std;
6
+ import std. _int ;
7
+ import std. _str ;
8
+
9
+ tag bottle { none; dual; single; multiple ( int) ; }
10
+
11
+ fn show ( bottle b) {
12
+ alt ( b) {
13
+ case ( none) {
14
+ log "No more bottles of beer on the wall, no more bottles of beer," ;
15
+ log "Go to the store and buy some more, "
16
+ +"99 bottles of beer on the wall." ;
17
+ }
18
+ case ( single) {
19
+ log "1 bottle of beer on the wall, 1 bottle of beer," ;
20
+ log "Take one down and pass it around, "
21
+ +"no more bottles of beer on the wall." ;
22
+ }
23
+ case ( dual) {
24
+ log "2 bottles of beer on the wall, 2 bottles of beer," ;
25
+ log "Take one down and pass it around, 1 bottle of beer on the wall." ;
26
+ }
27
+ case ( multiple ( ?n) ) {
28
+ let str nb = _int. to_str ( n, 10 u) ;
29
+ let str mb = _int. to_str ( n - 1 , 10 u) ;
30
+ log nb + " bottles of beer on the wall, " + nb + " bottles of beer," ;
31
+ log "Take one down and pass it around, "
32
+ + mb + " bottles of beer on the wall." ;
33
+ }
34
+ }
35
+ }
36
+ fn next ( bottle b) -> bottle {
37
+ alt ( b) {
38
+ case ( none) {
39
+ ret none;
40
+ }
41
+ case ( single) {
42
+ ret none;
43
+ }
44
+ case ( dual) {
45
+ ret single;
46
+ }
47
+ case ( multiple ( 3 ) ) {
48
+ ret dual;
49
+ }
50
+ case ( multiple ( ?n) ) {
51
+ ret multiple ( n - 1 ) ;
52
+ }
53
+ }
54
+ }
55
+ // Won't need this when tags can be compared with ==
56
+ fn more ( bottle b) -> bool {
57
+ alt ( b) {
58
+ case ( none) {
59
+ ret false ;
60
+ }
61
+ case ( _) {
62
+ ret true ;
63
+ }
64
+ }
65
+ }
66
+ fn main ( ) {
67
+ let bottle b = multiple ( 99 ) ;
68
+ let bool running = true ;
69
+ while ( running) {
70
+ show ( b) ;
71
+ log "" ;
72
+ running = more ( b) ;
73
+ b = next ( b) ;
74
+ }
75
+ }
0 commit comments