Code Analysis - MortyHub/DIVI-Interpreter GitHub Wiki

Code Analysis


Language

This is made in python using the extensions: os, random, codecs, time

How the shell works

This detects the commands by detecting stuff thats in a list, it loops through the list with all the tokens for all commands using the code below.

TOKENS = [
    "shell.log('", "')", "update()", "shell.stop()", "multiline()", "help('",
    "shell.tok('", ".setTok('", "shell.thru(", "shell.log(", "shell.create('",
    '.setShell()', "shell.current()", "var.create('", ".private()",
    ".public()", "shell.publics()", "shell.privates()", "func", "({",
    "function('", "renderHTML(\"", "renderImage('", "setDefault('",
    "shell.open('", "Create.Object('", ".setSprite('", "display('",
    "ShellDat('", ".setDat('", "shell.site('", "if(", "=", ">", "<", "-", "+",
    "/", ".request('", "log('", ".edit('"
]

def write(INPUT):
    global shell
    for i in TOKENS:

The command shell takes place in main.py using the code

while True:
	inp = input("Divi >> ")
	write(inp)

The function write() is defined in compile.py which is accessed with

from DIVI.src.compiler.compile import write

How it detects your input commands

This part of the code is the longest, this part contains all items for inputted commands including the object creation system. In this section we will go over how the command detection works, most commands have the same function so you can just look this code and you pretty much already know how every command works

All Commands

In this section we will be going over how the command detection system works

  1. The Help Command, this works by taking the index of the item you put into the help('command'), this will take the item you have and find the index of the item you put into the help command and add the index by 1 and print it out, this can be shown by the code below.
HELP = [
    'shell.log', 'Will print whatever into the console', 'update',
    'Will update your shell to the latest version', 'shell.stop',
    'Will exit the shell', 'multiline',
    'Activates Multiline mode where you can input multiple commands and it will run those commands after typing end',
    'shell.tok()', 'finds Token of defined shell', '.setTok()',
    'Sets token of choice', 'shell.thru', 'random function seperated by ,',
    'shell.create',
    'creates a Virtual shell, good for running virtual machines', 'shell',
    'a virtual runtime ran by the software, multiple can be ran at once',
    'shell.current', 'Shows current shell', 'var.create',
    'This creates a variable ', '.public',
    'This sets a shell to be public so it can be accessed with URL or name',
    '.private',
    'This sets a shell to be public so it can only be accessed with the token',
    'func', 'a function', 'renderHTML',
    'Arguments will be rendered into a gui on screen, must be HTML',
    'shell.open',
    'This will open a divi script file of the directory of your choosings.', 'Create.Object', 'creates objects', '.setsprite', 'sets sprite of object', 'ShellDat', 'Displays the data of a shell', '.setDat', 'Sets data of shells'
]

def write(INPUT):
    global shell
    for i in TOKENS:
        if i in INPUT:

            if i == TOKENS[5]:
                hel = INPUT.split("'")
                curr = 0
                for x in HELP:
                    curr += 1
                    if x == hel[1]:
                        print(HELP[curr])
  1. User input commands are detected using the following code:
def write(INPUT):
    global shell
    for i in TOKENS:
        if i in INPUT:
            
            if i == TOKENS[?]:
                input = INPUT.split("'")
                # Do something with input[1]
                if input[1] == 'hello world':
                    print('hello world!')
  1. Using Objects to set data, in the language you use shell.create('object name') To create objects which can be opened in the Shells folder in DIVI/src/compiler/Shells all objects will have their own file creates in that folder. This can be achieved with the following code:
# TOKENS[10] is the shell.create() also don't mind the variable names, i was bored
if i == TOKENS[10]:
    n = INPUT.split('shell.create(')
    q = n[1].split(')')
    # Splits the comma for shell.create('name', 'token') so that is can grab the token
    jojo = q[0].split(',')
    moomoo = jojo[0].split("'")
    mooooo = jojo[1].split("'")
    # This will make the shell identifiable
    SHELLS.append(moomoo[1])
    # This sets the shell token
    SHELL_TOK.append(mooooo[1])
    # This creates the file with the name of the shell you creates
    open('DIVI/src/compiler/Shells/' + moomoo[1] + '.divi', "a+")
  1. How the multiline() function works, this is the built-in multiple line runner, this can be used to make complex scripts. It will detect if you use multiline() and it will start it, once you type 'end' on its own in a line then it will exit multiline mode and run the script. The code below will show how this runs.
            # TOKENS[4] is equal to 'multiline()'
            if i == TOKENS[4]:
                # This is what will go into the Mult() function
                MULTI = []
                print("Type: end, to end the the script")
                # This is used to show what line your on
                line = 1
                y = True
                while y == True:
                    inpu = input(str(line) + ' >> ')
                    # Increase What line your on
                    line += 1
                    # when 'end' is on its own it will end the multiline script and call Mult() to run it
                    if inpu == 'end':
                        y = False
                        Mult(MULTI)
                    else:
                        # if its not equal to 'end' then it will keep adding what command you have into the list for it to run
                        MULTI.append(inpu)

def Mult(inc):
    # since inc is a list from the previous function and it has all the commands stored
    for i in range(len(inc)):
         if inc == []:
             break
         else:
             # this is calling the write function to run all commands you input at once.
             write(inc[i])

If Then Statements

This is the use of symbols in order to make the shell understand what you want in the if statement.

Detection order

  1. Check if the statement contains "if" and ":"
  2. Copy the 2 variables defined after the symbol and before the symbol
  3. Understand the symbol such as < > != == => <=
  4. Finds everything in between : and ; (After the condition is returned to be true)
  5. Runs everything in between : and ; if returned true

Code

        elif 'if ' in inc[i]:
            smool = inc[i].split("if ")
            cals = smool[1].split(":")
            if '==' in cals[0]:
                use = cals[0].split('==')
                first = VARIABLE_VAL[VARIABLES.index(use[0])]
                second = VARIABLE_VAL[VARIABLES.index(use[1])]
                if first == second:
                    needed = inc[inc.index('if ' + str(smool[1])):inc.index(';')]
                    del needed[0]
                    Mult(needed)
                inc[inc.index('if ' + str(smool[1])):inc.index(';')] = ['']
                new = inc
                Mult(new)
                break

            if '>' in cals[0]:
                use = cals[0].split('>')
                first = VARIABLE_VAL[VARIABLES.index(use[0])]
                second = VARIABLE_VAL[VARIABLES.index(use[1])]
                if first > second:
                    needed = inc[inc.index('if ' + str(smool[1])):inc.index(';')]
                    del needed[0]
                    Mult(needed)
                inc[inc.index('if ' + str(smool[1])):inc.index(';')] = ['']
                new = inc
                Mult(new)
                break

The variable names are weird because I got bored, it made it way harder to manage everything. It detects the symbol shows by if '>' in cals[0], this is used multiple times just replacing the symbols and in the if first > second: ordeal.

⚠️ **GitHub.com Fallback** ⚠️