-
Notifications
You must be signed in to change notification settings - Fork 1
/
codeoptimization_new.py
134 lines (120 loc) · 4.15 KB
/
codeoptimization_new.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
table = {}
f = []
while True:
try:
f.append(input())
except:
break
f = f[1:]
for line in f:
if("=" in line):
lst = [x.strip() for x in line.split(" = ")]
x=line.split("=")
lst=[y.strip() for y in x]
#print(lst)
lst1 = lst[1].split(" ")
#print(lst1)
#a = 1
if(lst1[0].isnumeric() and len(lst1) == 1 and (lst[0].isalpha())):
table[lst[0]] = int(lst1[0])
print(lst[0], "=", table[lst[0]])
#print(table)
#i = t1
elif(len(lst1) == 1 and (lst[0] not in table) and (lst[0].isalpha()) and (not lst1[0].isnumeric()) and (lst1[0] in table) ):
table[lst[0]] = table[lst1[0]]
print(lst[0], "=", table[lst[0]])
#del table[lst1[0]]
#t2 = t1
elif(len(lst1) == 1 and (not lst1[0].isnumeric()) and (lst1[0] in table) and (not lst[0].isnumeric()) and (lst[0] not in table) ):
table[lst[0]] = table[lst1[0]]
print(lst[0], "=", table[lst[0]])
#del table[lst1[0]]
#print(table)
# t1 = a + 5
elif(lst1[0].isalpha and len(lst1) > 1 and lst1[0] in table and lst1[2].isnumeric() ):
value = table[lst1[0]]
lst[1] = str(value) + " " + lst1[1] + " " + lst1[2]
table[lst[0]] = eval(lst[1])
print(lst[0] ,"=", table[lst[0]])
#print(table)
# t1 = 5 + a
elif(len(lst1) > 1 and lst1[2].isalpha and lst1[2] in table and lst1[0].isnumeric()):
value = table[lst1[2]]
lst[1] = lst1[0] + lst1[1] + str(value)
table[lst[0]] = eval(lst[1])
print(lst[0] ,"=", table[lst[0]])
#print(table)
#t1 = 2 + 2
elif(len(lst1) > 1 and lst1[0].isnumeric() and lst1[2].isnumeric()):
table[lst[0]] = eval(lst[1])
print(lst[0] ,"=", table[lst[0]])
#print(table)
#t3 = t1 + t2
elif((not lst1[0].isnumeric()) and len(lst1) > 1 and (not (lst1[2].isnumeric())) and (lst1[0] in table) and (lst1[2] in table)):
value1 = table[lst1[0]]
value2 = table[lst1[2]]
if(lst1[1] == "&&"):
lst[1] = str(value1) + " and " + str(value2)
table[lst[0]] = eval(lst[1])
print(lst[0] ,"=", table[lst[0]])
elif(lst1[1] == "||"):
lst[1] = str(value1) + " or " + str(value2)
table[lst[0]] = eval(lst[1])
print(lst[0] ,"=", table[lst[0]])
elif("<" in lst1[1] or ">" in lst1[1] or "<=" in lst1[1] or ">=" in lst1[1] or "==" in lst1[1]):
lst[1] = str(value1) + " " + lst1[1] + " " + str(value2)
table[lst[0]] = eval(lst[1])
print(lst[0] ,"=", table[lst[0]])
else:
lst[1] = str(value1) + " " + lst1[1] + " " + str(value2)
#print(lst[1])
table[lst[0]] = eval(lst[1])
print(lst[0] ,"=", table[lst[0]])
#print(table)
# t5 = t1 + 5
elif((not lst1[0].isnumeric()) and len(lst1) > 1 and (lst1[2].isnumeric()) and (lst1[0] in table)):
print(1)
value = table[lst1[0]]
if(lst1[1] == "&&" ):
lst[1] = str(value) + " and " + lst1[2]
table[lst[0]] = eval(lst[1])
print(lst[0] ,"=", table[lst[0]])
elif(lst1[1] == "||"):
lst[1] = str(value) + " or " + lst1[2]
table[lst[0]] = eval(lst[1])
print(lst[0] ,"=", table[lst[0]])
elif("<" in lst1[1] or ">" in lst1[1] or "<=" in lst1[1] or ">=" in lst1[1] or "==" in lst1[1]):
lst[1] = str(value) + " " + lst1[1] + " " + lst1[2]
table[lst[0]] = eval(lst[1])
print(lst[0] ,"=", table[lst[0]])
else:
lst[1] = str(value) + " " + lst1[1] + " " + lst1[2]
table[lst[0]] = eval(lst[1])
print(lst[0] ,"=", table[lst[0]])
#print(table)
# t5 = 5 + t1
elif(len(lst1) > 1 and (not lst1[2].isnumeric()) and (lst1[0].isnumeric()) and (lst1[2] in table)):
value = table[lst1[2]]
if("&&" in lst1[1] ):
lst[1] = lst1[0] + " and "+ str(value)
table[lst[0]] = eval(lst[1])
print(lst[0] ,"=", table[lst[0]])
elif("||" in lst1[1]):
lst[1] = lst1[0] + " or "+ str(value)
table[lst[0]] = eval(lst[1])
print(lst[0] ,"=", table[lst[0]])
elif("<" in lst1[1] or ">" in lst1[1] or "<=" in lst1[1] or ">=" in lst1[1] or "==" in lst1[1]):
lst[1] = lst1[0] + lst1[1] + str(value)
table[lst[0]] = eval(lst[1])
print(lst[0] ,"=", table[lst[0]])
else:
lst[1] = lst1[0] + lst1[1] + str(value)
table[lst[0]] = eval(lst[1])
print(lst[0] ,"=", table[lst[0]])
#print(table)
elif("print" in line):
print (line)
elif("goto" in line):
print(line)
elif("L" in line):
print(line)