Check if control in range(255)

This commit is contained in:
Julien 2011-09-22 13:48:42 +02:00
parent 2760961550
commit e3c1672b19

View File

@ -23,8 +23,7 @@ class InfiniteArray:
class Machine: class Machine:
def __init__(self, code, read, write, user_data=None, max_cycles=None): def __init__(self, code, read, write, max_cycles=None):
self.user_data = user_data
self.read = read self.read = read
self.write = write self.write = write
self.mem = InfiniteArray([ord(char) for char in code]) self.mem = InfiniteArray([ord(char) for char in code])
@ -102,14 +101,16 @@ class Machine:
while True: while True:
if self.mem[self.exc_ptr] == 0: if self.mem[self.exc_ptr] == 0:
return return
control = chr(self.mem[self.exc_ptr]) if self.mem[self.exc_ptr] <= 255:
if debug: control = chr(self.mem[self.exc_ptr])
print control if debug:
if hasattr(self, control): print control
getattr(self, control)() if hasattr(self, control):
getattr(self, control)()
self.exc_ptr += 1 self.exc_ptr += 1
if not self.cycle(): if not self.cycle():
return return
if __name__ == "__main__": if __name__ == "__main__":
Machine(sys.argv[1], sys.stdin.read, sys.stdout.write, None).run() Machine(sys.argv[1], sys.stdin.read, sys.stdout.write,
int(sys.argv[2]) if len(sys.argv) >= 2 else None).run()