A simple brainfuck compiler targeting asm.js.
BFSelf the self interpreter is the most fun :)
$ python bf2asmjs.py examples/hello.b > bf.js $ firefox-trunk index.html
To begin familiarizing myself with targeting asm.js I wanted to create the simplest possible compiler. I'll admit it's not very useful but it got me off the ground and it might help others.
What is asm.js?
What is brainfuck?
brainfuck is a programming language with a small instruction set and is designed to be as easy as possible to compile.
From Wikipedia > increment the data pointer (to point to the next cell to the right). < decrement the data pointer (to point to the next cell to the left). + increment (increase by one) the byte at the data pointer. - decrement (decrease by one) the byte at the data pointer. . output the byte at the data pointer. , accept one byte of input, storing its value in the byte at the data pointer. [ if the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ] command. ] if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [ command.
Heap & Memory.
Asm.js modules use instances of ArrayBuffer as heaps and then create "views" on them to address data. As brainfucks cells are bytes I use the Uint8Array view.
Input & Output
. , is done via the FFI. Default
. is writing to an element with
, is reading from an element with id