Skip to content
ndenissovPublic

About

An ultra-lightweight, fully compliant Brainfuck interpreter written in Go. Built using extreme code golfing techniques in under 10 lines of code, it features the standard 30,000-cell memory tape and zero dependencies. Use it as a fast CLI tool or embed it directly as a Golang library for esoteric programming execution.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Latest commit

 

History

10 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BF (BrainFuck Interpreter)

Go GitHub Repo Size License: MIT GitHub downloads GitHub stars

A highly condensed, fully compliant Brainfuck interpreter written in Go.

The core philosophy of this project is extreme code minimalism (code golfing). Despite its remarkably small footprint, the engine implements the complete standard Brainfuck specification, including a 30,000-cell memory tape, standard I/O operations, and support for complex nested loops.

Features

  • Ultra-Lightweight: The core engine logic spans fewer than 10 lines of heavily optimized Go code.
  • Standard Compliance: Uses the standard 30,000 byte array ([3e4]byte).
  • Zero Dependencies: Relies solely on the Go standard library.
  • Versatile: Can be used as a standalone Command-Line Interface (CLI) application or imported as a library into other Go projects.

Installation

As a Library

To integrate the interpreter into your Go project, use go get:

go get github.com/ndenissov/bf

As a CLI Tool

Clone the repository and build the executable:

git clone https://github.com/ndenissov/bf.git
cd bf
go build -o bf main.go

Usage

Command-Line Interface

Once compiled, pass the path of your .bf script as the first argument to the executable.

Example execution:

$ ./bf hw.bf

Input (hw.bf):

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

Output:

Hello World!

Go Package Integration

You can easily embed the interpreter into your own Go applications by passing a byte slice to the B.R() function.

package main

import (
	"os"
	"github.com/ndenissov/bf"
)

func main() {
	// Ensure a file argument is provided
	if len(os.Args) < 2 {
		return
	}

	// Read the file and pass the byte slice to the interpreter
	if f, err := os.ReadFile(os.Args[1]); err == nil {
		B.R(f)
	}
}

Architecture Details

The core package (B) utilizes advanced ASCII character offset calculations and a consolidated for loop to achieve its minimal size.

  • 44 and 46 map to , and . (I/O).
  • 43 and 45 map to + and - (Cell manipulation).
  • 60 and 62 map to < and > (Pointer manipulation).
  • 91 and 93 map to [ and ] (Loop branching).

Instead of parsing the syntax into an Abstract Syntax Tree (AST), the interpreter evaluates the instruction set on the fly, dynamically tracking nested loop depths via the k variable.

License

This project is released under the MIT License.

Copyright (c) 2023 Nikita Denissov.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

About

An ultra-lightweight, fully compliant Brainfuck interpreter written in Go. Built using extreme code golfing techniques in under 10 lines of code, it features the standard 30,000-cell memory tape and zero dependencies. Use it as a fast CLI tool or embed it directly as a Golang library for esoteric programming execution.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages