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
Flow control is the order in which statements or blocks of code are executed at runtime based on a condition. Learn Conditional statements, Iterative statements, and Transfer statements

03_Python_Flow_Control Introduction đź‘‹ The control flow statements are an essential part of the Python programming language. A control flow statement

Milaan Parmar / Милан пармар / _米兰 帕尔马 209 Oct 31, 2022
Aerospace utilities: flight conditions package, standard atmosphere model, and more.

Aerospace Utilities About Module that contains commonly-used aerospace utilities for problem solving. Flight Condition: input altitude to compute comm

1 Jan 03, 2022
Web interface for browsing, search and filtering recent arxiv submissions

Web interface for browsing, search and filtering recent arxiv submissions

Andrej 4.8k Jan 08, 2023
Old versions of Deadcord that are problematic or used as reference.

⚠️ Unmaintained and broken. We have decided to release the old version of Deadcord before our v1.0 rewrite. (which will be equiped with much more feat

Galaxzy 1 Feb 10, 2022
A pypi package details search python module

A pypi package details search python module

Fayas Noushad 5 Nov 30, 2021
Cylc: a workflow engine for cycling systems

Cylc: a workflow engine for cycling systems. Repository master branch: core meta-scheduler component of cylc-8 (in development); Repository 7.8.x branch: full cylc-7 system.

The Cylc Workflow Engine 205 Dec 20, 2022
Python library to decode the EU Covid-19 vaccine certificate

DCC Utils Python library to decode the EU Covid-19 vaccine certificate, as specified by the EU. Setup pip install dcc-utils Make sure zbar is installe

Developers Italia 13 Mar 11, 2022
hey, this repo is the backend of the sociio project

sociio backend Hey, this repository is a part of sociio project , In this repo we are working to create an independent server for everything you can i

2 Jun 09, 2022
Pengenalan para anggota KOMPETEGRAM

Pengenalan Anggota KOMPETEGRAM Apa isi repositori ini ? đź’¬ Repositori ini berisi pengenalan nama anggota KOMPETEGRAM dari seluruh angkatan atau Batch.

Repositori KOMPETEGRAM 7 Sep 17, 2022
Todos os exercĂ­cios do Curso de Python, do canal Curso em VĂ­deo, resolvidos em Python, Javascript, Java, C++, C# e mais...

ExercĂ­cios - CeV Oferecido por Linguagens utilizadas atualmente O que vai encontrar aqui? đź‘€ Esse repositĂłrio Ă© dedicado a armazenar todos os enunciad

Coding in Community 43 Nov 10, 2022
This interactive script demonstrates the Menezes-Vanstone-EC-Cryptosystem

Menezes-Vanstone-EC-Cryptosystem This interactive script demonstrates the Meneze

Nishaant Goswamy 1 Jan 02, 2022
Advanced Developing of Python Apps Final Exercise

Advanced-Developing-of-Python-Apps-Final-Exercise This is an exercise that I did for a python advanced learning course. The exercise is divided into t

Alejandro Méndez Fernández 1 Dec 04, 2021
This repo holds custom callback plugin, so your Ansible could write everything in the PostgreSQL database.

English What is it? This is callback plugin that dumps most of the Ansible internal state to the external PostgreSQL database. What is this for? If yo

Sergey Pechenko 19 Oct 21, 2022
Scripts to integrate DFIR-IRIS, MISP and TimeSketch

Scripts to integrate DFIR-IRIS, MISP and TimeSketch

Koen Van Impe 20 Dec 16, 2022
Python Project Template

A low dependency and really simple to start project template for Python Projects.

Bruno Rocha 651 Dec 29, 2022
Open source book about making Python packages.

Python packages Tomas Beuzen & Tiffany Timbers Python packages are a core element of the Python programming language and are how you create organized,

Python Packages 169 Jan 06, 2023
It's like Forth but in Python

It's like Forth but written in Python. But I don't actually know for sure since I never programmed in Forth, I only heard that it's some sort of stack-based programming language. Porth is also stack-

Tsoding 619 Dec 21, 2022
A variant caller for the GBA gene using WGS data

Gauchian: WGS-based GBA variant caller Gauchian is a targeted variant caller for the GBA gene based on a whole-genome sequencing (WGS) BAM file. Gauch

Illumina 16 Oct 13, 2022
My solutions to Advent of Code 2021 (written in Python)

Advent of Code 2021 This repository contains my solutions for the 2021 edition of Advent of Code. Please do not expect perfectly polished solutions, m

Nils 2 May 29, 2022
solsim is the Solana complex systems simulator. It simulates behavior of dynamical systems—DeFi protocols, DAO governance, cryptocurrencies, and more—built on the Solana blockchain

solsim is the Solana complex systems simulator. It simulates behavior of dynamical systems—DeFi protocols, DAO governance, cryptocurrencies, and more—built on the Solana blockchain

William Wolf 12 Jul 13, 2022