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
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
Simple project to assist in tracking/logging my working hours

Fill working hours Basic script to assist in the logging/tracking of my working hours How it works Create a file called projects.json in this director

Robin Kennedy-Reid 2 Oct 31, 2022
tgEasy | Easy for a Brighter Shine | Monkey Patcher Addon for Pyrogram

tgEasy | Easy for a Brighter Shine | Monkey Patcher Addon for Pyrogram

Jayant Hegde Kageri 35 Nov 12, 2022
Insights in greek football league 2020-2021 and bookmaker's accuracy

Greek_Football_League_Analysis_2020_2021 Aim of Project: This project aims in deriving useful insights from greek football league 2020-2021 by mean st

2 Jan 16, 2022
A timer for bird lovers, plays a random birdcall while displaying its image and info.

Birdcall Timer A timer for bird lovers. Siriema hatchling by Junior Peres Junior Background My partner needed a customizable timer for sitting and sta

Marcelo Sanches 1 Jul 08, 2022
ESteg - A simple steganography program for python

ESteg A simple steganography program to embed the contents of a text file into a

Jithin Renji 1 Jan 02, 2022
Ronin - Create Fud Meterpreter Payload To Hack Windows 11

Ronin - Create Fud Meterpreter Payload To Hack Windows 11

Dj4w3d H4mm4di 6 May 09, 2022
Open source stenotype engine

Plover Bringing stenography to everyone. Homepage Releases Wiki Blog Google Group Discord Chat About Installation Getting help Contributing Donations

Open Steno Project 2k Jan 09, 2023
Wordle Solver

Wordle Solver Installation Install the following onto your computer: Python 3.10.x Download Page Run pip install -r requirements.txt Instructions To r

John Bucknam 1 Feb 15, 2022
A Company Management System For Python

campany-management Getting started To make it easy for you to get started with GitLab, here's a list of recommended next steps. Already a pro? Just ed

hatice akpınar 3 Aug 29, 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
A collection of common regular expressions bundled with an easy to use interface.

CommonRegex Find all times, dates, links, phone numbers, emails, ip addresses, prices, hex colors, and credit card numbers in a string. We did the har

Madison May 1.5k Dec 31, 2022
edgetest is a tox-inspired python library that will loop through your project's dependencies, and check if your project is compatible with the latest version of each dependency

Bleeding edge dependency testing Full Documentation edgetest is a tox-inspired python library that will loop through your project's dependencies, and

Capital One 16 Dec 07, 2022
Small tool to use hero .json files created with Optolith for The Dark Eye/ Das Schwarze Auge 5 to perform talent probes.

DSA5-ProbeMaker A little tool for The Dark Eye 5th Edition (Das Schwarze Auge 5) to load .json from Optolith character generation and easily perform t

2 Jan 06, 2022
Chicks get hostloc points regularly

hostloc_getPoints 小鸡定时获取hostloc积分 github action大规模失效,mjj平均一人10鸡,以下可以部署到自己的小鸡上

59 Dec 28, 2022
This alerts you when the avalanche score a goal

This alerts you when the avalanche score a goal

Davis Burrill 1 Jan 15, 2022
Integration of CCURE access control system with automation HVAC of a commercial building

API-CCURE-Automation-Quantity-Floor Integration of CCURE access control system with automation HVAC of a commercial building CCURE is an access contro

Alexandre Edson Silva Pereira 1 Nov 24, 2021
Python Project For Beginner

Basic-Vitrual-AI-Assistant Python Project For Beginner Hey There, I had manipulated Selenium WebDriver to make this assistant. I hope, It will be help

Maruf Billah 13 Dec 12, 2022
Py-Parser est un parser de code python en python encore en plien dévlopement.

PY - PARSER Py-Parser est un parser de code python en python encore en plien dévlopement. Une fois achevé, il servira a de nombreux projets comme glad

pf4 3 Feb 21, 2022
A Python software implementation of the Intel 4004 processor

Pyntel4004 A Python software implementation of the Intel 4004 processor. General Information Two pass assembler using the original mnemonics, directiv

alshapton 5 Oct 01, 2022
Paxos in Python, tested with Jepsen

Python implementation of Multi-Paxos with a stable leader and reconfiguration, roughly following "Paxos Made Moderately Complex". Run python3 paxos/st

A. Jesse Jiryu Davis 25 Dec 15, 2022