Code:
# read file of first agrument
import sys, os
try:
data = open(sys.argv[1]).read()
except:
data = open("File.txt").read()
sys.argv.append("File.txt")
dct = {"@": data, "E": "0"}
iteration = 0
RESET = False
define = False
printf = False
replace = False
inp = False
join = False
Reverse = False
getascii = False
getfirstchar = False
removeallexceptcertainchar = False
readfile = False
comment = False
multilinecomment = False
takeagrument = False
definebutvar = False
getfilesandfolders = False
haltifeof = False
################################################################################
variable = ""
variable2 = ""
variable3 = ""
variable4 = ""
variable5 = ""
variable6 = ""
string = ""
escape = False
isready = False
isset = False
isgo = False
def erroring(errtype, errreason, tb):
print(errreason, end="", file=sys.stderr)
sys.excepthook = erroring
def dctremoveallexceptcertain(string, variable):
# removes all except certain characters
# string = string to be modified
# variable = characters to be kept
# returns modified string
global newstring
newstring = ""
for i in string:
if i in variable:
newstring += i
return newstring
while True:
# Note you should use try and except to catch errors, not if x in y:
for i in data:
if printf:
# print the variable of dct variable
if i in dct:
print(dct[i], end = "")
printf = False
else:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
break
elif define:
if isready:
if escape:
# check if i is quote or backslash add to string else add backslash and i
if i == "\"" or i == "\\":
string += i
escape = False
else:
string += "\\" + i
escape = False
elif i == "\"":
dct[variable] = string
string = ""
isready = False
define = False
# check if the variable is "@" change the data
if variable == "@":
RESET = True
break
elif i == "\\":
escape = True
continue
else:
string += i
else:
variable = i
isready = True
elif replace:
# get 3 variables
if isgo:
try:
dct[variable] = dct[variable2].replace(variable3, i)
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
break
replace = isset = isready = isgo = False
# check if the variable is "@" change the data
if variable == "@":
RESET = True
break
elif isset:
variable3 = i
isgo = True
elif isready:
variable2 = i
isset = True
else:
variable = i
isready = True
elif inp:
try:
dct[i] = input()
dct["E"] = "0"
except EOFError:
dct["E"] = "1"
inp = False
if i == "@":
RESET = True
break
elif join:
if isset:
try:
dct[variable] = dct[variable2] + dct[i]
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
break
join = isset = isready = isgo = False
# check if the variable is "@" change the data
if variable == "@":
RESET = True
break
elif isready:
variable2 = i
isset = True
else:
variable = i
isready = True
elif Reverse:
# this is equivalent to pseudocode var = var2.reverse()
if isready:
try:
dct[variable] = dct[i][::-1]
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
break
isready = False
Revert = False
if variable == "@":
RESET = True
break
else:
variable = i
isready = True
elif getfirstchar:
# gets the first character of the variable
if isready:
try:
dct[variable] = dct[i][0]
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
isready = False
getfirstchar = False
if variable == "@":
RESET = True
break
else:
variable = i
isready = True
elif getascii:
# gets ascii value of the variable
if isready:
try:
dct[i]
try:
dct[variable] = ord(dct[i])
except:
raise Exception("ERROR IN FILE", sys.argv[1], "INVALID NUMBER IN ITERATION", iteration)
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
getascii = False
isready = False
if variable == "@":
RESET = True
break
else:
variable = i
isready = True
elif removeallexceptcertainchar:
# removes all except certain character
if isready:
try:
dct[variable] = dctremoveallexceptcertain(dct[variable], dct[i])
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
isready = False
removeallexceptcertainchar = False
if variable == "@":
RESET = True
break
else:
variable = i
isready = True
elif readfile:
if isready:
try:
dct[i]
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
try:
dct[variable] = open(dct[i], "r").read()
dct["E"] = "0"
except:
dct["E"] = "1"
isready = False
readfile = False
if variable == "@":
RESET = True
break
else:
variable = i
isready = True
elif comment:
# ignore until a newline is found
if i == "\n":
comment = False
elif multilinecomment:
if i == "}":
multilinecomment = False
elif takeagrument:
# checks if the variable is defined and the variable is a number
if isready:
try:
dct[i]
try:
int(dct[i])
try:
dct[variable] = sys.argv[int(dct[i])]
dct["E"] = "0"
except:
dct["E"] = "1"
except:
raise Exception("ERROR IN FILE", sys.argv[1], "INVALID NUMBER", iteration)
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
isready = False
takeagrument = False
if variable == "@":
RESET = True
else:
variable = i
isready = True
elif definebutvar:
# like > but it defines the variable
if isready:
try:
dct[variable] = dct[i]
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
isready = False
definebutvar = False
if variable == "@":
RESET = True
break
else:
variable = i
isready = True
elif getfilesandfolders:
# gets all files and folders in a variable if folder not found the variable "E" is equal to "1" else "0"
if isready:
# check if variable is defined
try:
dct[i]
except:
raise Exception("ERROR IN FILE", sys.argv[1], "VARIABLE NOT FOUND IN ITERATION", iteration)
try:
files = os.listdir(dct[i])
dct[variable] = files
dct["E"] = "0"
except:
dct["E"] = "1"
isready = False
getfilesandfolders = False
if variable == "@":
RESET = True
break
else:
variable = i
isready = True
elif haltifeof:
if dct["E"] == "1":break
haltifeof = False
elif i == "@": #############################################################################################################
RESET = True
break
elif i == "H":
break
elif i == ">":
define = True
elif i == "*":
printf = True
elif i == "$":
replace = True
elif i == "&":
inp = True
elif i == "+":
join = True
elif i == "/":
Reverse = True
elif i == "?":
getfirstchar = True
elif i == ".":
getascii = True
elif i == "!":
removeallexceptcertainchar = True
elif i == ";":
readfile = True
elif i == ":":
comment = True
elif i == "{":
multilinecomment = True
elif i == "|":
takeagrument = True
elif i == "=":
definebutvar = True
elif i == "%":
getfilesandfolders = True
elif i == "_":
haltifeof = True
# its not necessary to raise error invalid command
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
iteration += 1
# check if string still contains something put it in dct
if string != "":
dct[variable] = string
string = ""
define = False
isready = False
isset = False
isgo = False
printf = False
replace = False
inp = False
join, Reverse = False, False
getfirstchar = False
getascii = False
removeallexceptcertainchar = False
readfile = False
comment = False
multilinecomment = False
takeagrument = False
definebutvar = False
getfilesandfolders = False
# check if dct is "@" change the data
if variable == "@":
RESET = True
if RESET:
RESET = False
data = dct["@"]
iteration = 0
continue
break
try:
print(dct["#"], end = "")
except:
pass
This is a programming language interpreter. I will show wiki.
Commands:
(a, b, c, d means variables next command)
"@" goto first char
"H" halt (must be uppercase, not lowercase)
">" define var Syntax: >~Hello"
Escape is backslash (the quote is optional)
"*" print the next char variable
"$" a = b.replace(c, d)
"&" Input the next char variable ("E" var is 1 if EOF)
"+" a = b.join(c)
"/" a = b.reverse()
"?" a = b.getfirstchar()
"." a = chr(b)
"!" a = a but non-b chars removed
";" a = readfile(b) ("E" = 1 if the file doesn't exist)
":" comment
"{" start multiline comment
"}" end multiline comment
"|" a = takearg(int(b))
"=" a = b
"%" a = str(getfilesandfolders(b))
"_" halt if EOF
Variables:
"@" if the variable is edited, the program changes
"#" print when program ends.
"E" EOF Var
Examples:
>#Hello, World!
Hello, World!
@
Infinite loop
&#*#
read a line and output without newline twice
>HHello, World!"
NOP
>H\""*H
print double quotes
Can you find bugs?