Coder Social home page Coder Social logo

bfwasm's Introduction

bfwasm

A non-optimizing Brainf_ck to WebAssembly compiler. Yes, compiler. Not interpreter.

Installation

npm install -g @surma/bfwasm

Usage (CLI)

Usage: bfwasm [options]

Options:
  -o, --output <file>  File to write compiled Wasm to
  -r, --run            Run compiled Wasm (implies --asyncify)
  --mem-dump <N>       Dump the first N cells of memory after run
  --hex-output         Turn std out into hexadecimap
  --asyncify           Run Binaryen Asyncify pass
  --wasi               Use WASI for I/O
  -h, --help           output usage information

Usage (API)

import { compile } from "@surma/bfwasm";

const wasmBuffer = compile(`
  ++++++++++[>++++++++++++++++++++++
  >+++++++++++++++>++++++++++++++++>+
  <<<<-]>++++++.>+++++++.>++++.
`);

const decoder = new TextDecoder();
const importsObj = {
  env: {
    in() {
      /* Called when bf programm needs input */
      return 0;
    },
    out(v) {
      /* Called when bf programm has output */
      console.log(
        decoder.decode(new Uint8Array([v]), {stream: true})
      );
    }
  }
};
const {instance} = await WebAssembly.instantiate(wasmBuffer, importsObj);
instance.exports._start();

compile(program, options) compiles program to a WebAssembly module exporting a "_start" function.

Options:

  • exportMemory (default: true) will export the memory as "memory".
  • autoRun (default: false) will declare "_start" as the module’s start function.
  • useWasi (default: false) use WASI core for I/O.

License Apache-2.0

bfwasm's People

Contributors

rreverser avatar surma avatar

Watchers

 avatar

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.