Update main.py

This commit is contained in:
Mateusz779 2022-04-03 18:39:30 +02:00
parent 4d6d22faa3
commit bf18df8d48

51
main.py
View File

@ -1,3 +1,4 @@
import re
pointer = 0 pointer = 0
cell = [] cell = []
code = "" code = ""
@ -11,11 +12,22 @@ def main():
global code, cell, pointer global code, cell, pointer
code = input("Enter the code: ") code = input("Enter the code: ")
print("\n\n") print("\n\n")
code=codeClear(code)
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")
def codeClear(input):
"""
Clears the code from unnecessary characters
"""
pattern = "[\][\[\,\.\>\<\-\+]"
tmp=re.findall(pattern, input)
result=""
for i in range(len(tmp)):
result+=tmp[i]
return result
def interpreter(znak, i): def interpreter(znak, i):
""" """
@ -39,10 +51,13 @@ def interpreter(znak, i):
match znak: match znak:
case ">": case ">":
pointer += 1 pointer += 1
if(pointer > len(cell) - 1):
cell.append(0)
case "<": case "<":
pointer -= 1 pointer -= 1
if pointer < 0: if pointer < 0:
pointer = 0 pointer = 0
cell.insert(0, 0)
case "+": case "+":
try: try:
cell[pointer] += 1 cell[pointer] += 1
@ -52,25 +67,35 @@ def interpreter(znak, i):
case "-": case "-":
try: try:
cell[pointer] -= 1 cell[pointer] -= 1
if cell[pointer] < 0:
cell[pointer] = 0
except: except:
cell.append(0) cell.append(0)
cell[pointer] -= 1
case ".": case ".":
if cell[pointer] == 10 or cell[pointer] == 13: try:
print("\n") if cell[pointer] == 10 or cell[pointer] == 13:
else: print("\n")
else:
print(chr(cell[pointer]), end="")
except:
cell.append(0)
print(chr(cell[pointer]), end="") print(chr(cell[pointer]), end="")
case ",": case ",":
cell[pointer] = ord(input("Enter a character: ")) try:
cell[pointer] = ord(input("Enter a character: "))
except:
cell.append(0)
cell[pointer] = ord(input("Enter a character: "))
case "[": case "[":
loop_begin = i try:
loop_end = code.find("]", i) loop_begin = 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)
except:
cell.append(0)
if __name__ == '__main__': if __name__ == '__main__':
main() main()