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.
- Ultra-Lightweight: The core engine logic spans fewer than 10 lines of heavily optimized Go code.
- Standard Compliance: Uses the standard
30,000byte 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.
To integrate the interpreter into your Go project, use go get:
go get github.com/ndenissov/bfClone the repository and build the executable:
git clone https://github.com/ndenissov/bf.git
cd bf
go build -o bf main.goOnce compiled, pass the path of your .bf script as the first argument to the executable.
Example execution:
$ ./bf hw.bfInput (hw.bf):
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
Output:
Hello World!
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)
}
}The core package (B) utilizes advanced ASCII character offset calculations and a consolidated for loop to achieve
its minimal size.
44and46map to,and.(I/O).43and45map to+and-(Cell manipulation).60and62map to<and>(Pointer manipulation).91and93map 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.
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.