Swiftpack.co - lebidouilleur/bf as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
A brainfuck interpreter and compiler in Swift.
.package(url: "https://github.com/lebidouilleur/bf.git", from: "v0.2")

A fast optimizing industrial-grade brainfuck interpreter and compiler written in swift


# Usage for bf -- the brainfuck interpreter
$ bf <input> [-l <l>] [--debug] [--interactive] [--optimize]

# Usage for bfc -- the brainfuck compiler
$ bfc <input> [--optimize] [--keep]


What is bf?

bf is an interpreter and a compiler for brainfuck programs.

  • bf is the interpreter, its goal is to run through a brainfuck program (such as hello.bf) and performs operations on each character.
    • ☑ interpret .bf files
    • ☑ scan the brainfuck code to tokenize it and generate an Intermediate Representation (IR) of it
      • [-] use that IR to perform optimizations, for examples:
        • ☐ Concatenate multiple occurrences of the same command
        • [+] and [-] as Clean
        • [>] and [<] as Goto [Next|Prev] empty cell in memory
        • [->+<] as Empty current cell and Add its value to the next one
    • ☑ allow the user to debug through the interpreter
      • ☐ run the debugger inside a TUI (probably with ncurses?)
  • bfc is the compiler, its goal is to convert a brainfuck program to assembly code (more specifically to nasm assembly) which will be compiled to native binary executable.
    • ☑ binary executable acts as if the program was interpreted
    • ☐ perform same optimizations as the interpreter
    • [-] compile to native binary with nasm for:
      • ☑ x86-64 macOS
      • ☐ amr64 macOS
      • ☐ x86 GNU/Linux
      • ☐ x86-64 GNU/Linux
      • ☐ arm64 GNU/Linux
      • ☐ x86-64 Windows
    • ☐ compile to C code for portability over other platforms


Here is a time performance benchmark between bf and bfc (on the Github Actions):

Program bf bfc
bangbang 145ms 9ms
bottles 205ms 56ms
hello 21ms 9ms
hello_complex 20ms 8ms
mandelbrot 10m33s 1s127ms
pi 462ms 10ms
quine 25ms 10ms
speedtest 573ms 9ms
squares 163ms 10ms

How does brainfuck works?

brainfuck operates on an array of (usually) 30000 memory cells, where each cell is initially set to zero, with a pointer to a specific memory cell, initially set to the first cell of the memory array.

Here is, as example, how you can initialize an interpreter in C:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

uint8_t  memory[30000];
uint8_t *pointer = memory;

int main(int argc, char **argv, char **env) {
    memset(memory, 0, sizeof(memory));

     * Your beautiful brainfuck interpreter goes here

     return EXIT_SUCCESS;

brainfuck performs operations on those memory cells and pointer with the following eight operations:

Command C equivalent Description
> pointer++; Increment the data pointer.
< pointer--; Decrement the data pointer.
+ *pointer++; Increment the byte at the data pointer.
- *pointer++; Decrement the byte at the data pointer.
. putchar(*pointer); Output the byte at the data pointer.
, *pointer = getchar(); Store the value of one input byte to the data pointer.
[ while (*pointer) { Jump past the matching ] if the byte at data pointer is 0.
] } Jump back to the matching [ if the byte at data pointer is 0.

For further information, check out wikipedia and esolangs.


Interpreter limitations

Compiler limitations



On macOS, you can install bf with homebrew like so:

# Add my tap to homebrew
$ brew tap lebidouilleur/formulae
# Installing bf
$ brew install lebidouilleur/formulae/bf

Other platforms

On other platforms, please, download the project sources and build it like so:

  # Getting the latest pre-release
$ git clone https://github.com/lebidouilleur/bf.git --depth 1 --branch v0.2
  # Go to the bf cloned directory
$ cd bf
  # build bf
$ swift build -c release --disable-sandbox
  # Move built executables to a better folder
$ mkdir bin && mv ./.build/release/bf bin/bf && mv ./.build/release/bfc bin/bfc

You now have access to the bf interpreter and compiler in the bin directory.



Stars: 0
Last commit: 8 hours ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

2 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics