Last active
July 25, 2019 00:43
-
-
Save WitherOrNot/3cba0322e31edb406fcfaf91e43cb904 to your computer and use it in GitHub Desktop.
BrainFuck Interpreter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import time | |
import os | |
class _Getch: | |
"""Gets a single character from standard input. Does not echo to the | |
screen.""" | |
def __init__(self): | |
try: | |
self.impl = _GetchWindows() | |
except ImportError: | |
self.impl = _GetchUnix() | |
def __call__(self): return self.impl() | |
class _GetchUnix: | |
def __init__(self): | |
import tty, sys | |
def __call__(self): | |
import sys, tty, termios | |
fd = sys.stdin.fileno() | |
old_settings = termios.tcgetattr(fd) | |
try: | |
tty.setraw(sys.stdin.fileno()) | |
ch = sys.stdin.read(1) | |
finally: | |
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | |
return ch | |
class _GetchWindows: | |
def __init__(self): | |
import msvcrt | |
def __call__(self): | |
import msvcrt | |
return msvcrt.getch() | |
getch = _Getch() | |
code = open(sys.argv[1], "r").read() | |
code = ''.join([x for x in code if x in "<>+-,.[]#"]) | |
if code[0] == "#": | |
debug = True | |
else: | |
debug = False | |
code = code.replace("#","") | |
def bmap(s): | |
st = [] | |
bm = {} | |
for i in range(len(s)): | |
if s[i] == "[": | |
st.append(i) | |
elif s[i] == "]": | |
a = st.pop() | |
bm[a] = i | |
bm[i] = a | |
return bm | |
bm = bmap(code) | |
clen = 10 | |
cells = [0]*clen | |
cellsf = "" | |
arr = [""]*clen | |
pout = [] | |
ci = 0 | |
ptr = 0 | |
if debug: | |
while ci < len(code): | |
ins = code[ci] | |
if ins == ">": | |
ptr += 1 | |
if ptr == len(cells): | |
cells += [0] | |
elif ins == "<": | |
if ptr <= 0: | |
ptr = 0 | |
else: | |
ptr -= 1 | |
elif ins == "+": | |
cells[ptr] = (cells[ptr]+1)%255 | |
elif ins == "-": | |
cells[ptr] -= 1 | |
if cells[ptr] == -1: | |
cells[ptr] = 255 | |
elif ins == "[" and cells[ptr] == 0: | |
ci = bm[ci] | |
elif ins == "]" and cells[ptr] != 0: | |
ci = bm[ci] | |
elif ins == ".": | |
pout.append(chr(cells[ptr])) | |
elif ins == ",": | |
print("Provide input.") | |
cells[ptr] = ord(getch()) | |
cellsf = "|"+'|'.join([str(x) for x in cells])+"|" | |
start = 0 | |
end = min([len(code),20]) | |
carr = [" "]*(end) | |
if ci == 0: | |
pass | |
elif ci >= end: | |
diff = ci - end | |
end += diff | |
start += diff | |
elif ci < start: | |
diff = start - ci | |
start -= diff | |
end -= diff | |
if ci-start == len(carr): | |
start += 1 | |
end += 1 | |
codef = "|"+code[start:end]+"|" | |
print("Code tape:") | |
print(codef) | |
carr[ci-start] = "^" | |
carr = " "+''.join(carr)+" " | |
print(carr) | |
print("Memory tape: ") | |
arr = [[" "]*len(str(x)) for x in cells] | |
arr[ptr][0] = "^" | |
arr = " "+' '.join([''.join(x) for x in arr]) | |
#os.system("cls||clear") | |
print(cellsf) | |
print(arr) | |
print("\n") | |
print("Output: \n"+''.join(pout)) | |
print("\n") | |
time.sleep(0.05) | |
ci += 1 | |
else: | |
while ci < len(code): | |
ins = code[ci] | |
if ins == ">": | |
ptr += 1 | |
if ptr == len(cells): | |
cells += [0] | |
elif ins == "<": | |
if ptr <= 0: | |
ptr = 0 | |
else: | |
ptr -= 1 | |
elif ins == "+": | |
cells[ptr] = (cells[ptr]+1)%255 | |
elif ins == "-": | |
cells[ptr] -= 1 | |
if cells[ptr] == -1: | |
cells[ptr] = 255 | |
elif ins == "[" and cells[ptr] == 0: | |
ci = bm[ci] | |
elif ins == "]" and cells[ptr] != 0: | |
ci = bm[ci] | |
elif ins == ".": | |
sys.stdout.write(chr(cells[ptr])) | |
elif ins == ",": | |
print("Provide input.") | |
cells[ptr] = ord(getch()) | |
ci += 1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment