Braces

From Esolang
Jump to navigation Jump to search

Braces is an esolang invented by User:None1, the only valid command is braces.

Execution

A program in Braces consists of several blocks of code side by side. When a program in Braces is executed, it is evaluated and the result is printed to standard output.

An empty pair of braces

When an empty pair of braces is evaluated, it reads a character from standard input and returns it.

One block of code inside a pair of braces

Execute the block of code inside forever.

Multiple blocks of code inside a pair of braces

Evaluate every block of code, and concatenate the results together, and reverse the result, and return it.

Example Programs

Cat Program

{{}}

One Time Cat

{}

Reverse a string with only 2 characters

{{}{}}


Implementations

Braces to Lua compiler using python

This compiler compiles the code to brainces and then to lua, Made by User:Krolkrol

def findall(char, str):
    r = []
    x = 0
    while str.__contains__(char):
        r += [str.find(char)+x]
        x += str.find(char)+1
        str = str[str.find(char)+1 :]
        
        

    return r

def contains_invalid_chars(code):
    return not set(code).issubset({'{', '}'})



def compile(code):
    
    left = findall('{',code)
    right = findall('}',code)
    if len(left) == len(right) and not ('{{{' in code) and not ('}}}' in code) and contains_invalid_chars(code) == False:
        code = code.replace('}}','}]')
        code = code.replace('{{','[{')
        code = code.replace('{}','*')
        compile_from_brainces(code)
    else:
        print('This is not valid braces code')
 


def compile_from_brainces(code):
    code = list(code)
    loop = False
    print("""function input()
-- define input function here
end""")
    for char in code:
        if char == '*':
                print('print(input())')
        elif char == '[':
            if loop == True:
                raise ValueError
            else:
                loop = True
               
                print('while true do')

        elif char == ']':
            if loop == False:
                raise ValueError
            else:
                loop = False
                print('end')

 

compile('code here')