BF is a language that was designed to challenge and amuse programmers. BF uses a simple machine model consisting, besides the program, of:
- An array of 30,000 byte cells – byte value ranges from 0 to 255 - initialized to zeroes.
- A movable pointer into a circular array (initialized to point to location 0)
- Two streams of bytes for input and output (most often connected to a keyboard and a monitor respectively, and using the ASCII character encoding).
In this problem you are asked to implement a BF interpreter, your program should read a BF program and an input stream and should print the output stream.
BF syntax is easy, only the following commands are available:
Character | Spelled as | Meaning |
> | Greater than | Increment the pointer (to point to the next cell to the right). |
< | Less than | Decrement the pointer (to point to the next cell to the left). |
+ | Plus sign | Increment (increase by one) the byte at the pointer. |
- | Minus sign | Decrement (decrease by one) the byte at the pointer. |
. | Dot (period) | Puts the value of the byte at the pointer into the output stream. |
, | Comma | Accept one byte of input - from the input stream - storing its value in the byte at the pointer. |
[ | Left square bracket | Jump forward to the command after the corresponding ‘]’ if the byte at the pointer is zero. |
] | Right square bracket | Jump back to the command after the corresponding ‘[‘ if the byte at the pointer is nonzero. |
The given BF programs will be syntactically valid also the ‘>’ and ‘<’ operators will never lead to a value outside range [0-30000].