picobrain

Tiny Javascript brainfuck interpreter


Keywords
brainfuck, golfed, golf, interpreter, language, minified, minify, minimal, nano, pico, tiny, codegolf, javascript, npm, package
License
MIT
Install
npm install picobrain@2.0.2

Documentation

picobrain

npm npm Github file size GitHub

Perhaps the smallest Javascript brainfuck interpreter

picobrain is a 250-byte Javascript brainfuck interpreter.

Usage

The library exports a generator function which yields on output.

var bf = require("picobrain")

bf("+++[>++++++++++<-]>+++."); // Yields "!"

You can also provide your own tape.

// Here, we specify that we want the interpreter to use a new `Uint8Array` as
// the tape. This will set the tape size to 20 and cause cell values to wrap on
// overflow/underflow.
// If no tape is provided, the interpreter will just use a new empty unbounded
// number array.
var tape = new Uint8Array(20);
bf("-", tape);

For the input command to work (,), you must provide a function which is to be invoked on input:

function input() {
  return window.prompt();
}

bf(",.", [], function() {
  return window.prompt();
});

Why

Why not?

This library is also an open codegolf challenge. Please feel free to submit pull requests if you're able to make the code even shorter, or fork the project and make your own API!

Install, Build & Test

To install the package, use the standard NPM install command:

npm install picobrain

The package is hand-written, minified Javascript; therefore, no build process is required, and you can directly edit and execute the source!

An NPM script has been created for simplicity when running tests, which can be run from the command line as shown below:

npm test

picobrain uses the Tape library for testing.

Content Delivery Network (CDN)

The picobrain library can be imported via unpkg as shown below:

<script type="application/javascript">var module= {};</script>
<script src="https://unpkg.com/picobrain/index.js"></script>
<script type="application/javascript">
  var bf = module.exports;
  // Library is imported as `bf`!
</script>

Be aware that picobrain is a CommonJS module and therefore uses module.exports; before importing the script, you will need to define module or use a CommonJS-compatible module loader.

License

Please see the LICENSE file for license information.