I have this obsession with esoteric programming languages. So I decided to spiff up my previous Brainfuck interpreter.
# Simple BrainF*** interpreter
# Class that stores lang variables
class Lang(object):
step = 0
cell = [0] * 30000
test_cell = [0] * 30000
pos = 0
test_pos = 0
loop = False
loop_ret = 0
# Main interpreter function
def interpreter():
code_input = raw_input('Code: ')
steps = len(code_input)
while Lang.step < steps:
if code_input[Lang.step] == '+':
Lang.cell[Lang.pos] += 1
elif code_input[Lang.step] == '-':
Lang.cell[Lang.pos] -= 1
elif code_input[Lang.step] == '>':
if Lang.pos < 30000:
Lang.pos += 1
elif Lang.pos > 30000:
Lang.pos = 0
elif code_input[Lang.step] == '<':
if Lang.pos > 0:
Lang.cell_pos -= 1
elif Lang.pos < 0:
Lang.pos = 30000
elif code_input[Lang.step] == '[':
if Lang.loop == False:
Lang.loop_ret = Lang.step
Lang.loop = True
elif code_input[Lang.step] == ']':
if Lang.cell[Lang.pos] != 0:
Lang.step = Lang.loop_ret
elif Lang.cell[Lang.pos] == 0:
Lang.loop = False
elif code_input[Lang.step] == '.':
print str(chr(Lang.cell[Lang.pos]))
elif code_input[Lang.step] == ',':
Lang.cell[Lang.pos] = int(raw_input())
elif code_input[Lang.step] == ":":
Lang.test_cell[Lang.test_pos] += 1
elif code_input[Lang.step] == ";":
Lang.test_cell[Lang.test_pos] -= 1
elif code_input[Lang.step] == "}":
if Lang.test_pos > 30000:
Lang.test_pos += 1
elif Lang.test_pos < 30000:
Lang.test_pos = 0
elif code_input[Lang.step] == "{":
if Lang.test_pos > 0:
Lang.test_pos -= 1
elif Lang.test_pos < 0:
Lang.test_pos = 30000
elif code_input[Lang.step] == "$":
if Lang.test_cell[Lang.test_pos] == Lang.cell[Lang.pos]:
print True
elif Lang.test_cell[Lang.test_pos] != Lang.cell[Lang.pos]:
print False
Lang.step += 1
# Running the program
if __name__ == "__main__":
interpreter()
If there are any issues, please mention them. All I'm looking for is any general improvements.