brainfuck: Added a simple Brainfuck interpreter
[nit.git] / contrib / brainfuck / examples / hello2.bf
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   This loop is a "comment loop", it's a simple way of adding a comment
4   to a BF program such that you don't have to worry about any command
5   characters. Any ".", ",", "+", "-", "<" and ">" characters are simply
6   ignored, the "[" and "]" characters just have to be balanced.
7 ]
8 +++++ +++               Set Cell #0 to 8
9 [
10     >++++               Add 4 to Cell #1; this will always set Cell #1 to 4
11     [                   as the cell will be cleared by the loop
12         >++             Add 2 to Cell #2
13         >+++            Add 3 to Cell #3
14         >+++            Add 3 to Cell #4
15         >+              Add 1 to Cell #5
16         <<<<-           Decrement the loop counter in Cell #1
17     ]                   Loop till Cell #1 is zero; number of iterations is 4
18     >+                  Add 1 to Cell #2
19     >+                  Add 1 to Cell #3
20     >-                  Subtract 1 from Cell #4
21     >>+                 Add 1 to Cell #6
22     [<]                 Move back to the first zero cell you find; this will
23                         be Cell #1 which was cleared by the previous loop
24     <-                  Decrement the loop Counter in Cell #0
25 ]                       Loop till Cell #0 is zero; number of iterations is 8
26 The result of this is:
27 Cell No :   0   1   2   3   4   5   6
28 Contents:   0   0  72 104  88  32   8
29 Pointer :   ^
30 >>.                     Cell #2 has value 72 which is 'H'
31 >---.                   Subtract 3 from Cell #3 to get 101 which is 'e'
32 +++++++..+++.          Likewise for 'llo' from Cell #3
33 >>.                     Cell #5 is 32 for the space
34 <-.                     Subtract 1 from Cell #4 for 87 to give a 'W'
35 <.                      Cell #3 was set to 'o' from the end of 'Hello'
36 +++.------.--------.    Cell #3 for 'rl' and 'd'
37 >>+.                    Add 1 to Cell #5 gives us an exclamation point
38 >++.                    And finally a newline from Cell #6