
A Brainfuck to WebAssembly compiler and playground - verdie-g
https://github.com/verdie-g/brainfuck2wasm
======
lifthrasiir
My first attempt was to put Jon Ripley's Lost Kingdom [1] to the input and it
obviously didn't work (or took too much time, I don't know). At the very least
there is an asm.js version that does work out of the box [2].

[1]
[http://jonripley.com/i-fiction/games/LostKingdomBF.html](http://jonripley.com/i-fiction/games/LostKingdomBF.html)

[2]
[https://github.com/rdebath/LostKingdom](https://github.com/rdebath/LostKingdom)

~~~
swolchok
Looks like the default Lost Kingdom BF interpreter uses 8-bit cells, whereas
the bf2wasm compiler uses 32-bit cells. This matters in Brainfuck; choosing
the "wrong" one of the idioms [-] and [+] for zeroing a cell gets worse as
cells get bigger. Maybe if you tweaked the bf2wasm compiler to use 8-bit
cells?

~~~
swolchok
Moreover, the Lost Kingdom interpreter will expand the memory arbitrarily as
it encounters > instructions, whereas the bf2wasm interpreter has a fixed
limit of 16384 4-byte cells according to its documentation.

~~~
lifthrasiir
Oh, you are right; I simply overlooked that part of the readme. In my opinion
that should have been 8-bit cells mainly because AFAIK simulating 16-bit or
32-bit in 8-bit cells is reasonable [1] but doing the other way without
slowing it down too much is highly dependent to the program.

[1]
[https://esolangs.org/wiki/Brainfuck_bitwidth_conversions](https://esolangs.org/wiki/Brainfuck_bitwidth_conversions)

~~~
verdie-g
I didn't know about the idiom of using [+] for zeroing. Are the cells supposed
to be signed ?

~~~
Doxin
Signedness depends on your interpreter. In practice it hardly matters since
there's no print function anyways, so if you want 2s complement you'll end up
implementing it yourself anyways.

[+] should always overflow to 0 at some point, which is entirely doable for 8
bit, but less so for 32bit. as for choosing between [-] and [+] you'll just
have to guesstimate if 0 or intmax is on average closer to your value.

------
daxterspeed
I love it! I've been working on a brainfuck to asm.js compiler myself and it's
a great learning experience, even if asm.js will be going away in favor of
wasm (though I believe Chrome compiles asm.js into wasm).

I'm excited to hear about how you intend to solve input instructions. I've
been struggling with that myself since I want to make it asynchronous, which
might require some nasty tricks.

~~~
seba_dos1
Chrome does nothing with asm.js; it's Firefox who applies special, faster
compilation routines to asm.js code.

~~~
TomMarius
Not sure about Chrome, but the support in Chromium has already been shipped
last year:
[https://bugs.chromium.org/p/v8/issues/detail?id=4203](https://bugs.chromium.org/p/v8/issues/detail?id=4203)

~~~
seba_dos1
Oh, so it changed! Didn't know that, thanks. When playing with Emscripten, I'm
still seeing asm.js working noticeably faster in Firefox than in Chromium, so
I just assumed that it's still how it was for years :)

------
hermitdev
Honest question: why (is it beyond I was bored, so I did it because I could)?

~~~
SunboX
learning by doing.

~~~
verdie-g
Indeed, I also learnt how WebAssembly works :)

