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
Reso is a low-level circuit design language and simulator, inspired by things like Redstone, Conway's Game of Life, and Wireworld.

Reso Reso is a low-level circuit design language and simulator, inspired by things like Redstone, Conway's Game of Life, and Wireworld. What is Reso?

Lynn 287 Nov 26, 2022
Cloud-native SIEM for intelligent security analytics for your entire enterprise.

Microsoft Sentinel Welcome to the Microsoft Sentinel repository! This repository contains out of the box detections, exploration queries, hunting quer

Microsoft Azure 2.9k Jan 02, 2023
使用京东cookie一键生成所有退会链接

JDMemberCloseLinks 本项目旨在使用京东cookie一键生成所有退会链接

hyzaw 68 Jun 10, 2022
Hoopoe - Get notified of important stuff, right away.

Hoopoe - Get notified of important stuff, right away. Report a Bug · Request a Feature . Ask a Question Table of Contents About Getting Started Prereq

Vahid Al 8 Nov 12, 2022
A continuation Of Project Glow By @glowstik-yt

Project Glow Greetings, I see you have stumbled upon project glow. Project glow is an open source bot worked on by many people to create a good and sa

1 Nov 17, 2021
Taking the fight to the establishment.

Throwdown Taking the fight to the establishment. Wat? I wanted a simple markdown interpreter in python and/or javascript to output html for my website

Trevor van Hoof 1 Feb 01, 2022
E5 自动续期

请选择跳转 新版本系统 (2021-2-9采用): 以后更新都在AutoApi,采用v0.0版本号覆盖式更新 AutoApi : 最新版 保留1到2个稳定的简易版,防止萌新大范围报错 AutoApi'X' : 稳定版1 ( 即本版AutpApiP ) AutoApiP ( 即v5.0,稳定版 ) —

95 Feb 15, 2021
Coded in Python 3 - I make for education, easily clone simple website.

Simple Website Cloner - Single Page Coded in Python 3 - I make for education, easily clone simple website. How to use ? Install Python 3 first. Instal

Phạm Đức Thanh 2 Jan 13, 2022
Python DSL for writing PDDL

PDDL in Python – Python DSL for writing a PDDL A minimal implementation of a DSL which allows people to write PDDL in python. Based on parsing python’

International Business Machines 21 Nov 22, 2022
A stupid obfuscation thing

StupidObfuscation A stupid obfuscation thing How it works The obfuscator takes a string, splits into pieces of one, then, using the table from letter.

Echo 2 May 03, 2022
A collection of existing KGQA datasets in the form of the huggingface datasets library, aiming to provide an easy-to-use access to them.

KGQA Datasets Brief Introduction This repository is a collection of existing KGQA datasets in the form of the huggingface datasets library, aiming to

Semantic Systems research group 21 Jan 06, 2023
A notebook explaining the principle of adversarial attacks and their defences

TL;DR: A notebook explaining the principle of adversarial attacks and their defences Abstract: Deep neural networks models have been wildly successful

1 Jan 22, 2022
🌍💉 Global COVID-19 vaccination data at the regional level.

COVID-19 vaccination data at subnational level. To ensure its officiality, the source data is carefully verified.

sociepy 61 Sep 21, 2022
Parametric Bottle in CADQuery

Parametric Bottle using CADQuery The proposed code makes it possible to generate different types and sizes of 3D bottles in order to train Pixel2mesh

Ayoub EL HOUDRI 1 May 22, 2022
Example platform plugin that fixes fentry calls in Binja

Example Binja Platform Plugin This is an example Binja platform plugin which fixes up linux kernel module calls to __fentry__. __fentry__ is the linux

_yrp 2 Oct 07, 2021
Cute study buddy that helps you study with the Pomodoro technique!

study-buddy Cute study buddy that helps you study with the Pomodoro (or Animedoro) technique! Kirby The Kirby folder has a Kirby, pink-themed Pomodoro

Ethan Emmanuel 1 Jan 19, 2022
Code for the manim-generated scenes used in 3blue1brown videos

This project contains the code used to generate the explanatory math videos found on 3Blue1Brown. This almost entirely consists of scenes generated us

Grant Sanderson 4.1k Jan 02, 2023
Repositorio com arquivos processados da CPI da COVID para facilitar analise

cpi4all Repositorio com arquivos processados da CPI da COVID para facilitar analise Organização No site do senado é possivel encontrar a lista de todo

Breno Rodrigues Guimarães 12 Aug 16, 2021
Esercizi di Python svolti per il biennio di Tecnologie Informatiche.

Esercizi di Python Un piccolo aiuto per Sofia che nel 2° quadrimestre inizierà Python :) Questo repository (termine tecnico di Git) puoi trovare tutti

Leonardo Essam Dei Rossi 2 Nov 07, 2022
A simple but flexible plugin system for Python.

PluginBase PluginBase is a module for Python that enables the development of flexible plugin systems in Python. Step 1: from pluginbase import PluginB

Armin Ronacher 1k Dec 16, 2022