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