npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

braincrunch

v1.2.1

Published

An embeddable performant Brainfuck interpreter

Downloads

18

Readme

BrainCrunch

GitHub license npm version CircleCI Status Greenkeeper badge

An embeddable performant Brainfuck interpreter written in Javascript.

yarn add braincrunch
var braincrunch = require('braincrunch');

var HELLO_WORLD = '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>' +
  '.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.';

var machine = new braincrunch.Machine({
  code: HELLO_WORLD,
  write: function(n) {
    process.stdout.write(String.fromCharCode(n));
  }
});
machine.run();
// Hello World!

When the Machine object is created, the Brainfuck program is parsed and optimized. Specific sequences are translated into more efficient instructions: [-] becomes a single operation that clears the current cell for example. Then sequences of non-looping instructions are translated into Javascript so that the Javascript VM can apply its own optimizations at runtime.

Some of the optimization strategies are inspired by this post: http://calmerthanyouare.org/2015/01/07/optimizing-brainfuck.html

BrainCrunch can be used in Browsers via a CommonJS bundler such as Browserify. Support for Typed Arrays is required. https://caniuse.com/#feat=typedarrays

API

new braincrunch.Machine(opts)

opts supports the following properties:

  • code: The Brainfuck program code.
  • read: Must be a string, array, function, or null. Used to provide values when the , instruction is hit. Return null to signal EOF. Return the machine.INTERRUPT value to pause the machine. This is useful if read's implementation is asynchronous. The read value may be supplied later by using machine.setReadValue, and then the machine may be resumed with machine.run.
  • write: Must be an array, function, or null. Will be appended to or called when the . instruction is hit. The function may return the machine.INTERRUPT value to pause the machine.
  • cellSize: The number of bits per cell. Valid values are 8, 16, and 32. Defaults to 8.
  • cellCount: Number of cells to have. Defaults to 4096.
  • EOF: Value to set when , gives EOF. Defaults to -1.
  • useEval: Translate code into Javascript when possible. Defaults to true.
  • noEvalWarning: If useEval is true and the Function constructor is not available at run-time (such as because of Content Security Policy restrictions), then a warning will be printed to the console unless this setting is set to true. Defaults to false.
  • allowInterrupts: If you never use a read or write function that can return machine.INTERRUPT, then setting this option to false can increase performance.

machine.run([steps])

Runs the machine for at least the given number of steps. steps defaults to Infinity if not given. This method can be called multiple times to continue running the machine. Giving a finite steps value is a useful protection against infinite loops in user-supplied programs, or to let the machine pause so other things can be done before resuming. Returns the number of steps that were executed.

machine.setReadValue(value)

If the machine has been interrupted during a read, then this method may be used to set the read value, and then the machine can be resumed by using machine.run. This method should not be used in other situations.

machine.complete

Boolean property that is set to true once the machine has finished its program.

machine.INTERRUPT

This is a special value that may be returned by a user-supplied read or write callback to cause the machine to stop. The machine may be resumed later by using machine.run.

Types

Flow type declarations for this module are included! If you are using Flow, they won't require any configuration to use.