Coder Social home page Coder Social logo

mgthomas99 / picobrain Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 26 KB

The 250-byte Brainfuck interpreter, written in Javascript!

License: MIT License

Brainfuck 55.48% JavaScript 44.52%
brainfuck codegolf interpreter javascript language minify npm package tiny

picobrain's Introduction

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.

picobrain's People

Contributors

mgthomas99 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

picobrain's Issues

Extra test for you.

This should print "Hello World!".
From reading your code I'm guessing it won't work.

+[>[<->+[>+++>[+++++++++++>][>]-[<]>-]]++++++++++<]>>
>>>>----.<<+++.<-..+++.<-.>>>.<<.+++.------.>-.<<+.<.

What about

module.exports=function*(a,t=[],u){for(i=n=p=0,c=1;c;i+=n>=0?1:-1)(n+=(c=a[i])=="["?n?1:!t[p]:c=="]"?n?-1:-!!t[p]:0)||(t[p+=c==">"?1:c=="<"?-1:0]=(t[p]||0)+(c=="+"?1:c=="-"?-1:0),c=="."?yield String.fromCharCode(t[p]):c==","&&(t[p]=u().charCodeAt()))};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.