2Deadfish

From Esolang
Jump to navigation Jump to search
This language is a derivative of Deadfish.

2Deadfish is a 2D variant of Deadfish in which whenever a command is read, the program pointer direction changes based on the command and the direction it was originally going (except for o, technically).

Commands

Like Deadfish, 2Deadfish has a counter which starts at 0 and can be incremented, decremented, squared, and output. The counter also cannot be 256 or -1. In 2Deadfish, the program pointer starts on the top line going left-to-right, and almost all programs are nonlinear as different commands cause the pointer to start traveling in different directions.

Command Description
i Increment the counter and rotate the traversal direction 90 degrees clockwise.
s Square the counter and reverse the traversal direction.
d Decrement the counter and rotate the traversal direction 90 degrees counterclockwise.
o Output the counter's value and rotate the counter's direction 360 degrees / 0 degrees.

Any other characters will be ignored.

The playfield does not wrap around itself, and if the pointer exits the playfield, the code ends.

Examples

This is the equivalent of the iissso Deadfish test.

 i
si
 ds
si
 do

Deadfish equivalent: iisidsdisido

This is the equivalent of the diissisdo Deadfish test.

i  s
 iidi
ddssio
    dd

Deadfish equivalent: iddiisidsdiisiddo

In the spirit of Deadfish/Constants, I have made 2Deadfish/Constants which you can update.

Computational Class

Because all it can do is change the value of an accumulator, this is unusable for computing, and it is even less usable than Deadfish.

In terms of instructions read, any 2Deadfish code has a corresponding Deadfish equivalent, but not vice versa. For example, Deadfish can execute the program diissisdo, but in 2Deadfish, the only command executed would be the first d, because the pointer would then exit the playfield from the top and the code would end.

As implied above, no 2Deadfish program can continue past d if d is only preceded by zero or more o's.

Any Deadfish program which begins with s preceded by zero or more o's will simply print 0 twice for every o preceding that s, while nothing past the s is executed.

Interpreter

This interpreter is in Python 3. It can take in text files or string input and returns a Deadfish equivalent of the code which was run.

def run_2deadfish(code_string: str, file_name=False):
    # This is the code itself.
    code = []

    # This debug string prints the equivalent code in Deadfish
    debug_string = ''

    # You can use text files
    # Newline characters don't affect anything
    # Therefore, the code is different when using or not using a text file
    if file_name:
        with open(code_string, 'r') as file:
            code = file.readlines()
    else:
        code = code_string.split('\n')

    # This makes sure you can still go down
    # when one line is shorter than all the rest
    max_line = max([len(line) for line in code])

    pointer = (0, 0)
    # This is the actual counter.
    counter = 0
    # The direction is equal to direction * 90 degrees counterclockwise from right.
    direction = 0
    while -1 < pointer[0] < len(code) and -1 < pointer[1] < max_line:
        row = pointer[0]
        column = pointer[1]
        if column < len(code[row]) and code[row][column] in 'diso':
            instruction = code[row][column]
            debug_string += instruction

            if instruction == 'o':
                print(counter)
            elif instruction == 'i':
                counter += 1
                direction += 1
            elif instruction == 's':
                counter **= 2
                direction += 2
            elif instruction == 'd':
                counter -= 1
                direction += 3

        # 4 * 90 = 360 degrees = 0 degrees
        direction %= 4

        if counter == 256 or counter == -1:
            counter = 0

        if direction == 0:
            pointer = (row, column+1)
        elif direction == 1:
            pointer = (row+1, column)
        elif direction == 2:
            pointer = (row, column-1)
        elif direction == 3:
            pointer = (row-1,column)

    return debug_string