Skip to main content

Posts

Showing posts from November, 2013

brainfuck in Clojure. Part II: compiler

Last time we developed brainfuck interpreter in Clojure. This time we will write a compiler. Compilation has two advantages over interpretation: the resulting program tends to be faster and source program is lost/obscured in binary. It turns out that a brainfuck compiler (to any assembly/bytecode) is not really that complex - brainfuck is very low level and similar to typical CPU architectures (chunk of mutable memory, modified one cell at a time). Thus we will go for something slightly different. Instead of producing JVM bytecode (which some already did) we shall write a Clojure macro that will generate code equivalent to any brainfuck program. In other words we will produce Clojure source equivalent to brainfuck source - at compile time.
This task is actually more challenging because idiomatic Clojure is much different from idiomatic brainfuck (if such thing as "idiomatic brainfuck" ever existed). Let's first think how such a Clojure code could look like and then writ…