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 – 2015-04-12T03:47:48.087Is the file anyone we choose or do we get it from the command line or stdin? – Juan – 2011-01-28T03:25:32.913
@Juan: You have to take input to the program you're interpreting on stdin. – Anon. – 2011-01-28T03:53:01.163
162 byte C version: http://j.mearie.org/post/1181041789/brainfuck-interpreter-in-2-lines-of-c
– luser droog – 2015-07-04T05:05:32.973I'd post the one I made a while ago, but it's over 1000 characters. – The_Basset_Hound – 2015-08-31T22:32:26.550
@Hannesh, https://code.google.com/p/awib/
– SeanC – 2012-09-12T19:14:15.1473I wonder if there should be two categories: Those programs that use eval (or shell out to compile) -- and those that don't. – MtnViewMark – 2011-02-15T07:52:27.457
Are cells signed or unsigned? – JPvdMerwe – 2011-01-28T11:38:30.323
@JPvdMerwe: I think that depends on the language. For example Python has unsigned bytes, Java signed bytes. – Alexandru – 2011-01-28T12:11:00.647
@Hannesh: http://esolangs.org/wiki/brainfuck#Self-interpreters
– M L – 2016-03-23T23:34:12.3003What 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 – 2016-04-01T14:07:31.2203Likewise, 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 – 2016-04-01T14:09:45.220
How do you end the
primes up to:
prompt? What doesalt 1 0
do? – Cyoce – 2016-05-03T22:37:00.3071I'm VTC as unclear because of the issues that Martin brought up. – Mego – 2016-06-30T05:09:00.580
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 – 2011-01-29T02:54:43.097
34I'd love to see someone answer this in brainfuck. – Hannesh – 2011-03-14T19:15:06.693
@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 – 2011-01-29T16:44:27.003
@SeanCheshire But that is a compiler, not an interpreter. – Justin – 2014-01-15T22:38:41.027
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 – 2011-01-30T15:14:03.480
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 – 2011-01-30T15:26:40.6002Another 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 – 2011-01-30T15:33:14.7031@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 – 2011-01-30T15:36:17.710How the array must be initialized ? with which value ? – BenjaminB – 2011-06-15T17:16:53.827
5
You should clarify about 1) size of memory 2) is memory circled 4) maybe any other details
– Nakilon – 2011-01-28T01:37:22.180@sepp2k: good one. – Alexandru – 2011-01-28T01:44:19.960