Update main.py

This commit is contained in:
Mateusz779 2022-04-03 17:41:33 +02:00
parent 749ee8842e
commit 034f9320e1

20
main.py
View File

@ -1,6 +1,7 @@
pointer = 0 pointer = 0
cell = [] cell = []
code="" code = ""
def main(): def main():
"""Main function""" """Main function"""
@ -9,7 +10,7 @@ def main():
print("\n\n") print("\n\n")
if code != "": if code != "":
for i in range(len(code)): for i in range(len(code)):
interpreter(code[i] ,i) interpreter(code[i], i)
print("\n\n") print("\n\n")
@ -21,15 +22,17 @@ def interpreter(znak, i):
- decrement (decrease by one) the byte at the data pointer. - decrement (decrease by one) the byte at the data pointer.
. output the byte at the data pointer. . output the byte at the data pointer.
, accept one byte of input, storing its value in the byte at the data pointer. , accept one byte of input, storing its value in the byte at the data pointer.
[ if the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ] [ if the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command,
] if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [.""" jump it forward to the command after the matching ]
] if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command,
jump it back to the command after the matching [."""
global code, cell, pointer global code, cell, pointer
match znak: match znak:
case ">": case ">":
pointer += 1 pointer += 1
case "<": case "<":
pointer -= 1 pointer -= 1
if pointer<0: if pointer < 0:
pointer = 0 pointer = 0
case "+": case "+":
try: try:
@ -44,7 +47,7 @@ def interpreter(znak, i):
cell.append(0) cell.append(0)
cell[pointer] -= 1 cell[pointer] -= 1
case ".": case ".":
if cell[pointer]==10 or cell[pointer]==13: if cell[pointer] == 10 or cell[pointer] == 13:
print("\n") print("\n")
else: else:
print(chr(cell[pointer]), end="") print(chr(cell[pointer]), end="")
@ -55,9 +58,10 @@ def interpreter(znak, i):
loop_end = code.find("]", i) loop_end = code.find("]", i)
if cell[pointer] != 0: if cell[pointer] != 0:
loop_end = code.find("]", i) loop_end = code.find("]", i)
for a in range(0,cell[pointer]-1): for a in range(0, cell[pointer]-1):
for j in range(1,loop_end-loop_begin): for j in range(1, loop_end-loop_begin):
interpreter(code[loop_begin + j], loop_begin + j) interpreter(code[loop_begin + j], loop_begin + j)
if __name__ == '__main__': if __name__ == '__main__':
main() main()