#Pythonista 3.5
# bfvx - brainfuck variable expander - 20 Mar 2019
# variable usage as described here:
# https://esolangs.org/wiki/Brainfuck_algorithms
from array import array
array_size = 7 # set specifically for each program
val_array = array('I')
val_array = [ 0 for i in range(array_size)]
var_list = array('I')
var_list = [ 0 for i in range(array_size)]
P = 0 # the Data Pointer!
prog = ""
prog_formatted = ""
command_set = "<>+-,.[]"
prog = ""
counter = 0
P = 0 # the data pointer
move_P = 0 # +/- change in P, output ">" or "<"
value = 0
##### DEFINITIONS #####
def get_value(x): # return value (i.e. assigned cell) for a specific variable
for i in range(array_size):
if((var_list[i]) == x):
return(val_array[i])
#######################
# load arrays with variable names and values
with open(input("bfv file: ")) as f:
for each_line in f:
if(each_line[0] == "="):
var_list[counter] = (each_line.split()[1])
val_array[counter] = (each_line.split()[2])
counter += 1
# create program listing without variable declarations
with open("a.txt") as f:
for each_line in f:
if(each_line[0] != "="):
prog = prog + each_line
# separate variables from commands
for i, each_char in enumerate(prog):
if(each_char in command_set):
prog_formatted = prog_formatted + ("\n" + each_char + "\n")
else:
prog_formatted = prog_formatted + each_char
# expand variables and print out program
for i in prog_formatted.splitlines():
if((len(i)) == 0): # remove blank lines from output
pass
elif(i == ">"): # still need to process "<" and ">"
P += 1
print(">", end = "")
elif(i == "<"):
P -= 1
print("<", end = "")
elif(not(i in command_set)):
value = get_value(i)
move_P = int(value) - P
P = P + move_P
if(move_P < 0):
print("<" * abs(move_P), end = "") # expand
else:
print(">" * move_P, end = "") # expand
else:
print(i, end = "")
Comments