node.bf

raw JSON →
0.1.0 verified Fri May 01 auth: no javascript

Node.bf is a JavaScript transpiler that compiles Node.js modules written in Brainfuck, an esoteric Turing-complete language using only characters +-<>[].,. Version 0.1.0 is the current stable release; the project appears to be in early development with no frequent updates. Unlike other Brainfuck interpreters or compilers, node.bf integrates with Node.js module system, allowing .bf files to be compiled into JavaScript modules that export a function. It uses Jison for parser generation and supports Webpack loaders for direct import of .bf files.

error Cannot find module 'node.bf'
cause Package not installed or incorrectly referenced.
fix
Run 'npm install node.bf' and ensure it's in package.json dependencies.
error SyntaxError: Unexpected token >
cause Attempting to run .bf file directly with Node without compiling.
fix
Use node.bf compiler to transpile .bf file to .js before running.
error TypeError: compile is not a function
cause Import/require syntax wrong or compile not exported.
fix
Use correct import: import { compile } from 'node.bf' or const compile = require('node.bf').compile.
error Error: Input must be a string
cause Calling compiled function with non-string argument.
fix
Ensure the function receives a string input for the Brainfuck program's input tape.
gotcha The compile function may require a callback; some versions might return a Promise.
fix Check documentation for the exact API signature. Use a callback wrapper or check if .compile() returns a Promise.
gotcha Direct require of .bf files without Webpack configuration will not work; you must either compile manually or use the custom loader.
fix Either compile .bf files via API first or configure Webpack with node-bf loader.
broken Missing export 'run'? Some builds might not export 'run'.
fix Use compile and create the module wrapper yourself, or check exports list.
npm install node.bf
yarn add node.bf
pnpm add node.bf

Shows how to compile a Brainfuck 'Hello World' program and use the resulting function with Node.js modules.

import { compile } from 'node.bf';
const fs = require('fs');
const path = require('path');

const bfSource = '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.';

// Compile the Brainfuck source to a JavaScript function
transpile(bfSource).then(jsCode => {
  // Create a Node module from the compiled code
  const modulePath = path.join(process.cwd(), 'hello.bf.js');
  fs.writeFileSync(modulePath, `module.exports = function(input) { ${jsCode} };`);
  const helloWorld = require('./hello.bf.js');
  console.log(helloWorld(''));
});

function transpile(source) {
  return new Promise((resolve, reject) => {
    compile(source, (err, result) => {
      if (err) reject(err);
      else resolve(result);
    });
  });
}