fabianishere/brainfuck

repository·master·Indexed 20 days ago

https://github.com/fabianishere/brainfuck

A Brainfuck interpreter written in C featuring a command-line interface with an interactive console and a C API for programmatic integration. It implements the standard Brainfuck instruction set and provides a static library and header files for use in C/C++ applications.

Tokens
1.3K
Snippets
5
Records
9
Agent score
71%

What's inside fabianishere-brainfuck

  1. Build and Install Brainfuck

    master

    To build the project, you need CMake, a C compiler (like Clang or GCC), and libedit (e.g., libedit-dev on Debian/Ubuntu or via macOS Xcode command line tools).

    Follow these steps to build from source:

    1. Clone the repository.
    2. Create and enter a build directory.
    3. Run cmake .. to generate Makefiles.
    4. Run make to compile.
    5. (Optional) Run make install to install the binaries to your local system.

    You can run the interpreter directly from the build directory without installing using ./brainfuck <path_to_file>.

    $ git clone https://github.com/fabianishere/brainfuck.git
    $ mkdir build
    $ cd build
    $ cmake ..
    $ make
    $ make install
  2. Understand Brainfuck language syntax

    master

    The interpreter implements the standard Brainfuck instruction set:

    • >: Increment the data pointer (move to the next cell to the right).
    • <: Decrement the data pointer (move to the next cell to the left).
    • +: Increment the byte at the data pointer by one.
    • -: Decrement the byte at the data pointer by one.
    • .,:
      • . (dot): Output the byte at the data pointer as an ASCII encoded character.
      • , (comma): Accept one byte of input and store its value in the byte at the data pointer.
    • []: Loop control.
      • [: If the byte at the data pointer is zero, jump forward to the command after the matching ] token.
      • ]: If the byte at the data pointer is nonzero, jump back to the command after the matching [ token.
  3. Explore Brainfuck quine examples

    master

    The examples/quine/bertram/ directory contains a collection of Brainfuck quines (programs that output their own source code) optimized for minimal size.

    • *.s files: These are the final, optimized Brainfuck quines.
    • *.b files: These are the bootstrap files used to generate the corresponding *.s quines. These bootstrap files are the actual source files that were edited during the development process.
  4. Use the Brainfuck C API

    master

    You can integrate the Brainfuck interpreter into your C applications using the provided API. The workflow involves creating a state, a context (with a specified tape size), parsing instructions, adding them to the state, and executing them.

    #include <stdio.h>
    #include <stdlib.h>
    #include <brainfuck.h>
        
    int main() {
    	BrainfuckState *state = brainfuck_state();
    	BrainfuckExecutionContext *context = brainfuck_context(BRAINFUCK_TAPE_SIZE);
    	BrainfuckInstruction *instruction = brainfuck_parse_string(",+++++.");
      brainfuck_add(state, instruction);
      brainfuck_execute(state->root, context);
    	brainfuck_destroy_context(context);
      brainfuck_destroy_state(state);
    	return EXIT_SUCCESS;
    }
  5. Use the Brainfuck CLI

    master

    The Brainfuck interpreter can be used via the command line to run Brainfuck code from files or directly via evaluation. If no arguments are provided, the interpreter enters an interactive console mode.

    brainfuck [-veh] file...
    
    Options:
    -e, --eval    run code directly
    -v, --version show version information
    -h, --help    show a help message.
  6. Integrate the brainfuck library programmatically

    master

    For developers wishing to run the interpreter within a C/C++ application, the project provides a static library and header files.

    Key files:

    • Header: /usr/local/include/brainfuck/brainfuck.h
    • Static Library: /usr/local/lib/libbrainfuck.a
  7. Run brainfuck code via the C API

    master

    If you are integrating this library into a C project, you can use the following functions to execute code programmatically:

    • run_file(FILE *file): Executes Brainfuck code from an open file stream. Returns EXIT_SUCCESS on success or EXIT_FAILURE on error.
    • run_string(char *code): Executes a Brainfuck code string directly. Returns EXIT_SUCCESS on success.

    Note: These functions are internal to the CLI entrypoint but demonstrate the underlying usage of the brainfuck.h library functions like brainfuck_state(), brainfuck_context(), brainfuck_parse_string(), and brainfuck_execute().