2021-09-28 22:35:18 +02:00
|
|
|
import sys
|
|
|
|
import re
|
2021-11-15 21:21:44 +01:00
|
|
|
import math
|
|
|
|
vars = {}
|
|
|
|
lines = open(sys.argv[1], "r").read().split("\n")
|
|
|
|
lines = list(map(lambda el: el.lstrip(), lines))
|
|
|
|
i = 0
|
|
|
|
jumps = {}
|
|
|
|
lastBefJmp = 0
|
|
|
|
|
|
|
|
|
|
|
|
def sostT(text, vars):
|
|
|
|
matches = re.findall(r"(?<=\$)(.*)(?=\$)", text)
|
|
|
|
for match in matches:
|
|
|
|
if match in vars.keys():
|
|
|
|
text = text.replace("$"+match+"$", str(vars[match]))
|
|
|
|
return text
|
|
|
|
def cast(to,val):
|
|
|
|
if to == "INT":
|
|
|
|
return int(val)
|
|
|
|
elif to == "DEC":
|
|
|
|
return float(val)
|
|
|
|
elif to == "LST":
|
|
|
|
return list(val)
|
|
|
|
elif to == "STR":
|
|
|
|
return str(val)
|
|
|
|
|
|
|
|
|
2021-09-28 22:35:18 +02:00
|
|
|
try:
|
2021-09-29 18:48:54 +02:00
|
|
|
for i in range(len(lines)):
|
2021-09-29 20:29:13 +02:00
|
|
|
if len(lines[i].strip()) == 0:
|
|
|
|
continue
|
2021-11-15 21:21:44 +01:00
|
|
|
ag = lines[i].split(" ")
|
2021-09-29 18:48:54 +02:00
|
|
|
if len(ag[0]):
|
2021-11-15 21:21:44 +01:00
|
|
|
if ag[0][0] == ":":
|
|
|
|
jumps[ag[0][1:]] = i
|
|
|
|
i = 0
|
|
|
|
while lines[i].strip() != "END" or i > len(lines):
|
2021-09-29 20:29:13 +02:00
|
|
|
if len(lines[i].strip()) == 0:
|
2021-11-15 21:21:44 +01:00
|
|
|
i += 1
|
2021-09-28 22:35:18 +02:00
|
|
|
continue
|
2021-11-15 21:21:44 +01:00
|
|
|
if lines[i][0] == "#":
|
|
|
|
i += 1
|
|
|
|
continue
|
|
|
|
ag = lines[i].split(" ")
|
2021-09-28 22:35:18 +02:00
|
|
|
if ag[0] == "INT":
|
2021-11-15 21:21:44 +01:00
|
|
|
vars[ag[1]] = 0
|
|
|
|
elif ag[0] == "SET":
|
|
|
|
vars[ag[1]] = int(ag[2])
|
|
|
|
elif ag[0] == "DEC":
|
|
|
|
if len(ag) == 3:
|
2021-09-29 18:48:54 +02:00
|
|
|
if ag[2].isnumeric():
|
2021-11-15 21:21:44 +01:00
|
|
|
n = int(ag[2])
|
2021-09-29 07:23:49 +02:00
|
|
|
else:
|
2021-11-15 21:21:44 +01:00
|
|
|
n = int(vars[ag[2]])
|
2021-09-29 07:23:49 +02:00
|
|
|
else:
|
|
|
|
n = 1
|
2021-11-15 21:21:44 +01:00
|
|
|
vars[ag[1]] = int(vars[ag[1]])-n
|
|
|
|
elif ag[0] == "INC":
|
|
|
|
if len(ag) == 3:
|
2021-09-29 18:48:54 +02:00
|
|
|
if ag[2].isnumeric():
|
2021-11-15 21:21:44 +01:00
|
|
|
n = int(ag[2])
|
2021-09-29 07:23:49 +02:00
|
|
|
else:
|
2021-11-15 21:21:44 +01:00
|
|
|
n = int(vars[ag[2]])
|
2021-09-29 07:23:49 +02:00
|
|
|
else:
|
|
|
|
n = 1
|
2021-11-15 21:21:44 +01:00
|
|
|
vars[ag[1]] = int(vars[ag[1]])+n
|
|
|
|
elif ag[0] == "CP":
|
|
|
|
vars[ag[1]] = vars[ag[2]]
|
|
|
|
elif ag[0] == "MOD":
|
2021-09-29 18:48:54 +02:00
|
|
|
if str(ag[1]).isnumeric():
|
2021-11-15 21:21:44 +01:00
|
|
|
m = float(ag[1])
|
2021-09-29 18:48:54 +02:00
|
|
|
else:
|
2021-11-15 21:21:44 +01:00
|
|
|
m = float(vars[ag[1]])
|
2021-09-29 18:48:54 +02:00
|
|
|
if str(ag[2]).isnumeric():
|
2021-11-15 21:21:44 +01:00
|
|
|
modTerm = float(ag[2])
|
2021-09-29 18:48:54 +02:00
|
|
|
else:
|
2021-11-15 21:21:44 +01:00
|
|
|
modTerm = float(vars[ag[2]])
|
|
|
|
vars[ag[3]] = m % modTerm
|
|
|
|
elif ag[0] == "JMP":
|
|
|
|
lastBefJmp = i
|
2021-09-29 20:29:13 +02:00
|
|
|
i = jumps[ag[1]]
|
2021-11-15 21:21:44 +01:00
|
|
|
elif ag[0] == "JLE":
|
|
|
|
lastBefJmp = i
|
2021-09-28 22:35:18 +02:00
|
|
|
if str(ag[3]).isnumeric():
|
2021-11-15 21:21:44 +01:00
|
|
|
athr = float(ag[3])
|
2021-09-28 22:35:18 +02:00
|
|
|
else:
|
2021-11-15 21:21:44 +01:00
|
|
|
athr = float(vars[ag[3]])
|
|
|
|
if vars[ag[2]] <= athr:
|
2021-09-28 22:35:18 +02:00
|
|
|
i = jumps[ag[1]]
|
2021-11-15 21:21:44 +01:00
|
|
|
elif ag[0] == "JME":
|
|
|
|
lastBefJmp = i
|
2021-09-28 22:35:18 +02:00
|
|
|
if str(ag[3]).isnumeric():
|
2021-11-15 21:21:44 +01:00
|
|
|
athr = float(ag[3])
|
2021-09-28 22:35:18 +02:00
|
|
|
else:
|
2021-11-15 21:21:44 +01:00
|
|
|
athr = float(vars[ag[3]])
|
|
|
|
if vars[ag[2]] >= athr:
|
2021-09-28 22:35:18 +02:00
|
|
|
i = jumps[ag[1]]
|
2021-11-15 21:21:44 +01:00
|
|
|
elif ag[0] == "JE":
|
|
|
|
lastBefJmp = i
|
2021-09-29 20:29:13 +02:00
|
|
|
if str(ag[3]).isnumeric():
|
2021-11-15 21:21:44 +01:00
|
|
|
athr = float(ag[3])
|
2021-09-29 20:29:13 +02:00
|
|
|
else:
|
2021-11-15 21:21:44 +01:00
|
|
|
athr = float(vars[ag[3]])
|
|
|
|
if vars[ag[2]] == athr:
|
2021-09-29 20:29:13 +02:00
|
|
|
i = jumps[ag[1]]
|
2021-11-15 21:21:44 +01:00
|
|
|
elif ag[0] == "JL":
|
|
|
|
lastBefJmp = i
|
2021-09-28 22:35:18 +02:00
|
|
|
if str(ag[3]).isnumeric():
|
2021-11-15 21:21:44 +01:00
|
|
|
athr = float(ag[3])
|
2021-09-28 22:35:18 +02:00
|
|
|
else:
|
2021-11-15 21:21:44 +01:00
|
|
|
athr = float(vars[ag[3]])
|
|
|
|
if vars[ag[2]] < athr:
|
2021-09-28 22:35:18 +02:00
|
|
|
i = jumps[ag[1]]
|
2021-11-15 21:21:44 +01:00
|
|
|
elif ag[0] == "JM":
|
|
|
|
lastBefJmp = i
|
2021-09-28 22:35:18 +02:00
|
|
|
if str(ag[3]).isnumeric():
|
2021-11-15 21:21:44 +01:00
|
|
|
athr = float(ag[3])
|
2021-09-28 22:35:18 +02:00
|
|
|
else:
|
2021-11-15 21:21:44 +01:00
|
|
|
athr = vars[ag[3]]
|
|
|
|
if vars[ag[2]] > athr:
|
2021-09-28 22:35:18 +02:00
|
|
|
i = jumps[ag[1]]
|
2021-11-15 21:21:44 +01:00
|
|
|
elif ag[0] == "OUT":
|
|
|
|
print(sostT(" ".join(ag[1:]), vars).replace("\\n", "\n"), end='')
|
|
|
|
elif ag[0] == "IN":
|
|
|
|
print(sostT(" ".join(ag[3:]), vars).replace("\\n", "\n"), end='')
|
|
|
|
vars[ag[2]] = int(input(""))
|
|
|
|
elif ag[0] == "RET":
|
|
|
|
i = lastBefJmp
|
|
|
|
elif ag[0] == "ADD":
|
|
|
|
vars[ag[3]] = vars[ag[1]] + vars[ag[2]]
|
|
|
|
elif ag[0] == "SUB":
|
|
|
|
vars[ag[3]] = vars[ag[1]] - vars[ag[2]]
|
|
|
|
elif ag[0] == "MUL":
|
|
|
|
vars[ag[3]] = vars[ag[1]] * vars[ag[2]]
|
|
|
|
elif ag[0] == "DIV":
|
|
|
|
vars[ag[3]] = vars[ag[1]] / vars[ag[2]]
|
|
|
|
elif ag[0] == "EL":
|
|
|
|
vars[ag[3]] = vars[ag[1]] ** vars[ag[2]]
|
|
|
|
elif ag[0] == "FLO":
|
|
|
|
vars[ag[2]] = int(vars[ag[1]])
|
|
|
|
elif ag[0] == "CEL":
|
|
|
|
vars[ag[2]] = math.ceil(vars[ag[1]])
|
|
|
|
i += 1
|
2021-09-28 22:35:18 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(vars)
|
|
|
|
print(jumps)
|
|
|
|
raise e
|
2021-11-15 21:21:44 +01:00
|
|
|
# print(lines)
|