Buffalo Lang

Loops

Buffalo uses the bracket symbols '(' and ')' to delimit loops.

Code inside these brackets will execute until the value at the currently pointed to address when the loop was first entered equals zero.

Buffalo Buffalo         #increment value
Buffalo Buffalo         #increment value
(                       #begin loop
    buffalo Buffalo     #shift pointer right
    Buffalo Buffalo     #increment value
    Buffalo Buffalo     #increment value
    Buffalo buffalo     #shift pointer left
    buffalo buffalo     #decrement value
)

This program demonstrates the calculation 2*2 in Buffalo leaving the value 4 in memory address 1.

 0  1  2  3  4  5  ... 255
[0][4][0][0][0][0][...][0]
 ^

First the program increases the pointed to value twice.

 0  1  2  3  4  5  ... 255
[2][0][0][0][0][0][...][0]
 ^

We then enter the loop and shift the pointer right once.

 0  1  2  3  4  5  ... 255
[2][0][0][0][0][0][...][0]
    ^

The currently pointed to value is then incremented twice.

 0  1  2  3  4  5  ... 255
[2][2][0][0][0][0][...][0]
    ^

The pointer is then shifted left and the pointed to value decremented by one unit.

 0  1  2  3  4  5  ... 255
[1][2][0][0][0][0][...][0]
 ^

As this value is not zero the loop executes again leaving four in memory address 1 and zero in memory address 0 where the loop exits and the program stops.

 0  1  2  3  4  5  ... 255
[0][4][0][0][0][0][...][0]
 ^