Brainfuck+3

From Esolang
Jump to navigation Jump to search

Brainfuck+3 is like brainfuck but it has a bidimensional space and it has 3 more commands.

Commands

Command Description
> Move the pointer to the right
< Move the pointer to the left
+ Increment the memory cell under the pointer
- Decrement the memory cell under the pointer
. Output the character signified by the cell at the pointer
, Input a character and store it in the cell at the pointer
[ Jump past the matching ] if the cell under the pointer is 0
] Jump back to the matching [ if the cell under the pointer is nonzero
^ Move the pointer up
v Move the pointer down
x Exit program.

Examples

This code will print "Hello World". It is based on the code from the Brainfuck page, but left is replaced with up and right is replaced with down.

++++++++[^++++[^++^+++^+++^+vvvv-]^+^+^-^^+[v]v-]^^.^---.+++++++..+++.^^.v-.v.+++.------.--------.^^+.

The normal Brainfuck code will also work.

Implementations

This is one implementation, done in Python.

def run_bfp3(code: str) -> None:
    pointer = 0  # for instructions
    row = 0     # for up and down tape
    column = 0  # for left and right tape
    tape = [[0]]
    input_string = chr(10)  # allows multiple inputs easily

    # checks balance
    balance = 0
    for char in code:
        if char == '[':
            balance += 1
        elif char == ']':
            balance -= 1
        assert balance >= 0, 'unmatched ]'
    assert balance == 0, 'unmatched ['

    while pointer < len(code):
        if code[pointer] == '>':
            column += 1
            if column == len(tape[row]):
                for row_number in range(len(tape)):
                    tape[row_number].append(0)
        elif code[pointer] == '<':
            column -= 1
            if column < 0:
                for row_number in range(len(tape)):
                    tape[row_number] = [0] + tape[row_number]
                column += 1
        elif code[pointer] == 'v':
            row += 1
            if row == len(tape):
                tape.append([0]*(column+1))
        elif code[pointer] == '^':
            row -= 1
            if row < 0:
                tape = [[0]*(column+1)] + tape
                row += 1
        elif code[pointer] == '+':
            tape[row][column] = (tape[row][column] + 1) % 256
        elif code[pointer] == '-':
            tape[row][column] = (tape[row][column] - 1) % 256
        elif code[pointer] == '.':
            print(chr(tape[row][column]),end='')
        elif code[pointer] == ',':
            if input_string == chr(10):
                input_string = input(">>") + chr(10)
            if len(input_string) > 0 and ord(input_string[0]) != chr(10):
                tape[row][column] = ord(input_string[0])
            input_string = input_string[1:]
        elif code[pointer] == '[':
            if tape[row][column] == 0:
                nest_counter = 0
                while code[pointer] != ']' or nest_counter >= 0:
                    pointer += 1
                    if code[pointer] == '[':
                        nest_counter += 1
                    elif code[pointer] == ']':
                        nest_counter -= 1
        elif code[pointer] == ']':
            if tape[row][column] != 0:
                nest_counter = 0
                while code[pointer] != '[' or nest_counter >= 0:
                    pointer -= 1
                    if code[pointer] == ']':
                        nest_counter += 1
                    elif code[pointer] == '[':
                        nest_counter -= 1
        elif code[pointer] == 'x':
            break
        pointer += 1