#brainfuck-compiler #memory #mini #super

bin+lib mini-brainfuck

A super small brainfuck interpreter and compiler

7 releases (2 stable)

1.0.1 Jan 29, 2022
0.1.4 Jan 29, 2022

#400 in Programming languages

MIT license

21KB
320 lines

Mini Brainfuck

A super small brainfuck interpreter and compiler.

About brainfuck

The idea behind brainfuck is memory manipulation. Basically you are given an array of 30,000 1byte memory blocks. The array size is actually dependent upon the implementation used in the compiler or interpretor, but standard brainfuck states 30,000. Within this array, you can increase the memory pointer, increase the value at the memory pointer, etc. Let me first present to you the 8 operators available to us.

> = increases memory pointer, or moves the pointer to the right 1 block.
< = decreases memory pointer, or moves the pointer to the left 1 block.
+ = increases value stored at the block pointed to by the memory pointer
- = decreases value stored at the block pointed to by the memory pointer
[ = like c while(cur_block_value != 0) loop.
] = if block currently pointed to's value is not zero, jump back to [
, = like c getchar(). input 1 character.
. = like c putchar(). print 1 character to the console

Extra characters:

Symbol Activation argument Description
# -v or -vv or -vvv This symbol is used to debug the current's cell value.
! --exit or -e The ! symbol will be used to exit the programm with an exit code 2

Some rules:

  • Any arbitrary character besides the 8 listed above should be ignored by the compiler or interpretor. Characters besides the 8 operators should be con- sidered comments.

  • All memory blocks on the "array" are set to zero at the beginning of the program. And the memory pointer starts out on the very left most memory block.

  • Loops may be nested as many times as you want. But all [ must have a corre- sponding ].

Installation

To install it, simply add the following line to your Cargo.toml

mini-brainfuck = "0.1.3"

You can also install the CLI by doing:

$ cargo install mini-brainfuck
$ brainfuck -V # Check the version for brainfuck.

Usage

Humans need to learn in some way. This interpreter haves 2 parts, a CLI and a module that you can use to interpret brainfuck code.

CLI

$ brainfuck [OPTIONS] [FILE]

To add verbosity, add -v to the [OPTIONS] if you whant to add more levels, add -vv or -vvv and so on...

To see the help menu, type the following in your command line.

$ brainfuck --help

Module

You will need to import the crate and a function called brainfuck will be imported. It takes 1 argument as a String wich will be brainfuck's source code and a second argument i32 wich will be debug's level.

use brainfuck::*;

fn main() {
    let bf_config =  BFConfig {
        ..default_bf_config()
    };

    brainfuck("+++++++>++>-.", config);
}

You can also get the used cells as a return value of the fuction.

let cells: [u8, 3000] = brainfuck(...);

Configuration

use brainfuck::*;

...

let bf_config =  BFConfig {
    debug: 0,
    exit_support: false,
    ..default_bf_config() // support for default values
};

This is the definition of the configuration's struct.

pub struct BFConfig {

    // The level of verbosity (default to 0)
    pub(crate) debug: i32,

    // Add support for the `!` symbol.
    pub(crate) exit_support: bool,
}

Examples

This is a hello world example in brainfuck.

>++++++++[<+++++++++>-]<.>++++[<+++++++>-]<+.+++++++..+++.>>++++++[<+++++++>-]<+
+.------------.>++++++[<+++++++++>-]<+.<.+++.------.--------.>>>++++[<++++++++>-
]<+.

Ideas

Brainfuck is a very small language that can be used for beginers to make their first turing complete programing language. Since it is so easy, there is always new ways to make this more interesting.

Here are some ideas:

  • Make a REPL
  • compiler to other language
  • More language extensions
    • # for cell debuging :tick:
    • ! for programm exit :tick:
    • ^ for importing other files
    • {} if statemets (for checking if cells are the same).

License

This project is under the license of MIT

Dependencies

~4MB
~68K SLoC