Establish some classes to implement the IO system in an OO manner.
Chris Pressey
1 year, 3 days ago
25 | 25 | |
26 | 26 | |
27 | 27 | # room |
28 | class Room: | |
28 | class Room(object): | |
29 | 29 | data = None |
30 | 30 | size = 0 |
31 | 31 | |
164 | 164 | self.transform(transform) |
165 | 165 | |
166 | 166 | |
167 | class Environment: | |
167 | class Environment(object): | |
168 | 168 | rooms = {} |
169 | 169 | ip = () |
170 | 170 | direction = () |
186 | 186 | roomY = y % self.roomsize |
187 | 187 | return room[roomX, roomY] |
188 | 188 | |
189 | def __init__(self, room, funcs): | |
190 | (infunc, outfunc) = funcs | |
189 | def __init__(self, room, io_system): | |
191 | 190 | self.rooms = { |
192 | 191 | (0, 0): room |
193 | 192 | } |
194 | 193 | self.roomsize = room.size |
195 | 194 | self.dir = Environment.RIGHT |
196 | 195 | self.edgemode = Environment.WRAP |
197 | self.infunc, self.outfunc = infunc, outfunc | |
196 | self.io_system = io_system | |
198 | 197 | self.halt = False |
199 | 198 | # find initial instruction pointer |
200 | 199 | |
207 | 206 | |
208 | 207 | if self.ip == (-1, -1): |
209 | 208 | raise ValueError("no $ in room") |
209 | ||
210 | def infunc(self): | |
211 | return self.io_system.units_in() | |
212 | ||
213 | def outfunc(self, unit): | |
214 | return self.io_system.units_out(unit) | |
210 | 215 | |
211 | 216 | def roomCoords(self, v): |
212 | 217 | (x, y) = v |
284 | 289 | self.step() |
285 | 290 | |
286 | 291 | |
287 | def main(argv): | |
288 | if len(argv) not in (2, 3) or (len(argv) == 3 and not argv[1] in ('bits', 'bytes')): | |
289 | print("""\ | |
290 | Usage: [python] %s [bits|bytes] filename | |
291 | bits/bytes: specify i/o mode | |
292 | ||
293 | In bits mode, i/o uses the characters '0' and '1' | |
294 | (and when reading input, everything that's not '0' | |
295 | or 1 is ignored). | |
296 | In bytes mode, i/o is done 8 bits at a time as ASCII. | |
297 | ||
298 | If no mode is given, bytes mode is used. | |
299 | """ % argv[0]) | |
300 | sys.exit() | |
301 | ||
302 | if len(argv) == 2: | |
303 | mode = 'bytes' | |
304 | fname = argv[1] | |
305 | else: | |
306 | mode = argv[1] | |
307 | fname = argv[2] | |
308 | ||
309 | # i/o functions | |
310 | def bits_in(): | |
292 | class NhohnhehrIO(object): | |
293 | def units_in(self): | |
294 | raise NotImplementedError('implement units_in please') | |
295 | ||
296 | def units_out(self, unit): | |
297 | raise NotImplementedError('implement units_out please') | |
298 | ||
299 | ||
300 | class BitsIO(NhohnhehrIO): | |
301 | def units_in(self): | |
311 | 302 | i = None |
312 | 303 | while i not in ('0', '1'): |
313 | 304 | i = sys.stdin.read(1) |
315 | 306 | raise IOError() # eof |
316 | 307 | return int(i) |
317 | 308 | |
318 | def bits_out(bit): | |
309 | def units_out(self, bit): | |
319 | 310 | sys.stdout.write(('0', '1')[bit]) |
320 | 311 | sys.stdout.flush() |
321 | 312 | |
322 | def bytes_in(bits=[[]]): | |
313 | ||
314 | class BytesIO(NhohnhehrIO): | |
315 | def units_in(self, bits=[[]]): | |
323 | 316 | # get data if necessary |
324 | 317 | if bits[0] == []: |
325 | 318 | i = sys.stdin.read(1) |
333 | 326 | bits[0] = bits[0][1:] |
334 | 327 | return bit |
335 | 328 | |
336 | def bytes_out(bit, bits=[[]]): | |
329 | def units_out(self, bit, bits=[[]]): | |
337 | 330 | bits[0].append(bit) |
338 | 331 | |
339 | 332 | # if we have 8 bits, output |
342 | 335 | sys.stdout.flush() |
343 | 336 | bits[0] = [] |
344 | 337 | |
345 | modes = { | |
346 | 'bits': (bits_in, bits_out), | |
347 | 'bytes': (bytes_in, bytes_out) | |
348 | } | |
338 | ||
339 | def main(argv): | |
340 | if len(argv) not in (2, 3) or (len(argv) == 3 and not argv[1] in ('bits', 'bytes')): | |
341 | print("""\ | |
342 | Usage: [python] %s [bits|bytes] filename | |
343 | bits/bytes: specify i/o mode | |
344 | ||
345 | In bits mode, i/o uses the characters '0' and '1' | |
346 | (and when reading input, everything that's not '0' | |
347 | or 1 is ignored). | |
348 | In bytes mode, i/o is done 8 bits at a time as ASCII. | |
349 | ||
350 | If no mode is given, bytes mode is used. | |
351 | """ % argv[0]) | |
352 | sys.exit() | |
353 | ||
354 | if len(argv) == 2: | |
355 | mode = 'bytes' | |
356 | fname = argv[1] | |
357 | else: | |
358 | mode = argv[1] | |
359 | fname = argv[2] | |
360 | ||
361 | io_system = { | |
362 | 'bits': BitsIO(), | |
363 | 'bytes': BytesIO(), | |
364 | }[mode] | |
349 | 365 | try: |
350 | 366 | with open(fname, 'r') as f: |
351 | Environment(Room(file=f), modes[mode]).run() | |
367 | Environment(Room(file=f), io_system).run() | |
352 | 368 | if mode == 'bits': |
353 | 369 | print # newline |
354 | 370 |