We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.

Cyclical

From Esolang
Jump to navigation Jump to search

Cyclical is a stack-based esolang developed by User:Broxol to be somewhat of an introduction to code golfing (programming using the least amount of bytes).

INTRODUCTION

Cyclical is based on one mechanic - cycling. Each time a function runs, the cycle value will change, up to 3 times (1, 2, 3, back to 1, and so on.) Functions have a certain timed cycle. If you mistime the cycle, the program will throw a CycleError at you (in python, it’s an AssertionError).

For example, this code:

   “Hello, World!” print 

works because print is timed to cycle 2.

This code:

   d “Hello, World!” print 

fails because the arbitrary d does count as a function (it stacks ‘d’ to the stack) and thus a CycleError will be thrown at you.

So, to correct it, you have to do:

   d “Hello, World!” output 

All functions have 3 versions, so all you need to do is count. For example, print has these three: write (cycle 1), print (cycle 2), and output (cycle 3).

The concept of cycling is to be used in tandem with code golfing. For example, you would want to find a way to not have to use the differentiate (cycle 2) function, because it’s really long. So, you would want to time it for something like cancel (cycle 3).

FUNCTIONS

Functions
Command Description
anything that isn’t a function pushes it onto stack
“…” String, just allows spaces to be used
write, print, output pops the top item of the stack and prints it
input, contribute, share pushes user input onto stack
int, integer, Z converts top item of stack to int
add, sum, combine Pops the top two items, adds them, then puts the result on top of the stack (can also be used for concatenation of strings)
subtract, differentiate, cancel Pops the top two items, subtracts them, then puts the result on top of the stack
multiply, times, product Pops the top two items, multiplies them, then puts the result on top of the stack
divide, quotient, split Pops the top two items, divides them, then puts the result on top of the stack (uses // so no floats)
duplicate, copy, restack Takes the top item of the stack and dupes it
if (>, <, =, !=) pops the top two items and checks them
stop, fail, exit stops an if statement
[…] Loop (resets cycle to 2 every time)
for, intend, since pops the top item and the next loop is a for loop that runs said popped item amount of times
reach, find, search Pops the top item, then pulls the -nth item (n being the popped item) and puts it on top of the stack

EXAMPLES

Hello World:

code = ‘“Hello, World!” print ’

CAT Program:

code = ‘input print ’

Fibonacci Sequence:

code = '0 integer 1 int contribute Z for [ copy 3 int find combine duplicate print ] '

Squares:

code = 'input integer since 1 integer output 1 integer [ 2 Z multiply copy output ] ‘

note: you can put for-intend-since as far back as there is a value in a stack

Basic Password:

code = '[ contribute 123456 if != "Try again." output ] exit Correct! output '

INTERPRETER

Remember to put your Cyclical code in the code = ‘’


code = '"Hello, World!" print '

code = list(code)

token_build = []
tokens = []
string = False
repeating = False
fool = False
no_check = 0
for_check = 0

j = 0
while j < len(code):
    if string == True:
        if code[j] == '"':
            token_build = "".join(token_build)
            tokens.append(token_build)
            token_build = []
            string = False
            j += 1
        else:
            token_build.append(str(code[j]))
            j += 1
    else:
        if code[j] == " ":
            token_build = "".join(token_build)
            tokens.append(token_build)
            token_build = []
            j += 1
        elif code[j] == '"':
            string = True
            j += 1
        elif code[j] == "[" or code[j] == "]":
            tokens.append(code[j])
            j += 1
        else:
            token_build.append(str(code[j]))
            j += 1

code = tokens

for item in code:
    if item == "":
        code.pop(code.index(item))
print(code)

left = 0
right = 0
for item in code:
    if item == "[":
        left += 1
    elif item == "]":
        right += 1
if left > right:
    assert left == right, "LoopError: ']' missing"
elif right > left:
    assert right == left, "LoopError: '[' missing"
        
i = 0
cycle = 1
stack = []

while i < len(code):
    if code[i] == "write":
        assert cycle == 1, f"CycleError: Code 'write' was used in wrong cycle {cycle}"
        print(stack.pop())
        i += 1
    elif code[i] == "print":
        assert cycle == 2, f"CycleError: Code 'print' was used in wrong cycle {cycle}"
        print(stack.pop())
        i += 1
    elif code[i] == "output":
        assert cycle == 3, f"CycleError: Code 'output' was used in wrong cycle {cycle}"
        print(stack.pop())
        i += 1
    elif code[i] == "input":
        assert cycle == 1, f"CycleError: Code 'input' was used in wrong cycle {cycle}"
        stack.append(str(input()))
        i += 1
    elif code[i] == "contribute":
        assert cycle == 2, f"CycleError: Code 'contribute' was used in wrong cycle {cycle}"
        stack.append(str(input()))
        i += 1
    elif code[i] == "share":
        assert cycle == 3, f"CycleError: Code 'share' was used in wrong cycle {cycle}"
        stack.append(str(input()))
        i += 1
    elif code[i] == "int":
        assert cycle == 1, f"CycleError: Code 'int' was used in wrong cycle {cycle}"
        stack[-1] = int(stack[-1])
        i += 1
    elif code[i] == "integer":
        assert cycle == 2, f"CycleError: Code 'integer' was used in wrong cycle {cycle}"
        stack[-1] = int(stack[-1])
        i += 1
    elif code[i] == "Z":
        assert cycle == 3, f"CycleError: Code 'Z' was used in wrong cycle {cycle}"
        stack[-1] = int(stack[-1])
        i += 1
    elif code[i] == "add":
        assert cycle == 1, f"CycleError: Code 'add' was used in wrong cycle {cycle}"
        store1 = stack.pop()
        store2 = stack.pop()
        store1 = store1 + store2
        stack.append(store1)
        i += 1
    elif code[i] == "sum":
        assert cycle == 2, f"CycleError: Code 'sum' was used in wrong cycle {cycle}"
        store1 = stack.pop()
        store2 = stack.pop()
        store1 = store1 + store2
        stack.append(store1)
        i += 1
    elif code[i] == "combine":
        assert cycle == 3, f"CycleError: Code 'combine' was used in wrong cycle {cycle}"
        store1 = stack.pop()
        store2 = stack.pop()
        store1 = store1 + store2
        stack.append(store1)
        i += 1
    elif code[i] == "subtract":
        assert cycle == 1, f"CycleError: Code 'subtract' was used in wrong cycle {cycle}"
        store1 = stack.pop()
        store2 = stack.pop()
        store1 = store1 - store2
        stack.append(store1)
        i += 1
    elif code[i] == "differentiate":
        assert cycle == 2, f"CycleError: Code 'differentiate' was used in wrong cycle {cycle}"
        store1 = stack.pop()
        store2 = stack.pop()
        store1 = store1 - store2
        stack.append(store1)
        i += 1
    elif code[i] == "cancel":
        assert cycle == 3, f"CycleError: Code 'cancel' was used in wrong cycle {cycle}"
        store1 = stack.pop()
        store2 = stack.pop()
        store1 = store1 - store2
        stack.append(store1)
        i += 1
    elif code[i] == "multiply":
        assert cycle == 1, f"CycleError: Code 'multiply' was used in wrong cycle {cycle}"
        store1 = stack.pop()
        store2 = stack.pop()
        store1 = store1 * store2
        stack.append(store1)
        i += 1
    elif code[i] == "times":
        assert cycle == 2, f"CycleError: Code 'times' was used in wrong cycle {cycle}"
        store1 = stack.pop()
        store2 = stack.pop()
        store1 = store1 * store2
        stack.append(store1)
        i += 1
    elif code[i] == "product":
        assert cycle == 3, f"CycleError: Code 'product' was used in wrong cycle {cycle}"
        store1 = stack.pop()
        store2 = stack.pop()
        store1 = store1 * store2
        stack.append(store1)
        i += 1
    elif code[i] == "divide":
        assert cycle == 1, f"CycleError: Code 'divide' was used in wrong cycle {cycle}"
        store1 = stack.pop()
        store2 = stack.pop()
        store1 = store1 // store2
        stack.append(store1)
        i += 1
    elif code[i] == "quotient":
        assert cycle == 2, f"CycleError: Code 'quotient' was used in wrong cycle {cycle}"
        store1 = stack.pop()
        store2 = stack.pop()
        store1 = store1 // store2
        stack.append(store1)
        i += 1
    elif code[i] == "split":
        assert cycle == 3, f"CycleError: Code 'split' was used in wrong cycle {cycle}"
        store1 = stack.pop()
        store2 = stack.pop()
        store1 = store1 // store2
        stack.append(store1)
        i += 1
    elif code[i] == "duplicate":
        assert cycle == 1, f"CycleError: Code 'duplicate' was used in wrong cycle {cycle}"
        stack.append(stack[-1])
        i += 1
    elif code[i] == "copy":
        assert cycle == 2, f"CycleError: Code 'copy' was used in wrong cycle {cycle}"
        stack.append(stack[-1])
        i += 1
    elif code[i] == "restack":
        assert cycle == 3, f"CycleError: Code 'restack' was used in wrong cycle {cycle}"
        stack.append(stack[-1])
        i += 1
    elif code[i] == "if":
        store1 = stack.pop()
        store2 = stack.pop()
        if code[i + 1] == "=":
            if store1 == store2:
                i += 2
            else:
                while True:
                    i += 1
                    cycle += 1
                    if cycle > 3:
                        cycle = 1
                    if code[i] == "stop":
                        assert cycle == 1, f"CycleError: Code 'stop' was used in wrong cycle {cycle}"
                        break
                    elif code[i] == "fail":
                        assert cycle == 2, f"CycleError: Code 'fail' was used in wrong cycle {cycle}"
                        break
                    elif code[i] == "exit":
                        assert cycle == 3, f"CycleError: Code 'exit' was used in wrong cycle {cycle}"
                        break
        elif code[i + 1] == "!=":
            if store1 != store2:
                i += 2
            else:
                while True:
                    i += 1
                    cycle += 1
                    if cycle > 3:
                        cycle = 1
                    if code[i] == "stop":
                        assert cycle == 1, f"CycleError: Code 'stop' was used in wrong cycle {cycle}"
                        break
                    elif code[i] == "fail":
                        assert cycle == 2, f"CycleError: Code 'fail' was used in wrong cycle {cycle}"
                        break
                    elif code[i] == "exit":
                        assert cycle == 3, f"CycleError: Code 'exit' was used in wrong cycle {cycle}"
                        break
        elif code[i + 1] == ">":
            if store1 > store2:
                i += 2
            else:
                while True:
                    i += 1
                    cycle += 1
                    if cycle > 3:
                        cycle = 1
                    if code[i] == "stop":
                        assert cycle == 1, f"CycleError: Code 'stop' was used in wrong cycle {cycle}"
                        break
                    elif code[i] == "fail":
                        assert cycle == 2, f"CycleError: Code 'fail' was used in wrong cycle {cycle}"
                        break
                    elif code[i] == "exit":
                        assert cycle == 3, f"CycleError: Code 'exit' was used in wrong cycle {cycle}"
                        break
        elif code[i + 1] == "<":
            if store1 < store2:
                i += 2
            else:
                while True:
                    i += 1
                    cycle += 1
                    if cycle > 3:
                        cycle = 1
                    if code[i] == "stop":
                        assert cycle == 1, f"CycleError: Code 'stop' was used in wrong cycle {cycle}"
                        break
                    elif code[i] == "fail":
                        assert cycle == 2, f"CycleError: Code 'fail' was used in wrong cycle {cycle}"
                        break
                    elif code[i] == "exit":
                        assert cycle == 3, f"CycleError: Code 'exit' was used in wrong cycle {cycle}"
                        break
        elif code[i + 1] == "in":
            if store1 in store2:
                i += 2
            else:
                while True:
                    i += 1
                    cycle += 1
                    if cycle > 3:
                        cycle = 1
                    if code[i] == "stop":
                        assert cycle == 1, f"CycleError: Code 'stop' was used in wrong cycle {cycle}"
                        break
                    elif code[i] == "fail":
                        assert cycle == 2, f"CycleError: Code 'fail' was used in wrong cycle {cycle}"
                        break
                    elif code[i] == "exit":
                        assert cycle == 3, f"CycleError: Code 'exit' was used in wrong cycle {cycle}"
                        break
    elif code[i] == "]":
        if repeating == True:
            no_check += 1
            if no_check >= for_check:
                i += 1
                repeating = False
            else:
                while code[i] != "[":
                    i -= 1
        else:
            while code[i] != "[":
                i -= 1
    elif code[i] == "for":
        assert cycle == 1, f"CycleError: Code 'for' used in wrong cycle {cycle}"
        no_check = 0
        for_check = stack.pop()
        repeating = True
        i += 1
    elif code[i] == "intend":
        assert cycle == 2, f"CycleError: Code 'intend' used in wrong cycle {cycle}"
        no_check = 0
        for_check = stack.pop()
        repeating = True
        i += 1
    elif code[i] == "since":
        assert cycle == 3, f"CycleError: Code 'since' used in wrong cycle {cycle}"
        no_check = 0
        for_check = stack.pop()
        repeating = True
        i += 1
    elif code[i] == "[":
        cycle = 1
        i += 1
    elif code[i] == "reach":
        assert cycle == 1, f"CycleError: Code 'reach' used in wrong cycle {cycle}"
        save = stack.pop(-stack.pop())
        stack.append(save)
        i += 1
    elif code[i] == "find":
        assert cycle == 2, f"CycleError: Code 'find' used in wrong cycle {cycle}"
        save = stack.pop(-stack.pop())
        stack.append(save)
        i += 1
    elif code[i] == "search":
        assert cycle == 3, f"CycleError: Code 'search' used in wrong cycle {cycle}"
        save = stack.pop(-stack.pop())
        stack.append(save)
        i += 1
    elif code[i] == "stop" or code[i] == "fail" or code[i] == "exit" or code[i] == "[": 
        i += 1
    else:
        stack.append(str(code[i]))
        i += 1
    cycle += 1
    if cycle > 3:
        cycle = 1