def test_caret_move_behind_top_border(): lines = [['>', 'v'], ['<', '^']] fld = Field(2, 2, lines) stk = Stack() crt = Caret(stk, fld) crt.set_direction(Up) crt.move(fld) assert crt.direction == Up assert crt.pos == Vec(0, 1)
def test_caret_move_first(): lines = [['>', 'v'], ['<', '^']] fld = Field(2, 2, lines) stk = Stack() crt = Caret(stk, fld) crt.move(fld) crt.read_instruction(fld) assert crt.direction == Vec(0, 0) assert crt.current_instruction == '>'
def __init__(self, stack: Stack, field: Field, max_new_line_count=10, debug=False): self.direction = Vec(0, 0) self.pos = Vec(0, 0) self.current_instruction = ' ' self.executor = Executor() self.stack = stack self.field = field self.string_mode = False self.new_line_count = 0 self.max_new_line_count = max_new_line_count self.diff = '' self.output = '' self.debug = debug self.debug_messages = [] logger.debug("Caret init")
def test_caret_init(): lines = [['>', 'v'], ['<', '^']] fld = Field(2, 2, lines) stk = Stack() try: crt = Caret(stk, fld) except Exception as e: raise e else: assert crt is not None assert crt.pos is not None assert crt.string_mode is False assert crt.field is fld assert crt.direction == Vec(0, 0)
def test_change_direction(): lines = [['>', 'v'], ['^', '<']] fld = Field(2, 2, lines) stk = Stack() crt = Caret(stk, fld) crt.move(fld) crt.read_instruction(fld) crt.execute_instruction() assert crt.direction == Right crt.move(fld) crt.read_instruction(fld) crt.execute_instruction() assert crt.direction == Down crt.move(fld) crt.read_instruction(fld) crt.execute_instruction() assert crt.direction == Left crt.move(fld) crt.read_instruction(fld) crt.execute_instruction() assert crt.direction == Up crt.move(fld) assert crt.pos == Vec(0, 0)
def main(execute=True): term = Terminal() if from_file: field = Field.load_file(filename) elif from_pipe: lines = sys.stdin.readlines() text = lines[0] for line in lines: text += line program = text field = Field.from_text(program) else: return stack = Stack() caret = Caret(stack, field, max(3, to_int(term.height) - field.height), debug) if debug: logger.set_output(caret) caret.executor.execute = execute logger.debug("Objects created") # if super_debug: # input("Continue?") if debug: try: char = readchar() if char == 'c': print('\nForced exit') return except Exception: print("You shouldn't use pipe without -p option") return # field height shouldn't change print_field(caret, field, term) caret.read_instruction(field) caret.execute_instruction() if caret.direction == Vec(0, 0): caret.direction = Right logger.debug("Starting loop") while caret.executor.execute: caret.move(field) caret.read_instruction(field) if debug or super_debug: print_field(caret, field, term) else: print(caret.diff, end='') caret.execute_instruction() if debug: char = readchar() if char == 'c': print('\nForced exit') return logger.debug("Move performed") print()