User:DoggyDogWhirl/One-Dimensional Storage Visualizer
Jump to navigation
Jump to search
Written in Python 3. brainfuck, deadfish, and Elevatorfuck are built-in.
code="""code goes here"""
language="language name goes here"
visualization="visualization method goes here"
# Languages:
# brainfuck, deadfish, Elevatorfuck
# Visualizaton Methods:
# colornumber, pointnumber, binary, bar
# colornumber and pointnumber support 3 digits
# binary supports 8 bits
# one-dimensional storage visualizer by DoggyDogWhirl
#----------Imports----------
import getch
import sys
from colorama import Back
#----------Languages----------
def brainfuck(code):
code="".join([i for i in code if i in "<>+-.,[]"])
temp_bracestack, bracemap = [], {}
for position, command in enumerate(code):
if command == "[": temp_bracestack.append(position)
if command == "]":
assert len(temp_bracestack)!=0,"Brackets do not match"
start = temp_bracestack.pop()
bracemap[start] = position
bracemap[position] = start
assert len(temp_bracestack)==0,"Brackets do not match"
cells, codeptr, cellptr = [0], 0, 0
while codeptr < len(code):
command = code[codeptr]
if command == ">": cellptr += 1
if cellptr == len(cells): cells.append(0)
if command == "<": cellptr = 0 if cellptr <= 0 else cellptr - 1
if command == "+": cells[cellptr] += 1
if command == "-": cells[cellptr] -= 1
if command == "[" and cells[cellptr] == 0: codeptr = bracemap[codeptr]
if command == "]" and cells[cellptr] != 0: codeptr = bracemap[codeptr]
if command == ".": print(chr(cells[cellptr]))
if command == ",": cells[cellptr] = ord(getch.getch())
codeptr += 1
yield cells, cellptr
def deadfish(code):
a=0
for i in code:
if i=="i": a+=1
if i=="d": a-=1
if i=="s": a*=a
if i=="o": print(a)
if a==256 or a==-1: a=0
yield a
def Elevatorfuck(code):
code="".join([i for i in code if i in "<>^v-.,[]~"])
temp_bracestack, bracemap = [], {}
for position, command in enumerate(code):
if command == "[": temp_bracestack.append(position)
if command == "]":
assert len(temp_bracestack)!=0,"Brackets do not match"
start = temp_bracestack.pop()
bracemap[start] = position
bracemap[position] = start
assert len(temp_bracestack)==0,"Brackets do not match"
cells, celldirs, codeptr, cellptr = [0], [0], 0, 0
while codeptr < len(code):
command = code[codeptr]
if command == ">": cellptr += 1
if cellptr == len(cells): cells.append(0); celldirs.append(0)
if command == "<": cellptr = 0 if cellptr <= 0 else cellptr - 1
if command == "^": celldirs[cellptr] = 1
if command == "v": celldirs[cellptr] = -1
if command == "-": celldirs[cellptr] = 0
if command == "[" and cells[cellptr] == 0: codeptr = bracemap[codeptr]
if command == "]" and cells[cellptr] != 0: codeptr = bracemap[codeptr]
if command == ".": print(chr(cells[cellptr]))
if command == ",": cells[cellptr] = ord(getch.getch())
for i in range(len(cells)):
cells[i]+=celldirs[i]
if cells[i] == 256: cells[i] = 0
if cells[i] == -1: cells[i] = 255
codeptr += 1
yield cells, cellptr
#----------Visualizations----------
def colornumber(tape, point=None):
tape=["%3.3s"%i for i in tape]
if point != None: tape[point]=Back.RED + tape[point] + Back.RESET
tape=" "+" ".join(tape)
print(tape)
def pointnumber(tape, point=None):
tape=" ".join(["%4.3s"%i for i in tape])
print(tape)
if point != None: print(" "*point+" ^")
def binary(tape, point=None):
tape=["{:b}".format(i) for i in tape]
tape=["{:0>8.8}".format(i) for i in tape]
print("\n".join([" ".join(i) for i in zip(*tape)]))
print(" "*point+"^" if point!=None else "")
def bar(tape, point=None):
s="@"
tape=[s*i for i in tape]
m=max([len(i) for i in tape])
tape=[("{:>%d}"%m).format(i) for i in tape]
print("\n".join([" ".join(i) for i in zip(*tape)]))
print(" "*point+"^" if point!=None else "")
#----------Execution----------
language=eval(language)
visualization=eval(visualization)
for i in language(code): visualization(*i)