# python 3
# bf_format - format brainfuck code listings using nested indentation
# updated 2019-05-26-09:23 comment out the lf insertion for ']'
# I think it looks better this way, also results in shorter listing
ilv = -1
ich = " " # indent character - four regular spaces
prog = "" # store the whole program in a variable
command_set = "><+-,.[]"
f = open(input("bf file: "), 'r')
# read file
while True:
nextch = f.read(1)
if(not nextch):
f.close()
break
if(nextch in command_set):
prog += (nextch)
# format
for i in range(len(prog)):
if(prog[i] == "["):
print()
ilv += 1
print(ich * ilv, end = "")
if(prog[i] == "]"):
# print() # remove lf after ']'
# print(ich * ilv, end = "")
ilv -= 1
print(prog[i], end = "")
Comments