An implementation of an interpreter for the Brainfuck esoteric language in Python

Overview

Brainfuck Interpreter in Python

An implementation of an interpreter for the Brainfuck esoteric language in Python.

🧠 The Brainfuck Language

Created in 1993 by Urban Müller, Brainfuck is an esoteric programming language, which means it is not supposed to be used for production. It is widely known for its minimalist syntax, having only eight commands in total.

It's name comes from the fact that it is very difficult to write meaningful and useful logic using the language, as the syntax can get very confusing and unreadable.

How to use

In Brainfuck, we have an array will unsigned bytes, which means it has an indefinite amount of cells containing numbers from 0 to 255. The "traditional dialect" had 30000 cells, but newer interpreters sometimes include larger buffers with different types of data to store more than 255 or negative values.

To traverse through memory, we use a data pointer, which points to one cell at a time. Operations are made based on the value at the pointer. The eight commands we have to manipulate values are:

  • >: moves the pointer to the right;
  • <: moves the pointer to the left;
  • +: increments value currently at the pointer by 1;
  • -: decrements value currently at the pointer by 1;
  • .: prints the value at the current pointer;
  • ,: replaces the value at the pointer by a user input;
  • [: if value at pointer is 0, goes to the next ] in the code;
  • ]: if value at pointer is not 0, goes back to the first [ it finds in the code.

The latter two are used to create loops. When we hit a [, we either execute everything until the next ] or we just skip it all. Once we get to the ], if the value at the pointer is 0, we just keep going and executing the rest of the code; otherwise, we go all the way back to the matching [ and start executing everything again.

It is usual to translate the value to its ASCII table equivalent when the . command is executed. Therefore, a cell containing a value of 65 comes out as an a. That's how you create your first "Hello World!" application, for example.

There are no if statements or comparison operators, people usually find work-arounds when those are needed using the loops.

The Python interpreter

There are two classes in the Python package: Brainfuck (the interpreter) and BrainfuckException (in case something goes wrong).

The Brainfuck interpreter accepts two arguments:

  • safe (bool): safe-mode. When turned off, Brainfuck errors raise the BrainfuckException in Python, which behaves as a regular exception in application. When turned on, Brainfuck errors are only printed. Default value is True (turned on);
  • memory_size: (int): the size of the buffer array. Default value is 30000.

When instantiated, the interpreter has three methods:

  • run_file: reads and interprets a .bf/.b file;
  • prompt: opens the Brainfuck interpreter in the terminal;
  • run: receives and interprets a string with Brainfuck code.

The memory array is stored in the data property, which can be accessed anytime by using instance.data, where instance is a Brainfuck interpreter instance.

Example:

The following program is used to print "Hello, World!" to the screen:

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

💻 Requirements

The following setup was used to make the source code on this repository:

  • Windows 10
  • Python 3.10
  • Numpy
You might also like...
sawa (ꦱꦮ) is an open source programming language, an interpreter to be precise, where you can write python code using javanese character.
sawa (ꦱꦮ) is an open source programming language, an interpreter to be precise, where you can write python code using javanese character.

ꦱꦮ sawa (ꦱꦮ) is an open source programming language, an interpreter to be precise, where you can write python code using javanese character. sawa iku

Simple python code for compile brainfuck program.

py-brainf*ck Just a basic compiled that compiles your brainf*ck codes and gives you informations about memory, used cells, dumped version, logs etc...

The Official interpreter for the Pix programming language.
The Official interpreter for the Pix programming language.

The official interpreter for the Pix programming language. Pix Pix is a programming language dedicated to readable syntax and usability Q) Is Pix the

Encode and decode cancro lang files to and from brainfuck

cancrolang Encode and decode cancro lang files to and from brainfuck. examples python3 main.py -f hello.cancro --run Hello World! the interpreter is n

Would upload anything I do with/related to brainfuck

My Brainfu*k Repo Basically wanted to create something with Brainfu*k but realized that with the smol brain I have, I need to see the cell values real

Recreate the joys of Office Assistant from the comfort of the Python interpreter

Recreate the joys of Office Assistant from the comfort of the Python interpreter.

Expression interpreter written in Python

Calc Interpreter An interpreter modeled after a calculator implemented in Python 3. The program currently only supports basic mathematical expressions

Ked interpreter built with Lex, Yacc and Python

Ked Ked is the first programming language known to hail from The People's Republic of Cork. It was first discovered and partially described by Adam Ly

Learning with Peter Norvig's lis.py interpreter

Learning with lis.py This repository contains variations of Peter Norvig's lis.py interpreter for a subset of Scheme, described in (How to Write a (Li

Owner
Carlos Santos
Software developer and tech enthusiast from Brazil. Currently working as a Data Science Intern at Mercedes-Benz. Working with Python and learning a lot more.
Carlos Santos
A Brainfuck interpreter written in Python.

A Brainfuck interpreter written in Python.

Ethan Evans 1 Dec 5, 2021
Rick Astley Language is a rick roll oriented, dynamic, strong, esoteric programming language.

Rick Roll Language / Rick Astley Language A rick roll oriented, dynamic, strong, esoteric programming language. Prolegomenon The reasons that I made t

Rick Roll Programming Language 658 Jan 9, 2023
A brainfuck-based game oriented language written in python.

GF.py STILL WIP Gamefuck.py is a programming language based off brainfuck. It is oriented towards game development, and as such has many commands spec

Xenon 1 Feb 23, 2022
MindF**k it's a programming language as BrainFuck, but with some cool features.

MindF**k Description MindF**k it's a programming language as BrainFuck, but with some cool features. Symbol What does symbol mean > Next slot < Previo

tixcode 0 Jun 15, 2022
A simple assembly- and brainfuck-inspired stack-based language

asm-stackfuck A simple assembly- and brainfuck-inspired stack-based language. The language has a few goals: Be stack-based Look like assembly Have a s

Nils Trinity 1 Feb 6, 2022
A partial-transpiler that converts a subset of Python to the Folders esoteric programming language

Py2Folders A partial-transpiler that converts a subset of Python to the Folders esoteric programming language Folders Folders is an esoteric programmi

Daniel Johnson 1 Dec 23, 2021
An esoteric programming language that supports concurrency, regex, and web requests.

The Hofstadter Esoteric Programming Language Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's La

Austin Henley 19 Dec 27, 2022
NORETURN is an esoteric programming language, based around the idea of not going back

NORETURN NORETURN is an esoteric programming language, based around the idea of not going back Concept Program coded in noreturn runs over one array,

null 1 Dec 15, 2021
A gamey, snakey esoteric programming language

Snak Snak is an esolang based on the classic snake game. Installation You will need python3. To use the visualizer, you will need the curses module. T

David Rutter 3 Oct 10, 2022
Binary++ is an esoteric programming language based on* binary

Binary++ is an esoteric programming language based on* binary. * It's meant to be based on binary, but you can write Binary++ code using different mea

Supercolbat 3 Feb 18, 2022