-
Notifications
You must be signed in to change notification settings - Fork 0
/
brainfuck.py
executable file
·274 lines (216 loc) · 8.62 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
__author__ = "Moritz Hilscher"
__license__ = "GPL"
__version__ = "0.9"
import sys
import termios
import tty
# see http://code.activestate.com/recipes/134892/
class Getch:
@staticmethod
def getch():
try:
return Getch._windows_impl()
except ImportError:
try:
return Getch._unix_impl()
except termios.error:
return sys.stdin.read(1)
@staticmethod
def _unix_impl():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
@staticmethod
def _windows_impl():
import msvcrt
return msvcrt.getch()
class BrainfuckStack:
"""Manages the Brainfuck cells for the interpreter"""
def __init__(self):
"""Constructor"""
self._dict = {}
def get_list(self):
"""Returns a list of all cells"""
return self._dict.values()
def __getitem__(self, index):
"""Returns a cell"""
if not self._dict.has_key(index):
self._dict[index] = 0
return self._dict[index]
def __setitem__(self, index, value):
"""Sets a cell"""
self._dict[index] = value
class BrainfuckInterpreter:
"""The Brainfuck interpreter"""
def __init__(self, commands):
"""Constructor"""
self._stack = BrainfuckStack()
self._stack_position = 0
self._commands = list(commands)
self._commands_position = 0
self._stream_input = None
self._stream_output = sys.stdout
self._operators = {">" : self._increment_pointer,
"<" : self._decrement_pointer,
"+" : self._increment,
"-" : self._decrement,
"." : self._output,
"," : self._input,
"[" : self._loop_begin,
"]" : self._loop_end}
def _increment_pointer(self):
"""Increments the pointer (Command >)"""
self._stack_position += 1
def _decrement_pointer(self):
"""Decrements the pointer (Command: <)"""
self._stack_position -= 1
def _increment(self):
"""Increments the current cell (Command: +)"""
self._stack[self._stack_position] += 1
def _decrement(self):
"""Decrements the current cell (Command: -)"""
self._stack[self._stack_position] -= 1
def _output(self):
"""Outputs the current cell (Command: .)"""
self._stream_output.write(chr(self._stack[self._stack_position]))
def _input(self):
"""Reads one char from the input (or stream) into the current cell (Command: ,)"""
if not self._stream_input:
byte = Getch.getch()
else:
byte = self._stream_input.read(1)
value = 0
if len(byte) == 1:
value = ord(byte)
self._stack[self._stack_position] = value
def _loop_begin(self):
"""A loop begin (Command: [). Jumps to the loop end if cell == 0"""
if self._stack[self._stack_position] != 0:
return
found = 0
for i in range(self._commands_position + 1, len(self._commands)):
command = self._commands[i]
if command == "]" and found == 0:
self._commands_position = i + 1
return
elif command == "]":
found += 1
elif command == "[":
found -= 1
def _loop_end(self):
"""A loop begin (Command: ]). Jumps to the loop begin if cell != 0"""
if self._stack[self._stack_position] == 0:
return
found = 0
for i in range(self._commands_position - 1, 0, -1):
command = self._commands[i]
if command == "[" and found == 0:
self._commands_position = i
return
elif command == "[":
found += 1
elif command == "]":
found -= 1
def is_end(self):
"""Checks if the run is finished"""
return self._commands_position >= len(self._commands)
def single_step(self):
"""Executes a single step"""
if self.is_end():
return
command = self._commands[self._commands_position]
if not self._operators.has_key(command):
self._commands_position += 1
return
self._operators[command]()
self._commands_position += 1
def execute(self):
"""Executes the interpreter"""
while not self.is_end():
self.single_step()
def set_command(self, commands):
"""Sets the command. This resets the stack"""
self._commands = list(commands)
self._commands_position = 0
self._stack = BrainfuckStack()
self._stack_position = 0
def set_input_stream(self, stream):
"""Sets the input stream. You can also use a StringIO object"""
self._stream_input = stream
def set_output_stream(self, stream):
"""Sets the output stream. You can also use a StringIO object"""
self._stream_output = stream
def get_output_stream(self):
"""Returns the output stream. If you set as output StringIO object,
you can use stream.getvalue() to get the output"""
return self._stream_output
def to_c(self):
"""Converts the brainfuck commands to c code"""
c_commands = {"+" : "++cells[pos];",
"-" : "--cells[pos];",
">" : "pos++;",
"<" : "pos--;",
"." : "putchar(cells[pos]);",
"," : "cells[pos] = getchar();",
"[" : "while(cells[pos] != 0) {",
"]" : "}"}
source = "#include <stdio.h>\nint main() {\nint pos = 0;\nchar cells[30000];\n"
for command in self._commands:
if not self._operators.has_key(command) or not c_commands.has_key(command):
continue
source += c_commands[command]
source += "\n"
source += "return 0; }"
return source
if __name__ == "__main__":
import optparse
parser = optparse.OptionParser()
parser.set_usage("Usage: %prog action [options]")
parser.add_option("-x", "--execute", action="store_true", help="executes the file", default=False, dest="execute")
parser.add_option("-c", "--compile", action="store_true", help="converts the brainfuck code to c code", default=False, dest="compile")
parser.add_option("-i", "--input", help="the Brainfuck input file")
parser.add_option("-o", "--output", help="the c output file")
options, args = parser.parse_args()
if options.execute and options.compile:
parser.error("You can only use one action (-x or -c)!")
sys.exit(-1)
elif not options.execute and not options.compile:
parser.error("Please insert a action (-x or -c)!")
sys.exit(-1)
if not options.input:
input = sys.stdin.read()
else:
f = open(options.input, "r")
input = f.read()
f.close()
if not input:
input = ""
if not options.output:
output = sys.stdout
else:
output = open(options.output, "w")
interpreter = BrainfuckInterpreter(input)
if options.execute:
interpreter.execute()
elif options.compile:
output.write(interpreter.to_c())
if options.output:
output.close()