Crate brainfuck [−] [src]
Simple brainfuck interpreter in Rust.
The brainfuck language was created with the purpose of being a
very minimal language which is very easy to write an interpreter
for. This is one such interpreter. For more information on the
brainfuck language start with the documentation of each
instruction in the language, or
some material online. Brainfuck itself is syntactically
challenging for humans, but is really not all that complicated. +.
for example increments the value in the first cell and outputs that
value. The instructions that trip people up the most are the control
flow contructs [
and ]
. +++>,<[>+.<-]
for example prints the 3
values after the input value. For more about control flow in brainfuck
read the section on control flow.
Examples
use brainfuck; // Evaluate a simple brainfuck program from a string. brainfuck::eval_string("+>."); // Evaluate a brainfuck program from a file. brainfuck::eval_file("fixtures/helloworld.rs");
Semantics and Portability
The brainfuck language has a few areas that are undefined behavior. These undefined behaviors are given explicit semantics in this implmentation. Most brainfuck programs should work as expected in this implmentation. For more information on portabiliy of brainfuck programs read The Unofficial Constraints on Portable Brainfuck Implementations. The deatils below should cover all of the undefined behavior in brainfuck with respect to this implmentation.
- The tape contains
TAPE_LENGTH
(currently 30,000) cells. - The tape's pointer may not be moved below or above the begining
or the end of the tape. The interpreter will return an
Err
if the program does so. - The values of the tape are unsigned bytes (
u8
in rust). - Values may not be incremented or decremented above 255, or below 0.
The interpreter will return an
Err
if the program does so. - Attpempts to read input when there is no more input to be read will be effective noops (potentially with a warning).
- Programs cannot contain unmatching brackets, and functions like
Program::parse
ensure this before running the program.
Modules
program |
Brainfuck programs are the best kind of programs too! |
tape |
Brainfuck programs have the best underlying data structure. |
Structs
Interpreter |
A brainfuck interpreter, with the needed state for execution. |
Enums
Error |
A general error type for problems inside of the interpreter. |
Instruction |
An executable instruction in the language. |
Constants
CYCLE_LIMIT |
The number of instructions allowed to execute before the interpreter
errors with |
Functions
eval_file |
Parse a program from the given file path and |
eval_string |
Parse a program from the given string and |