Beispiel #1
0
def test_horizontal_if():
    lines = [['_']]
    fld = Field(1, 1, lines)
    stk = Stack()
    crt = Caret(stk, fld)

    stk.push(174)
    crt.read_instruction(fld)
    crt.execute_instruction()
    assert crt.direction == Left
    stk.push(0)
    crt.read_instruction(fld)
    crt.execute_instruction()
    assert crt.direction == Right
Beispiel #2
0
def test_vertical_if():
    lines = [['|']]
    fld = Field(1, 1, lines)
    stk = Stack()
    crt = Caret(stk, fld)

    stk.push(174)
    crt.read_instruction(fld)
    crt.execute_instruction()
    assert crt.direction == Up
    stk.push(0)
    crt.read_instruction(fld)
    crt.execute_instruction()
    assert crt.direction == Down
Beispiel #3
0
def test_trampoline():
    lines = [['>', '#', '1', '.']]
    fld = Field(4, 1, lines)
    stk = Stack()
    crt = Caret(stk, fld)

    crt.read_instruction(fld)
    crt.execute_instruction()
    assert crt.direction == Right
    crt.move(fld)
    crt.read_instruction(fld)
    assert crt.current_instruction == '#'
    crt.execute_instruction()
    crt.move(fld)
    crt.read_instruction(fld)
    assert crt.current_instruction == '.'
Beispiel #4
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)
Beispiel #5
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()