Skip to content

Instantly share code, notes, and snippets.

/bfsint.py Secret

Created March 29, 2016 19:52
Show Gist options
  • Save anonymous/b9d2fbd692cc1be814a28f0288a22a24 to your computer and use it in GitHub Desktop.
Save anonymous/b9d2fbd692cc1be814a28f0288a22a24 to your computer and use it in GitHub Desktop.
bf# Python esolang interpreter
#!/usr/bin/python3
# Python interpreter for Brainfuck# (bf#) by TheCrimulo
# bf# by TheCrimulo
# Open domain, feel free to use
#
# paukkupalika: brainkek
# me: yustin b-evr
import sys
# Get filename, open it and convert it to a plain string.
if len(sys.argv) == 1:
with open('bfsint.py', 'r+') as f:
for l in f.readlines():
print(l)
quit()
filename = sys.argv[1]
with open(filename, 'r') as f:
evr = f.read().replace('\n', '')
# Tape and pointers
tape = list(range(256))
for i in tape:
tape[i] = 0;
pointer_pos = 0
cycle = 0
# Extra vars
assign = []
stack = list(range(1))
for i in stack:
stack[i] = 0;
depthl = 0
depthlsave = 0
# Iterate through each character and interpret the actions
#for i in range(0, len(evr)):
i = 0
while i < len(evr):
cval = tape[pointer_pos]
if evr[i] == ">":
pointer_pos += 1
elif evr[i] == "<":
pointer_pos -= 1
elif evr[i] == "+":
tape[pointer_pos] += 1
elif evr[i] == "-":
tape[pointer_pos] -= 1
elif evr[i] == ".":
print('#i' + str(pointer_pos) + ': ' + str(cval))
elif evr[i] == "@":
asciival = str(chr(tape[pointer_pos]))
print('#a' + str(pointer_pos) + ': ' + asciival)
elif evr[i] == "$":
f = i + 1
while evr[f] != "*":
assign.append(evr[f])
f += 1
assl = ''.join(assign)
assld = int(assl, 16)
tape[pointer_pos] = assld
# $2A*[>++[>++<-]<-]
elif evr[i] == "[":
depthl += 1
depthlsave = depthl
if cval == 0:
while (evr[i] != "]") or (depthl != depthlsave):
i += 1
if evr[i] == "[":
depthl += 1
if evr[i] == "]":
depthl -= 1
continue
else:
stack.append(i)
elif evr[i] == "]":
if cval == 0:
if len(stack) == 1:
del stack[0]
continue
else:
lenstack = len(stack) - 1
del stack[lenstack]
continue
else:
i = stack[len(stack) - 1]
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment