mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-15 08:23:26 +00:00
Initial check-in of 99 Bottles Of Beer
using different methods (simple, iterator, tail-call, pattern match)
This commit is contained in:
parent
4a3404803b
commit
f6e3e6903b
67
src/test/bench/99-bottles/99bob-iter.rs
Normal file
67
src/test/bench/99-bottles/99bob-iter.rs
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
|
75
src/test/bench/99-bottles/99bob-pattern.rs
Normal file
75
src/test/bench/99-bottles/99bob-pattern.rs
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
61
src/test/bench/99-bottles/99bob-simple.rs
Normal file
61
src/test/bench/99-bottles/99bob-simple.rs
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
|
43
src/test/bench/99-bottles/99bob-tail.rs
Normal file
43
src/test/bench/99-bottles/99bob-tail.rs
Normal file
@ -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);
|
||||||
|
}
|
20
src/test/bench/99-bottles/Makefile
Normal file
20
src/test/bench/99-bottles/Makefile
Normal file
@ -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 $@
|
3
src/test/bench/99-bottles/r.sh
Executable file
3
src/test/bench/99-bottles/r.sh
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
make -k $1.x86
|
||||||
|
DYLD_LIBRARY_PATH=../../.. ./$1.x86
|
Loading…
Reference in New Issue
Block a user