-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathsimple_brainfuck.py
114 lines (83 loc) · 3.59 KB
/
simple_brainfuck.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
from collections import defaultdict
def get_loops_block(source):
begin_block = []
blocks = dict()
for i, s in enumerate(source):
if s is "[":
begin_block.append(i)
elif s is "]":
b_i = begin_block.pop() # b_i -- begin index
blocks[i] = b_i
blocks[b_i] = i
return blocks
def execute(source, silent=True):
"""
EN:
The function parses source code Brainfuck and execute it.
RU:
Функция выполняет разбор исходного кода Brainfuck и выполняет его.
:param source: Исходный код
:return:
"""
i = 0 # A pointer to the row index in the code
x = 0 # Cell index
# Dictionary, which is stored in the key index of the cell, and in the value - its value
bf = defaultdict(int)
l = len(source) # Number of code symbols
loops_block = get_loops_block(source)
result_list = []
while i < l:
s = source[i]
if s is ">": # Go to the next cell
x += 1
elif s is "<": # Go to the previous cell
x -= 1
elif s is "+": # Increasing the value of the current cell on 1
bf[x] += 1
elif s is "-": # Decrease the value of the current cell on 1
bf[x] -= 1
elif s is ".": # Printing the value of the current cell
value = chr(bf[x])
result_list.append(value)
if not silent:
print(value, end="")
elif s is ",": # Enter a value in the current cell
bf[x] = int(input("Input = "))
elif s is "[": # Begin loop
# If bf[x] == 0, then gets the index of the closing parenthesis
if not bf[x]:
i = loops_block[i]
elif s is "]": # End loop
if bf[x]: # Если bf[x] != 0, then gets the index of the opening parenthesis
i = loops_block[i]
i += 1
return "".join(result_list)
if __name__ == "__main__":
text = (
"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++"
"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
"+++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
"++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++"
"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
".>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
"++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++.>+++++++++"
"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++."
">++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
"+++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++"
"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++"
"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
"++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++"
"+++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++."
)
result = execute(text)
print("result: " + repr(result))
text = """
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++
.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.
------.--------.>+.>.
"""
result = execute(text)
print("result: " + repr(result))