G85

From Esolang
Jump to navigation Jump to search

G85 is a programming language originally created in 2018 by User:danielle, but not refined until 2021. The name is inspired by the word Gates, referring to the loops, and the fact that it only uses 5 symbols (Gates -> Gate5 -> G85)

Language overview

G85 uses a tape that can contain the values 0, 1, and 2. This tape can only be read one symbol at a time from the left side, which erases the symbol, and written to one symbol at a time from the right side. All logic is done through a special type of loop which can be nested inside other loops. The program is read left to right, but the program counter can be moved by loops, which are described below. Commands for use in a program are:


Command Description
0, 1, 2 Writes a 0, 1, or 2 respectively to the right end of the tape (e.g. 0110010 -> 01100101 in the case of 1)
(x)(y) Begins a while-like loop. A single value is taken from the left side of the tape and deleted. If this value is 0, it executes everything between the first set of parentheses, labeled 'x'. If the value is 1, everything between the second set of parentheses labeled 'y' is executed. If the value is 2 or the tape is blank, execution is resumed from after the loop closes.

Any other characters, including whitespace and newlines, are ignored which is useful for writing comments. Note that all loops must use two sets of parentheses, otherwise undefined behaviour will occur. However, two separate loops can be placed side to side, as long as both use two sets of parentheses.

A "cycle" can be defined as occurring whenever the tape is read or written to. The Python interpreter outputs the amount of cycles that have taken place after program execution.

Examples

More examples are yet to come.

Tape reverser

The following program reverses any input tape. (e.g. 10110 -> 01101)

 22((0)(1)20(0)(1)2)((0)(1)21(0)(1)2)(0)(1)

Tape duplicator

The following program takes any input tape (including empty) and appends a copy of it. (e.g. 10110 -> 1011010110)

 222((0)(1)2(0)(1)02(0)(1)02)((0)(1)2(0)(1)12(0)(1)12)(0)(1)(0)(1)

Interpreters

There is currently one interpreter for G85.

Python

 program = input("Enter program: ")
 condition, indent, counter, cycles = 0, 0, 0, 0
 phases = {}
 
 compiled = ""
 tape = [int(c) for c in input("Enter tape: ")]
 
 def addline(text):
   global compiled
   compiled += "\t" * indent + text + "\n"
 
 while counter != len(program):
   char = program[counter]
   nextchar = program[counter + 1] if counter + 1 < len(program) else None
   if char in "012":
     addline("tape.append(" + char + ")")
     addline("cycles += 1")
   elif char == "(":
     if phases.get(indent, 0) == 0:
       phases[indent] = 1
       addline("while True:")
       indent += 1
       addline("condition = tape.pop(0) if len(tape) else 2")
       addline("cycles += 1")
       addline("if condition == 0:")
       indent += 1
     else:
       phases[indent] = 0
       indent += 1
       addline("elif condition == 1:")
       indent += 1
     if nextchar == ")":
       addline("pass")
   elif char == ")":
     if phases.get(indent - 2, 0) == 0:
       indent -= 1
       addline("elif condition == 2:")
       indent += 1
       addline("break")
     indent -= 2
   counter += 1
 
 print(tape)
 exec(compiled)
 print(tape)
 print("Cycles: " + str(cycles))

External resources