Skip to content

r1file/brainfuck

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Brainfuck Interpreter

A complete Brainfuck programming language interpreter implemented in C.

Quick Start

Compilation

clang -g brainfuck.c -o brainfuck
# or
gcc -g brainfuck.c -o brainfuck

Execution

Method 1: Execute code directly

./brainfuck '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.'

Method 2: Execute from file

./brainfuck bf_tests/test.bf

Brainfuck Language Guide

Brainfuck has 8 basic commands that operate on a memory array and a pointer:

Command Operation Description
> Pointer++ Move data pointer one position to the right
< Pointer-- Move data pointer one position to the left
+ Value++ Increment current cell value by 1 (wraps 0-255)
- Value-- Decrement current cell value by 1 (wraps 0-255)
. Output Output ASCII character of current cell value
, Input Read one character into current cell
[ Loop Start Jump to matching ] if current value is 0
] Loop End Jump to matching [ if current value is non-zero

All other characters are ignored, allowing for comments in the code.

Memory Model

  • Memory Size: 30,000 bytes (cells)
  • Initial Value: All cells initialized to 0
  • Data Type: Unsigned byte (0-255, wrapping)
  • Pointer Range: 0-29,999 (out of bounds causes error)

Features

  • Complete Command Support - All 8 standard Brainfuck commands
  • Bracket Matching Check - Automatically detects mismatched [ and ]
  • Error Handling - Detects pointer out-of-bounds and bracket errors
  • File and CLI Support - Supports reading from files or passing code directly
  • Standard I/O - Full support for input (,) and output (.)
  • 30KB Memory - Sufficient for running most Brainfuck programs

Compilation Options

Debug Mode

clang -g -O0 brainfuck.c -o brainfuck

Optimized Version

clang -O2 brainfuck.c -o brainfuck

Testing

The project includes a test script run_tests.sh to verify all functionality:

./run_tests.sh

Test Coverage:

  • ✅ Hello World program
  • ✅ Single character output
  • ✅ Multiple character output
  • ✅ File execution
  • ✅ Loops and calculations

Project Structure

.
├── brainfuck.c          # Interpreter source code
├── brainfuck            # Compiled executable
├── bf_tests/            # Stored Brainfuck test files
│   ├── test.bf
│   ├── test2.bf
│   ├── test5.bf
│   └── test8.bf
├── run_tests.sh         # Automated test script
└── README.md            # This file

References

License

MIT

Author

r1file


Note: Brainfuck is a minimalist, Turing-complete programming language designed to demonstrate the possibility of an extremely minimal programming language. While rarely used in practice, it's valuable for learning compiler and interpreter design principles.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors