diff options
Diffstat (limited to 'examples/add2.tf')
-rw-r--r-- | examples/add2.tf | 441 |
1 files changed, 27 insertions, 414 deletions
diff --git a/examples/add2.tf b/examples/add2.tf index 3c1529b..bdb0ddf 100644 --- a/examples/add2.tf +++ b/examples/add2.tf | |||
@@ -5,7 +5,7 @@ from https://en.wikipedia.org/wiki/Brainfuck#Adding_two_values | |||
5 | 5 | ||
6 | 6 | ||
7 | ALL ABOARD | 7 | ALL ABOARD |
8 | chug chug | 8 | chug chug |
9 | END OF THE LINE | 9 | END OF THE LINE |
10 | Cell c0 = 2 | 10 | Cell c0 = 2 |
11 | 11 | ||
@@ -13,15 +13,15 @@ ALL ABOARD | |||
13 | choo | 13 | choo |
14 | 14 | ||
15 | END OF THE LINE | 15 | END OF THE LINE |
16 | 16 | ||
17 | ALL ABOARD | 17 | ALL ABOARD |
18 | chug chug chug chug chug | 18 | chug chug chug chug chug |
19 | END OF THE LINE | 19 | END OF THE LINE |
20 | Cell c1 = 5 | 20 | Cell c1 = 5 |
21 | 21 | ||
22 | 22 | ||
23 | ALL ABOARD | 23 | ALL ABOARD |
24 | tickets please | 24 | tickets please |
25 | END OF THE LINE | 25 | END OF THE LINE |
26 | Start your loops with your cell pointer on the loop counter (c1 in our case) | 26 | Start your loops with your cell pointer on the loop counter (c1 in our case) |
27 | 27 | ||
@@ -29,9 +29,9 @@ ALL ABOARD | |||
29 | choo choo | 29 | choo choo |
30 | 30 | ||
31 | END OF THE LINE | 31 | END OF THE LINE |
32 | 32 | ||
33 | ALL ABOARD | 33 | ALL ABOARD |
34 | chug | 34 | chug |
35 | END OF THE LINE | 35 | END OF THE LINE |
36 | Add 1 to c0 | 36 | Add 1 to c0 |
37 | 37 | ||
@@ -39,14 +39,14 @@ ALL ABOARD | |||
39 | choo | 39 | choo |
40 | 40 | ||
41 | END OF THE LINE | 41 | END OF THE LINE |
42 | 42 | ||
43 | ALL ABOARD | 43 | ALL ABOARD |
44 | chugga | 44 | chugga |
45 | END OF THE LINE | 45 | END OF THE LINE |
46 | Subtract 1 from c1 | 46 | Subtract 1 from c1 |
47 | 47 | ||
48 | ALL ABOARD | 48 | ALL ABOARD |
49 | your ticket please | 49 | your ticket please |
50 | END OF THE LINE | 50 | END OF THE LINE |
51 | End your loops with the cell pointer on the loop counter | 51 | End your loops with the cell pointer on the loop counter |
52 | 52 | ||
@@ -58,26 +58,26 @@ We use a loop to compute 48 = 6 * 8 | |||
58 | 58 | ||
59 | 59 | ||
60 | ALL ABOARD | 60 | ALL ABOARD |
61 | chug chug chug chug | 61 | chug chug chug chug |
62 | END OF THE LINE | 62 | END OF THE LINE |
63 | 63 | ||
64 | ALL ABOARD | 64 | ALL ABOARD |
65 | chug chug chug chug | 65 | chug chug chug chug |
66 | END OF THE LINE | 66 | END OF THE LINE |
67 | c1 = 8 and this will be our loop counter again | 67 | c1 = 8 and this will be our loop counter again |
68 | 68 | ||
69 | ALL ABOARD | 69 | ALL ABOARD |
70 | tickets please | 70 | tickets please |
71 | choo choo | 71 | choo choo |
72 | 72 | ||
73 | END OF THE LINE | 73 | END OF THE LINE |
74 | 74 | ||
75 | ALL ABOARD | 75 | ALL ABOARD |
76 | chug chug chug | 76 | chug chug chug |
77 | END OF THE LINE | 77 | END OF THE LINE |
78 | 78 | ||
79 | ALL ABOARD | 79 | ALL ABOARD |
80 | chug chug chug | 80 | chug chug chug |
81 | END OF THE LINE | 81 | END OF THE LINE |
82 | Add 6 to c0 | 82 | Add 6 to c0 |
83 | 83 | ||
@@ -85,416 +85,29 @@ ALL ABOARD | |||
85 | choo | 85 | choo |
86 | 86 | ||
87 | END OF THE LINE | 87 | END OF THE LINE |
88 | 88 | ||
89 | ALL ABOARD | 89 | ALL ABOARD |
90 | chugga | 90 | chugga |
91 | END OF THE LINE | 91 | END OF THE LINE |
92 | Subtract 1 from c1 | 92 | Subtract 1 from c1 |
93 | 93 | ||
94 | ALL ABOARD | 94 | ALL ABOARD |
95 | your ticket please | 95 | your ticket please |
96 | choo choo | 96 | choo choo |
97 | 97 | ||
98 | END OF THE LINE | 98 | END OF THE LINE |
99 | 99 | ||
100 | ALL ABOARD | 100 | ALL ABOARD |
101 | click | 101 | click |
102 | END OF THE LINE | 102 | END OF THE LINE |
103 | Print out c0 which has the value 55 which translates to "7"! | 103 | Print out c0 which has the value 55 which translates to "7"! |
104 | 104 | ||
105 | Finally print a newline: | 105 | Finally print a newline: |
106 | 106 | ||
107 | ALL ABOARD | 107 | ALL ABOARD |
108 | chugga chugga chugga chugga chugga chugga chugga chugga chugga chugga chugga | 108 | chugga chugga chugga chugga chugga chugga chugga chugga chugga chugga chugga |
109 | chugga chugga chugga chugga chugga chugga chugga chugga chugga chugga chugga | 109 | chugga chugga chugga chugga chugga chugga chugga chugga chugga chugga chugga |
110 | chugga chugga chugga chugga chugga chugga chugga chugga chugga chugga chugga | 110 | chugga chugga chugga chugga chugga chugga chugga chugga chugga chugga chugga |
111 | chugga chugga chugga chugga chugga chugga chugga chugga chugga chugga chugga | 111 | chugga chugga chugga chugga chugga chugga chugga chugga chugga chugga chugga |
112 | chugga click | 112 | chugga click |
113 | This program prints "Hello World!" and a newline to the screen, its | ||
114 | length is 106 active command characters. [It is not the shortest.] | ||
115 | |||
116 | This loop is an "initial comment loop", a simple way of adding a comment | ||
117 | to a BF program such that you don't have to worry about any command | ||
118 | characters. Any ".", ",", "+", "-", "<" and ">" characters are simply | ||
119 | ignored, the "[" and "]" characters just have to be balanced. This | ||
120 | loop and the commands it contains are ignored because the current cell | ||
121 | defaults to a value of 0; the 0 value causes this loop to be skipped. | ||
122 | |||
123 | from https://en.wikipedia.org/wiki/Brainfuck#Hello_World! | ||
124 | |||
125 | |||
126 | ALL ABOARD | ||
127 | chug chug chug chug chug chug chug chug | ||
128 | END OF THE LINE | ||
129 | Set Cell #0 to 8 | ||
130 | |||
131 | ALL ABOARD | ||
132 | tickets please | ||
133 | choo | ||
134 | chug chug chug chug | ||
135 | END OF THE LINE | ||
136 | Add 4 to Cell #1; this will always set Cell #1 to 4 | ||
137 | |||
138 | ALL ABOARD | ||
139 | tickets please | ||
140 | END OF THE LINE | ||
141 | as the cell will be cleared by the loop | ||
142 | |||
143 | ALL ABOARD | ||
144 | choo | ||
145 | chug chug | ||
146 | END OF THE LINE | ||
147 | Add 2 to Cell #2 | ||
148 | |||
149 | ALL ABOARD | ||
150 | choo | ||
151 | chug chug chug | ||
152 | END OF THE LINE | ||
153 | Add 3 to Cell #3 | ||
154 | |||
155 | ALL ABOARD | ||
156 | choo | ||
157 | chug chug chug | ||
158 | END OF THE LINE | ||
159 | Add 3 to Cell #4 | ||
160 | |||
161 | ALL ABOARD | ||
162 | choo | ||
163 | chug | ||
164 | END OF THE LINE | ||
165 | Add 1 to Cell #5 | ||
166 | |||
167 | ALL ABOARD | ||
168 | choo choo | ||
169 | choo choo | ||
170 | choo choo | ||
171 | choo choo | ||
172 | chugga | ||
173 | END OF THE LINE | ||
174 | Decrement the loop counter in Cell #1 | ||
175 | |||
176 | ALL ABOARD | ||
177 | your ticket please | ||
178 | END OF THE LINE | ||
179 | Loop until Cell #1 is zero; number of iterations is 4 | ||
180 | |||
181 | ALL ABOARD | ||
182 | choo | ||
183 | chug | ||
184 | END OF THE LINE | ||
185 | Add 1 to Cell #2 | ||
186 | |||
187 | ALL ABOARD | ||
188 | choo | ||
189 | chug | ||
190 | END OF THE LINE | ||
191 | Add 1 to Cell #3 | ||
192 | |||
193 | ALL ABOARD | ||
194 | choo | ||
195 | chugga | ||
196 | END OF THE LINE | ||
197 | Subtract 1 from Cell #4 | ||
198 | |||
199 | ALL ABOARD | ||
200 | choo | ||
201 | choo | ||
202 | chug | ||
203 | END OF THE LINE | ||
204 | Add 1 to Cell #6 | ||
205 | |||
206 | ALL ABOARD | ||
207 | tickets please choo choo | ||
208 | your ticket please | ||
209 | END OF THE LINE | ||
210 | Move back to the first zero cell you find; this will | ||
211 | be Cell #1 which was cleared by the previous loop | ||
212 | |||
213 | ALL ABOARD | ||
214 | choo choo | ||
215 | chugga | ||
216 | END OF THE LINE | ||
217 | Decrement the loop Counter in Cell #0 | ||
218 | |||
219 | ALL ABOARD | ||
220 | your ticket please | ||
221 | END OF THE LINE | ||
222 | Loop until Cell #0 is zero; number of iterations is 8 | ||
223 | |||
224 | The result of this is: | ||
225 | Cell no : 0 1 2 3 4 5 6 | ||
226 | Contents: 0 0 72 104 88 32 8 | ||
227 | Pointer : ^ | ||
228 | |||
229 | |||
230 | ALL ABOARD | ||
231 | choo | ||
232 | choo | ||
233 | click | ||
234 | END OF THE LINE | ||
235 | Cell #2 has value 72 which is 'H' | ||
236 | |||
237 | ALL ABOARD | ||
238 | choo | ||
239 | chugga chugga chugga click | ||
240 | END OF THE LINE | ||
241 | Subtract 3 from Cell #3 to get 101 which is 'e' | ||
242 | |||
243 | ALL ABOARD | ||
244 | chug chug chug chug chug chug chug clickety click chug chug chug click | ||
245 | |||
246 | END OF THE LINE | ||
247 | Likewise for 'llo' from Cell #3 | ||
248 | |||
249 | ALL ABOARD | ||
250 | choo | ||
251 | choo | ||
252 | click | ||
253 | END OF THE LINE | ||
254 | Cell #5 is 32 for the space | ||
255 | |||
256 | ALL ABOARD | ||
257 | choo choo | ||
258 | chugga clickety | ||
259 | END OF THE LINE | ||
260 | Subtract 1 from Cell #4 for 87 to give a 'W' | ||
261 | |||
262 | ALL ABOARD | ||
263 | choo choo | ||
264 | clickety | ||
265 | END OF THE LINE | ||
266 | Cell #3 was set to 'o' from the end of 'Hello' | ||
267 | |||
268 | ALL ABOARD | ||
269 | chug chug chug click chugga chugga chugga chugga chugga chugga clickety | ||
270 | chugga chugga chugga chugga chugga chugga chugga chugga click | ||
271 | END OF THE LINE | ||
272 | Cell #3 for 'rl' and 'd' | ||
273 | |||
274 | ALL ABOARD | ||
275 | choo | ||
276 | choo | ||
277 | chug click | ||
278 | END OF THE LINE | ||
279 | Add 1 to Cell #5 gives us an exclamation point | ||
280 | |||
281 | ALL ABOARD | ||
282 | choo | ||
283 | chug chug click | ||
284 | END OF THE LINE | ||
285 | And finally a newline from Cell #6 | ||
286 | ROT13 in Brainfuck | ||
287 | from https://en.wikipedia.org/wiki/Brainfuck#ROT13 | ||
288 | |||
289 | |||
290 | |||
291 | |||
292 | ALL ABOARD | ||
293 | chugga clack chug tickets please | ||
294 | END OF THE LINE | ||
295 | Read first character and start outer character reading loop | ||
296 | |||
297 | ALL ABOARD | ||
298 | chugga tickets please | ||
299 | END OF THE LINE | ||
300 | Skip forward if character is 0 | ||
301 | |||
302 | ALL ABOARD | ||
303 | choo | ||
304 | choo | ||
305 | chug chug chug chug tickets please choo | ||
306 | chug chug chug chug chug chug chug chug | ||
307 | choo choo | ||
308 | chugga your ticket please | ||
309 | END OF THE LINE | ||
310 | Set up divisor (32) for division loop | ||
311 | (MEMORY LAYOUT: dividend copy remainder divisor quotient zero zero) | ||
312 | |||
313 | ALL ABOARD | ||
314 | choo choo | ||
315 | chug choo choo | ||
316 | chugga tickets please | ||
317 | END OF THE LINE | ||
318 | Set up dividend (x minus 1) and enter division loop | ||
319 | |||
320 | ALL ABOARD | ||
321 | choo | ||
322 | chug choo | ||
323 | chug choo | ||
324 | chugga tickets please choo | ||
325 | choo | ||
326 | choo | ||
327 | your ticket please | ||
328 | END OF THE LINE | ||
329 | Increase copy and remainder / reduce divisor / Normal case: skip forward | ||
330 | |||
331 | ALL ABOARD | ||
332 | choo choo | ||
333 | tickets please tickets please choo | ||
334 | chug choo choo | ||
335 | chugga your ticket please choo | ||
336 | choo | ||
337 | chug choo | ||
338 | your ticket please | ||
339 | |||
340 | END OF THE LINE | ||
341 | Special case: move remainder back to divisor and increase quotient | ||
342 | |||
343 | ALL ABOARD | ||
344 | choo choo | ||
345 | choo choo | ||
346 | choo choo | ||
347 | choo choo | ||
348 | choo choo | ||
349 | chugga | ||
350 | END OF THE LINE | ||
351 | Decrement dividend | ||
352 | |||
353 | ALL ABOARD | ||
354 | your ticket please | ||
355 | END OF THE LINE | ||
356 | End division loop | ||
357 | |||
358 | ALL ABOARD | ||
359 | your ticket please choo | ||
360 | choo | ||
361 | choo | ||
362 | tickets please chugga your ticket please chug | ||
363 | END OF THE LINE | ||
364 | End skip loop; zero former divisor and reuse space for a flag | ||
365 | |||
366 | ALL ABOARD | ||
367 | choo | ||
368 | chugga chugga tickets please chugga tickets please choo choo | ||
369 | chugga choo | ||
370 | chug chug chug | ||
371 | tickets please chugga your ticket please your ticket please your ticket please | ||
372 | choo choo | ||
373 | tickets please | ||
374 | END OF THE LINE | ||
375 | Zero that flag unless quotient was 2 or 3; zero quotient; check flag | ||
376 | |||
377 | ALL ABOARD | ||
378 | chug chug chug chug chug chug chug chug chug chug chug chug choo choo | ||
379 | tickets please | ||
380 | |||
381 | END OF THE LINE | ||
382 | If flag then set up divisor (13) for second division loop | ||
383 | (MEMORY LAYOUT: zero copy dividend divisor remainder quotient zero zero) | ||
384 | |||
385 | ALL ABOARD | ||
386 | choo | ||
387 | chugga tickets please choo | ||
388 | chug choo | ||
389 | choo | ||
390 | your ticket please | ||
391 | END OF THE LINE | ||
392 | Reduce divisor; Normal case: increase remainder | ||
393 | |||
394 | ALL ABOARD | ||
395 | choo | ||
396 | tickets please chug tickets please choo choo | ||
397 | chug choo | ||
398 | chugga your ticket please choo | ||
399 | chug | ||
400 | choo | ||
401 | choo | ||
402 | your ticket please | ||
403 | END OF THE LINE | ||
404 | Special case: increase remainder / move it back to divisor / increase quotient | ||
405 | |||
406 | ALL ABOARD | ||
407 | choo choo | ||
408 | choo choo | ||
409 | choo choo | ||
410 | choo choo | ||
411 | choo choo | ||
412 | chugga | ||
413 | END OF THE LINE | ||
414 | Decrease dividend | ||
415 | |||
416 | ALL ABOARD | ||
417 | your ticket please | ||
418 | END OF THE LINE | ||
419 | End division loop | ||
420 | |||
421 | ALL ABOARD | ||
422 | choo | ||
423 | choo | ||
424 | tickets please choo choo | ||
425 | chug choo | ||
426 | chugga your ticket please | ||
427 | END OF THE LINE | ||
428 | Add remainder back to divisor to get a useful 13 | ||
429 | |||
430 | ALL ABOARD | ||
431 | choo | ||
432 | tickets please | ||
433 | END OF THE LINE | ||
434 | Skip forward if quotient was 0 | ||
435 | |||
436 | ALL ABOARD | ||
437 | chugga tickets please | ||
438 | END OF THE LINE | ||
439 | Decrement quotient and skip forward if quotient was 1 | ||
440 | |||
441 | ALL ABOARD | ||
442 | chugga choo choo | ||
443 | choo choo | ||
444 | tickets please chugga your ticket please choo | ||
445 | choo | ||
446 | |||
447 | END OF THE LINE | ||
448 | Zero quotient and divisor if quotient was 2 | ||
449 | |||
450 | ALL ABOARD | ||
451 | your ticket please choo choo | ||
452 | choo choo | ||
453 | tickets please choo choo | ||
454 | choo choo | ||
455 | chugga choo | ||
456 | choo | ||
457 | chugga your ticket please choo | ||
458 | choo | ||
459 | |||
460 | END OF THE LINE | ||
461 | Zero divisor and subtract 13 from copy if quotient was 1 | ||
462 | |||
463 | ALL ABOARD | ||
464 | your ticket please choo choo | ||
465 | choo choo | ||
466 | tickets please choo choo | ||
467 | choo choo | ||
468 | chug choo | ||
469 | choo | ||
470 | chugga your ticket please | ||
471 | END OF THE LINE | ||
472 | Zero divisor and add 13 to copy if quotient was 0 | ||
473 | |||
474 | ALL ABOARD | ||
475 | your ticket please | ||
476 | END OF THE LINE | ||
477 | End outer skip loop (jump to here if ((character minus 1)/32) was not 2 or 3) | ||
478 | |||
479 | ALL ABOARD | ||
480 | choo choo | ||
481 | tickets please chugga your ticket please | ||
482 | END OF THE LINE | ||
483 | Clear remainder from first division if second division was skipped | ||
484 | |||
485 | ALL ABOARD | ||
486 | choo choo | ||
487 | click tickets please chugga your ticket please | ||
488 | END OF THE LINE | ||
489 | Output ROT13ed character from copy and clear it | ||
490 | |||
491 | ALL ABOARD | ||
492 | choo choo | ||
493 | chugga clack chug | ||
494 | END OF THE LINE | ||
495 | Read next character | ||
496 | |||
497 | ALL ABOARD | ||
498 | your ticket please | ||
499 | END OF THE LINE | 113 | END OF THE LINE |
500 | End character reading loop | ||