Update main.py
This commit is contained in:
parent
4d6d22faa3
commit
bf18df8d48
51
main.py
51
main.py
@ -1,3 +1,4 @@
|
||||
import re
|
||||
pointer = 0
|
||||
cell = []
|
||||
code = ""
|
||||
@ -11,11 +12,22 @@ def main():
|
||||
global code, cell, pointer
|
||||
code = input("Enter the code: ")
|
||||
print("\n\n")
|
||||
code=codeClear(code)
|
||||
if code != "":
|
||||
for i in range(len(code)):
|
||||
interpreter(code[i], i)
|
||||
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):
|
||||
"""
|
||||
@ -39,10 +51,13 @@ def interpreter(znak, i):
|
||||
match znak:
|
||||
case ">":
|
||||
pointer += 1
|
||||
if(pointer > len(cell) - 1):
|
||||
cell.append(0)
|
||||
case "<":
|
||||
pointer -= 1
|
||||
if pointer < 0:
|
||||
pointer = 0
|
||||
cell.insert(0, 0)
|
||||
case "+":
|
||||
try:
|
||||
cell[pointer] += 1
|
||||
@ -52,25 +67,35 @@ def interpreter(znak, i):
|
||||
case "-":
|
||||
try:
|
||||
cell[pointer] -= 1
|
||||
if cell[pointer] < 0:
|
||||
cell[pointer] = 0
|
||||
except:
|
||||
cell.append(0)
|
||||
cell[pointer] -= 1
|
||||
case ".":
|
||||
if cell[pointer] == 10 or cell[pointer] == 13:
|
||||
print("\n")
|
||||
else:
|
||||
try:
|
||||
if cell[pointer] == 10 or cell[pointer] == 13:
|
||||
print("\n")
|
||||
else:
|
||||
print(chr(cell[pointer]), end="")
|
||||
except:
|
||||
cell.append(0)
|
||||
print(chr(cell[pointer]), end="")
|
||||
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 "[":
|
||||
loop_begin = i
|
||||
loop_end = code.find("]", i)
|
||||
if cell[pointer] != 0:
|
||||
loop_end = code.find("]", i)
|
||||
for a in range(0, cell[pointer]-1):
|
||||
for j in range(1, loop_end-loop_begin):
|
||||
interpreter(code[loop_begin + j], loop_begin + j)
|
||||
|
||||
try:
|
||||
loop_begin = i
|
||||
if cell[pointer] != 0:
|
||||
loop_end = code.find("]", i)
|
||||
for a in range(0, cell[pointer]-1):
|
||||
for j in range(1, loop_end-loop_begin):
|
||||
interpreter(code[loop_begin + j], loop_begin + j)
|
||||
except:
|
||||
cell.append(0)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
Loading…
Reference in New Issue
Block a user