diff options
author | Case Duckworth | 2022-06-29 00:59:59 -0500 |
---|---|---|
committer | Case Duckworth | 2022-06-29 00:59:59 -0500 |
commit | 0a78720644e45bd27c4a57cbddf2d32aacb555c3 (patch) | |
tree | 29783dc9a534dbbfe172d5c84e66f5efcd9c43dc /examples | |
parent | Recognize bf comments (diff) | |
download | trainfuck-0a78720644e45bd27c4a57cbddf2d32aacb555c3.tar.gz trainfuck-0a78720644e45bd27c4a57cbddf2d32aacb555c3.zip |
Probably version ... whatever the next higher one is
Diffstat (limited to 'examples')
-rw-r--r-- | examples/add2.bf | 27 | ||||
-rw-r--r-- | examples/hello-world.bf | 45 | ||||
-rw-r--r-- | examples/helloworld.bf | 1 | ||||
-rw-r--r-- | examples/helloworld.trainfuck | 152 | ||||
-rw-r--r-- | examples/rot13.bf | 32 |
5 files changed, 104 insertions, 153 deletions
diff --git a/examples/add2.bf b/examples/add2.bf new file mode 100644 index 0000000..42361db --- /dev/null +++ b/examples/add2.bf | |||
@@ -0,0 +1,27 @@ | |||
1 | [ Add two values | ||
2 | from https://en.wikipedia.org/wiki/Brainfuck#Adding_two_values | ||
3 | ] | ||
4 | |||
5 | ++ Cell c0 = 2 | ||
6 | > +++++ Cell c1 = 5 | ||
7 | |||
8 | [ Start your loops with your cell pointer on the loop counter (c1 in our case) | ||
9 | < + Add 1 to c0 | ||
10 | > - Subtract 1 from c1 | ||
11 | ] End your loops with the cell pointer on the loop counter | ||
12 | |||
13 | At this point our program has added 5 to 2 leaving 7 in c0 and 0 in c1 | ||
14 | but we cannot output this value to the terminal since it is not ASCII encoded | ||
15 | |||
16 | To display the ASCII character "7" we must add 48 to the value 7 | ||
17 | We use a loop to compute 48 = 6 * 8 | ||
18 | |||
19 | ++++ ++++ c1 = 8 and this will be our loop counter again | ||
20 | [ | ||
21 | < +++ +++ Add 6 to c0 | ||
22 | > - Subtract 1 from c1 | ||
23 | ] | ||
24 | < . Print out c0 which has the value 55 which translates to "7"! | ||
25 | |||
26 | Finally print a newline: | ||
27 | ---------------------------------------------. | ||
diff --git a/examples/hello-world.bf b/examples/hello-world.bf new file mode 100644 index 0000000..dce24e2 --- /dev/null +++ b/examples/hello-world.bf | |||
@@ -0,0 +1,45 @@ | |||
1 | [ This program prints "Hello World!" and a newline to the screen, its | ||
2 | length is 106 active command characters. [It is not the shortest.] | ||
3 | |||
4 | This loop is an "initial comment loop", a simple way of adding a comment | ||
5 | to a BF program such that you don't have to worry about any command | ||
6 | characters. Any ".", ",", "+", "-", "<" and ">" characters are simply | ||
7 | ignored, the "[" and "]" characters just have to be balanced. This | ||
8 | loop and the commands it contains are ignored because the current cell | ||
9 | defaults to a value of 0; the 0 value causes this loop to be skipped. | ||
10 | |||
11 | from https://en.wikipedia.org/wiki/Brainfuck#Hello_World! | ||
12 | ] | ||
13 | ++++++++ Set Cell #0 to 8 | ||
14 | [ | ||
15 | >++++ Add 4 to Cell #1; this will always set Cell #1 to 4 | ||
16 | [ as the cell will be cleared by the loop | ||
17 | >++ Add 2 to Cell #2 | ||
18 | >+++ Add 3 to Cell #3 | ||
19 | >+++ Add 3 to Cell #4 | ||
20 | >+ Add 1 to Cell #5 | ||
21 | <<<<- Decrement the loop counter in Cell #1 | ||
22 | ] Loop until Cell #1 is zero; number of iterations is 4 | ||
23 | >+ Add 1 to Cell #2 | ||
24 | >+ Add 1 to Cell #3 | ||
25 | >- Subtract 1 from Cell #4 | ||
26 | >>+ Add 1 to Cell #6 | ||
27 | [<] Move back to the first zero cell you find; this will | ||
28 | be Cell #1 which was cleared by the previous loop | ||
29 | <- Decrement the loop Counter in Cell #0 | ||
30 | ] Loop until Cell #0 is zero; number of iterations is 8 | ||
31 | |||
32 | The result of this is: | ||
33 | Cell no : 0 1 2 3 4 5 6 | ||
34 | Contents: 0 0 72 104 88 32 8 | ||
35 | Pointer : ^ | ||
36 | |||
37 | >>. Cell #2 has value 72 which is 'H' | ||
38 | >---. Subtract 3 from Cell #3 to get 101 which is 'e' | ||
39 | +++++++..+++. Likewise for 'llo' from Cell #3 | ||
40 | >>. Cell #5 is 32 for the space | ||
41 | <-. Subtract 1 from Cell #4 for 87 to give a 'W' | ||
42 | <. Cell #3 was set to 'o' from the end of 'Hello' | ||
43 | +++.------.--------. Cell #3 for 'rl' and 'd' | ||
44 | >>+. Add 1 to Cell #5 gives us an exclamation point | ||
45 | >++. And finally a newline from Cell #6 | ||
diff --git a/examples/helloworld.bf b/examples/helloworld.bf deleted file mode 100644 index 8fa0f72..0000000 --- a/examples/helloworld.bf +++ /dev/null | |||
@@ -1 +0,0 @@ | |||
1 | ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++. | ||
diff --git a/examples/helloworld.trainfuck b/examples/helloworld.trainfuck deleted file mode 100644 index 1f68f11..0000000 --- a/examples/helloworld.trainfuck +++ /dev/null | |||
@@ -1,152 +0,0 @@ | |||
1 | TRAINFUCK BABEEE | ||
2 | CHOO CHOO MUTHERFUCKER | ||
3 | |||
4 | This example adapted from https://en.wikipedia.org/wiki/Brainfuck | ||
5 | |||
6 | This program prints "Hello World!" and a newline to the screen, its | ||
7 | length is 106 active command characters. | ||
8 | |||
9 | The program doesn't start until we have ALL ABOARD on a line by itself, so | ||
10 | we've got one better than an initial comment loop. | ||
11 | |||
12 | This file was handwritten, but soon(TM) we'll have a fucktrain command, | ||
13 | that'll translate to trainfuck from brainfuck. | ||
14 | |||
15 | ALL ABOARD | ||
16 | chug chug chug chug chug chug chug chug | ||
17 | END OF THE LINE | ||
18 | Set Cell #0 to 8 | ||
19 | (Also note: you can have comments betwen END OF THE LINE and ALL ABOARD) | ||
20 | ALL ABOARD | ||
21 | tickets please | ||
22 | choo | ||
23 | chug chug chug chug | ||
24 | END OF THE LINE | ||
25 | Add 4 to Cell #1; this will always set Cell #1 to 4 | ||
26 | ALL ABOARD | ||
27 | tickets please | ||
28 | END OF THE LINE | ||
29 | as the cell will be cleared by the loop | ||
30 | ALL ABOARD | ||
31 | choo | ||
32 | chug chug | ||
33 | END OF THE LINE | ||
34 | Add 2 to Cell #2 | ||
35 | ALL ABOARD | ||
36 | choo | ||
37 | chug chug chug | ||
38 | END OF THE LINE | ||
39 | Add 3 to Cell #3 | ||
40 | ALL ABOARD | ||
41 | choo | ||
42 | chug chug chug | ||
43 | END OF THE LINE | ||
44 | Add 3 to Cell #4 | ||
45 | ALL ABOARD | ||
46 | choo | ||
47 | chug | ||
48 | END OF THE LINE | ||
49 | Add 1 to Cell #5 | ||
50 | ALL ABOARD | ||
51 | choo choo | ||
52 | choo choo | ||
53 | choo choo | ||
54 | choo choo | ||
55 | chugga | ||
56 | END OF THE LINE | ||
57 | Decrement the loop counter in Cell #1 | ||
58 | ALL ABOARD | ||
59 | your ticket please | ||
60 | END OF THE LINE | ||
61 | Loop till Cell #1 is zero; number of iterations is 4 | ||
62 | ALL ABOARD | ||
63 | choo | ||
64 | chug | ||
65 | END OF THE LINE | ||
66 | Add 1 to Cell #2 | ||
67 | ALL ABOARD | ||
68 | choo | ||
69 | chug | ||
70 | END OF THE LINE | ||
71 | Add 1 to Cell #3 | ||
72 | ALL ABOARD | ||
73 | choo | ||
74 | chugga | ||
75 | END OF THE LINE | ||
76 | Subtract 1 from Cell #4 | ||
77 | ALL ABOARD | ||
78 | choo | ||
79 | choo | ||
80 | chug | ||
81 | END OF THE LINE | ||
82 | Add 1 to Cell #6 | ||
83 | ALL ABOARD | ||
84 | tickets pleasechoo choo | ||
85 | your ticket please | ||
86 | END OF THE LINE | ||
87 | Move back to the first zero cell you find; this will | ||
88 | ALL ABOARD | ||
89 | |||
90 | END OF THE LINE | ||
91 | be Cell #1 which was cleared by the previous loop | ||
92 | ALL ABOARD | ||
93 | choo choo | ||
94 | chugga | ||
95 | END OF THE LINE | ||
96 | Decrement the loop Counter in Cell #0 | ||
97 | ALL ABOARD | ||
98 | your ticket please | ||
99 | END OF THE LINE | ||
100 | Loop till Cell #0 is zero; number of iterations is 8 | ||
101 | ALL ABOARD | ||
102 | END OF THE LINE | ||
103 | The result of this is: | ||
104 | Cell No : 0 1 2 3 4 5 6 | ||
105 | Contents: 0 0 72 104 88 32 8 | ||
106 | Pointer : ^ | ||
107 | |||
108 | ALL ABOARD | ||
109 | choo | ||
110 | choo | ||
111 | click | ||
112 | END OF THE LINE | ||
113 | Cell #2 has value 72 which is 'H' | ||
114 | ALL ABOARD | ||
115 | choo | ||
116 | chugga chugga chugga click | ||
117 | END OF THE LINE | ||
118 | Subtract 3 from Cell #3 to get 101 which is 'e' | ||
119 | ALL ABOARD | ||
120 | chug chug chug chug chug chug chug click click chug chug chug click | ||
121 | END OF THE LINE | ||
122 | Likewise for 'llo' from Cell #3 | ||
123 | ALL ABOARD | ||
124 | choo | ||
125 | choo | ||
126 | click | ||
127 | END OF THE LINE | ||
128 | Cell #5 is 32 for the space | ||
129 | ALL ABOARD | ||
130 | choo choo | ||
131 | chugga click | ||
132 | END OF THE LINE | ||
133 | Subtract 1 from Cell #4 for 87 to give a 'W' | ||
134 | ALL ABOARD | ||
135 | choo choo | ||
136 | click | ||
137 | END OF THE LINE | ||
138 | Cell #3 was set to 'o' from the end of 'Hello' | ||
139 | ALL ABOARD | ||
140 | chug chug chug click chugga chugga chugga chugga chugga chugga click chugga chugga chugga chugga chugga chugga chugga chugga click | ||
141 | END OF THE LINE | ||
142 | Cell #3 for 'rl' and 'd' | ||
143 | ALL ABOARD | ||
144 | choo | ||
145 | choo | ||
146 | chug click | ||
147 | END OF THE LINE | ||
148 | Add 1 to Cell #5 gives us an exclamation point | ||
149 | ALL ABOARD | ||
150 | choo | ||
151 | chug chug click | ||
152 | END OF THE LINE | ||
diff --git a/examples/rot13.bf b/examples/rot13.bf new file mode 100644 index 0000000..bfcde73 --- /dev/null +++ b/examples/rot13.bf | |||
@@ -0,0 +1,32 @@ | |||
1 | [ ROT13 in Brainfuck | ||
2 | from https://en.wikipedia.org/wiki/Brainfuck#ROT13 | ||
3 | ] | ||
4 | |||
5 | -,+[ Read first character and start outer character reading loop | ||
6 | -[ Skip forward if character is 0 | ||
7 | >>++++[>++++++++<-] Set up divisor (32) for division loop | ||
8 | (MEMORY LAYOUT: dividend copy remainder divisor quotient zero zero) | ||
9 | <+<-[ Set up dividend (x minus 1) and enter division loop | ||
10 | >+>+>-[>>>] Increase copy and remainder / reduce divisor / Normal case: skip forward | ||
11 | <[[>+<-]>>+>] Special case: move remainder back to divisor and increase quotient | ||
12 | <<<<<- Decrement dividend | ||
13 | ] End division loop | ||
14 | ]>>>[-]+ End skip loop; zero former divisor and reuse space for a flag | ||
15 | >--[-[<->+++[-]]]<[ Zero that flag unless quotient was 2 or 3; zero quotient; check flag | ||
16 | ++++++++++++<[ If flag then set up divisor (13) for second division loop | ||
17 | (MEMORY LAYOUT: zero copy dividend divisor remainder quotient zero zero) | ||
18 | >-[>+>>] Reduce divisor; Normal case: increase remainder | ||
19 | >[+[<+>-]>+>>] Special case: increase remainder / move it back to divisor / increase quotient | ||
20 | <<<<<- Decrease dividend | ||
21 | ] End division loop | ||
22 | >>[<+>-] Add remainder back to divisor to get a useful 13 | ||
23 | >[ Skip forward if quotient was 0 | ||
24 | -[ Decrement quotient and skip forward if quotient was 1 | ||
25 | -<<[-]>> Zero quotient and divisor if quotient was 2 | ||
26 | ]<<[<<->>-]>> Zero divisor and subtract 13 from copy if quotient was 1 | ||
27 | ]<<[<<+>>-] Zero divisor and add 13 to copy if quotient was 0 | ||
28 | ] End outer skip loop (jump to here if ((character minus 1)/32) was not 2 or 3) | ||
29 | <[-] Clear remainder from first division if second division was skipped | ||
30 | <.[-] Output ROT13ed character from copy and clear it | ||
31 | <-,+ Read next character | ||
32 | ] End character reading loop | ||