Write the shortest program in your favourite language to interpret a brainfuck program.
You should take a string of source code, containing only valid brainfuck commands +-[]<>.,
, and a string of ASCII input to the program, and return the output of the brainfuck program.
The program operates on a tape of at least 30000 cells each containing an unsigned 8-bit integer (i.e. 0 to 255). It has a tape pointer, which initially points to the first cell in the tape, and an instruction pointer which points to the first character of the source code.
Brainfuck has eight instructions:
+
increments the cell at the tape pointer by 1.-
decrements the cell at the tape pointer by 1.<
moves the tape pointer left by one cell.>
moves the tape pointer right by one cell.,
reads a byte of input into the cell at the tape pointer. Reading past the end of input is undefined behaviour..
prints the cell at the tape pointer as an ASCII character.[
jumps to its matching]
if the cell at the tape pointer is zero.]
jumps to the instruction before its matching[
. You can assume[
and]
are balanced in the input.
After executing an instruction, the instruction pointer is moved forward by one character.
The cells should contain at least 8-bit unsigned integers (i.e. 0 to 255), and incrementing a cell containing 255 or decrementing 0 is undefined behaviour. Likewise, the tape should contain at least 30000 cellls, and going past these is undefined behaviour. This means that you can implement arbitrarily large integers and an arbitrarily large tape, but the program will never attempt to use values outside of 0...255 or more than 30000 cells.
Here's a test case that reads a decimal integer from input, followed by a newline, and outputs a list of primes up to that number:
>++++++++[<++++++++>-]<++++++++++++++++.[-]>++++++++++[<++++++++++>-]<++++++++++++++.[-]>++++++++++[<++++++++++>-]<+++++.[-]>++++++++++[<++++++++++>-]<+++++++++.[-]>++++++++++[<++++++++++>-]<+.[-]>++++++++++[<++++++++++>-]<+++++++++++++++.[-]>+++++[<+++++>-]<+++++++.[-]>++++++++++[<++++++++++>-]<+++++++++++++++++.[-]>++++++++++[<++++++++++>-]<++++++++++++.[-]>+++++[<+++++>-]<+++++++.[-]>++++++++++[<++++++++++>-]<++++++++++++++++.[-]>++++++++++[<++++++++++>-]<+++++++++++.[-]>+++++++[<+++++++>-]<+++++++++.[-]>+++++[<+++++>-]<+++++++.[-]+[->,----------[<+>-------------------------------------->[>+>+<<-]>>[<<+>>-]<>>>+++++++++[<<<[>+>+<<-]>>[<<+>>-]<[<<+>>-]>>-]<<<[-]<<[>+<-]]<]>>[<<+>>-]<<>+<-[>+[>+>+<<-]>>[<<+>>-]<>+<-->>>>>>>>+<<<<<<<<[>+<-<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<<<>[>>+>+<<<-]>>>[<<<+>>>-]<<<<>>>[>+>+<<-]>>[<<+>>-]<<<[>>>>>+<<<[>+>+<<-]>>[<<+>>-]<[>>[-]<<-]>>[<<<<[>+>+<<-]>>[<<+>>-]<>>>-]<<<-<<-]+>>[<<[-]>>-]<<>[-]<[>>>>>>[-]<<<<<<-]<<>>[-]>[-]<<<]>>>>>>>>[-<<<<<<<[-]<<[>>+>+<<<-]>>>[<<<+>>>-]<<<>>[>+<-]>[[>+>+<<-]>>[<<+>>-]<>+++++++++<[>>>+<<[>+>[-]<<-]>[<+>-]>[<<++++++++++>>-]<<-<-]+++++++++>[<->-]<[>+<-]<[>+<-]<[>+<-]>>>[<<<+>>>-]<>+++++++++<[>>>+<<[>+>[-]<<-]>[<+>-]>[<<++++++++++>>>+<-]<<-<-]>>>>[<<<<+>>>>-]<<<<>[-]<<+>]<[[>+<-]+++++++[<+++++++>-]<-><.[-]>>[<<+>>-]<<-]>++++[<++++++++>-]<.[-]>>>>>>>]<<<<<<<<>[-]<[-]<<-]++++++++++.[-]
Its output will look something like this:
Primes up to: 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Additionally, a few smaller test cases:
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
should take no input and outputHello World!
++++++++[->+>++++++<<]>++[->.+<]
should output0123456789
,[.,]
is a cat program - it should output the string given as input
,
on EOF? Or that it's up to us to choose a value when trying,
on EOF? Or is EOF undefined behaviour altogether? \$\endgroup\$