git @ Cat's Eye Technologies Nhohnhehr / edade5e
Establish some classes to implement the IO system in an OO manner. Chris Pressey 7 months ago
1 changed file(s) with 53 addition(s) and 37 deletion(s). Raw diff Collapse all Expand all
2525
2626
2727 # room
28 class Room:
28 class Room(object):
2929 data = None
3030 size = 0
3131
164164 self.transform(transform)
165165
166166
167 class Environment:
167 class Environment(object):
168168 rooms = {}
169169 ip = ()
170170 direction = ()
186186 roomY = y % self.roomsize
187187 return room[roomX, roomY]
188188
189 def __init__(self, room, funcs):
190 (infunc, outfunc) = funcs
189 def __init__(self, room, io_system):
191190 self.rooms = {
192191 (0, 0): room
193192 }
194193 self.roomsize = room.size
195194 self.dir = Environment.RIGHT
196195 self.edgemode = Environment.WRAP
197 self.infunc, self.outfunc = infunc, outfunc
196 self.io_system = io_system
198197 self.halt = False
199198 # find initial instruction pointer
200199
207206
208207 if self.ip == (-1, -1):
209208 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)
210215
211216 def roomCoords(self, v):
212217 (x, y) = v
284289 self.step()
285290
286291
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):
311302 i = None
312303 while i not in ('0', '1'):
313304 i = sys.stdin.read(1)
315306 raise IOError() # eof
316307 return int(i)
317308
318 def bits_out(bit):
309 def units_out(self, bit):
319310 sys.stdout.write(('0', '1')[bit])
320311 sys.stdout.flush()
321312
322 def bytes_in(bits=[[]]):
313
314 class BytesIO(NhohnhehrIO):
315 def units_in(self, bits=[[]]):
323316 # get data if necessary
324317 if bits[0] == []:
325318 i = sys.stdin.read(1)
333326 bits[0] = bits[0][1:]
334327 return bit
335328
336 def bytes_out(bit, bits=[[]]):
329 def units_out(self, bit, bits=[[]]):
337330 bits[0].append(bit)
338331
339332 # if we have 8 bits, output
342335 sys.stdout.flush()
343336 bits[0] = []
344337
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]
349365 try:
350366 with open(fname, 'r') as f:
351 Environment(Room(file=f), modes[mode]).run()
367 Environment(Room(file=f), io_system).run()
352368 if mode == 'bits':
353369 print # newline
354370