Skip to content

Commit d9afd5c

Browse files
pedro-wgraydon
authored andcommitted
---
yaml --- r: 733 b: refs/heads/master c: f6e3e69 h: refs/heads/master i: 731: 8ee3238 v: v3
1 parent 19f14c4 commit d9afd5c

File tree

7 files changed

+270
-1
lines changed

7 files changed

+270
-1
lines changed

[refs]

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 4a3404803b6c6de079a590a4bc96313d9a68f164
2+
refs/heads/master: f6e3e6903b20df01a6abe36d741c39d6e9696ebb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
fn b1() -> str {
10+
ret "# of beer on the wall, # of beer.";
11+
}
12+
fn b2() -> str {
13+
ret "Take one down and pass it around, # of beer on the wall.";
14+
}
15+
16+
fn b7() ->str {
17+
ret "No more bottles of beer on the wall, no more bottles of beer.";
18+
}
19+
fn b8() -> str {
20+
ret "Go to the store and buy some more, # of beer on the wall.";
21+
}
22+
23+
fn sub(str t, int n) -> str {
24+
let str b = "";
25+
let uint i = 0u;
26+
let str ns;
27+
alt (n) {
28+
case (0) {
29+
ns = "no more bottles";
30+
}
31+
case (1) {
32+
ns = "1 bottle";
33+
}
34+
case (_) {
35+
ns = _int.to_str(n, 10u) + " bottles";
36+
}
37+
}
38+
while (i < _str.byte_len(t)) {
39+
if (t.(i) == ('#' as u8)) {
40+
b += ns;
41+
}
42+
else {
43+
b += t.(i);
44+
}
45+
i += 1u;
46+
}
47+
ret b;
48+
}
49+
50+
/* Using an interator */
51+
iter ninetynine() -> int {
52+
let int n = 100;
53+
while (n > 1) {
54+
n -= 1;
55+
put n;
56+
}
57+
}
58+
fn main() {
59+
for each (int n in ninetynine()) {
60+
log sub(b1(), n);
61+
log sub(b2(), n-1);
62+
log "";
63+
}
64+
log b7();
65+
log b8();
66+
}
67+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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, 10u);
29+
let str mb = _int.to_str(n - 1, 10u);
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
fn b1() -> str {
10+
ret "# of beer on the wall, # of beer.";
11+
}
12+
fn b2() -> str {
13+
ret "Take one down and pass it around, # of beer on the wall.";
14+
}
15+
fn b7() ->str {
16+
ret "No more bottles of beer on the wall, no more bottles of beer.";
17+
}
18+
fn b8() -> str {
19+
ret "Go to the store and buy some more, # of beer on the wall.";
20+
}
21+
22+
fn sub(str t, int n) -> str {
23+
let str b = "";
24+
let uint i = 0u;
25+
let str ns;
26+
alt (n) {
27+
case (0) {
28+
ns = "no more bottles";
29+
}
30+
case (1) {
31+
ns = "1 bottle";
32+
}
33+
case (_) {
34+
ns = _int.to_str(n, 10u) + " bottles";
35+
}
36+
}
37+
while (i < _str.byte_len(t)) {
38+
if (t.(i) == ('#' as u8)) {
39+
b += ns;
40+
}
41+
else {
42+
b += t.(i);
43+
}
44+
i += 1u;
45+
}
46+
ret b;
47+
}
48+
49+
/* Straightforward counter */
50+
fn main() {
51+
let int n=99;
52+
while (n > 0) {
53+
log sub(b1(), n);
54+
log sub(b2(), n - 1);
55+
log "";
56+
n -= 1;
57+
}
58+
log b7();
59+
log sub(b8(),99);
60+
}
61+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
fn main() {
10+
fn multiple(int n) {
11+
let str nb = _int.to_str(n, 10u);
12+
let str mb = _int.to_str(n - 1, 10u);
13+
log nb + " bottles of beer on the wall, " + nb + " bottles of beer,";
14+
log "Take one down and pass it around, "
15+
+ mb + " bottles of beer on the wall.";
16+
log "";
17+
if (n > 3) {
18+
be multiple(n - 1);
19+
}
20+
else {
21+
be dual();
22+
}
23+
}
24+
fn dual() {
25+
log "2 bottles of beer on the wall, 2 bottles of beer,";
26+
log "Take one down and pass it around, 1 bottle of beer on the wall.";
27+
log "";
28+
be single();
29+
}
30+
fn single() {
31+
log "1 bottle of beer on the wall, 1 bottle of beer,";
32+
log "Take one down and pass it around, "
33+
+ "no more bottles of beer on the wall.";
34+
log "";
35+
be none();
36+
}
37+
fn none() {
38+
log "No more bottles of beer on the wall, no more bottles of beer,";
39+
log "Go to the store and buy some more, 99 bottles of beer on the wall.";
40+
log "";
41+
}
42+
multiple(99);
43+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
RC:=../../../rustboot
2+
RFLAGS:=-L ../../..
3+
TARGETS:= 99bob-simple 99bob-iter 99bob-tail 99bob-pattern
4+
TARGET_X86:=$(addsuffix .x86,$(TARGETS))
5+
TARGET_LLVM:=$(addsuffix .llvm,$(TARGETS))
6+
7+
all : x86s llvms
8+
9+
clean:
10+
rm $(TARGET_X86) $(TARGET_LLVM)
11+
12+
x86s : $(TARGET_X86)
13+
14+
llvms: $(TARGET_LLVM)
15+
16+
%.x86 : %.rs
17+
$(RC) $(RFLAGS) $^ -o $@
18+
19+
%.llvm : %.rs
20+
$(RC) $(RFLAGS) -llvm $^ -o $@

trunk/src/test/bench/99-bottles/r.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
make -k $1.x86
3+
DYLD_LIBRARY_PATH=../../.. ./$1.x86

0 commit comments

Comments
 (0)