PCPL
parent
26e26c1627
commit
d7ef81d180
|
@ -0,0 +1,48 @@
|
|||
import importlib.util
|
||||
import sys
|
||||
import os
|
||||
|
||||
class loader():
|
||||
def __init__(self, bar):
|
||||
self.instructions = {}
|
||||
self.loadedpaths = []
|
||||
self.bar = bar
|
||||
|
||||
def RISloading(self, path, IR):
|
||||
count = 0
|
||||
for i in range(len(os.listdir(path))):
|
||||
i = os.listdir(path)[i]
|
||||
if os.path.isdir(path+"/"+i):
|
||||
for f in os.listdir(path+"/"+i):
|
||||
if os.path.isfile(path+"/"+i+"/"+f):
|
||||
count += 1
|
||||
else:
|
||||
count += 1
|
||||
stat = self.bar(count)
|
||||
stat.__iter__()
|
||||
for i in range(len(os.listdir(path))):
|
||||
i = os.listdir(path)[i]
|
||||
if os.path.isdir(path+"/"+i):
|
||||
for f in os.listdir(path+"/"+i):
|
||||
if os.path.isfile(path+"/"+i+"/"+f):
|
||||
self.loadIS(path+"/"+i+"/"+f, IR)
|
||||
stat.__next__()
|
||||
else:
|
||||
self.loadIS(path+"/"+i, IR)
|
||||
stat.__next__()
|
||||
try:
|
||||
stat.__next__()
|
||||
except StopIteration:
|
||||
pass
|
||||
|
||||
|
||||
def loadIS(self, path, IR):
|
||||
spec = importlib.util.spec_from_file_location(os.path.basename(path), path)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
sys.modules[os.path.basename(path)] = module
|
||||
spec.loader.exec_module(module)
|
||||
try:
|
||||
self.instructions[os.path.basename(path)] = module.main(IR)
|
||||
except:
|
||||
self.instructions[os.path.basename(path)] = module.main()
|
||||
self.loadedpaths.append(path)
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,20 @@
|
|||
class main():
|
||||
def IND(self, index):
|
||||
print("Indexing is not supported with this instruction")
|
||||
return (1)
|
||||
def EXEC(self, params):
|
||||
text = ""
|
||||
if len(params) > 0:
|
||||
text = params[0]
|
||||
if len(params) > 1:
|
||||
return (1)
|
||||
return (0, input(text))
|
||||
def ATR(self, target):
|
||||
print("Getting an attribute is not supported with this instruction")
|
||||
return (1)
|
||||
def STAT(self, params):
|
||||
print("Statements are not supported with this instruction")
|
||||
return (1)
|
||||
def OP(self, arg1, arg2):
|
||||
print("Operations are not supported with this instruction")
|
||||
return (1)
|
|
@ -0,0 +1,27 @@
|
|||
class main():
|
||||
def IND(self, index):
|
||||
print("Indexing is not supported with this instruction")
|
||||
return (1)
|
||||
def EXEC(self, params):
|
||||
end = "\n"
|
||||
flush = False
|
||||
text = ""
|
||||
if len(params) > 0:
|
||||
text = params[0]
|
||||
if len(params) > 1:
|
||||
end = params[1]
|
||||
if len(params) > 2:
|
||||
flush = bool(params[2])
|
||||
if len(params) > 3:
|
||||
return (1)
|
||||
print(text, end=end, flush=flush)
|
||||
return (0, None)
|
||||
def ATR(self, target):
|
||||
print("Getting an attribute is not supported with this instruction")
|
||||
return (1)
|
||||
def STAT(self, params):
|
||||
print("Statements are not supported with this instruction")
|
||||
return (1)
|
||||
def OP(self, arg1, arg2):
|
||||
print("Operations are not supported with this instruction")
|
||||
return (1)
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,18 @@
|
|||
class main():
|
||||
def IND(self, index):
|
||||
print("Indexing is not supported with this instruction")
|
||||
return (1)
|
||||
def EXEC(self, params):
|
||||
print("Execution is not supported with this instruction")
|
||||
return (1)
|
||||
def ATR(self, target):
|
||||
print("Getting an attribute is not supported with this instruction")
|
||||
return (1)
|
||||
def STAT(self, params):
|
||||
print("Statements are not supported with this instruction")
|
||||
return (1)
|
||||
def OP(self, arg1, arg2):
|
||||
if arg1.isnumeric() and arg2.isnumeric():
|
||||
return (0, int(arg1)+int(arg2))
|
||||
else:
|
||||
return (0, arg1+arg2)
|
|
@ -0,0 +1,18 @@
|
|||
class main():
|
||||
def IND(self, index):
|
||||
print("Indexing is not supported with this instruction")
|
||||
return (1)
|
||||
def EXEC(self, params):
|
||||
print("Execution is not supported with this instruction")
|
||||
return (1)
|
||||
def ATR(self, target):
|
||||
print("Getting an attribute is not supported with this instruction")
|
||||
return (1)
|
||||
def STAT(self, params):
|
||||
print("Statements are not supported with this instruction")
|
||||
return (1)
|
||||
def OP(self, arg1, arg2):
|
||||
if arg1.isnumeric() and arg2.isnumeric():
|
||||
return (0, int(arg1)/int(arg2))
|
||||
else:
|
||||
return (1)
|
|
@ -0,0 +1,18 @@
|
|||
class main():
|
||||
def IND(self, index):
|
||||
print("Indexing is not supported with this instruction")
|
||||
return (1)
|
||||
def EXEC(self, params):
|
||||
print("Execution is not supported with this instruction")
|
||||
return (1)
|
||||
def ATR(self, target):
|
||||
print("Getting an attribute is not supported with this instruction")
|
||||
return (1)
|
||||
def STAT(self, params):
|
||||
print("Statements are not supported with this instruction")
|
||||
return (1)
|
||||
def OP(self, arg1, arg2):
|
||||
if str(arg1) == str(arg2):
|
||||
return (0, True)
|
||||
else:
|
||||
return (0, False)
|
|
@ -0,0 +1,21 @@
|
|||
class main():
|
||||
def IND(self, index):
|
||||
print("Indexing is not supported with this instruction")
|
||||
return (1)
|
||||
def EXEC(self, params):
|
||||
print("Execution is not supported with this instruction")
|
||||
return (1)
|
||||
def ATR(self, target):
|
||||
print("Getting an attribute is not supported with this instruction")
|
||||
return (1)
|
||||
def STAT(self, params):
|
||||
print("Statements are not supported with this instruction")
|
||||
return (1)
|
||||
def OP(self, arg1, arg2):
|
||||
if arg1.isnumeric() and arg2.isnumeric():
|
||||
if arg1 > arg2:
|
||||
return (0, True)
|
||||
else:
|
||||
return (0, False)
|
||||
else:
|
||||
return (1)
|
|
@ -0,0 +1,21 @@
|
|||
class main():
|
||||
def IND(self, index):
|
||||
print("Indexing is not supported with this instruction")
|
||||
return (1)
|
||||
def EXEC(self, params):
|
||||
print("Execution is not supported with this instruction")
|
||||
return (1)
|
||||
def ATR(self, target):
|
||||
print("Getting an attribute is not supported with this instruction")
|
||||
return (1)
|
||||
def STAT(self, params):
|
||||
print("Statements are not supported with this instruction")
|
||||
return (1)
|
||||
def OP(self, arg1, arg2):
|
||||
if arg1.isnumeric() and arg2.isnumeric():
|
||||
if arg1 < arg2:
|
||||
return (0, True)
|
||||
else:
|
||||
return (0, False)
|
||||
else:
|
||||
return (1)
|
|
@ -0,0 +1,18 @@
|
|||
class main():
|
||||
def IND(self, index):
|
||||
print("Indexing is not supported with this instruction")
|
||||
return (1)
|
||||
def EXEC(self, params):
|
||||
print("Execution is not supported with this instruction")
|
||||
return (1)
|
||||
def ATR(self, target):
|
||||
print("Getting an attribute is not supported with this instruction")
|
||||
return (1)
|
||||
def STAT(self, params):
|
||||
print("Statements are not supported with this instruction")
|
||||
return (1)
|
||||
def OP(self, arg1, arg2):
|
||||
if arg1.isnumeric() and arg2.isnumeric():
|
||||
return (0, int(arg1)*int(arg2))
|
||||
else:
|
||||
return (0, arg1*arg2)
|
|
@ -0,0 +1,18 @@
|
|||
class main():
|
||||
def IND(self, index):
|
||||
print("Indexing is not supported with this instruction")
|
||||
return (1)
|
||||
def EXEC(self, params):
|
||||
print("Execution is not supported with this instruction")
|
||||
return (1)
|
||||
def ATR(self, target):
|
||||
print("Getting an attribute is not supported with this instruction")
|
||||
return (1)
|
||||
def STAT(self, params):
|
||||
print("Statements are not supported with this instruction")
|
||||
return (1)
|
||||
def OP(self, arg1, arg2):
|
||||
if arg1.isnumeric() and arg2.isnumeric():
|
||||
return (0, int(arg1)%int(arg2))
|
||||
else:
|
||||
return (1)
|
|
@ -0,0 +1,16 @@
|
|||
class main():
|
||||
def IND(self, index):
|
||||
print("Indexing is not supported with this instruction")
|
||||
return (1)
|
||||
def EXEC(self, params):
|
||||
print("Execution is not supported with this instruction")
|
||||
return (1)
|
||||
def ATR(self, target):
|
||||
print("Getting an attribute is not supported with this instruction")
|
||||
return (1)
|
||||
def STAT(self, params):
|
||||
print("Statements are not supported with this instruction")
|
||||
return (1)
|
||||
def OP(self, arg1, arg2):
|
||||
if arg1.isnumeric() and arg2.isnumeric():
|
||||
return (0, int(arg1)-int(arg2))
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,41 @@
|
|||
class main():
|
||||
def __init__(self, interpreter):
|
||||
self.interpreter = interpreter
|
||||
|
||||
def IND(self, index):
|
||||
print("Indexing is not supported with this instruction")
|
||||
return (1)
|
||||
def EXEC(self, params):
|
||||
print("Execution is not supported with this instruction")
|
||||
return (1)
|
||||
def ATR(self, target):
|
||||
print("Getting an attribute is not supported with this instruction")
|
||||
return (1)
|
||||
def STAT(self, params):
|
||||
class temp():
|
||||
def __init__(self, interpreter):
|
||||
self.interpreter = interpreter
|
||||
self.CS = ""
|
||||
self.STRtypes = ['"', "<", "{", "[", "(", ":"]
|
||||
self.ENDtypes = ['"', ">", "}", "]", ")", ";"]
|
||||
def IND(self, index):
|
||||
return (0, self.CS[index])
|
||||
def EXEC(self, params):
|
||||
PREPEDAST = self.interpreter.lexer.tokenGEN(self.CS, self.STRtypes, self.ENDtypes)
|
||||
PREPEDAST = self.interpreter.PREPAST(PREPEDAST)
|
||||
PREPEDAST = self.interpreter.EXECINS(PREPEDAST)
|
||||
return (0, PREPEDAST)
|
||||
def ATR(self, target):
|
||||
return (0, self.CS)
|
||||
def STAT(self, params):
|
||||
self.CS = params[0]
|
||||
return (0, None)
|
||||
def OP(self, arg1, arg2):
|
||||
print("Operations are not supported with this CI")
|
||||
return (1)
|
||||
tempC = temp(self.interpreter)
|
||||
self.interpreter.vars[params[0]] = tempC
|
||||
return (0, None)
|
||||
def OP(self, arg1, arg2):
|
||||
print("Operations are not supported with this instruction")
|
||||
return (1)
|
|
@ -0,0 +1,23 @@
|
|||
class main():
|
||||
def __init__(self, interpreter):
|
||||
self.interpreter = interpreter
|
||||
|
||||
def IND(self, index):
|
||||
print("Indexing is not supported with this instruction")
|
||||
return (1)
|
||||
def EXEC(self, params):
|
||||
print("Execution is not supported with this instruction")
|
||||
return (1)
|
||||
def ATR(self, target):
|
||||
print("Getting an attribute is not supported with this instruction")
|
||||
return (1)
|
||||
def STAT(self, params):
|
||||
if str(params[1]) == "True":
|
||||
if params[0] in self.interpreter.instructions:
|
||||
self.interpreter.instructions[params[0]].EXEC([])
|
||||
elif params[0] in self.interpreter.vars:
|
||||
self.interpreter.vars[params[0]].EXEC([])
|
||||
return (0, None)
|
||||
def OP(self, arg1, arg2):
|
||||
print("Operations are not supported with this instruction")
|
||||
return (1)
|
|
@ -0,0 +1,105 @@
|
|||
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}")
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,281 @@
|
|||
import os
|
||||
|
||||
class compiler():
|
||||
def __init__(self, bar):
|
||||
self.bar = bar
|
||||
|
||||
def compilecode(self, code, modules, onefile="1"):
|
||||
print("Initialising compiler...")
|
||||
out = ""
|
||||
print("Adding interpreter...")
|
||||
out = out + """
|
||||
class interpreter():
|
||||
def __init__(self, lexer):
|
||||
self.instructions = {}
|
||||
self.lexer = lexer
|
||||
self.vars = {}
|
||||
self.types = {"<": self.INDEXING, "{": self.EXECUTE, "[": self.ATTRIBUTING, "(": self.STATEMENT, ":": self.OPERATION}
|
||||
|
||||
def addinstruction(self, name, instruction):
|
||||
self.instructions[name] = instruction
|
||||
|
||||
def INDEXING(self, target):
|
||||
target = target[1:]
|
||||
target = target[:-1]
|
||||
target = target.split(",")
|
||||
for i in range(len(target)):
|
||||
target[i] = target[i].replace(".", ",")
|
||||
if target[0] in self.instructions:
|
||||
return self.instructions[target[0]].IND(target[1])
|
||||
else:
|
||||
return self.vars[target[0]].IND(target[1])
|
||||
|
||||
def EXECUTE(self, target):
|
||||
target = target[1:]
|
||||
target = target[:-1]
|
||||
target = target.split(",")
|
||||
for i in range(len(target)):
|
||||
target[i] = target[i].replace(".", ",")
|
||||
if target[0] in self.instructions:
|
||||
return self.instructions[target[0]].EXEC(target[1:])
|
||||
else:
|
||||
return self.vars[target[0]].EXEC(target[1:])
|
||||
|
||||
def ATTRIBUTING(self, target):
|
||||
target = target[1:]
|
||||
target = target[:-1]
|
||||
target = target.split(",")
|
||||
for i in range(len(target)):
|
||||
target[i] = target[i].replace(".", ",")
|
||||
if target[0] in self.instructions:
|
||||
return self.instructions[target[0]].ATR(target[1])
|
||||
else:
|
||||
return self.vars[target[0]].ATR(target[1])
|
||||
|
||||
def STATEMENT(self, target):
|
||||
target = target[1:]
|
||||
target = target[:-1]
|
||||
target = target.split(",")
|
||||
for i in range(len(target)):
|
||||
target[i] = target[i].replace(".", ",")
|
||||
if target[0] in self.instructions:
|
||||
return self.instructions[target[0]].STAT(target[1:])
|
||||
else:
|
||||
return self.vars[target[0]].STAT(target[1:])
|
||||
|
||||
def OPERATION(self, target):
|
||||
target = target[1:]
|
||||
target = target[:-1]
|
||||
target = target.split(",")
|
||||
for i in range(len(target)):
|
||||
target[i] = target[i].replace(".", ",")
|
||||
if target[0] in self.instructions:
|
||||
return self.instructions[target[0]].OP(target[1], target[2])
|
||||
else:
|
||||
return self.vars[target[0]].OP(target[1], target[2])
|
||||
|
||||
def EXECINS(self, target):
|
||||
back = self.types[target[0]](target)
|
||||
if back[0] == 1:
|
||||
raise TypeError(f"Error in instruction, HALT! {target}")
|
||||
return back
|
||||
|
||||
def PREPAST(self, target):
|
||||
# return codes 0: Normal 1: Error
|
||||
# print(f"Error in instruction, HALT! {target}")
|
||||
ACM = ""
|
||||
for i in range(len(target)):
|
||||
index = i
|
||||
i = target[i]
|
||||
if type(i) == list:
|
||||
i = self.PREPAST(i)
|
||||
#i = i.replace('"', "")
|
||||
if index != len(target)-1:
|
||||
if i[0] in self.types:
|
||||
back = self.EXECINS(i)
|
||||
ACM = ACM + "," + str(back[1])
|
||||
else:
|
||||
ACM = ACM + i
|
||||
else:
|
||||
i = i[:-1]+ ACM + i[-1]
|
||||
return i
|
||||
"""
|
||||
print("Adding lexer to output...")
|
||||
out = out + """
|
||||
class lexer():
|
||||
def scantill(self, target, goin, ends, RS=False):
|
||||
temp = []
|
||||
tempt = target[0]
|
||||
breaki = None
|
||||
f = 1
|
||||
ignore = False
|
||||
while True:
|
||||
index = f
|
||||
i = target[f]
|
||||
if i == '"' and ignore == False:
|
||||
ignore = True
|
||||
elif i == '"' and ignore == True:
|
||||
ignore = False
|
||||
else:
|
||||
if ignore == False:
|
||||
if i == ends[goin.index(target[0])]:
|
||||
breaki = index
|
||||
tempt = tempt + i
|
||||
break
|
||||
elif i in goin:
|
||||
TI,tempo = self.scantill(target[index:], goin, ends, True)
|
||||
f += TI
|
||||
temp.append(tempo)
|
||||
else:
|
||||
tempt = tempt + i
|
||||
else:
|
||||
tempt = tempt + i
|
||||
f += 1
|
||||
if breaki == None:
|
||||
breaki = len(target)-1
|
||||
temp.append(tempt)
|
||||
if RS == True:
|
||||
return breaki,temp
|
||||
else:
|
||||
return temp
|
||||
|
||||
def tokenGEN(self, target, starts, ends):
|
||||
#AST = {}
|
||||
#types = {'"': 0, "<": 1, "{": 2, "[": 3, "(": 4, "-":5}
|
||||
temp = self.scantill(target, starts, ends)
|
||||
#for i in temp:
|
||||
# if type(i) == str:
|
||||
# if i[0] != "-":
|
||||
# AST[i] = types[i[0]]
|
||||
# else:
|
||||
# AST[i] = types[i[0]] + i[1]
|
||||
# else:
|
||||
|
||||
return temp
|
||||
#return AST
|
||||
"""
|
||||
print("Adding init to output...")
|
||||
out = out + """
|
||||
lexerC = lexer()
|
||||
interpreterC = interpreter(lexerC)
|
||||
"""
|
||||
print("Reading modules...")
|
||||
if onefile == "1":
|
||||
instructions = []
|
||||
IDs = []
|
||||
for i in self.bar(len(modules)):
|
||||
if not os.path.exists(modules[i]):
|
||||
print(f"{modules[i]} not found.")
|
||||
continue
|
||||
file = open(modules[i], 'r')
|
||||
file = file.read()
|
||||
# os.path.basename(modules[i]).split(".")[0]
|
||||
file = file[:6]+os.path.basename(modules[i]).split(".")[0]+file[6:]
|
||||
out = out + file + "\n"
|
||||
instructions.append(os.path.basename(modules[i]).split(".")[0])
|
||||
IDs.append(os.path.basename(modules[i]).split(".")[0]+"main")
|
||||
print("Adding INS to output...")
|
||||
out = out + "INS = "+str(instructions)+"\n"
|
||||
out = out + "IDs = "+str(IDs).replace("'", "")
|
||||
print("Adding INS adder to output...")
|
||||
out = out + """
|
||||
for i in range(len(INS)):
|
||||
try:
|
||||
interpreterC.addinstruction(INS[i], IDs[i](interpreterC))
|
||||
except:
|
||||
interpreterC.addinstruction(INS[i], IDs[i]())
|
||||
"""
|
||||
else:
|
||||
os.mkdir("IS")
|
||||
for i in self.bar(len(modules)):
|
||||
if not os.path.exists(modules[i]):
|
||||
print(f"{modules[i]} not found.")
|
||||
continue
|
||||
file = open(modules[i], 'r')
|
||||
tcode = file.read()
|
||||
file.close()
|
||||
# os.path.basename(modules[i]).split(".")[0]
|
||||
file = open(f"IS/{os.path.basename(modules[i])}", 'w')
|
||||
file.write(tcode)
|
||||
file.close()
|
||||
print("Adding ISL because onefile is 0...")
|
||||
out = out + "RCHAR = " + repr("\r") + "\n"
|
||||
out = out + """
|
||||
import importlib.util
|
||||
import sys
|
||||
import os
|
||||
|
||||
class statusbar:
|
||||
def __init__(self, repeat, length=30, step=1):
|
||||
self.length = length
|
||||
self.repeat = repeat
|
||||
self.steps = repeat/length
|
||||
self.step = step
|
||||
|
||||
def __iter__(self):
|
||||
self.current = 0
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
print("["+("#"*int(self.current/self.steps))+(" "*(self.length-int(self.current/self.steps)))+"]"+f"{self.current}/{self.repeat}", end=RCHAR, flush=True)
|
||||
if self.current >= self.repeat:
|
||||
print()
|
||||
raise StopIteration
|
||||
self.current += self.step
|
||||
return self.current-1
|
||||
|
||||
class loader():
|
||||
def __init__(self):
|
||||
self.instructions = {}
|
||||
self.loadedpaths = []
|
||||
|
||||
def RISloading(self, path, IR):
|
||||
temp = os.listdir(path)
|
||||
for i in statusbar(len(temp)):
|
||||
i = temp[i]
|
||||
if os.path.isdir(path+"/"+i):
|
||||
for f in os.listdir(path+"/"+i):
|
||||
if os.path.isfile(path+"/"+i+"/"+f):
|
||||
self.loadIS(path+"/"+i+"/"+f, IR)
|
||||
else:
|
||||
self.loadIS(path+"/"+i, IR)
|
||||
print("finished RIS instruction loading")
|
||||
|
||||
|
||||
def loadIS(self, path, IR):
|
||||
spec = importlib.util.spec_from_file_location(os.path.basename(path), path)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
sys.modules[os.path.basename(path)] = module
|
||||
spec.loader.exec_module(module)
|
||||
try:
|
||||
self.instructions[os.path.basename(path)] = module.main(IR)
|
||||
except:
|
||||
self.instructions[os.path.basename(path)] = module.main()
|
||||
self.loadedpaths.append(path)
|
||||
loaderC = loader()
|
||||
"""
|
||||
print("Adding INS adder to output...")
|
||||
out = out + """
|
||||
loaderC.RISloading("IS", interpreterC)
|
||||
for i in loaderC.instructions:
|
||||
interpreterC.addinstruction(i.split(".")[0], loaderC.instructions[i])
|
||||
"""
|
||||
print("Adding STR and END types to output...")
|
||||
out = out + """
|
||||
STRtypes = ['"', "<", "{", "[", "(", ":"]
|
||||
ENDtypes = ['"', ">", "}", "]", ")", ";"]
|
||||
"""
|
||||
print("Adding target code...")
|
||||
code = code.replace("\n", "<PCPL-COM-NEWLINE>")
|
||||
out = out + 'code = '+ ('"'*3) + code + ('"'*3)
|
||||
print("Adding execution code...")
|
||||
out = out + """
|
||||
for i in code.split("<PCPL-COM-NEWLINE>"):
|
||||
AST = lexerC.tokenGEN(i, STRtypes, ENDtypes)
|
||||
AST = interpreterC.PREPAST(AST)
|
||||
interpreterC.EXECINS(AST)
|
||||
"""
|
||||
print("Done.")
|
||||
if onefile == "0":
|
||||
print("The folder 'IS' needs to always be in the same directory as the .py file or else the compiled program will crash.")
|
||||
return out
|
|
@ -0,0 +1,77 @@
|
|||
PCPL (Python Compilable Programming Language)
|
||||
|
||||
PCPL style
|
||||
|
||||
Strings:
|
||||
start: "
|
||||
end: "
|
||||
type: 0
|
||||
Indexing:
|
||||
start: <
|
||||
end: >
|
||||
type: 1
|
||||
Execute:
|
||||
start: {
|
||||
end: }
|
||||
type: 2
|
||||
Attributes:
|
||||
start: [
|
||||
end: ]
|
||||
type: 3
|
||||
Statements:
|
||||
start: (
|
||||
end: )
|
||||
type: 4
|
||||
|
||||
Operations:
|
||||
Operators (JUFS-BASE-PACK):
|
||||
equal:
|
||||
type: E
|
||||
higher:
|
||||
type: H
|
||||
lower:
|
||||
type: L
|
||||
add:
|
||||
type: A
|
||||
subtract:
|
||||
type: S
|
||||
multiply:
|
||||
type: M
|
||||
divide:
|
||||
type: D
|
||||
remainder:
|
||||
type: R
|
||||
start: :
|
||||
end: ;
|
||||
BASE-type: 5
|
||||
Example-type: 5-H This would mean an operation with the operator "higher" which checks if argument 1 is higher than argument 2
|
||||
|
||||
Example instruction:
|
||||
|
||||
<indexobject{function,5}>
|
||||
|
||||
This would return the indexed thing from "indexobject" with the index from the result of the function named "function" that is given the argument "5"
|
||||
|
||||
Example instruction class:
|
||||
|
||||
class main():
|
||||
def IND(self, index):
|
||||
print("Indexing is not supported with this instruction")
|
||||
return (1)
|
||||
def EXEC(self, params):
|
||||
print("Execution is not supported with this instruction")
|
||||
return (1)
|
||||
def ATR(self, target):
|
||||
print("Getting an attribute is not supported with this instruction")
|
||||
return (1)
|
||||
def STAT(self, params):
|
||||
print("Statements are not supported with this instruction")
|
||||
return (1)
|
||||
def OP(self, arg1, arg2):
|
||||
print("Operations are not supported with this instruction")
|
||||
return (1)
|
||||
|
||||
!WARNING! in strings (between ") instead of , use . the dots will be replaced by , automatically
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,107 @@
|
|||
class interpreter():
|
||||
def __init__(self, lexer):
|
||||
self.instructions = {}
|
||||
self.lexer = lexer
|
||||
self.vars = {}
|
||||
self.types = {"<": self.INDEXING, "{": self.EXECUTE, "[": self.ATTRIBUTING, "(": self.STATEMENT, ":": self.OPERATION}
|
||||
|
||||
def resetvars(self):
|
||||
self.vars = {}
|
||||
|
||||
def addinstruction(self, name, instruction):
|
||||
self.instructions[name] = instruction
|
||||
|
||||
def INDEXING(self, target):
|
||||
target = target[1:]
|
||||
target = target[:-1]
|
||||
target = target.split(",")
|
||||
for i in range(len(target)):
|
||||
target[i] = target[i].replace(".", ",")
|
||||
if target[0] in self.instructions:
|
||||
return self.instructions[target[0]].IND(target[1])
|
||||
elif target[0] in self.vars:
|
||||
return self.vars[target[0]].IND(target[1])
|
||||
else:
|
||||
raise TypeError("Referenced Operation doesnt exist.")
|
||||
|
||||
def EXECUTE(self, target):
|
||||
target = target[1:]
|
||||
target = target[:-1]
|
||||
target = target.split(",")
|
||||
for i in range(len(target)):
|
||||
target[i] = target[i].replace(".", ",")
|
||||
if target[0] in self.instructions:
|
||||
return self.instructions[target[0]].EXEC(target[1:])
|
||||
elif target[0] in self.vars:
|
||||
return self.vars[target[0]].EXEC(target[1:])
|
||||
else:
|
||||
raise TypeError("Referenced Operation doesnt exist.")
|
||||
|
||||
def ATTRIBUTING(self, target):
|
||||
target = target[1:]
|
||||
target = target[:-1]
|
||||
target = target.split(",")
|
||||
for i in range(len(target)):
|
||||
target[i] = target[i].replace(".", ",")
|
||||
if target[0] in self.instructions:
|
||||
return self.instructions[target[0]].ATR(target[1])
|
||||
elif target[0] in self.vars:
|
||||
return self.vars[target[0]].ATR(target[1])
|
||||
else:
|
||||
raise TypeError("Referenced Operation doesnt exist.")
|
||||
|
||||
def STATEMENT(self, target):
|
||||
target = target[1:]
|
||||
target = target[:-1]
|
||||
target = target.split(",")
|
||||
for i in range(len(target)):
|
||||
target[i] = target[i].replace(".", ",")
|
||||
if target[0] in self.instructions:
|
||||
return self.instructions[target[0]].STAT(target[1:])
|
||||
elif target[0] in self.vars:
|
||||
return self.vars[target[0]].STAT(target[1:])
|
||||
else:
|
||||
raise TypeError("Referenced Operation doesnt exist.")
|
||||
|
||||
def OPERATION(self, target):
|
||||
target = target[1:]
|
||||
target = target[:-1]
|
||||
target = target.split(",")
|
||||
for i in range(len(target)):
|
||||
target[i] = target[i].replace(".", ",")
|
||||
if target[0] in self.instructions:
|
||||
return self.instructions[target[0]].OP(target[1], target[2])
|
||||
elif target[0] in self.vars:
|
||||
return self.vars[target[0]].OP(target[1], target[2])
|
||||
else:
|
||||
raise TypeError("Referenced Operation doesnt exist.")
|
||||
|
||||
def EXECINS(self, target):
|
||||
back = self.types[target[0]](target)
|
||||
if back[0] == 1:
|
||||
raise TypeError(f"Error in instruction, HALT! {target}")
|
||||
return back
|
||||
|
||||
def PREPAST(self, target):
|
||||
# return codes 0: Normal 1: Error
|
||||
# print(f"Error in instruction, HALT! {target}")
|
||||
ACM = ""
|
||||
for i in range(len(target)):
|
||||
index = i
|
||||
i = target[i]
|
||||
if type(i) == list:
|
||||
i = self.PREPAST(i)
|
||||
#i = i.replace('"', "")
|
||||
if index != len(target)-1:
|
||||
if i[0] in self.types:
|
||||
back = self.EXECINS(i)
|
||||
ACM = ACM + "," + str(back[1])
|
||||
else:
|
||||
ACM = ACM + i
|
||||
else:
|
||||
i = i[:-1]+ ACM + i[-1]
|
||||
return i
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
class lexer():
|
||||
def scantill(self, target, goin, ends, RS=False):
|
||||
temp = []
|
||||
tempt = target[0]
|
||||
breaki = None
|
||||
f = 1
|
||||
ignore = False
|
||||
while True:
|
||||
if f > len(target)-1:
|
||||
raise TypeError("No end detected!")
|
||||
index = f
|
||||
i = target[f]
|
||||
if i == '"' and ignore == False:
|
||||
ignore = True
|
||||
elif i == '"' and ignore == True:
|
||||
ignore = False
|
||||
else:
|
||||
if ignore == False:
|
||||
if i == ends[goin.index(target[0])]:
|
||||
breaki = index
|
||||
tempt = tempt + i
|
||||
break
|
||||
elif i in goin:
|
||||
TI,tempo = self.scantill(target[index:], goin, ends, True)
|
||||
f += TI
|
||||
temp.append(tempo)
|
||||
else:
|
||||
tempt = tempt + i
|
||||
else:
|
||||
tempt = tempt + i
|
||||
f += 1
|
||||
if breaki == None:
|
||||
breaki = len(target)-1
|
||||
temp.append(tempt)
|
||||
if RS == True:
|
||||
return breaki,temp
|
||||
else:
|
||||
return temp
|
||||
|
||||
def tokenGEN(self, target, starts, ends):
|
||||
#AST = {}
|
||||
#types = {'"': 0, "<": 1, "{": 2, "[": 3, "(": 4, "-":5}
|
||||
temp = self.scantill(target, starts, ends)
|
||||
#for i in temp:
|
||||
# if type(i) == str:
|
||||
# if i[0] != "-":
|
||||
# AST[i] = types[i[0]]
|
||||
# else:
|
||||
# AST[i] = types[i[0]] + i[1]
|
||||
# else:
|
||||
|
||||
return temp
|
||||
#return AST
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
class statusbar:
|
||||
def __init__(self, repeat, length=30, step=1):
|
||||
self.length = length
|
||||
self.repeat = repeat
|
||||
self.steps = repeat/length
|
||||
self.step = step
|
||||
|
||||
def __iter__(self):
|
||||
self.current = 0
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
print("["+("#"*int(self.current/self.steps))+(" "*(self.length-int(self.current/self.steps)))+"]"+f"{self.current}/{self.repeat}", end="\r", flush=True)
|
||||
if self.current >= self.repeat:
|
||||
print()
|
||||
raise StopIteration
|
||||
self.current += self.step
|
||||
return self.current-1
|
Loading…
Reference in New Issue