a tiny brainfuck interpreter
This project is not meant to be practical. It is an exercise in code golf.
My goal was to make a brainfuck interpreter with JavaScript in 140
bytes. That did not happen, but it is only 249
bytes. In order
to accomplish this some rules were broken. All the variables are leaked
into the global scope and they all have single byte names. There is no
indentation. The entire logic is performed in a single for
loop. It
relies on the scope chain to find required identifiers outside of its
own logic (mainly because input and output are environment dependent).
Now for the good parts. It is small, but still maintainable. Each line only performs one task (just in a slightly convoluted manner). It follows the brainfuck implementation standards as closely as possible, which is easy because there really isn't a standard. And finally, the code doesn't take long to read (but probably a little longer to understand).
Since input
and output
are dependent on the environment, they need
to be implemented before running the interpreter. The scope chain is
checked for the existence of three identifiers (B
, I
and O
).
B
: string of brainfuck code to runI
: string of input for the brainfuck codeO
: callback function to handle output. It is passed a single parameter containing the character to write.
Warning: The following global variables are created:
$
,F
,U
,C
,K
,_
,f
,u
,c
,k
Check the index.js
file and examples
directory for implementation
details.
There are HTML (browser) and Nodejs (CLI) demos in the examples
directory. The HTML demo is live here and the
Nodejs CLI example is called bf
. CLI usage:
bf <file> [input]
bf hello.b
bf rot13.b 'hello world'
Copyright (C) 2022 Randy Schneck
This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License, Version 2, as published by Sam Hocevar. See
the LICENSE file for more details.