Two Four

From Esolang
Jump to navigation Jump to search
Two Four's official logo by User:00Her0
This is still a work in progress. It may be changed in the future.

Two Four is an esoteric programming language by User:00Her0. It is named for its Four instructions, each of which are written as Two bits.

Two Four
Designed by 00Her0
Appeared in 2022
Computational class Unknown
Major implementations Python
File extension(s) .tf

Concepts

Input Layer The input layer is a tape of 16 bits. The Bitfield becomes this when the program is run.
SOBA SOBA stands for Selected One Bit Accumulator. It is a position on the Bitfield.
Bitfield A tape of 16 bits, 0..F. Although this is 1D, it's often formatted as a 4x4 "field".
Tapes Programs in Two Four are written on multiple tapes. In the current interpreter, a newline represents the end of a tape.

Instructions

"soba" refers to the position of the soba, while "mem@soba" refers to the value at the soba.

00 if mem@soba==0, soba+=4, else, soba+=1.
11 mem@soba++ (equivalent to mem@soba=!mem@soba). Even, consecutive calls also do soba++.
10 soba=0.
01 if mem@soba==0, skip the rest of this tape.

Note: white space is ignored.

Examples

Boolean Operators

"AND" program:

00 01 00 11

Explanation:

Takes two bits as input at memory 0 and 1, and writes its output to memory 2.

Input 00 or 01:

  • 00 moves soba to 4
  • 01 skips the rest of the program

Input 10:

  • 00 moves soba to 1
  • 01 skips the rest of the program

Input 11:

  • 00 moves soba to 1
  • 01 does nothing
  • 00 moves soba to 2
  • 11 writes output (1)

"OR" program:

11 01 00 11 01 00 11
10 11 11 11 11 11

"NOT" program:

11

Explanation:

Takes one bit as input at memory 0, and writes its output to memory 0 (yes, the same position.)

Input 1:

  • 11 writes 0 to memory 0

Input 0:

  • 11 writes 1 to memory 0

Turing Completeness

Two Four is almost certainly not Turing-Complete because it has a limited amount of data storage.

Implementations

Python Interpreter

I know that Python isn't the best language for implementations because it's not as fast as, say, C derivates, but I am familiar with it, and Two Four is very simple, so take this for now. C++ interpreter coming soon! Some notes:

  • There is no way to change the input layer other than to edit the input_layer variable.
  • There is a config variable that currently only has one value: step. When it's true, the field prints it's value after each instruction is executed. Otherwise, the field just prints it's value after all instructions have been executed.
  • This code isn't the cleanest. Updates might come.
  • This code removes all white space and turns newlines into ;;. I think it still count as an interpreted language though...
# Two Four Interpreter
# By 00Her0

input_layer=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
field=list(input_layer)
soba=0

one_calls=0
skip=False

code_path='code.tf'

cd=open(code_path, 'r')
code=cd.read()

code=code.replace('\n',';;').replace(' ',"")
#print(code)

CONFIG={
    "step":False
}

while True:
    for i in range(0,len(code),2):
        sel_token=code[i]+code[i+1]
        if not skip:
            if sel_token=='11':
                one_calls=one_calls+1
                if one_calls%2==0:
                    if field[soba]==1:
                        field[soba]=0
                    else:
                        field[soba]=1
                    soba+=1
                    if soba>15:
                        soba=0
                else:
                    if field[soba]==1:
                        field[soba]=0
                    else:
                        field[soba]=1
            elif sel_token=='00':
                one_calls=0
                if field[soba]==0:
                    for i in range(0,4):
                        soba+=1
                        if soba>15:
                            soba=0
                else:
                    soba+=1
                    if soba>15:
                        soba=0
            elif sel_token=='01':
                one_calls=0
                if field[soba]==0:
                    skip=True
            elif sel_token=='10':
                one_calls=0
                soba=0
        if sel_token==';;':
            one_calls=0
            skip=False
        if CONFIG['step']:
            for i in range(0,len(field)):
                print(str(field[i]),end="")
                if (i+1)/2%2==0:
                    print("")
                if (i+1)/8%2==0:
                    print("")
    if not CONFIG['step']:
        for i in range(0,len(field)):
                print(str(field[i]),end="")
                if (i+1)/2%2==0:
                    print("")
                if (i+1)/8%2==0:
                    print("")
    print('***')
    field=list(input_layer)
    soba=0
    one_calls=0
    skip=False
    inp=input('Press ENTER to run again... ')

Running

Windows

  1. Download Python 3
  2. Paste the code below into a file named twofour.py.
  3. Make a code.tf file in the same directory (folder) as twofour.py.
  4. Open "Command Prompt" app.
  5. type cd path/to/twofour, replacing path/to/twofour with your path to the folder containing twofour.py. Something like C:/Users/Administrator/python_projects/twofour.
  6. type py twofour.py.

Others

Currently I don't have any instructions for running Two Four on others, but it should be fairly similar. If you have a Mac or Linux computer and don't mind writing up steps for running, please do! However, you can run it in your browser via an online python interpreter. But, you will have to use a modified interpreter. To change the Two Four code the interpreter is running you will just have to change the code variable, since online python interpreters don't typically have support for file reading. here it is:

# Two Four Interpreter
# By 00Her0

input_layer=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
field=list(input_layer)
soba=0

one_calls=0
skip=False

code="""
11 00 11
00 11
"""

code=code.replace('\n',';;').replace(' ',"")
#print(code)

CONFIG={
    "step":False
}

while True:
    for i in range(0,len(code),2):
        sel_token=code[i]+code[i+1]
        if not skip:
            if sel_token=='11':
                one_calls=one_calls+1
                if one_calls%2==0:
                    if field[soba]==1:
                        field[soba]=0
                    else:
                        field[soba]=1
                    soba+=1
                    if soba>15:
                        soba=0
                else:
                    if field[soba]==1:
                        field[soba]=0
                    else:
                        field[soba]=1
            elif sel_token=='00':
                one_calls=0
                if field[soba]==0:
                    for i in range(0,4):
                        soba+=1
                        if soba>15:
                            soba=0
                else:
                    soba+=1
                    if soba>15:
                        soba=0
            elif sel_token=='01':
                one_calls=0
                if field[soba]==0:
                    skip=True
            elif sel_token=='10':
                one_calls=0
                soba=0
        if sel_token==';;':
            one_calls=0
            skip=False
        if CONFIG['step']:
            for i in range(0,len(field)):
                print(str(field[i]),end="")
                if (i+1)/2%2==0:
                    print("")
                if (i+1)/8%2==0:
                    print("")
    if not CONFIG['step']:
        for i in range(0,len(field)):
                print(str(field[i]),end="")
                if (i+1)/2%2==0:
                    print("")
                if (i+1)/8%2==0:
                    print("")
    print('***')
    field=list(input_layer)
    soba=0
    one_calls=0
    skip=False
    inp=input('Press ENTER to run again... ')