Skip to content

NeutrinoZh/brainfuck

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Brainfuck interpreter

GitHub license

A simple Brainfuck interpreter written in C++. Brainfuck is a minimal and esoteric programming language with an extremely limited set of commands, making it an excellent subject for study and amusement.

Just for fun. Enjoy!

Usage

  1. Clone the repository:

    git clone https://github.com/neutrinozh/brainfuck.git
  2. Build the project:

    cd brainfuck
    cmake -B ./build/
    cmake --build ./build/
  3. Run the interpreter with a Brainfuck program:

    cd ..
    ./build/brainfuck ./examples/hello-world.bf

Examples

Sample Brainfuck programs can be found in the examples directory. You can use them to test the interpreter.

Brainfuck Syntax

Brainfuck consists of only 8 commands:

  • >: Increment the pointer
  • <: Decrement the pointer
  • +: Increment the value at the current cell
  • -: Decrement the value at the current cell
  • [: Begin loop (jump forward to the corresponding ] if the value at the current cell is 0)
  • ]: End loop (jump back to the corresponding [ if the value at the current cell is nonzero)
  • ,: Input a character (ASCII)
  • .: Output a character (ASCII)

Interpreter optimization

  • Replacing multiple > or < with one instruction with a parameter of count increments or decrements pointer.
  • Replacing multiple + or - with one instruction with a parameter of value for incrementing or decrementing the value of the current cell.
  • Saving instruction numbers for [ and ] allows executing jumps between corresponding instructions without the need to iterate over all instructions, preventing delays in program execution.
  • Optimizing [+] and [-] into one instruction set_zero

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Brainfuck interpreter. Just for fun. Enjoy!

Topics

Resources

License

Stars

Watchers

Forks