import argparse as ap
import time

# Define arguments
parser = ap.ArgumentParser(description='An interpreter for XEPro')
parser.add_argument('-v', action='store_true', help='Verbose mode, says what commands it finds and what its doing.')
parser.add_argument('-d', action='store_true', help='Debug mode, displays the contents of the file as well as what the interpreter reads.')
parser.add_argument('-nc', action='store_true', help='Does not say "Completed running code!" after completing executing code.')
parser.add_argument('file', type=ap.FileType('r'), help='The file that the XEPro interpreter executes.')
args = parser.parse_args()

# Define command tokens
vie = '%' # vie = variable in echo
mws = '"' # mws = multi word string
functionPrefix = '!'
variablePrefix = '.'
filePrefix = '.'
consolePrefix = '.'
mathPrefix = ','

# misc
version = '1.3.0'

# read arguments
if args.v:
    print(f'[XEPro {version}] Initializing Interpreter...')

content = args.file.read()

# Interpreter Class
class XEPro_Interpreter:
    def __init__(self):
        if args.v:
            print(f'[XEPro {version}] Verbose Mode Enabled!')
    # function to make sense of and run the instructions given.
    def run(self, content):
        # read file
        if args.d:
            print(f'[XEPro {version}] Contents of file: \n{content}')
        content = content.split()
        if args.d:
            print(f'[XEPro {version}] Readable Data: {content}')
        # code to understand the stuff
        for x in range(len(content)):
            #if content[x].startswith('.DefVar'):
            #    if args.v:
            #        print(f'[XEPro {version}] DefVar command found!')
            #    varName = content[x+2]
            #    varContents = content[x+1]
            #    varName.replace("'", "")
            #    with open('metadata.py', 'w') as f:
            #        f.write("# Generated by the XEPro Interpreter. \n# Do not touch! This is metadata to know what the variables are!\n\nvar = {}\nvarName = '{}'".format(varContents,varName))
            if content[x].startswith('FILLER_DO_NOT_USE_EMPTY'):
                if args.v:
                    print(f'[XEPro {version}] Filler detected. Skipping.')
           
            # shitty if statements, will fix later

            if content[x].startswith(f'{consolePrefix}Echo'):
                import os
                if content[x+1].startswith(mws):
                    if content[x+2].endswith(mws):
                        print('{} {}'.format(str(content[x+1]).replace('"', ''), str(content[x+2]).replace('"', '')))
                #else:
                    #print('{}'.format(x))
                        #if os.path.exists('./metadata.py'):
                        #    if args.v:
                        #        print(f'[XEPro {version}] Metadata file found!')
                        #    import metadata
                        #    if metadata.varName in content[x+1]:
                        #        print(str(metadata.var))
                        #else:
                        #    if args.v:
                        #        print(f'[XEPro {version}] Metadata file does not exist! Won\'t be able to use variables unless one is generated!')
                
            if content[x].startswith('NewLine'):
                print('\n')
            
            if content[x].startswith(f'{filePrefix}ReadFile'):
                if args.v:
                    print(f'[XEPro {version}] Found read file command, reading from {content[x+1]}')
                try:
                    with open(content[x+1], 'r') as f:
                        print(f.read())
                except FileNotFoundError:
                    if args.v:
                        print(f'[XEPro {version}] File not found')
            
            if content[x].startswith(f'{filePrefix}WriteFile'):
                with open(str(content[x+1]), 'w') as w:
                    if args.v:
                        print(f'[XEPro {version}] Found write file command, writing to {content[x+1]}')
                    w.write(str(content[x+2]))
                    w.close()
            
            if content[x].startswith(f'{filePrefix}AppendFile'):
                with open(str(content[x+1]), 'a') as a:
                    if args.v:
                        print(f'[XEPro {version}] Found append file command, appending to {content[x+1]}')
                    a.write(content[x+2])
                    a.close()

            if content[x].startswith(f'{filePrefix}DeleteFile'):
                import os
                os.remove(f'./{content[x+1]}')
                if args.v:
                    print(f'[XEPro {version}] Found delete file command, deleting {content[x+1]}')
            
            if content[x].startswith('Wait'):
                if args.v:
                    print(f'[XEPro {version}] Found wait command, waiting {content[x+1]}s')
                time.sleep(int(content[x+1]))

            if content[x].startswith(f'{filePrefix}listDir'):
                path = content[x+1]
                path = path.replace('\\', '/')
                import os
                print(str(os.listdir(str(path))))
                if args.v:
                    print(f'[XEPro {version}] Found list dir command, listing {path}')

            if content[x].startswith(f'{filePrefix}OpenProgram'):
                import os
                program = content[x+1]
                if args.v:
                    print(f'[XEPro {version}] Found open program command, opening {program}')
                    print(f'[XEPro {version}] Please close that window for program execution to continue')
                os.system(program)

            if content[x].startswith(f'{mathPrefix}Math'):
                num1 = content[x+1]
                num2 = content[x+3]
                sym = content[x+2]
                if args.v:
                    print(f'[XEPro {version}] Found math command, calculating {num1} {sym} {num2}')
                print(str(eval(f'{num1} {sym} {num2}')))
            
            if content[x].startswith(f'{filePrefix}CreateFile'):
                fileName = str(content[x+1])
                try:
                    f = open(fileName, 'x')
                    f.close()
                    print(f'Created file named {fileName}')
                except FileExistsError:
                    print(f'Cannot create {fileName} because it already exists')
                    
            if content[x].startswith(f'{mathPrefix}Mod'):
                num1 = int(content[x+1])
                num2 = int(content[x+2])
                if args.v:
                    print(f'[XEPro {version}] Found modulo command, calculating {str(num1)} % {str(num2)}.')
                print(num1 % num2)

            if content[x].startswith(f'{mathPrefix}Pow'):
                num1 = int(content[x+1])
                num2 = int(content[x+1])
                if args.v:
                    print(f'[XEPro {version}] Found power command, calculating {str(num1)} ** {str(num2)}.')
                print(num1 ** num2)
            
            if content[x].startswith(f'{mathPrefix}GetIntLimit'):
                x = int(content[x+1])
                if args.v:
                    print(f'[XEPro {version}] Found integer limit command, calculating 2 ** {str(x-1)}-1.')
                y = 2**(x-1)-1
                print(y)

