9.1.1 • Published 1 year ago

hirnfick v9.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Hirnfick

GitHub license npm version Tests codecov npm downloads

A Brainfuck source-to-source compiler that runs in Node.js, Deno and web-browsers.

Contents

  1. Installation
  2. Usage
    1. Compiler
    2. Library
  3. Supported Output Languages
  4. Examples

Installation

Run npm i -g hirnfick to install globally or npm i hirnfick to install for a single project.

Usage

C++ style single-line comments (i.e. // I'm a comment) are supported.

Compiler

Options:

  • --lang [language] - Output language (default=js-node).
    • Supported options: js-web, js-node, js-deno, python, c, cpp, qbasic, pascal, rust.
  • --memory-size [fixed|dynamic] - Type of cells array (default=fixed).

Library

  • Use compileTo[VARIANT]() where [VARIANT] is the output language/variant ( e.g. compileToJsWeb()).
  • compileToJsWeb() generates a function that returns an object with two members:
    1. output - The output of the program.
    2. cells - The array of cells that were used by the program.
  • QBasic programs with dynamic arrays require PDS 7.1 or FreeBASIC to compile.
  • Single-line C/C++/JS style comments are supported.
  • For more information see the documentation.

Supported Output Languages

  • JavaScript.
  • Python.
  • C.
  • C++.
  • QBasic (manually tested with FreeBASIC 1.09.0, QuickBASIC 4.5 and PDS 7.1).
  • Pascal (tested with Free Pascal 3.2.2 and Borland Pascal 7.0).
  • Rust.

Table 1: Supported Commands by Output Language

Language>\<+-.,[]Memory Size
JavaScript (Web)30,000/Dynamic
JavaScript (Node.js)30,000/Dynamic
JavaScript (Deno)30,000/Dynamic
Python30,000/Dynamic
C30,000
C++30,000/Dynamic
QBasic30,000/Dynamic
Pascal30,000
Rust30,000

Examples

CommonJS (Node)

const hirnfick = require('hirnfick');

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

try {
  const helloWorldJS = hirnfick.compileToJsCli(helloWorldBF);
  const helloWorld = new Function(`${helloWorldJS}`);
  helloWorld();
} catch (err) {
  console.error(`Error: ${err.message}`);
}

Program Validation - CommonJS (Node)

const hirnfick = require('hirnfick');

// This code prints 'Hello World!' to the screen
const validCode = '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.'
  + '<-.<.+++.------.--------.>>+.>++.';

// This code has a mismatching numbers of opening brackets and closing brackets
const invalidCode = '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>'
  + '.<-.<.]]].------.--------.>>+.>++.';

console.log(hirnfick.isValidProgram(validCode)); // true
console.log(hirnfick.isValidProgram(invalidCode)); // false

ESM (Node)

import * as hirnfick from 'hirnfick';

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

try {
  const helloWorldJS = hirnfick.compileToJsCli(helloWorldBF);
  const helloWorld = new Function(`${helloWorldJS}`);
  helloWorld();
} catch (err) {
  console.error(`Error: ${err.message}`);
}

ESM (Deno)

import hirnfick from "https://jspm.dev/hirnfick";

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

try {
  const helloJs = hirnfick.compileToJsWeb(helloWorldBF);
  const runHello = new Function(`${helloJs} return main().output.trim();`);
  console.log(runHello());
} catch (err) {
  console.error(`Error: ${err.message}`);
}

TypeScript (Deno)

import * as hirnfick from "https://cdn.jsdelivr.net/gh/synthetic-borealis/hirnfick/deno/index.ts";

const helloWorldBF =
  "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---." +
  "+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";

try {
  const helloJs = hirnfick.compileToJsWeb(helloWorldBF);
  const runHello = new Function(`${helloJs} return main().output.trim();`);
  console.log(runHello());
} catch (err) {
  console.error(`Error: ${err.message}`)
}

Web

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/hirnfick@8.0.3/dist/hirnfick.js"></script>
  </head>
  <body>
    <p>
      <textarea id="output-box" readonly rows="14" cols="24"
                style="width: 90%;resize: none;"></textarea>
    </p>
    <button id="run-button">Run</button>
    <script>
      const runButton = document.getElementById('run-button');
      const outputBox = document.getElementById('output-box');
      const helloWorldBF = '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.'
        + '+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.';

      outputBox.value = '';
      runButton.addEventListener('click', () => {
        try {
          const helloWorldProgram = hirnfick.compileToJsWeb(helloWorldCode);
          const helloWorld = new Function(`${helloWorldProgram} return main().output;`);

          outputBox.value += helloWorld();
        } catch (err) {
          outputBox.value = `Error: ${err.message}`;
        }
      });
    </script>
  </body>
</html>
9.1.1

1 year ago

9.1.0

2 years ago

9.0.0

2 years ago

5.0.0

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

6.0.3

2 years ago

6.0.2

2 years ago

7.0.0

2 years ago

7.1.2

2 years ago

7.2.0

2 years ago

7.1.1

2 years ago

7.1.0

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

8.0.3

2 years ago

8.0.2

2 years ago

4.0.0

2 years ago

3.1.0

2 years ago

3.0.0

2 years ago

2.3.0

2 years ago

2.2.0

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago