Baba

From Esolang
Jump to navigation Jump to search

by User:Akalysi

(this language is pretty much unfinished, im gonna add more probably)


baba is an esolang i made after the 2019 puzzle game "Baba Is You"

its syntax is to use [NOUN] [VERB] [QUALITY]


nouns: PROGRAM, MEMORY, POINTER

verbs: IS, ISNOT

qualities: START, END, INTEGER, STRING, INCREMENT, DECREMENT, PLUS10, PLUS100, COMPACT, OUT


table for IS outcomes
PROGRAM MEMORY POINTER X
starts parsing program X puts pointer at start of memory START
exits program X puts pointer at end of memory END
X allocates an int to memory X INTEGER
X allocates a string to memory X STRING
starts another parse and continues increments current cell increments pointer INCREMENT
ends this parse decrements current cell decrements pointer DECREMENT
starts 10 new parses and ends this one adds 10 to current cell adds 10 to pointer PLUS10
starts 100 new parses and ends this one adds 100 to current cell adds 100 to pointer PLUS100
X sets current cell to concatenated string X COMPACT
prints "PROGRAM" prints current cell prints pointer value OUT
table for ISNOT outcomes
PROGRAM MEMORY POINTER X
exits program X puts pointer at end of memory START
starts parsing program X puts pointer at start of memory END
X removes the pointerth int from memory X INTEGER
X removes the pointerth string from memory X STRING
X decrements current cell decrements pointer INCREMENT
X increments current cell increments pointer DECREMENT
X removes 10 from current cell removes 10 from the pointer PLUS10
X removes 100 from current cell removes 100 from the pointer PLUS100
X X prints pointer value OUT


Implementation

from rich import print

nouns = ["PROGRAM", "MEMORY", "POINTER"]
verbs = ["IS", "ISNOT"]
qualities = ["START", "END", "INTEGER", "STRING", "INCREMENT", "DECREMENT", "PLUS10", "PLUS100", "COMPACT", "OUT"]

memory = []
pointer = 0

