import re
type_key = ["int","char","long","short","bool"]
_key = ["if","else","const","typedef","swtch","for","while","do","and","or","xor"]
mp = {}
code = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
u = 0
def fun(text):
stack = []
c = re.compile("//.*")
text = re.sub(c, "", text)
run = 1
while run:
run = 0
for i in range(0, len(text)):
if text[i] == "/" and text[i + 1] == "*":
stack.append(i)
run = 1
elif text[i] == "*" and text[i + 1] == "/":
text = text[: stack[-1]] + text[i + 2 :]
stack.pop()
run = 1
break
run = 1
while run:
run = 0
for old, new in [
["\t", ""],
[" ", " "],
["\r\n", "\n"],
["\n\n", "\n"],
[";;", ";"],
[") {", "){"],
[")\n{", "){"],
["( )", "()"],
]:
while old in text:
text = text.replace(old, new)
run = 1
for i in "\n;+-*/()&^%<>[]|=,{}!":
text = text.replace(f" {i}", i)
text = text.replace(f"{i} ", i)
return text
def _find(m):
n = [[[it.group()], it.start(), it.end()] for it in m]
cnt = 0
for i in range(1, len(n)):
if n[cnt][2] == n[i][1]:
n[cnt][0] += n[i][0]
n[cnt][2] = n[i][2]
else:
n[(cnt := cnt + 1)] = n[i]
return n[: cnt + 1]
def meger1(text, flag):
cin = re.compile(f"{flag}[^;]*;\n")
m = re.finditer(cin, text)
if not m:
return text
n = _find(m)
for u, b, e in n[::-1]:
for j in range(0, len(u) - 1):
u[j] = u[j].replace(";\n", "")
for j in range(1, len(u)):
u[j] = u[j].replace(flag, "")
con = "".join(u)
text = text[:b] + con + text[e:]
return text
def meger2(text, flag):
c = re.compile(f'{flag}[(]"[^"]*?",[^;]*?[)];\n')
m = re.finditer(c, text)
if not m:
return text
c = re.compile(f'{flag}[(]"(?P
[^"]*?)",(?P[^;]*?)[)];\n')
n = _find(m)
for i, j, k in n[::-1]:
u = []
for item in i:
u.append(*re.findall(c, item))
greet = "".join([v[0] for v in u])
orz = ",".join([v[1] for v in u])
con = f'{flag}("{greet}",{orz});\n'
text = text[:j] + con + text[k:]
return text
def meger3(text, flag):
c = re.compile(rf"\b(?[a-zA-Z_0-9]+)=(?P[^;]*);\n")
if not m:
return text
n = _find(m)
m = []
for i, j, k in n:
u = [re.findall(c, it) for it in i]
u = [j for i in u for j in i]
for i in range(len(u) - 1):
if u[i][0] == u[i + 1][0]:
u[i] = 0
u = [list(it) for it in u if it]
for i in range(len(u) - 1):
if u[i + 1][1] == u[i][1]:
u[i][1] = u[i + 1][0]
elif u[i + 1][1] == u[i][0]:
tmp = u[i][1]
u[i][1] = u[i + 1][0]
u[i + 1][1] = tmp
m.append([[[]], j, k])
for i in range(len(u)):
m[-1][0][-1].append(u[i][0])
m[-1][0][-1].append(u[i][1])
if i + 1 < len(u):
if u[i][1] != u[i + 1][0]:
m[-1][0].append([])
for i, j, k in m[::-1]:
v = []
for orz in i:
cnt = 0
for p in range(len(orz)):
if orz[cnt] != orz[p]:
orz[(cnt := cnt + 1)] = orz[p]
v.append("=".join(orz[: cnt + 1]))
v = [f"{i};\n" for i in v]
v = "".join(v)
text = text[:j] + v + text[k:]
return text
def merge5(text):
pattern = re.compile(r"(\w+)=\1(\+|\-|\*|\/|%|&|\||\^)\s*(\w+);")
new_text = re.sub(pattern, r"\1 \2= \3;", text)
return new_text
def number(x):
if x // 53:
return number(x // 53) + code[x % 53]
return code[x % 53]
def replace1(text):
global u
cnt = [i for i in mp]
cnt = sorted(cnt, key=lambda x: len(x) * mp[x], reverse=True)
ids = []
for i in cnt:
key = number(u)
u += 1
for j in re.finditer(rf"\b{i}\b", text):
ids.append([key, j.start(), j.end()])
ids = sorted(ids, key=lambda x: -x[1])
for i, j, k in ids:
text = text[:j] + i + text[k:]
return text
def replace2(text):
global u
c = re.compile("\d*")
m = re.finditer(c,text)
cnt = [j.group() for j in m]
cnt = {j: cnt.count(j) for j in cnt}
tot = 1
for i in cnt:
key = number(u)
u += 1
cod = f"#define {key} {i}\n"
p =len(cod) + len(key) * cnt[i]
q = len(i)*cnt[i]
if p> q:
u -= 1
else:
text = text.replace(i, key)
endl = [i for i in range(len(text)) if text[i] == "\n"]
idx = endl[tot]
tot += 1
text = text[: idx + 1] + cod + text[idx + 1 :]
return text
def replace3(text):
if "#define rep" in text:
return text
c = re.compile(
r"for\(int (?P\w+)\=(?P[^;]*);(?P=i)<\=(?P[^;]*);(?P=i)\+\+\)"
)
m = re.findall(c, text)
if not m:
return text
n = []
idx = 0
for i, a, b in m:
cod = f"for(int {i}={a};{i}<={b};{i}++)"
idx = text.find(cod, idx)
n.append([i, a, b, cod, idx])
idx += 1
sl = sum([len(i) for i in n])
codeing = "#define rep(i,a,b) for(int i=a;i<=b;i++)\n"
sv = sum([len(f"rep({i},{a},{b})") for i, a, b in m])
if sl < len(codeing) + sv:
for i, a, b, cod, idx in n[::-1]:
code = f"rep({i},{a},{b})"
text = text[:idx] + code + text[idx + len(cod) :]
b = [i for i in range(len(text)) if text[i] == "\n"]
text = text[: b[1] + 1] + codeing + text[b[1] + 1 :]
return text
def replace4(text):
if "#define lep" in text:
return text
c = re.compile(
r"for\(int (?P\w+)\=(?P[^;]*);(?P=i)>\=(?P[^;]*);(?P=i)\-\-\)"
)
m = re.findall(c, text)
if not m:
return text
n = []
idx = 0
for i, a, b in m:
cod = f"for(int {i}={a};{i}>={b};{i}--)"
idx = text.find(cod, idx)
n.append([i, a, b, cod, idx])
idx += 1
sl = sum([len(cod) for _, _, _, cod, _ in n])
codeing = "#define lep(i,a,b) for(int i=a;i>=b;i--)\n"
sv = sum([len(f"lep({i},{a},{b})") for i, a, b in m])
if sl > len(code) + sv:
for i, a, b, cod, idx in n[::-1]:
code = f"lep({i},{a},{b})"
text = text[:idx] + code + text[idx + len(cod) :]
b = [i for i in range(len(text)) if text[i] == "\n"]
text = text[: b[1] + 1] + codeing + text[b[1] + 1 :]
return text
def replac