This repository has been archived on 2024-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
microgentambly/gg.py

150 lines
4.4 KiB
Python
Raw Permalink Normal View History

2021-09-28 22:35:18 +02:00
import sys
import re
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
ag = lines[i].split(" ")
2021-09-29 18:48:54 +02:00
if len(ag[0]):
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:
i += 1
2021-09-28 22:35:18 +02:00
continue
if lines[i][0] == "#":
i += 1
continue
ag = lines[i].split(" ")
2021-09-28 22:35:18 +02:00
if ag[0] == "INT":
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():
n = int(ag[2])
2021-09-29 07:23:49 +02:00
else:
n = int(vars[ag[2]])
2021-09-29 07:23:49 +02:00
else:
n = 1
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():
n = int(ag[2])
2021-09-29 07:23:49 +02:00
else:
n = int(vars[ag[2]])
2021-09-29 07:23:49 +02:00
else:
n = 1
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():
m = float(ag[1])
2021-09-29 18:48:54 +02:00
else:
m = float(vars[ag[1]])
2021-09-29 18:48:54 +02:00
if str(ag[2]).isnumeric():
modTerm = float(ag[2])
2021-09-29 18:48:54 +02:00
else:
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]]
elif ag[0] == "JLE":
lastBefJmp = i
2021-09-28 22:35:18 +02:00
if str(ag[3]).isnumeric():
athr = float(ag[3])
2021-09-28 22:35:18 +02:00
else:
athr = float(vars[ag[3]])
if vars[ag[2]] <= athr:
2021-09-28 22:35:18 +02:00
i = jumps[ag[1]]
elif ag[0] == "JME":
lastBefJmp = i
2021-09-28 22:35:18 +02:00
if str(ag[3]).isnumeric():
athr = float(ag[3])
2021-09-28 22:35:18 +02:00
else:
athr = float(vars[ag[3]])
if vars[ag[2]] >= athr:
2021-09-28 22:35:18 +02:00
i = jumps[ag[1]]
elif ag[0] == "JE":
lastBefJmp = i
2021-09-29 20:29:13 +02:00
if str(ag[3]).isnumeric():
athr = float(ag[3])
2021-09-29 20:29:13 +02:00
else:
athr = float(vars[ag[3]])
if vars[ag[2]] == athr:
2021-09-29 20:29:13 +02:00
i = jumps[ag[1]]
elif ag[0] == "JL":
lastBefJmp = i
2021-09-28 22:35:18 +02:00
if str(ag[3]).isnumeric():
athr = float(ag[3])
2021-09-28 22:35:18 +02:00
else:
athr = float(vars[ag[3]])
if vars[ag[2]] < athr:
2021-09-28 22:35:18 +02:00
i = jumps[ag[1]]
elif ag[0] == "JM":
lastBefJmp = i
2021-09-28 22:35:18 +02:00
if str(ag[3]).isnumeric():
athr = float(ag[3])
2021-09-28 22:35:18 +02:00
else:
athr = vars[ag[3]]
if vars[ag[2]] > athr:
2021-09-28 22:35:18 +02:00
i = jumps[ag[1]]
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
# print(lines)