-
-
Notifications
You must be signed in to change notification settings - Fork 301
/
brainfuck.py
201 lines (151 loc) · 5.03 KB
/
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Made by : @Hackintush || github.com/ToxygenX
# Made For : https://github.com/TeamUltroid/UltroidAddons
"""
✘ Commands Available
• `{i}bf`
Text to Brainfuck String Generator with text or reply.
• `{i}rbf`
Brainfuck Interpreter with string or reply.
"""
from . import *
def evaluate(commands):
interpreter = BrainfuckInterpreter(commands)
while interpreter.available():
interpreter.step()
return interpreter.output.read()
__all__ = "BrainfuckInterpreter"
class IOStream:
def __init__(self, data=None):
self._buffer = data or ""
def __len__(self):
return len(self._buffer)
def read(self, length=None):
if not length:
data = self._buffer
self._buffer = ""
else:
data = self._buffer[:length]
self._buffer = self._buffer[length:]
return data
def write(self, data):
self._buffer += data
class IncrementalByteCellArray:
def __init__(self):
self.byte_cells = [0]
self.data_pointer = 0
def __getitem__(self, item):
cell_amount = len(self.byte_cells)
if item > cell_amount - 1:
self.extend(item - cell_amount + 1)
return self.byte_cells[item]
def __setitem__(self, key: int, value: int):
cell_amount = len(self.byte_cells)
if key > cell_amount - 1:
self.extend(key - cell_amount + 1)
self.byte_cells[key] = value
def __len__(self):
return len(self.byte_cells)
def __repr__(self):
return self.byte_cells.__repr__()
def extend(self, size: int):
self.byte_cells += [0] * size
def increment(self):
new_val = (self.get() + 1) % 256
self.set(new_val)
def decrement(self):
new_val = self.get() - 1
if new_val < 0:
new_val = 255
self.set(new_val)
def set(self, value: int):
self.__setitem__(self.data_pointer, value)
def get(self):
return self.__getitem__(self.data_pointer)
class BrainfuckInterpreter:
def __init__(self, commands: str):
self._commands = commands
self.input = IOStream()
self.output = IOStream()
self.instruction_pointer = 0
self.cells = IncrementalByteCellArray()
self._opening_bracket_indexes = []
def _look_forward(self):
remaining_commands = self._commands[self.instruction_pointer :]
loop_counter = 0
index = self.instruction_pointer
for command in remaining_commands:
if command == "[":
loop_counter += 1
elif command == "]":
loop_counter -= 1
if loop_counter == 0:
return index
index += 1
def _interpret(self):
instruction = self._commands[self.instruction_pointer]
if instruction == ">":
self.cells.data_pointer += 1
elif instruction == "<":
self.cells.data_pointer -= 1
elif instruction == "+":
self.cells.increment()
elif instruction == "-":
self.cells.decrement()
elif instruction == ".":
self.output.write(chr(self.cells.get()))
elif instruction == ",":
self.cells.set(self.input.read(1))
elif instruction == "[":
if self.cells.get() == 0:
loop_end = self._look_forward()
self.instruction_pointer = loop_end
else:
self._opening_bracket_indexes.append(self.instruction_pointer)
elif instruction == "]":
if self.cells.get() != 0:
opening_bracket_index = self._opening_bracket_indexes.pop(-1)
self.instruction_pointer = opening_bracket_index - 1
else:
self._opening_bracket_indexes.pop(-1)
def step(self) -> None:
self._interpret()
self.instruction_pointer += 1
def available(self) -> bool:
return not self.instruction_pointer >= len(self._commands)
def command(self):
return self._commands[self.instruction_pointer]
def bf(text):
items = []
for c in text:
items.append(
"[-]>[-]<"
+ ("+" * (ord(c) // 10))
+ "[>++++++++++<-]>"
+ ("+" * (ord(c) % 10))
+ ".<"
)
return "".join(items)
@ultroid_cmd(
pattern="bf",
)
async def _(event):
input_ = event.text[4:]
if not input_:
if event.reply_to_msg_id:
previous_message = await event.get_reply_message()
input_ = previous_message.message
else:
return await eod(event, "Give me some text lol", time=5)
await event.eor(bf(input_))
@ultroid_cmd(
pattern="rbf",
)
async def _(event):
input_ = event.text[5:]
if not input_:
if event.reply_to_msg_id:
previous_message = await event.get_reply_message()
input_ = previous_message.message
else:
return await eod(event, "Give me some text lol", time=5)
await event.eor(f"{evaluate(input_)}")