brainwash
Overview
brainwash is an optimizing interpreter for the Brainfuck programming language written in C. It is developed with speed, low memory footprint, functionality and portability in mind.
Some features and/or design details:
- Program data is stored in cells. Each cell is of type signed integer, with
cell values ranging from -231 (-2147483648) to 231 – 1
(2147483647), inclusive, on most of today’s (32-bit) systems. When a cell is
already at its maximum (minimum) value and a
+(-) command is executed, it will assume its minimum (maximum) value (i.e. cells wrap-around). - Initially, the pointer points to a single cell initialized to zero. A new cell is dynamically created, linked and initialized (to zero) on demand; that is, each time the pointer attempts to move further right than the right-most available cell. This makes the right edge of the array, virtually, infinite.
- In case a
,(comma) command is executed after the end of input, brainwash will leave the cell values intact. - Characters other than the eight Brainfuck commands are considered to be
comments and are thus ignored. This, with the exception of
!and#that sometimes act as commands and other times don’t (comments). When both the code and its input are read from stdin,!is (a command) used to separate them. When the-doption is used,#is (a command) used to print, to stderr, a memory snapshot of the surrounding cells (useful for debugging). - Optimization is supported, via the
-Ooption, resulting in faster interpretation. - When the first line of a program begins with
#!, brainwash treats this line as a shebang and ignores it. This makes it possible to have executable Brainfuck programs.
Note: As of version 0.3, it is possible to choose between cells of type signed integer and unsigned character (1 byte ranging from 0 to 255) prior to compilation. Please refer to the installation for details on how to do this.
Installation
For gzip (.gz):
$ tar -xvzf brainwash-0.3.tar.gz
For bzip2 (.bz2):
$ tar -xvjf brainwash-0.3.tar.bz2
Change to the source directory:
$ cd brainwash-0.3/src/
For cells of type signed integer (default), issue:
$ make
Alternatively, for cells of type unsigned character, issue:
$ make char
This will create the brainwash executable under the current working directory.
To remove brainwash from your system, issue:
$ make clean
Usage
The output of ./brainwash -h follows:
$ ./brainwash -h brainwash 0.3 Copyright (c) 2004, 2005, 2006 Aggelos Orfanakos usage: ./brainwash [options] [file] options: -O optimize interpretation -d [N] print a memory snapshot of N surrounding cells upon # [11 or less] -i <file> read input from <file> -o <file> write output to <file> -V print version information and exit -h print this help message and exit
License
brainwash is licensed under the terms of the MIT license.
Download
brainwash-0.3.tar.gz (6.41 KiB)
Alternatives
For something significantly smaller and slower, you may want to give (my own) tinybf.rb a try. Other than that, the web is full of Brainfuck compilers and interpreters.