aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPeter Hull <[email protected]>2010-09-20 22:08:28 +0100
committerGraydon Hoare <[email protected]>2010-09-30 13:50:25 -0700
commitf6e3e6903b20df01a6abe36d741c39d6e9696ebb (patch)
tree0a6754a03a66878aa0b4567e5b1420c5a006d785 /src
parentimplemented break for while-loop case (diff)
downloadrust-f6e3e6903b20df01a6abe36d741c39d6e9696ebb.tar.xz
rust-f6e3e6903b20df01a6abe36d741c39d6e9696ebb.zip
Initial check-in of 99 Bottles Of Beer
using different methods (simple, iterator, tail-call, pattern match)
Diffstat (limited to 'src')
-rw-r--r--src/test/bench/99-bottles/99bob-iter.rs67
-rw-r--r--src/test/bench/99-bottles/99bob-pattern.rs75
-rw-r--r--src/test/bench/99-bottles/99bob-simple.rs61
-rw-r--r--src/test/bench/99-bottles/99bob-tail.rs43
-rw-r--r--src/test/bench/99-bottles/Makefile20
-rwxr-xr-xsrc/test/bench/99-bottles/r.sh3
6 files changed, 269 insertions, 0 deletions
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 00000000..336939c8
--- /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 00000000..58d2d2c3
--- /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 00000000..10fec27a
--- /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 00000000..16e66d33
--- /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 00000000..8d1d27f9
--- /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 00000000..9da274e4
--- /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