
    
        
        
        
            
{"version":"https:\/\/jsonfeed.org\/version\/1","title":"mathspp.com feed","home_page_url":"https:\/\/mathspp.com\/blog\/tags\/brainfuck","feed_url":"https:\/\/mathspp.com\/blog\/tags\/brainfuck.json","description":"Stay up-to-date with the articles on mathematics and programming that get published to mathspp.com.","author":{"name":"Rodrigo Gir\u00e3o Serr\u00e3o"},"items":[{"title":"Implementing an interpreter in 14 lines of Python.","date_published":"2020-11-18T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/writing-interpreter-in-15-loc","url":"https:\/\/mathspp.com\/blog\/writing-interpreter-in-15-loc","content_html":"<p>In this blog post I'll show you how you can write a full interpreter for the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Brainfuck\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">brainf*ck<\/a> programming language in just 14 lines of Python. Be prepared, however, to see some unconventional Python code!<\/p>\n\n<p><img alt=\"A slightly edited photo of the code in question\" src=\"\/images\/1\/9\/8\/6\/b\/1986bfbd47435acea902a92352b4a150320a1446-code.webp\"><\/p>\n<h2 id=\"preview\">Preview<a href=\"#preview\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>By the time you've gone through this blog post, you'll have a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Brainfuck\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">brainf*ck<\/a> interpreter that takes the brainf*ck code <code>+[--&gt;-[&gt;&gt;+&gt;-----&lt;&lt;]&lt;--&lt;---]&gt;-.&gt;&gt;&gt;+.&gt;&gt;..+++[.&gt;]&lt;&lt;&lt;&lt;.+++.------.&lt;&lt;-.&gt;&gt;&gt;&gt;+.<\/code> and interprets it, so that it prints <code>\"Hello, World!\"<\/code>.<\/p>\n<p>And what is more, we'll do it in just 14 lines of (very unconventional!) Python code!<\/p>\n<h3 id=\"the-code\">The code<a href=\"#the-code\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h3>\n<p>You can go ahead and download the code <a href=\"https:\/\/github.com\/RodrigoGiraoSerrao\/languages\/blob\/master\/brainfck\/brainfck_terse_terse.py\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">from GH<\/a>.\nFor your convenience, you can also just copy the code from <a href=\"https:\/\/raw.githubusercontent.com\/RojerGS\/languages\/master\/brainfck\/brainfck_terse_terse.py\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">here<\/a>.\nTo run it, you need Python 3.8 or greater.\nAssuming you saved it to the file <code>brainfck.py<\/code>, you can run the example above with<\/p>\n<pre><code class=\"language-sh\">python brainfck.py \"+[--&gt;-[&gt;&gt;+&gt;-----&lt;&lt;]&lt;--&lt;---]&gt;-.&gt;&gt;&gt;+.&gt;&gt;..+++[.&gt;]&lt;&lt;&lt;&lt;.+++.------.&lt;&lt;-.&gt;&gt;&gt;&gt;+.\"<\/code><\/pre>\n<p>but perhaps a simpler example is<\/p>\n<pre><code class=\"language-sh\">python brainfck.py \",[&gt;,]&lt;[.&lt;]\" \"This will get reversed!\"<\/code><\/pre>\n<p>The calling syntax is <code>brainfck.py [code | filepath] [program input]<\/code> and the code that is getting called is this:<\/p>\n<pre><code class=\"language-py\">from sys import*;import io,os;V=argv;V.extend([\"\"]*2);stdin=io.StringIO(V[2])if V[2]else stdin\nr=os.path.exists(V[1]);f=r and open(V[1]);b=f.read()if r else V[1];r and f.close()\ndef I(b,t,p):\n while b: # interpret while there's code\n  c,*b=b;c=\"+-&gt;&lt;,.[]\".find(c) # get next op\n  if c in[0,1]:t[p]+=1-2*c;t[p]%=256 # increase memory cell and wrap at 256\n  if c in[2,3]:p+=5-2*c;t=[0]*(p&lt;0)+t+[0]*(p==len(t));p=max(p,0) # move pointer and adjust tape\n  if c==4:i=stdin.read(1)or chr(0);t[p]=ord(i)%256 # read one char as numeric input\n  if c==5:stdout.write(chr(t[p])) # print one char as output\n  if c==6:\n   d=1;j=[d:=d+(x==\"[\")-(x==\"]\")for x in b].index(0);b,b_=b[j+1:],b[:j]\n   while t[p]:t,p=I(b_,t,p) # loop while memory cell is non-zero\n return t,p\nt,p=I(b,[0],0);print();print(t,p) # interpret and print debugging info<\/code><\/pre>\n<p>Keep reading below to see what this does!<\/p>\n<h2 id=\"brainf-ck\">Brainf*ck<a href=\"#brainf-ck\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>(skip to \"<a href=\"#implementing-an-interpreter\">Implementing an interpreter<\/a>\" if you know what brainf*ck is and how to program in it.)<\/p>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Brainfuck\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Brainf*ck<\/a> is a minimalistic <a href=\"https:\/\/en.wikipedia.org\/wiki\/Esoteric_programming_language\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">esoteric programming language<\/a>,\nmeaning brainf*ck was not created with actual usability in mind.\nPrograms in brainf*ck are written with eight simple commands,\nthem being <code>+-&gt;&lt;,.[]<\/code>, and programming in brainf*ck is more or less\nlike programming directly on a Turing machine...\n(If you don't know what that means, don't worry.)<\/p>\n<h3 id=\"programming-in-brainf-ck\">Programming in brainf*ck<a href=\"#programming-in-brainf-ck\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h3>\n<p>Program execution in brainf*ck happens on top of a tape\nthat is made of infinitely many cells,\neach cell holding an 8bit unsigned integer (that is, integers from 0 to 255).<\/p>\n<p>At any time in the execution of a brainf*ck program,\nwe are looking at a single cell of those infinitely many,\nand the cell we are looking at is controlled by a pointer.<\/p>\n<p>All brainf*ck programs start at the same point:<\/p>\n<pre><code>tape cells\n----------\n[0][0][0][0][0]...\n ^ pointer<\/code><\/pre>\n<p>4 of the 8 basic operations are used to move the pointer right <code>&gt;<\/code> and left <code>&lt;<\/code>,\nand to increment <code>+<\/code> and decrement <code>-<\/code> the current cell.<\/p>\n<p>For example, the program <code>+++<\/code> gives<\/p>\n<pre><code>tape cells\n----------\n[3][0][0][0][0]...\n ^ pointer<\/code><\/pre>\n<p>and the program <code>+++&gt;++&gt;&gt;++<\/code> gives<\/p>\n<pre><code>tape cells\n----------\n[3][2][0][2][0]...\n          ^ pointer<\/code><\/pre>\n<p>The cells can only hold values between 0 and 255,\nso many implementations choose to wrap around when values go above 255 or below 0.\nIn doing that,...<\/p>","summary":"In this blog post I&#039;ll show how to write a full interpreter for a programming language in less than 15 loc of Python.","date_modified":"2025-07-23T16:49:02+02:00","tags":["brainfuck","interpreters","programming","python"],"image":"\/user\/pages\/02.blog\/writing-interpreter-in-15-loc\/code.webp"}]}