interpreter = XEPro_Interpreter()
interpreter.run(content)
if not args.nc:
    print('Completed running code!')

import argparse as ap
import time

# Define arguments
parser = ap.ArgumentParser(description='An interpreter for XEPro')
parser.add_argument('-v', action='store_true', help='Verbose mode, says what commands it finds and what its doing.')
parser.add_argument('-d', action='store_true', help='Debug mode, displays the contents of the file as well as what the interpreter reads.')
parser.add_argument('-nc', action='store_true', help='Does not say "Completed running code!" after completing executing code.')
parser.add_argument('file', type=ap.FileType('r'), help='The file that the XEPro interpreter executes.')
args = parser.parse_args()

# Define command tokens
vie = '%' # vie = variable in echo
mws = '"' # mws = multi word string
functionPrefix = '!'
variablePrefix = '.'
filePrefix = '.'
consolePrefix = '.'
mathPrefix = ','

# misc
version = '1.3.0'

# read arguments
if args.v:
    print(f'[XEPro {version}] Initializing Interpreter...')

content = args.file.read()

# Interpreter Class
class XEPro_Interpreter:
    def __init__(self):
        if args.v:
            print(f'[XEPro {version}] Verbose Mode Enabled!')
    # function to make sense of and run the instructions given.
    def run(self, content):
        # read file
        if args.d:
            print(f'[XEPro {version}] Contents of file: \n{content}')
        content = content.split()
        if args.d:
            print(f'[XEPro {version}] Readable Data: {content}')
        # code to understand the stuff
        for x in range(len(content)):
            #if content[x].startswith('.DefVar'):
            #    if args.v:
            #        print(f'[XEPro {version}] DefVar command found!')
            #    varName = content[x+2]
            #    varContents = content[x+1]
            #    varName.replace("'", "")
            #    with open('metadata.py', 'w') as f:
            #        f.write("# Generated by the XEPro Interpreter. \n# Do not touch! This is metadata to know what the variables are!\n\nvar = {}\nvarName = '{}'".format(varContents,varName))
            if content[x].startswith('FILLER_DO_NOT_USE_EMPTY'):
                if args.v:
                    print(f'[XEPro {version}] Filler detected. Skipping.')
           
            # shitty if statements, will fix later

            if content[x].startswith(f'{consolePrefix}Echo'):
                import os
                if content[x+1].startswith(mws):
                    if content[x+2].endswith(mws):
                        print('{} {}'.format(str(content[x+1]).replace('"', ''), str(content[x+2]).replace('"', '')))
                #else:
                    #print('{}'.format(x))
                        #if os.path.exists('./metadata.py'):
                        #    if args.v:
                        #        print(f'[XEPro {version}] Metadata file found!')
                        #    import metadata
                        #    if metadata.varName in content[x+1]:
                        #        print(str(metadata.var))
                        #else:
                        #    if args.v:
                        #        print(f'[XEPro {version}] Metadata file does not exist! Won\'t be able to use variables unless one is generated!')
                
            if content[x].startswith('NewLine'):
                print('\n')
            
            if content[x].startswith(f'{filePrefix}ReadFile'):
                if args.v:
                    print(f'[XEPro {version}] Found read file command, reading from {content[x+1]}')
                try:
                    with open(content[x+1], 'r') as f:
                        print(f.read())
                except FileNotFoundError:
                    if args.v:
                        print(f'[XEPro {version}] File not found')
            
            if content[x].startswith(f'{filePrefix}WriteFile'):
                with open(str(content[x+1]), 'w') as w:
                    if args.v:
                        print(f'[XEPro {version}] Found write file command, writing to {content[x+1]}')
                    w.write(str(content[x+2]))
                    w.close()
            
            if content[x].startswith(f'{filePrefix}AppendFile'):
                with open(str(content[x+1]), 'a') as a:
                    if args.v:
                        print(f'[XEPro {version}] Found append file command, appending to {content[x+1]}')
                    a.write(content[x+2])
                    a.close()

            if content[x].startswith(f'{filePrefix}DeleteFile'):
                import os
                os.remove(f'./{content[x+1]}')
                if args.v:
                    print(f'[XEPro {version}] Found delete file command, deleting {content[x+1]}')
            
            if content[x].startswith('Wait'):
                if args.v:
                    print(f'[XEPro {version}] Found wait command, waiting {content[x+1]}s')
                time.sleep(int(content[x+1]))

            if content[x].startswith(f'{filePrefix}listDir'):
                path = content[x+1]
                path = path.replace('\\', '/')
                import os
                print(str(os.listdir(str(path))))
                if args.v:
                    print(f'[XEPro {version}] Found list dir command, listing {path}')

            if content[x].startswith(f'{filePrefix}OpenProgram'):
                import os
                program = content[x+1]
                if args.v:
                    print(f'[XEPro {version}] Found open program command, opening {program}')
                    print(f'[XEPro {version}] Please close that window for program execution to continue')
                os.system(program)

            if content[x].startswith(f'{mathPrefix}Math'):
                num1 = content[x+1]
                num2 = content[x+3]
                sym = content[x+2]
                if args.v:
                    print(f'[XEPro {version}] Found math command, calculating {num1} {sym} {num2}')
                print(str(eval(f'{num1} {sym} {num2}')))
            
            if content[x].startswith(f'{filePrefix}CreateFile'):
                fileName = str(content[x+1])
                try:
                    f = open(fileName, 'x')
                    f.close()
                    print(f'Created file named {fileName}')
                except FileExistsError:
                    print(f'Cannot create {fileName} because it already exists')
                    
            if content[x].startswith(f'{mathPrefix}Mod'):
                num1 = int(content[x+1])
                num2 = int(content[x+2])
                if args.v:
                    print(f'[XEPro {version}] Found modulo command, calculating {str(num1)} % {str(num2)}.')
                print(num1 % num2)

            if content[x].startswith(f'{mathPrefix}Pow'):
                num1 = int(content[x+1])
                num2 = int(content[x+1])
                if args.v:
                    print(f'[XEPro {version}] Found power command, calculating {str(num1)} ** {str(num2)}.')
                print(num1 ** num2)
            
            if content[x].startswith(f'{mathPrefix}GetIntLimit'):
                x = int(content[x+1])
                if args.v:
                    print(f'[XEPro {version}] Found integer limit command, calculating 2 ** {str(x-1)}-1.')
                y = 2**(x-1)-1
                print(y)

interpreter = XEPro_Interpreter()
interpreter.run(content)
if not args.nc:
    print('Completed running code!')

# Define arguments # Define command tokens # vie = variable in echo # mws = multi word string # misc # read arguments # Interpreter Class # function to make sense of and run the instructions given. # read file # code to understand the stuff #if content[x].startswith('.DefVar'): #    if args.v: #        print(f'[XEPro {version}] DefVar command found!') #    varName = content[x+2] #    varContents = content[x+1] #    varName.replace("'", "") #    with open('metadata.py', 'w') as f: #        f.write("# Generated by the XEPro Interpreter. \n# Do not touch! This is metadata to know what the variables are!\n\nvar = {}\nvarName = '{}'".format(varContents,varName)) # shitty if statements, will fix later #else: #print('{}'.format(x)) #if os.path.exists('./metadata.py'): #    if args.v: #        print(f'[XEPro {version}] Metadata file found!') #    import metadata #    if metadata.varName in content[x+1]: #        print(str(metadata.var)) #else: #    if args.v: #        print(f'[XEPro {version}] Metadata file does not exist! Won\'t be able to use variables unless one is generated!')