Skip to content

Instantly share code, notes, and snippets.

@pineapplebox
pineapplebox / BFjs.md
Last active April 24, 2024 12:55
A 158-byte JS Brainf*ck interpreter

BF.js

Summary

After realizing how bloated most available Javascript brainf*ck interpreters are, I created BF.js- what I believe to be the smallest brainf*ck interpreter built with JS (158 bytes). Please let me know if there are any possible improvements to the code, or if there are any smaller interpreters in JS.

for(a=[p=x=i=0],r="";l=s[i++];">"==l?p++:"<"==l?p--:"+"==l?a[p]++:"-"==l?a[p]--:"."==l?r+="&#"+v:","==l?a[p]=~~prompt():"["==l?x=i:"]"==l&&v>0?i=x:x)v=a[p]|=0

Interpreter with nested loop support (178 bytes):

for(x=[r=""],a=[p=i=c=0];l=s[i++];">"==l?p++:"<"==l?p--:"+"==l?a[p]++:"-"==l?a[p]--:"."==l?r+="&#"+v:","==l?a[p]=~~prompt():"["==l?x[c++]=i:"]"==l&&0<v?i=x[c---1]-1:c--)v=a[p]|=0