From f6e3e6903b20df01a6abe36d741c39d6e9696ebb Mon Sep 17 00:00:00 2001
From: Peter Hull <peterhull90@gmail.com>
Date: Mon, 20 Sep 2010 22:08:28 +0100
Subject: [PATCH] Initial check-in of 99 Bottles Of Beer using different
 methods (simple, iterator, tail-call, pattern match)

---
 src/test/bench/99-bottles/99bob-iter.rs    | 67 +++++++++++++++++++
 src/test/bench/99-bottles/99bob-pattern.rs | 75 ++++++++++++++++++++++
 src/test/bench/99-bottles/99bob-simple.rs  | 61 ++++++++++++++++++
 src/test/bench/99-bottles/99bob-tail.rs    | 43 +++++++++++++
 src/test/bench/99-bottles/Makefile         | 20 ++++++
 src/test/bench/99-bottles/r.sh             |  3 +
 6 files changed, 269 insertions(+)
 create mode 100644 src/test/bench/99-bottles/99bob-iter.rs
 create mode 100644 src/test/bench/99-bottles/99bob-pattern.rs
 create mode 100644 src/test/bench/99-bottles/99bob-simple.rs
 create mode 100644 src/test/bench/99-bottles/99bob-tail.rs
 create mode 100644 src/test/bench/99-bottles/Makefile
 create mode 100755 src/test/bench/99-bottles/r.sh

diff --git a/src/test/bench/99-bottles/99bob-iter.rs b/src/test/bench/99-bottles/99bob-iter.rs
new file mode 100644
index 00000000000..336939c8d35
--- /dev/null
+++ b/src/test/bench/99-bottles/99bob-iter.rs
@@ -0,0 +1,67 @@
+/* -*- 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;
+
+fn b1() -> str {
+  ret "# of beer on the wall, # of beer.";
+}
+fn b2() -> str {
+  ret "Take one down and pass it around, # of beer on the wall.";
+}
+
+fn b7() ->str {
+  ret "No more bottles of beer on the wall, no more bottles of beer.";
+}
+fn b8() -> str {
+  ret "Go to the store and buy some more, # of beer on the wall.";
+}
+
+fn sub(str t, int n) -> str {
+  let str b = "";
+  let uint i = 0u;
+  let str ns;
+  alt (n) {
+    case (0) {
+      ns = "no more bottles";
+    }
+case (1) {
+    ns = "1 bottle";
+  }
+ case (_) {
+    ns = _int.to_str(n, 10u) + " bottles";
+  }
+  }
+  while (i < _str.byte_len(t)) {
+    if (t.(i) == ('#' as u8)) {
+      b += ns;
+    }
+    else {
+      b += t.(i);
+    }
+    i += 1u;
+  }
+  ret b;
+}
+
+/* Using an interator */
+iter ninetynine() -> int {
+    let int n = 100;
+    while (n > 1) {
+      n -= 1;
+      put n;
+    }
+  }
+fn main() {
+  for each (int n in ninetynine()) {
+    log sub(b1(), n);
+    log sub(b2(), n-1);
+    log "";
+  }
+  log b7();
+  log b8();
+}
+
diff --git a/src/test/bench/99-bottles/99bob-pattern.rs b/src/test/bench/99-bottles/99bob-pattern.rs
new file mode 100644
index 00000000000..58d2d2c3e54
--- /dev/null
+++ b/src/test/bench/99-bottles/99bob-pattern.rs
@@ -0,0 +1,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);
+  }
+}
\ No newline at end of file
diff --git a/src/test/bench/99-bottles/99bob-simple.rs b/src/test/bench/99-bottles/99bob-simple.rs
new file mode 100644
index 00000000000..10fec27ac87
--- /dev/null
+++ b/src/test/bench/99-bottles/99bob-simple.rs
@@ -0,0 +1,61 @@
+/* -*- 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;
+
+fn b1() -> str {
+  ret "# of beer on the wall, # of beer.";
+}
+fn b2() -> str {
+  ret "Take one down and pass it around, # of beer on the wall.";
+}
+fn b7() ->str {
+  ret "No more bottles of beer on the wall, no more bottles of beer.";
+}
+fn b8() -> str {
+  ret "Go to the store and buy some more, # of beer on the wall.";
+}
+
+fn sub(str t, int n) -> str {
+  let str b = "";
+  let uint i = 0u;
+  let str ns;
+  alt (n) {
+    case (0) {
+      ns = "no more bottles";
+    }
+case (1) {
+    ns = "1 bottle";
+  }
+ case (_) {
+    ns = _int.to_str(n, 10u) + " bottles";
+  }
+  }
+  while (i < _str.byte_len(t)) {
+    if (t.(i) == ('#' as u8)) {
+      b += ns;
+    }
+    else {
+      b += t.(i);
+    }
+    i += 1u;
+  }
+  ret b;
+}
+
+/* Straightforward counter */
+fn main() {
+  let int n=99;
+  while (n > 0) {
+    log sub(b1(), n);
+    log sub(b2(), n - 1);
+    log "";
+    n -= 1;
+  }
+  log b7();
+  log sub(b8(),99);
+} 
+
diff --git a/src/test/bench/99-bottles/99bob-tail.rs b/src/test/bench/99-bottles/99bob-tail.rs
new file mode 100644
index 00000000000..16e66d333d3
--- /dev/null
+++ b/src/test/bench/99-bottles/99bob-tail.rs
@@ -0,0 +1,43 @@
+/* -*- 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;
+
+fn main() {
+  fn multiple(int 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.";
+    log "";
+    if (n > 3) {
+      be multiple(n - 1);
+    }
+    else {
+      be dual();
+    }
+  }
+  fn 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.";
+    log "";
+    be single();
+  }
+  fn 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.";
+    log "";
+    be none();
+  }
+  fn 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.";
+    log "";
+  }
+  multiple(99);
+}
\ No newline at end of file
diff --git a/src/test/bench/99-bottles/Makefile b/src/test/bench/99-bottles/Makefile
new file mode 100644
index 00000000000..8d1d27f99f8
--- /dev/null
+++ b/src/test/bench/99-bottles/Makefile
@@ -0,0 +1,20 @@
+RC:=../../../rustboot
+RFLAGS:=-L ../../..
+TARGETS:= 99bob-simple 99bob-iter 99bob-tail 99bob-pattern
+TARGET_X86:=$(addsuffix .x86,$(TARGETS))
+TARGET_LLVM:=$(addsuffix .llvm,$(TARGETS))
+
+all : x86s llvms
+
+clean:
+	rm  $(TARGET_X86) $(TARGET_LLVM)
+
+x86s : $(TARGET_X86) 
+
+llvms:  $(TARGET_LLVM)
+
+%.x86 : %.rs
+	$(RC) $(RFLAGS) $^ -o $@
+
+%.llvm : %.rs
+	$(RC) $(RFLAGS) -llvm $^ -o $@
\ No newline at end of file
diff --git a/src/test/bench/99-bottles/r.sh b/src/test/bench/99-bottles/r.sh
new file mode 100755
index 00000000000..9da274e41f9
--- /dev/null
+++ b/src/test/bench/99-bottles/r.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+make -k $1.x86
+DYLD_LIBRARY_PATH=../../.. ./$1.x86