aboutsummaryrefslogtreecommitdiff
path: root/src/test/bench/99-bottles/99bob-pattern.rs
blob: 311fca460c8c511315b65c6c0d767d6454fc2561 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* -*- mode::rust;indent-tabs-mode::nil -*- 
 * Implementation of 99 Bottles of Beer
 * http://99-bottles-of-beer.net/
 */
use std;
import std::_int;
import std::_str;

tag bottle { none; dual; single; multiple(int);}

fn show(bottle b) {
  alt(b) {
    case (none) {
      log "No more bottles of beer on the wall, no more bottles of beer,";
      log "Go to the store and buy some more, "
        +"99 bottles of beer on the wall.";
    }
    case (single) {
      log "1 bottle of beer on the wall, 1 bottle of beer,";
      log "Take one down and pass it around, "
        +"no more bottles of beer on the wall.";
    }
    case (dual) {
      log "2 bottles of beer on the wall, 2 bottles of beer,";
      log "Take one down and pass it around, 1 bottle of beer on the wall.";
    }
    case (multiple(?n)) {
      let str nb =  _int::to_str(n, 10u);
      let str mb = _int::to_str(n - 1, 10u);
      log nb + " bottles of beer on the wall, " + nb + " bottles of beer,";
      log "Take one down and pass it around, " 
        + mb + " bottles of beer on the wall.";
    }
  }
}
fn next(bottle b) -> bottle {
  alt(b) {
    case (none) {
      ret none;
    }
    case (single) {
      ret none;
    }
    case (dual) {
      ret single;
    }
    case (multiple(3)) {
      ret dual;
    }
    case (multiple(?n)) {
      ret multiple(n - 1);
    }
  }
}
// Won't need this when tags can be compared with ==
fn more(bottle b) -> bool {
  alt(b) {
    case (none) {
      ret false;
    }
    case (_) {
      ret true;
    }
  }
}
fn main() {
  let bottle b = multiple(99);
  let bool running = true;
  while (running) {
    show(b);
    log "";
    running = more(b);
    b = next(b);
  }
}