def main(fname):
    def parse():
        global nouns, verbs, qualities, memory, pointer
        for i, line in enumerate(lines):
            line = line.strip()
            if line == "":
                continue

            chunks = line.split(" ")

            for chunk in chunks:
                if chunk not in nouns and chunk not in verbs and chunk not in qualities and chunk:
                    print(f"[red]PROGRAM IS ERROR\n{chunk} ON STAGE_START IS NOT KNOWN\nLINE IS {i+1}[/red]")
                    exit()

            intmem = [item for item in memory if isinstance(item, int)]
            strmem = [item for item in memory if isinstance(item, str)]

            if chunks[0] == "PROGRAM":
                if chunks[1] == "IS":
                    if chunks[2] == "END":
                        exit()
                    elif chunks[2] == "START":
                        pass
                    elif chunks[2] == "INCREMENT":
                        parse()
                    elif chunks[2] == "DECREMENT":
                        return
                    elif chunks[2] == "PLUS10":
                        for i in range(10):
                            parse()
                        return
                    elif chunks[2] == "PLUS100":
                        for i in range(100):
                            parse()
                        return
                    elif chunks[2] == "OUT":
                        print("PROGRAM")
                    else:
                        print(f"[red]PROGRAM IS ERROR\nQUALITY IS NOT ALLOWED\nLINE IS {i+1}[/red]")
                        exit()
                elif chunks[1] == "ISNOT":
                    if chunks[2] == "END":
                        parse()
                    elif chunks[2] == "START":
                        exit()
                    else:
                        print(f"[red]PROGRAM IS ERROR\nQUALITY IS NOT ALLOWED\nLINE IS {i+1}[/red]")
                        exit()
                else:
                    print(f"[red]PROGRAM IS ERROR\nVERB IS NOT ALLOWED\nLINE IS {i+1}[/red]")

            # --- #

            elif chunks[0] == "MEMORY":
                if chunks[1] == "IS":
                    if chunks[2] == "INTEGER":
                        memory.append(0)
                    elif chunks[2] == "STRING":
                        memory.append("")
                    elif chunks[2] == "INCREMENT":
                        try:
                            memory[pointer] += 1
                        except IndexError:
                            print(f"[red]PROGRAM IS ERROR\nINDEX IS NOT ON RANGE\nLINE IS {i+1}[/red]")
                        except TypeError:
                            print(f"[red]PROGRAM IS ERROR\nCELL IS NOT INTEGER\nLINE IS {i+1}[/red]")
                    elif chunks[2] == "DECREMENT":
                        try:
                            memory[pointer] -= 1
                        except IndexError:
                            print(f"[red]PROGRAM IS ERROR\nINDEX IS NOT ON RANGE\nLINE IS {i+1}[/red]")
                        except TypeError:
                            print(f"[red]PROGRAM IS ERROR\nCELL IS NOT INTEGER\nLINE IS {i+1}[/red]")
                    elif chunks[2] == "PLUS10":
                        try:
                            memory[pointer] += 10
                        except IndexError:
                            print(f"[red]PROGRAM IS ERROR\nINDEX IS NOT ON RANGE\nLINE IS {i+1}[/red]")
                        except TypeError:
                            print(f"[red]PROGRAM IS ERROR\nCELL IS NOT INTEGER\nLINE IS {i+1}[/red]")
                    elif chunks[2] == "PLUS100":
                        try:
                            memory[pointer] += 100
                        except IndexError:
                            print(f"[red]PROGRAM IS ERROR\nINDEX IS NOT ON RANGE\nLINE IS {i+1}[/red]")
                        except TypeError:
                            print(f"[red]PROGRAM IS ERROR\nCELL IS NOT INTEGER\nLINE IS {i+1}[/red]")
                    elif chunks[2] == "COMPACT":
                        try:
                            if isinstance(memory[pointer], str):
                                string = ""
                                for num in intmem:
                                    string += chr(num)
                                memory[pointer] = string
                            else:
                                print(f"[red]PROGRAM IS ERROR\nCOMPACTING_MEMORY IS NOT STRING\nLINE IS {i+1}[/red]")
                        except IndexError:
                            print(f"[red]PROGRAM IS ERROR\nINDEX IS NOT ON RANGE\nLINE IS {i+1}[/red]")
                    elif chunks[2] == "OUT":
                        print(memory[pointer])
                    else:
                        print(f"[red]PROGRAM IS ERROR\nQUALITY IS NOT ALLOWED\nLINE IS {i+1}[/red]")
                        exit()
                if chunks[1] == "ISNOT":
                    if chunks[2] == "INTEGER":
                        memory.remove(intmem[pointer])
                    elif chunks[2] == "STRING":
                        memory.remove(strmem[pointer])
                    elif chunks[2] == "INCREMENT":
                        try:
                            memory[pointer] -= 1
                        except IndexError:
                            print(f"[red]PROGRAM IS ERROR\nINDEX IS NOT ON RANGE\bLINE IS {i+1}[/red]")
                    elif chunks[2] == "DECREMENT":
                        try:
                            memory[pointer] += 1
                        except IndexError:
                            print(f"[red]PROGRAM IS ERROR\nINDEX IS NOT ON RANGE\nLINE IS {i+1}[/red]")
                    elif chunks[2] == "PLUS10":
                        try:
                            memory[pointer] -= 10
                        except IndexError:
                            print(f"[red]PROGRAM IS ERROR\nINDEX IS NOT ON RANGE\nLINE IS {i+1}[/red]")
                    elif chunks[2] == "PLUS100":
                        try:
                            memory[pointer] -= 100
                        except IndexError:
                            print(f"[red]PROGRAM IS ERROR\nINDEX IS NOT ON RANGE\nLINE IS {i+1}[/red]")
                    else:
                        print(f"[red]PROGRAM IS ERROR\nQUALITY IS NOT ALLOWED\nLINE IS {i+1}[/red]")
                        exit()
            
            # --- #
            
            elif chunks[0] == "POINTER":
                if chunks[1] == "IS": 
                    if chunks[2] == "END":
                        pointer = len(memory)-1
                    elif chunks[2] == "START":
                        pointer = 0
                    elif chunks[2] == "INCREMENT":
                        pointer += 1
                    elif chunks[2] == "DECREMENT":
                        pointer -= 1
                    elif chunks[2] == "PLUS10":
                        pointer += 10
                    elif chunks[2] == "PLUS100":
                        pointer += 100
                    elif chunks[2] == "OUT":
                        print(pointer)
                    else:
                        print(f"[red]PROGRAM IS ERROR\nQUALITY IS NOT ALLOWED\nLINE IS {i+1}[/red]")
                        exit()
                elif chunks[1] == "ISNOT":
                    if chunks[2] == "END":
                        pointer = 0
                    elif chunks[2] == "START":
                        pointer = len(memory)-1
                    elif chunks[2] == "INCREMENT":
                        pointer -= 1
                    elif chunks[2] == "DECREMENT":
                        pointer += 1
                    elif chunks[2] == "PLUS10":
                        pointer -= 10
                    elif chunks[2] == "PLUS100":
                        pointer -= 100
                    else:
                        print(f"[red]PROGRAM IS ERROR\nQUALITY IS NOT ALLOWED\nLINE IS {i+1}[/red]")
                        exit()
                else:
                    print(f"[red]PROGRAM IS ERROR\nVERB IS NOT ALLOWED\nLINE IS {i+1}[/red]")
    
    with open(fname) as file:
        lines = file.readlines()

    for i, line in enumerate(lines):
        line = line.strip()
        if line == "":
            continue

        chunks = line.split(" ")

        for chunk in chunks:
            if chunk not in nouns and chunk not in verbs and chunk not in qualities and chunk:
                print(f"[red]PROGRAM IS ERROR\n{chunk} ON STAGE_START IS NOT KNOWN\nLINE IS {i+1}[/red]")
                exit()
        
        if chunks[0] == "PROGRAM":
            if chunks[1] == "IS":
                if chunks[2] == "START":
                    parse()
                else:
                    print(f"[red]PROGRAM IS ERROR\nQUALITY ON STAGE_START IS NOT ALLOWED\nLINE IS {i+1}[/red]")
                    exit()
            else:
                print(f"[red]PROGRAM IS ERROR\nQUALITY ON STAGE_START IS NOT ALLOWED\nLINE IS {i+1}[/red]")
                exit()

