brainfuck2ascii.py

Created by ciel

Created on June 02, 2021

2.35 KB


ABOUT♡

Here’s a little Brainfuck interpreter from Brainfuck to ASCII and Int.
If you don’t know Brainfuck, I strongly advise you to go check this course
And if you want to improve the interpreter, here’s the GitHub repo

USAGE

BrainF(your lovely code, your gorgeous input) (the input is optional if you don’t need it just doesn’t write it)


def BrainF(code, entrees = None):
    code = str(code)
    if entrees != None : #make a list of integers with the inputs
        entrees = str(entrees)
        entrees = entrees.split(" ")
        for input in entrees:
            input = int(input)
    y = 0 #where we are in the input/entrees list
    code = [char for char in code] #make a list of char with the input brainfuck code
    result = [] #the full result in ASCII
    i = 0 #number of iterations to read your code
    array = [i]# the array you're 'writing in'
    l = 0#the memory pointer index in array
    g = 0#on which iteration we are, I mean on which '[]'
    while len(code)>i:
        print(array)
        if code[i] == '-' :
            array[l] = array[l] - 1
        if code[i] == '+' :
            array[l] = array[l] + 1
        if code[i] == '[' :
            limitsOfIterationList = [i for i,x in enumerate(code) if x==']']#index of the ']' in your code to know where to stop the iteration
            limitOfIteration= limitsOfIterationList[g]
            codeInIteration = []
            h = i + 1
            while h < limitOfIteration:
                codeInIteration.append(code[h])
                h = h + 1
            lbis = l#to keep an eye on the original postion of the pointer to know when it's equal to 0
            e = 0
            while array[lbis] != 0 :
                if codeInIteration[e] == '-' :
                    array[l] = array[l] - 1
                if codeInIteration[e] == '+' :
                    array[l] = array[l] + 1
                if codeInIteration[e] == '<' :
                    l = l - 1
                if codeInIteration[e] == '>' :
                    l =l + 1
                e = e + 1
                if  e == len(codeInIteration)  :
                    e = 0
            i = i + (limitOfIteration-i)
            g = g + 1
        if code[i] == '<' :
            l = l - 1
        if code[i] == '>' :
            if (len(array)-1) < (l+1) :
                array.append(0)#we've to had a 0 to not be out of range
                l = l + 1
            else : l = l + 1
        if code[i] == ',' :
            array[l] = int(entrees[y])
        if code[i] == '.' :
            print('int -> {} char/ASCII -> {}'.format(array[l],chr(array[l])))
            result.append(chr(array[l]))
        i = i + 1
    for char in result :
        print(char,end ='')

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.