Hacker News new | past | comments | ask | show | jobs | submit login
30 years of Brainfuck (deusinmachina.substack.com)
100 points by Decabytes on Jan 14, 2023 | hide | past | favorite | 23 comments



Brainfuck is one of my favourite languages. Not that I _write_ any code in it, mind you, but there's a bunch of code out there written for it (thank you, you crazy people), and it's so tiny and so simple that you can write an interpreter for it in an hour or two.

Because the syntax is so minimal, there's little point for a naive interpreter to include a parser and you can write it directly as a bytecode interpreter (and my experience has been that most beginners will do exactly that on their first attempt).

However, if you _do_ decide to write a (really basic) parser for it, you can easily end up with a tree walking interpreter instead, and, from there, it's only a tiny jump to start manipulating the AST. E.g. the simplest change you can make is to do away with the separate Increment and Decrement instructions, and replace them with a generic Add instruction, so + and - get parsed as Add 1 and Add -1 respectively. Then you can replace runs of mixed - and + characters as one single Add instruction (--+-+++ becomes Add 1, for example). If you add a Set instruction, the [-] sequence can be translated to Set 0. Likewise, [.>] can become printf(ptr) instead of a loop of putchar(*ptr).

Put all of this together, and it turns out that BF is perfect to introduce people to interpreters, and to demistify the whole "writing a language" thing.


And then after creating a more efficient bytecode, compile it to machine code with JIT compilation!

https://eli.thegreenplace.net/2017/adventures-in-jit-compila...


It seems to be a "disassembler" for BF :D


Never thought of doing "optimizations" (like a compiler) in a BF interpreter, but that's genius!


It's quite fun to write a bf->c "compiler" and look at the assembly output that results from compiling that c. The result can be remarkably efficient; GCC will trivially reduce a double-loop of incrementation down to a single multiply instruction for example, it will optimize away pointer manipulation and just use registers, etc.


You are Reading the Name of this Esolang[1] is my favorite of the Brainfuck derivative esolangs.

It consists of "0" and "1" characters which map to Brainfuck symbols via a Huffman encoding as well as brackets which wrap a subprogram which are reduced to "1" if the subprogram halts and "0" if the subprogram loops forever. This means that the problem of deciding whether a given string is a valid You are Reading the Name of this Esolang program is undecidable.

[1]: https://esolangs.org/wiki/You_are_Reading_the_Name_of_this_E...


This is one of the most hilarious things I've ever read:

> Pronunciation of You are Reading the Name of this Esolang: The name of the language is pronounced as an English speaker would pronounce "you are hearing the name of this esolang."


Keep my nationality out of it!


It's an interesting/challenging exercise to work through implementing some basic functions in BF, ie thinking only in loops.

I implemented an optimizing interpreter in Bash[1] on a plane ride, and it's still one of my favorite pet projects.

[1]: https://github.com/Gandalf-/BrainBash


In bash? You're a madman.

Here's my entry - https://github.com/danthedaniel/BF-JIT


Ever since I learned about Brainfuck myself (which I obviously couldn't believe at first that it's an actual thing), I keep peeping at esolangs.org to find some bizarre creations of (often) very bored programmers.


30 years?! Wow, the first time I ever heard of it was 15 years ago. And even then I thought it was some weird university group project that gained internet infamy.


You have to admit it doesn’t look a day over 24


Brainfuck makes it very easy to benchmark various optimizations that you can do to its interpreter.[0] Rendering a Mandelbrot set goes from 28.57 to 6.49 seconds with basic preprocessing!

  $ nix run github:siraben/bf
[0] https://github.com/siraben/bf


Today I learned that python was older than java. Nice surprise.


The Mathematisch Centrum/CWI is responsible not only for incubating Python but for other computing antics, most memorably https://godfatherof.nl/kremvax.html (something Sir Piet* neglects to mention is that in 1984 all US<->Europe usenet traffic went by way of mcvax, so as far as metadata was concerned, the forgery was perfect when seen from the US side!)

* De hoogwelgeboren heer Piet ridder Beertema as he is now?


The pure functional esolang, Binary Lambda Calculus, also originates at CWI...


Quine relay [1] is to this day the most "I will never understand this" brainfuck project I have ever seen.

> This is a Ruby program that generates Rust program that generates Scala program that generates ...(through 128 languages in total, including brainfuck!)... REXX program that generates the original Ruby code again.

It is so mysterious how it's even possible, and hilariously the explanation of how it was made exists in Japanese only. I think at this point it would be easier to learn Japanese than to try to understand this project.

Other things like a brainfuck interpreter in brainfuck are impressive but don't come nearly as close to black magic as quine-relay.

[1] https://github.com/mame/quine-relay


The proceedings of SIGBOVIK 2022 has an article [0] detailing brainfuck++, which includes a number of helpful enhancements.

[0] http://sigbovik.org/2022/proceedings.pdf, page 23.


Oh no I've already been sucked in to brainfuck by writing an optimizer that compresses instructions using loops.

Example "+++++++++++++++++++++++++++++++++++++++++++." to

"++[>+++++++++++++++++++++++++<-]>."

34 instructions vs 51. But this might pull me in deeper


Oh interesting. I haven’t seen a BF optimizer that just writes more optimized BF.


BF loops necessarily need to use more space. How do you determine which cell is free to overwrite? It is often non-trivial because BF data structures are very sensitive to exact strides (e.g. only works when the data pointer is at 5n+4).


I remember discovering Brainfuck... and then discovering Malbolge [0].

[0] https://en.wikipedia.org/wiki/Malbolge




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: