-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.py
86 lines (74 loc) · 2.06 KB
/
Program.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
code: str = str()
cells: list[int] = []
currentCell = currentInstruction = instructionsRan = 0
def interpret(c: str):
global currentCell
match c:
case '>':
currentCell += 1
expand(currentCell)
case '<':
currentCell -= 1
expand(currentCell)
case '+':
cells[currentCell] += 1
case '-':
cells[currentCell] -= 1
case '.':
print(chr(cells[currentCell]), end = '')
case ',':
cells[currentCell] = input()[0]
case '[':
if (cells[currentCell] == 0):
jumpForward()
case ']':
if (cells[currentCell] != 0):
jumpBack()
case _:
global instructionsRan
instructionsRan -= 1
def expand(cell: int):
for i in range(len(cells), cell + 1):
cells.append(0)
def jumpForward():
global currentInstruction
currentInstruction += 1
open = 1
while open > 0:
if code[currentInstruction] == '[':
open += 1
elif code[currentInstruction] == ']':
open -= 1
currentInstruction += 1
currentInstruction -= 1
def jumpBack():
global currentInstruction
currentInstruction -= 1
open = 1
while open > 0:
if code[currentInstruction] == '[':
open -= 1
elif code[currentInstruction] == ']':
open += 1
currentInstruction -= 1
currentInstruction += 1
while True:
cells.clear()
cells.append(0)
currentCell = currentInstruction = instructionsRan = 0
code = ""
print("Enter BrainFudge code: (press enter twice to run)")
while True: # do while loop
codeLine = input()
code += codeLine
if codeLine.strip() == "":
break
while currentInstruction < len(code):
interpret(code[currentInstruction])
currentInstruction += 1
instructionsRan += 1
print(f"{instructionsRan} instructions ran.")
print("Cell Data:")
for x in cells:
print(x)
print()