about summary refs log tree commit diff stats
path: root/examples
diff options
context:
space:
mode:
authorCase Duckworth2022-06-29 00:59:59 -0500
committerCase Duckworth2022-06-29 00:59:59 -0500
commit0a78720644e45bd27c4a57cbddf2d32aacb555c3 (patch)
tree29783dc9a534dbbfe172d5c84e66f5efcd9c43dc /examples
parentRecognize bf comments (diff)
downloadtrainfuck-0a78720644e45bd27c4a57cbddf2d32aacb555c3.tar.gz
trainfuck-0a78720644e45bd27c4a57cbddf2d32aacb555c3.zip
Probably version ... whatever the next higher one is
Diffstat (limited to 'examples')
-rw-r--r--examples/add2.bf27
-rw-r--r--examples/hello-world.bf45
-rw-r--r--examples/helloworld.bf1
-rw-r--r--examples/helloworld.trainfuck152
-rw-r--r--examples/rot13.bf32
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
2from 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
13At this point our program has added 5 to 2 leaving 7 in c0 and 0 in c1
14but we cannot output this value to the terminal since it is not ASCII encoded
15
16To display the ASCII character "7" we must add 48 to the value 7
17We 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
26Finally 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
2length is 106 active command characters. [It is not the shortest.]
3
4This loop is an "initial comment loop", a simple way of adding a comment
5to a BF program such that you don't have to worry about any command
6characters. Any ".", ",", "+", "-", "<" and ">" characters are simply
7ignored, the "[" and "]" characters just have to be balanced. This
8loop and the commands it contains are ignored because the current cell
9defaults to a value of 0; the 0 value causes this loop to be skipped.
10
11from 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
28be 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
32The result of this is:
33Cell no : 0 1 2 3 4 5 6
34Contents: 0 0 72 104 88 32 8
35Pointer : ^
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 @@
1TRAINFUCK BABEEE
2CHOO CHOO MUTHERFUCKER
3
4This example adapted from https://en.wikipedia.org/wiki/Brainfuck
5
6This program prints "Hello World!" and a newline to the screen, its
7length is 106 active command characters.
8
9The program doesn't start until we have ALL ABOARD on a line by itself, so
10we've got one better than an initial comment loop.
11
12This file was handwritten, but soon(TM) we'll have a fucktrain command,
13that'll translate to trainfuck from brainfuck.
14
15ALL ABOARD
16chug chug chug chug chug chug chug chug
17END OF THE LINE
18 Set Cell #0 to 8
19 (Also note: you can have comments betwen END OF THE LINE and ALL ABOARD)
20ALL ABOARD
21tickets please
22choo
23chug chug chug chug
24END OF THE LINE
25 Add 4 to Cell #1; this will always set Cell #1 to 4
26ALL ABOARD
27tickets please
28END OF THE LINE
29 as the cell will be cleared by the loop
30ALL ABOARD
31choo
32chug chug
33END OF THE LINE
34 Add 2 to Cell #2
35ALL ABOARD
36choo
37chug chug chug
38END OF THE LINE
39 Add 3 to Cell #3
40ALL ABOARD
41choo
42chug chug chug
43END OF THE LINE
44 Add 3 to Cell #4
45ALL ABOARD
46choo
47chug
48END OF THE LINE
49 Add 1 to Cell #5
50ALL ABOARD
51choo choo
52choo choo
53choo choo
54choo choo
55chugga
56END OF THE LINE
57 Decrement the loop counter in Cell #1
58ALL ABOARD
59your ticket please
60END OF THE LINE
61 Loop till Cell #1 is zero; number of iterations is 4
62ALL ABOARD
63choo
64chug
65END OF THE LINE
66 Add 1 to Cell #2
67ALL ABOARD
68choo
69chug
70END OF THE LINE
71 Add 1 to Cell #3
72ALL ABOARD
73choo
74chugga
75END OF THE LINE
76 Subtract 1 from Cell #4
77ALL ABOARD
78choo
79choo
80chug
81END OF THE LINE
82 Add 1 to Cell #6
83ALL ABOARD
84tickets pleasechoo choo
85your ticket please
86END OF THE LINE
87 Move back to the first zero cell you find; this will
88ALL ABOARD
89
90END OF THE LINE
91 be Cell #1 which was cleared by the previous loop
92ALL ABOARD
93choo choo
94chugga
95END OF THE LINE
96 Decrement the loop Counter in Cell #0
97ALL ABOARD
98your ticket please
99END OF THE LINE
100 Loop till Cell #0 is zero; number of iterations is 8
101ALL ABOARD
102END OF THE LINE
103The result of this is:
104Cell No : 0 1 2 3 4 5 6
105Contents: 0 0 72 104 88 32 8
106Pointer : ^
107
108ALL ABOARD
109choo
110choo
111click
112END OF THE LINE
113 Cell #2 has value 72 which is 'H'
114ALL ABOARD
115choo
116chugga chugga chugga click
117END OF THE LINE
118 Subtract 3 from Cell #3 to get 101 which is 'e'
119ALL ABOARD
120chug chug chug chug chug chug chug click click chug chug chug click
121END OF THE LINE
122 Likewise for 'llo' from Cell #3
123ALL ABOARD
124choo
125choo
126click
127END OF THE LINE
128 Cell #5 is 32 for the space
129ALL ABOARD
130choo choo
131chugga click
132END OF THE LINE
133 Subtract 1 from Cell #4 for 87 to give a 'W'
134ALL ABOARD
135choo choo
136click
137END OF THE LINE
138 Cell #3 was set to 'o' from the end of 'Hello'
139ALL ABOARD
140chug chug chug click chugga chugga chugga chugga chugga chugga click chugga chugga chugga chugga chugga chugga chugga chugga click
141END OF THE LINE
142 Cell #3 for 'rl' and 'd'
143ALL ABOARD
144choo
145choo
146chug click
147END OF THE LINE
148 Add 1 to Cell #5 gives us an exclamation point
149ALL ABOARD
150choo
151chug chug click
152END 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
2from 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