def run(self, reader: t.TextIO, writer: t.TextIO) -> None: input_stream = BytesBidirectionalStreamOverTextIo(reader) output_stream = BytesBidirectionalStreamOverTextIo(writer) while True: code = self.__read(reader, writer) if self.__is_quit(code): return result = self.__evaluate(input_stream, output_stream, code) self.__print(writer, result)
def run(file: t.Optional[t.Text], program: t.Optional[t.Text], input_: t.Optional[t.Text], output: t.Optional[t.Text]) -> None: """Run brainfuck code.""" if (file is None) == (program is None): raise UsageError("Define either file argument or code option! Run `bfpy run --help` for more information.") machine = _create_machine() with _open_if_presented(input_, "r", sys.stdin) as fin, _open_if_presented(output, "w", sys.stdout) as fout: input_stream = BytesBidirectionalStreamOverTextIo(fin) output_stream = BytesBidirectionalStreamOverTextIo(fout) # NOTE: Obviously in this section of function either file is defined or code which is passed via option, # and it is impossible that this expression can be evaluated as `None`. But MyPy raises an error # `Argument 3 to "execute" of "Machine" has incompatible type "Optional[str]"; expected "str"`. code = t.cast(t.Text, _read_code(file) if file is not None else program) machine.execute(input_stream, output_stream, code)
def byte_output_stream(output_bytes: io.BytesIO) -> ByteOutputStream: return BytesBidirectionalStreamOverTextIo(io.TextIOWrapper(output_bytes))
def input_stream(input_bytes: io.BytesIO) -> ByteInputStream: return BytesBidirectionalStreamOverTextIo(io.TextIOWrapper(input_bytes))