Tercet

From Esolang
Jump to navigation Jump to search
Tercet
Designed by User: Insulation
Appeared in 2025
Computational class Turing Complete
Reference implementation Unimplemented
File extension(s) .trc

Tercet is an esoteric programming language that aims to be Turing-complete with only 3 instructions.

For output, any nonzero number put into the first cell will be output an ASCII character based on the value of the first memory cell.

Tercet uses 1-based indexing. The memory pointer also cannot go below positive numbers.

Its memory model is a simple, cell-based one.

Instructions

Instruction Description
A [X] Increment/Decrement memory cell. Supports negative numbers. For input, A i takes the first character of the input, and stores its ASCII number in the current memory cell.
M [X] Set cell pointer to [X].
J [X] If the current cell is 0, jump to line [X]. Or else, continue.

Turing-completeness

Tercet is Turing-complete, as it allows arbitrarily incrementing/decrementing values, moving the memory pointer, has I/O and supports conditional jumps. This is sufficient to simulate brainfuck (hopefully), which itself is turing-complete.

Examples

Hello World!

A 72
A -72
A 101
A -101
A 108
A -108
A 108
A -108
A 111
A -111
A 32
A -32
A 87
A -87
A 111
A -111
A 114
A -114
A 108
A -108
A 100
A -100
A 33

XKCD Random Number

A 52

TODO: Add examples for other instructions

Interpreters

Python 3 Interpreter:

def tercet(program_lines, input_str=""):
    memory = [0] * 200_000
    pointer = 1
    pc = 0
    input_buffer = list(input_str)
    output = []

    def get_int_arg(line):
        parts = line.split()
        if len(parts) != 2:
            raise ValueError(f"Invalid instruction format: {line}")
        opcode, arg = parts
        if arg == "I":
            return "I"
        else:
            return int(arg)

    while pc < len(program_lines):
        line = program_lines[pc].strip()
        if not line:
            pc += 1
            continue
        opcode = line[0]
        arg = get_int_arg(line)

        if opcode == "A":
            if arg == "i":
                if input_buffer:
                    memory[pointer - 1] = ord(input_buffer.pop(0))
                else:
                    memory[pointer - 1] = 0
            else:
                memory[pointer - 1] = (memory[pointer - 1] + arg) % 256
            if pointer == 1 and memory[0] != 0:
                output.append(chr(memory[0]))
            pc += 1

        elif opcode == "M":
            if arg < 1 or arg > len(memory):
                raise IndexError(f"Memory pointer out of bounds: {arg}")
            pointer = arg
            pc += 1

        elif opcode == "J":
            if memory[pointer - 1] == 0:
                if arg < 1 or arg > len(program_lines):
                    raise IndexError(f"Jump target out of bounds: {arg}")
                pc = arg - 1
            else:
                pc += 1
        else:
            raise ValueError(f"Unknown opcode: {opcode}")

    return "".join(output)
    
tercet_program = [
   "A 72",
   "A -72",
   "A 101",
   "A -101",
   "A 108",
   "A -108",
   "A 108",
   "A -108",
   "A 111",
   "A -111",
   "A 32",
   "A -32",
   "A 87",
   "A -87",
   "A 111",
   "A -111",
   "A 114",
   "A -114",
   "A 108",
   "A -108",
   "A 100",
   "A -100",
   "A 33",
] # Put your programs in here. This is the "Hello World!" program shown in the examples.
print(tercet(tercet_program, input_str=""))