-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathbrainfuck.py
255 lines (207 loc) · 6.98 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
import sys
import optparse
class Buffer:
def __init__(self, size):
self.array = [0] * size
self.ptr = 0
def move(self, n):
self.ptr += n
def increment(self):
self.array[self.ptr]+=1
def decrement(self):
self.array[self.ptr]-=1
def current(self):
return self.array[self.ptr]
def store(self, val):
self.array[self.ptr] = val
def dump(self, start, end):
return self.array[start:end]
def __str__(self):
return "ptr: %d value: %d" % (self.ptr, self.current())
class Program:
def __init__(self, program):
self.program = program
self.pos = 0
def advance(self, n=1):
self.pos += n
def current(self):
return self.program[self.pos]
def eof(self):
return self.pos == len(self.program)
def __str__(self):
return "pos: %d op: '%s'" % (self.pos, self.current())
class EOFException(Exception):
def __init__(self):
Exception.__init__(self, "EOF")
class StdinStream:
def __init__(self):
pass
def put(self, val):
raise Exception("invalid")
def get(self):
data = sys.stdin.read(1)
if not data:
raise EOFException()
return data[0]
class StdoutStream:
def __init__(self):
pass
def put(self, val):
sys.stdout.write(val)
# we want to see partial results before EOLs:
sys.stdout.flush()
def get(self):
raise Exception("invalid")
def dump(self):
sys.stdout.flush()
class BufStream:
def __init__(self, buffer):
self.buffer = buffer
self.pos = 0
def put(self, val):
self.buffer.append(val)
def get(self):
if self.pos == (self.buffer):
raise EOFException()
ret = self.buffer[self.pos]
self.pos+=1
return ret
def __str__(self):
return "".join(self.buffer)
def dump(self):
sys.stdout.write(str(self))
class Interpreter:
INC_PTR = ">"
DEC_PTR = "<"
INC_BYTE = "+"
DEC_BYTE = "-"
OUTPUT_BYTE = "."
INPUT_BYTE = ","
JUMP_FORWARD = "["
JUMP_BACKWARD = "]"
def __init__(self, program, buffer, instream, outstream, verbose):
self.program = program
self.buffer = buffer
self.instream = instream
self.outstream = outstream
self.verbose = verbose
def __handle_inc_ptr(self):
"""
increment the data pointer (to point to the next cell to the right).
"""
self.buffer.move(+1)
def __handle_dec_ptr(self):
"""
decrement the data pointer (to point to the next cell to the left).
"""
self.buffer.move(-1)
def __handle_inc_byte(self):
"""increment (increase by one) the byte at the data pointer."""
self.buffer.increment()
def __handle_dec_byte(self):
"""decrement (decrease by one) the byte at the data pointer."""
self.buffer.decrement()
def __handle_output_byte(self):
"""
output the byte at the data pointer as an ASCII encoded character.
"""
self.outstream.put(chr(self.buffer.current()))
def __handle_input_byte(self):
"""
accept one byte of input, storing its value in the byte at the data
pointer.
"""
self.buffer.store(ord(self.instream.get()))
def __handle_jump_forward(self):
"""
if the byte at the data pointer is zero, then instead of moving the
instruction pointer forward to the next command, jump it forward to
the command after the matching ] command.
"""
if self.buffer.current() == 0:
count = 1
while count:
self.__dump_state("__handle_jump_forward: (count: %d)" % count)
self.program.advance()
if self.program.current() == self.JUMP_FORWARD:
count += 1
elif self.program.current() == self.JUMP_BACKWARD:
count -= 1
def __handle_jump_backward(self):
"""
if the byte at the data pointer is nonzero, then instead of moving
the instruction pointer forward to the next command, jump it back
to the command after the matching [ command.
"""
# (Alternatively, the ] command may instead be translated as an
# unconditional jump to the corresponding [ command, or vice versa;
# programs will behave the same but will run more slowly, due to
# unnecessary double searching.)
if self.buffer.current() != 0:
count = 1
while count:
self.__dump_state("__handle_jump_backward: (count: %d)" % count)
self.program.advance(-1)
if self.program.current() == self.JUMP_BACKWARD:
count += 1
elif self.program.current() == self.JUMP_FORWARD:
count -= 1
def __dump_state(self, msg):
if self.verbose:
#print msg, self.program, self.buffer
print "%s" % self.buffer.dump(0, 10)
def execute(self):
op_handler = {
self.INC_PTR : self.__handle_inc_ptr,
self.DEC_PTR : self.__handle_dec_ptr,
self.INC_BYTE : self.__handle_inc_byte,
self.DEC_BYTE : self.__handle_dec_byte,
self.OUTPUT_BYTE : self.__handle_output_byte,
self.INPUT_BYTE : self.__handle_input_byte,
self.JUMP_FORWARD : self.__handle_jump_forward,
self.JUMP_BACKWARD : self.__handle_jump_backward
}
while not self.program.eof():
self.__dump_state("execute:")
handler = op_handler.get(self.program.current())
if handler:
handler()
self.program.advance()
if __name__ == "__main__":
parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", dest="verbose",
action="store_true", default=False,
help="Verbosity ON")
options, args = parser.parse_args()
if args:
with open(args[0], "r") as input_file:
contents = input_file.read()
else:
contents = sys.stdin.read()
# Define program
program = Program(contents)
#program = Program("[[[]]]")
# Memory buffer
buffer = Buffer(30000)
# Input stream
#instream = BufStream("asdf")
instream = StdinStream()
# Output stream
if options.verbose:
outstream = BufStream([])
else:
outstream = StdoutStream()
try:
interpreter = Interpreter(program,
buffer,
instream,
outstream,
options.verbose)
interpreter.execute()
except EOFException:
pass
except KeyboardInterrupt:
pass
except Exception, e:
print >> sys.stderr, "ERROR:", e
outstream.dump()