bf17

BF variable expander

Mar 19th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. #Pythonista 3.5
  2. # bfvx - brainfuck variable expander - 20 Mar 2019
  3. # variable usage as described here:
  4. # https://esolangs.org/wiki/Brainfuck_algorithms
  5.  
  6. from array import array
  7. array_size = 7 # set specifically for each program
  8. val_array = array('I')
  9. val_array = [ 0 for i in range(array_size)]
  10. var_list = array('I')
  11. var_list = [ 0 for i in range(array_size)]
  12. P = 0 # the Data Pointer!
  13. prog = ""
  14. prog_formatted = ""
  15. command_set = "<>+-,.[]"
  16. prog = ""
  17. counter = 0
  18. P = 0 # the data pointer
  19. move_P = 0 # +/- change in P, output ">" or "<"
  20. value = 0
  21.  
  22. ##### DEFINITIONS #####
  23. def get_value(x): # return value (i.e. assigned cell) for a specific variable
  24.     for i in range(array_size):
  25.         if((var_list[i]) == x):
  26.             return(val_array[i])
  27. #######################
  28.  
  29. # load arrays with variable names and values
  30. with open(input("bfv file: ")) as f:
  31.     for each_line in f:
  32.         if(each_line[0] == "="):
  33.             var_list[counter] = (each_line.split()[1])
  34.             val_array[counter] = (each_line.split()[2])
  35.             counter += 1
  36.  
  37. # create program listing without variable declarations
  38. with open("a.txt") as f:
  39.     for each_line in f:
  40.         if(each_line[0] != "="):
  41.             prog = prog + each_line
  42.  
  43. # separate variables from commands
  44. for i, each_char in enumerate(prog):
  45.     if(each_char in command_set):
  46.         prog_formatted = prog_formatted + ("\n" + each_char + "\n")
  47.     else:
  48.         prog_formatted = prog_formatted + each_char
  49.  
  50. # expand variables and print out program
  51. for i in prog_formatted.splitlines():
  52.     if((len(i)) == 0): # remove blank lines from output
  53.         pass
  54.     elif(i == ">"): # still need to process "<" and ">"
  55.         P += 1
  56.         print(">", end = "")
  57.     elif(i == "<"):
  58.         P -= 1
  59.         print("<", end = "")
  60.     elif(not(i in command_set)):
  61.         value = get_value(i)
  62.         move_P = int(value) - P
  63.         P = P + move_P
  64.         if(move_P < 0):
  65.             print("<" * abs(move_P), end = "") # expand
  66.         else:
  67.             print(">" * move_P, end = "") # expand
  68.     else:
  69.         print(i, end = "")
Advertisement
Add Comment
Please, Sign In to add comment