DetailedFuck Python interpreter

From Esolang
Jump to navigation Jump to search
Back to DetailedFuck

The following is an interpreter in Python by User:None1 for DetailedFuck.

import sys
def detailed_fuck(code):
    s1=[]
    s2=[]
    matches={}
    tape=[0]*1000000
    for i,j in enumerate(code):
        if j=="IF THE CELL UNDER THE MEMORY POINTER'S VALUE IS ZERO INSTEAD OF READING THE NEXT COMMAND IN THE PROGRAM JUMP TO THE CORRESPONDING COMMAND EQUIVALENT TO THE ] COMMAND IN BRAINFUCK":
            s1.append(i)
        if j=="IF THE CELL UNDER THE MEMORY POINTER'S VALUE IS NOT ZERO INSTEAD OF READING THE NEXT COMMAND IN THE PROGRAM JUMP TO THE CORRESPONDING COMMAND EQUIVALENT TO THE [ COMMAND IN BRAINFUCK":
            m=s1.pop()
            matches[m]=i
            matches[i]=m
    cp=0
    p=0
    while cp<len(code):
        if code[cp]=='INCREMENT THE CELL UNDER THE MEMORY POINTER BY ONE':
            tape[p]=(tape[p]+1)%256
        if code[cp]=='DECREMENT THE CELL UNDER THE MEMORY POINTER BY ONE':
            tape[p]=(tape[p]-1)%256
        if code[cp]=='REPLACE THE CELL UNDER THE MEMORY POINTER\'S VALUE WITH THE ASCII CHARACTER CODE OF USER INPUT':
            tape[p]=ord(sys.stdin.read(1))%256
        if code[cp]=='PRINT THE CELL UNDER THE MEMORY POINTER\'S VALUE AS AN ASCII CHARACTER':
            print(chr(tape[p]),end='')
        if code[cp]=='MOVE THE MEMORY POINTER ONE CELL TO THE LEFT':
            p-=1
        if code[cp]=='MOVE THE MEMORY POINTER ONE CELL TO THE RIGHT':
            p+=1
        if code[cp]=="IF THE CELL UNDER THE MEMORY POINTER'S VALUE IS ZERO INSTEAD OF READING THE NEXT COMMAND IN THE PROGRAM JUMP TO THE CORRESPONDING COMMAND EQUIVALENT TO THE ] COMMAND IN BRAINFUCK":
            if not tape[p]:
                cp=matches[cp]
        if code[cp]=="IF THE CELL UNDER THE MEMORY POINTER'S VALUE IS NOT ZERO INSTEAD OF READING THE NEXT COMMAND IN THE PROGRAM JUMP TO THE CORRESPONDING COMMAND EQUIVALENT TO THE [ COMMAND IN BRAINFUCK":
            if tape[p]:
                cp=matches[cp]
        cp+=1
code=sys.stdin.read()
detailed_fuck(code.split('\n'))