def main(): um = UniversalMachine(Path('sandmark.umz').read_bytes()) um.output_buffer_limit = 1 t = time() while um.state != UniversalMachine.State.HALT: assert um.state == UniversalMachine.State.IDLE output = um.run() print(output.decode('ascii'), end='', flush=True) print(f'\ntime elapsed:{time() - t:.4}')
def get_um_copy(): global um with um_lock: if um is None: logging.info('loading um...') um = UniversalMachine(Path('umix.umz').read_bytes()) b = b'guest\nmail\ntelnet 127.0.0.1 80\n' run(um, umin = ByteReader(io.BytesIO(b)), umout=BaseWriter()) logging.info('done') return UniversalMachine(um)
def run_user(): um = UniversalMachine(Path('umix.umz').read_bytes()) with Path('logs/default.out').open('wb') as f, \ Path('logs/input.in').open('wb') as g: logwriter = ByteWriter(f) run(um, umin=ForkReader(TextReader(sys.stdin), [logwriter, ByteWriter(g)]), umout=ForkWriter(TextWriter(sys.stdout), logwriter))
def run_compiled(umcode: bytes, binary): um = UniversalMachine(umcode) output = io.BytesIO() if binary else io.StringIO() Writer = ByteWriter if binary else TextWriter run(um, umin=BaseReader(), umout=Writer(output)) return output.getvalue()
def main(): timestart = time() # Load machine and run pre-defined commands from file UM = UniversalMachine(Path('umix.umz').read_bytes()) infile = Path('logs/howie.in').open('rb') outfile = Path('logs/howie.out').open('wb') writers = ForkWriter(ByteWriter(outfile), TextWriter(sys.stdin)) run(um, umin=ForkReader(ByteReader(infile), [writers]), umout=writers) # bot: traverse rooms explorer = ExplorerBot() try: explorer.run(UM, writers) except Exception as e: raise e finally: out.write(explorer.log) citymap = traverser.map() # first we run file... UM.run(input_string) out.write(UM.get_output()) # Input file and bot are over, stop duplicating commands to stdout. Write input to file directly. UM.setmode(defaultmode) input_string = "" # ...then keyboard. while UM.run(input_string): out.write(UM.get_output()) input_string = input() +'\n' out.write(input_string) assert UM.halt # write last output out.write(UM.get_output()) if UM.error_message: print("Error occured:\n ", UM.error_message) print("\ntime elapsed: %.2f" % (time() - timestart)) out.close()
def run(um: UniversalMachine, *, umin: BaseReader, umout: BaseWriter): while True: # if um.state == UniversalMachine.State.ERROR: # # TODO # print(um.error_message) # return if um.state == UniversalMachine.State.IDLE: out = um.run() for byte in out: umout.writebyte(byte) continue if um.state == UniversalMachine.State.HALT: return assert um.state == UniversalMachine.State.WAITING c = umin.readbyte() if c is None: return um.write_input(c)
def test_io_smoke(): um = UniversalMachine(Path('umix.umz').read_bytes()) run(um, umin=BaseReader(), umout=BaseWriter()) assert um.state == UniversalMachine.State.WAITING output = io.StringIO('') run(um, umin=TextReader(io.StringIO('guest\nexit\n')), umout=ForkWriter(TextWriter(output), TextWriter(sys.stdout))) assert um.state == UniversalMachine.State.HALT assert 'logged in as guest' in output.getvalue()
def run_file(filename): path = Path('logs') um = UniversalMachine(Path('umix.umz').read_bytes()) with (path / 'input.in').open('w') as keyboard, \ (path / (filename + '.in')).open('r') as infile, \ (path / (filename + '.out')).open('wb') as outfile: logwriter = ByteWriter(outfile) conswriter = TextWriter(sys.stdout) doublewriter = ForkWriter(logwriter, conswriter) # run file run(um, umin=ForkReader(TextReader(infile), [doublewriter]), umout=doublewriter) run(um, umin=ForkReader(TextReader(sys.stdin), [logwriter, TextWriter(keyboard)]), umout=doublewriter)
def main(): um = UniversalMachine(Path('codex.umz').read_bytes()) password = rb'(\b.bb)(\v.vv)06FHPVboundvarHRAkp' output = um.run() for c in password: assert um.state == UniversalMachine.State.WAITING, um.state um.write_input(c) output += um.run() assert um.state == UniversalMachine.State.HALT end_of_preamble = b'follows colon:' m = output.find(end_of_preamble) + len(end_of_preamble) assert m != -1 print(output[:m].decode('ascii')) Path('umix.umz').write_bytes(output[m:])