Advertisement
bf17

BF formatter

Apr 5th, 2019
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. # python 3
  2. # bf_format - format brainfuck code listings using nested indentation
  3. # updated 2019-05-26-09:23 comment out the lf insertion for ']'
  4. #   I think it looks better this way, also results in shorter listing
  5.  
  6. ilv = -1
  7. ich = "    " # indent character - four regular spaces
  8. prog = ""    # store the whole program in a variable
  9. command_set = "><+-,.[]"
  10.  
  11. f = open(input("bf file: "), 'r')
  12. # read file
  13. while True:
  14.    nextch = f.read(1)
  15.    if(not nextch):
  16.        f.close()
  17.        break
  18.    if(nextch in command_set):
  19.        prog += (nextch)
  20. # format
  21. for i in range(len(prog)):
  22.     if(prog[i] == "["):
  23.         print()
  24.         ilv += 1
  25.         print(ich * ilv, end = "")
  26.     if(prog[i] == "]"):
  27. #        print()                     # remove lf after ']'
  28. #        print(ich * ilv, end = "")
  29.         ilv -= 1
  30.     print(prog[i], end = "")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement