An open API service indexing awesome lists of open source software.

https://github.com/prat-man/brainfuck

A fast Brainfuck interpreter and compiler written in C
https://github.com/prat-man/brainfuck

brainfuck brainfuck-interpreter c interpreter

Last synced: 9 months ago
JSON representation

A fast Brainfuck interpreter and compiler written in C

Awesome Lists containing this project

README

          

Brainfuck Interpreter Logo

# Brainfuck

Brainfuck is an esoteric programming language created in 1993 by Urban Müller, and is notable for its extreme minimalism.

The language consists of only eight simple commands and an instruction pointer. While it is fully Turing complete, it is not intended for practical use, but to challenge and amuse programmers. Brainfuck simply requires one to break commands into microscopic steps.

The language's name is a reference to the slang term brainfuck, which refers to things so complicated or unusual that they exceed the limits of one's understanding.


## Brainfuck Interpreter

This project provides a fast Brainfuck interpreter written in C.

It is implemented with multiple optimizations to increase execution speed.

Default behaviour is to interpret and execute immediately.

It is cross-platform compatible, and has been tested on Windows, Linux, and macOS.


## Brainfuck Compiler

This project provides a fast Brainfuck compiler written in C.

It converts brainfuck code to highly optimized C code and then compiles the C code into machine executable file using GCC.
To compile brainfuck code, use the -c or --compile option.

If desired, brainfuck code can be translated to C code without compiling to executable using the -x or --translate option.

The generated C code is cross-platform compatible, and has been tested on Windows, Linux, and macOS.


## Usage

brainfuck [options]

#### Options

-c
--compile Translate to C and compile to machine code [requires GCC]

-x
--translate Translate to C but do not compile

-t
--tape Size of interpreter tape [must be equal to or above 1000]

-s
--stack Size of interpreter stack [must be equal to or above 100]

-v
--version Show product version and exit

-i
--info Show product information and exit

-h
--help Show this help message and exit

__Note:__ Default behaviour is to interpret and execute immediately.


## Build

Run the following commands inside src directory.

gcc main.c -o main.o -c -O3
gcc stack.c -o stack.o -c -O3
gcc -o brainfuck main.o stack.o -O3


## Optimizations

* Jumps between [ and ]
* Compacts consecutive > and <
* Compacts consecutive + and -
* Optimizes [-] to set(0)
* Optimizes [<] to scan_left(0)
* Optimizes [>] to scan_right(0)
* Removes consecutive > and < if the net movement is zero
* Removes consecutive + and - if the net change is zero
* Removes consecutive + and - if immediately followed by an input operation ( , )


## Credits

The brainfuck interpreter/compiler icon is made by Smashicons from www.flaticon.com.