Crate brainfuck_exe

source ·
Expand description

Brainfuck-exe

a simple brainfuck interpreter crate implemented in rust 🦀 with many available customizations for flexibility

see the Brainfuck struct for more information on usage

Usage

In your Cargo.toml:

brainfuck-exe = "*"

If you are only using it as a library, and the CLI is not needed, disable the cli (included by default) feature to remove unecessary dependencies:

brainfuck-exe = { version = "*", default-features = false }

Example

Below is a basic example on how to use the crate


use std::fs::File;
// import Result typealias and interpreter struct
use brainfuck_exe::{Result, Brainfuck};

fn main() -> Result<()> {
    // brainfuck code to print "Hello, World!"
    let code = ">++++++++[<+++++++++>-]<.>++++[<+++++++>-]<+.+++++++..+++.>>++++++[<+++++++>-]<+
    +.------------.>++++++[<+++++++++>-]<+.<.+++.------.--------.>>>++++[<++++++++>-]<+.";
    // instantiate a new interpreter instance with the code
    Brainfuck::new(code)
        // optional builder method to write the output into a file not STDOUT
        .with_output(
            File::options()
                .write(true)
                .open("tests/output.txt")
                .unwrap()
        )
        // executes the code
        .execute()?;

    // alternatively use this to retrieve the code from an existing source file
    Brainfuck::from_file("tests/hello_world.bf")?
        .execute()?;

    Ok(())
}

CLI

You can also use this crate as a CLI program

# installation
$ cargo install brainfuck-exe
# usage
$ brainfuck --help
$ brainfuck [CODE] [-f FILE] [OPTIONS]

Re-exports

pub use error::Error;
pub use error::Result;

Modules

module containing the Error enum and corresponding Result typealias for this crate

Structs

The struct representing a brainfuck interpreter instance
struct containing various information regarding the program execution such as the final memory array and the final pointer index etc.

Enums

a helper wrapper enum that is used for storing the input stream this allows for it to be passed by value OR reference
a helper wrapper enum that is used for storing the output stream this allows for it to be passed by value OR reference

Constants

default max value a cell can have