if __name__ == "__main__":
    main("index.baba")


Hello World

PROGRAM IS START

MEMORY IS INTEGER
MEMORY IS INTEGER
MEMORY IS INTEGER
MEMORY IS INTEGER
MEMORY IS INTEGER
MEMORY IS INTEGER
MEMORY IS INTEGER
MEMORY IS INTEGER
MEMORY IS INTEGER
MEMORY IS INTEGER
MEMORY IS INTEGER
MEMORY IS INTEGER
MEMORY IS INTEGER
MEMORY IS STRING

MEMORY IS PLUS100
MEMORY IS INCREMENT
MEMORY IS INCREMENT
MEMORY IS INCREMENT
MEMORY IS INCREMENT

POINTER IS INCREMENT

MEMORY IS PLUS100
MEMORY IS INCREMENT

POINTER IS INCREMENT

MEMORY IS PLUS100
MEMORY IS PLUS10
MEMORY IS DECREMENT
MEMORY IS DECREMENT

POINTER IS INCREMENT

MEMORY IS PLUS100
MEMORY IS PLUS10
MEMORY IS DECREMENT
MEMORY IS DECREMENT

POINTER IS INCREMENT

MEMORY IS PLUS100
MEMORY IS PLUS10
MEMORY IS INCREMENT

POINTER IS INCREMENT

MEMORY IS PLUS10
MEMORY IS PLUS10
MEMORY IS PLUS10
MEMORY IS PLUS10
MEMORY IS INCREMENT
MEMORY IS INCREMENT
MEMORY IS INCREMENT
MEMORY IS INCREMENT

POINTER IS INCREMENT

MEMORY IS PLUS10
MEMORY IS PLUS10
MEMORY IS PLUS10
MEMORY IS INCREMENT
MEMORY IS INCREMENT

POINTER IS INCREMENT

MEMORY IS PLUS100
MEMORY IS PLUS10
MEMORY IS PLUS10
MEMORY IS DECREMENT

POINTER IS INCREMENT

MEMORY IS PLUS100
MEMORY IS PLUS10
MEMORY IS INCREMENT

POINTER IS INCREMENT

MEMORY IS PLUS100
MEMORY IS PLUS10
MEMORY IS INCREMENT
MEMORY IS INCREMENT
MEMORY IS INCREMENT
MEMORY IS INCREMENT

POINTER IS INCREMENT

MEMORY IS PLUS100
MEMORY IS PLUS10
MEMORY IS DECREMENT
MEMORY IS DECREMENT

POINTER IS INCREMENT

MEMORY IS PLUS100

POINTER IS INCREMENT

MEMORY IS PLUS10
MEMORY IS PLUS10
MEMORY IS PLUS10
MEMORY IS INCREMENT
MEMORY IS INCREMENT
MEMORY IS INCREMENT

POINTER IS INCREMENT

MEMORY IS COMPACT
MEMORY IS OUT

PROGRAM IS END

author: kesi
description: a hello world program
hello, world!