114
29
Write the shortest program in your favourite language to interpret a brainfuck program. The program is read from a file. Input and output are standard input and standard output.
- Cell size: 8bit unsigned. Overflow is undefined.
- Array size: 30000 bytes (not circled)
- Bad commands are not part of the input
Comments begin with # and extend to the end of lineComments are everything not in+-.,[]<>
- no EOF symbol
A very good test can be found here. It reads a number and then prints the prime numbers up to that number. To prevent link rot, here is a copy of the code:
compute prime numbers
to use type the max number then push Alt 1 0
===================================================================
======================== OUTPUT STRING ============================
===================================================================
>++++++++[<++++++++>-]<++++++++++++++++.[-]
>++++++++++[<++++++++++>-]<++++++++++++++.[-]
>++++++++++[<++++++++++>-]<+++++.[-]
>++++++++++[<++++++++++>-]<+++++++++.[-]
>++++++++++[<++++++++++>-]<+.[-]
>++++++++++[<++++++++++>-]<+++++++++++++++.[-]
>+++++[<+++++>-]<+++++++.[-]
>++++++++++[<++++++++++>-]<+++++++++++++++++.[-]
>++++++++++[<++++++++++>-]<++++++++++++.[-]
>+++++[<+++++>-]<+++++++.[-]
>++++++++++[<++++++++++>-]<++++++++++++++++.[-]
>++++++++++[<++++++++++>-]<+++++++++++.[-]
>+++++++[<+++++++>-]<+++++++++.[-]
>+++++[<+++++>-]<+++++++.[-]
===================================================================
======================== INPUT NUMBER ============================
===================================================================
+ cont=1
[
- cont=0
>,
======SUB10======
----------
[ not 10
<+> cont=1
=====SUB38======
----------
----------
----------
--------
>
=====MUL10=======
[>+>+<<-]>>[<<+>>-]< dup
>>>+++++++++
[
<<<
[>+>+<<-]>>[<<+>>-]< dup
[<<+>>-]
>>-
]
<<<[-]<
======RMOVE1======
<
[>+<-]
]
<
]
>>[<<+>>-]<<
===================================================================
======================= PROCESS NUMBER ===========================
===================================================================
==== ==== ==== ====
numd numu teid teiu
==== ==== ==== ====
>+<-
[
>+
======DUP======
[>+>+<<-]>>[<<+>>-]<
>+<--
>>>>>>>>+<<<<<<<< isprime=1
[
>+
<-
=====DUP3=====
<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<<<
=====DUP2=====
>[>>+>+<<<-]>>>[<<<+>>>-]<<< <
>>>
====DIVIDES=======
[>+>+<<-]>>[<<+>>-]< DUP i=div
<<
[
>>>>>+ bool=1
<<<
[>+>+<<-]>>[<<+>>-]< DUP
[>>[-]<<-] IF i THEN bool=0
>>
[ IF i=0
<<<<
[>+>+<<-]>>[<<+>>-]< i=div
>>>
- bool=0
]
<<<
- DEC i
<<
-
]
+>>[<<[-]>>-]<<
>[-]< CLR div
=====END DIVIDES====
[>>>>>>[-]<<<<<<-] if divides then isprime=0
<<
>>[-]>[-]<<<
]
>>>>>>>>
[
-
<<<<<<<[-]<<
[>>+>+<<<-]>>>[<<<+>>>-]<<<
>>
===================================================================
======================== OUTPUT NUMBER ===========================
===================================================================
[>+<-]>
[
======DUP======
[>+>+<<-]>>[<<+>>-]<
======MOD10====
>+++++++++<
[
>>>+<< bool= 1
[>+>[-]<<-] bool= ten==0
>[<+>-] ten = tmp
>[<<++++++++++>>-] if ten=0 ten=10
<<- dec ten
<- dec num
]
+++++++++ num=9
>[<->-]< dec num by ten
=======RROT======
[>+<-]
< [>+<-]
< [>+<-]
>>>[<<<+>>>-]
<
=======DIV10========
>+++++++++<
[
>>>+<< bool= 1
[>+>[-]<<-] bool= ten==0
>[<+>-] ten = tmp
>[<<++++++++++>>>+<-] if ten=0 ten=10 inc div
<<- dec ten
<- dec num
]
>>>>[<<<<+>>>>-]<<<< copy div to num
>[-]< clear ten
=======INC1=========
<+>
]
<
[
=======MOVER=========
[>+<-]
=======ADD48========
+++++++[<+++++++>-]<->
=======PUTC=======
<.[-]>
======MOVEL2========
>[<<+>>-]<
<-
]
>++++[<++++++++>-]<.[-]
===================================================================
=========================== END FOR ===============================
===================================================================
>>>>>>>
]
<<<<<<<<
>[-]<
[-]
<<-
]
======LF========
++++++++++.[-]
@
Example run:
$ python2 bf.py PRIME.BF
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
1
@Hannesh http://codegolf.stackexchange.com/a/37887/38891 Someone actually did it.
– ASCIIThenANSI – 10 years agoIs the file anyone we choose or do we get it from the command line or stdin? – Juan – 14 years ago
@Juan: You have to take input to the program you're interpreting on stdin. – Anon. – 14 years ago
162 byte C version: http://j.mearie.org/post/1181041789/brainfuck-interpreter-in-2-lines-of-c
– luser droog – 10 years agoI'd post the one I made a while ago, but it's over 1000 characters. – The_Basset_Hound – 9 years ago
@Hannesh, https://code.google.com/p/awib/
– SeanC – 12 years ago3I wonder if there should be two categories: Those programs that use eval (or shell out to compile) -- and those that don't. – MtnViewMark – 14 years ago
Are cells signed or unsigned? – JPvdMerwe – 14 years ago
@JPvdMerwe: I think that depends on the language. For example Python has unsigned bytes, Java signed bytes. – Alexandru – 14 years ago
@Hannesh: http://esolangs.org/wiki/brainfuck#Self-interpreters
– M L – 9 years ago3What does "no EOF symbol" mean? That the cell value remains unchanged when trying
,
on EOF? Or that it's up to us to choose a value when trying,
on EOF? Or is EOF undefined behaviour altogether? – Martin Ender – 9 years ago3Likewise, what should happen when someone tries to leave the 30k cells to either side? Should the tape head remain in place or is this undefined behaviour? – Martin Ender – 9 years ago
How do you end the
primes up to:
prompt? What doesalt 1 0
do? – Cyoce – 9 years ago1I'm VTC as unclear because of the issues that Martin brought up. – Mego – 9 years ago
1When you say bad commands are not part of the input, you mean that all programs will behave in the given guidelines? Or is validation on where the memory pointer is and what values are in memory needed? Also, is there a limit to the input file size? – Juan – 14 years ago
34I'd love to see someone answer this in brainfuck. – Hannesh – 14 years ago
@Juan: 1. Yes, all programs behave in the given guidelines. 2. No limit on input file size, but it will fit in ram with plenty of extra space for your internal data structures. – Alexandru – 14 years ago
@SeanCheshire But that is a compiler, not an interpreter. – Justin – 11 years ago
Is integer overflow an error or is it defined to wrap around? I mean if we don't have access to 8-bit integers do we have to use modulo on it, or is anything ok as long as it works as expected for integers that never exceed the 8-bit range? – sepp2k – 14 years ago
My suggestion is not to bother with error checking. In my implementation cells are unsigned 8-bit and wrap around (
b[i]=(b[i]+1)&255
). – Alexandru – 14 years ago2Another question: Why the unusual comment syntax? Usually everything that isn't
,.-+[]<>
is a comment in brainfuck. (I'm asking because it makes my implementation quite a bit longer than it needs to be) – sepp2k – 14 years ago1@Alexandru: My point about the behaviour being defined was that if you say "overflow is undefined", then just using normal integers (i.e. removing the
&255
part in your solution) would still leave the solution valid (which I kinda prefer, since it's another way to lose a couple of chars). – sepp2k – 14 years agoHow the array must be initialized ? with which value ? – BenjaminB – 14 years ago
5
You should clarify about 1) size of memory 2) is memory circled 4) maybe any other details
– Nakilon – 14 years ago@sepp2k: good one. – Alexandru – 14 years ago