hashhub/PCPL/__init__.py

106 lines
4.2 KiB
Python

version = 0.5
modules = ["interpreter", "ISL", "lexer", "compiler"]
initmodules = {}
def INITMODULES():
global interpreter
global lexer
global loader
global compiler
global statusbar
if __name__ == "__main__":
import interpreter
import lexer
import ISL as loader
import compiler
import statusbar
else:
from PCPL import interpreter
from PCPL import lexer
from PCPL import ISL as loader
from PCPL import compiler
from PCPL import statusbar
statusbar = statusbar.statusbar
lexer = lexer.lexer()
interpreter = interpreter.interpreter(lexer)
loader = loader.loader(statusbar)
compiler = compiler.compiler(statusbar)
INITMODULES()
STRtypes = ['"', "<", "{", "[", "(", ":"]
ENDtypes = ['"', ">", "}", "]", ")", ";"]
def compile(target, onefile):
code = compiler.compilecode(target, loader.loadedpaths, onefile)
return code
def run(target):
for i in target.split("\n"):
#try:
if True:
AST = lexer.tokenGEN(i, STRtypes, ENDtypes)
AST = interpreter.PREPAST(AST)
interpreter.EXECINS(AST)[1]
#except Exception as e:
#print(f"Critical Error, HALT! {e}")
#break
def LIS(target):
try:
loader.RISloading(target, interpreter)
for i in loader.instructions:
interpreter.addinstruction(i.split(".")[0], loader.instructions[i])
except Exception as e:
print(f"Critical Error while loading instruction set {e}")
def resetvar():
interpreter.resetvars()
if __name__ == "__main__":
print("Types initialised")
print(f"PCPL Command Line Version {version}, Made by Justus Wolff.")
print(f"Type 'help' to get a list of commands.")
commands = ["compile <path> <target> compiles the file at the specified path and writes output to target", "load <path> executes the file at the specified path", "Reload-M Reloads core modules", "LIS <target instruction set name> loads the specific instruction set under the file path", "exit exits the PCPL Command Line", "help Prints this message"]
while True:
command = input(">")
if command.startswith("compile"):
if len(command.split(" ")) != 4:
print("The compile command only accepts three parameters! <src> <dst> <onefile 1/0>")
else:
print(f"compiling {command.split(' ')[1]} as {command.split(' ')[3]} and saving at {command.split(' ')[2]}. y/n?")
while True:
temp = input("compile? >")
if temp == "y":
compile()
targetfile = open(command.split(" ")[1], 'r')
code = targetfile.read()
targetfile.close()
code = compile(code, loader.loadedpaths, command.split(" ")[3])
targetfile = open(command.split(" ")[2], 'w')
targetfile.write(code)
targetfile.close()
break
elif temp == "n":
break
elif command.startswith("load"):
if len(command.split(" ")) != 2:
print("The load command only accepts one parameter!")
else:
targetfile = open(command.split(" ")[1], 'r')
code = targetfile.read()
targetfile.close()
run(code)
elif command.startswith("Reload-M"):
INITMODULES()
elif command.startswith("help"):
for i in commands:
print(i)
elif command.startswith("LIS"):
if len(command.split(" ")) != 2:
print("The LIS command only accepts one parameter!")
else:
LIS(command.split(" ")[1])
elif command.startswith("exit"):
exit()
else:
try:
AST = lexer.tokenGEN(command, STRtypes, ENDtypes)
AST = interpreter.PREPAST(AST)
print(interpreter.EXECINS(AST)[1])
except Exception as e:
print(f"Critical Error, HALT! {e}")