30 years of Brainfuck
deusinmachina.substack.com
deusinmachina.substack.com
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.
https://eli.thegreenplace.net/2017/adventures-in-jit-compila...
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...
> 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."
I implemented an optimizing interpreter in Bash[1] on a plane ride, and it's still one of my favorite pet projects.
Here's my entry - https://github.com/danthedaniel/BF-JIT
$ nix run github:siraben/bf
[0] https://github.com/siraben/bf* De hoogwelgeboren heer Piet ridder Beertema as he is now?
> 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.
[0] http://sigbovik.org/2022/proceedings.pdf, page 23.
Example "+++++++++++++++++++++++++++++++++++++++++++." to
"++[>+++++++++++++++++++++++++<-]>."
34 instructions vs 51. But this might pull me in deeper