-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiSimplifier.cpp
98 lines (95 loc) · 3.73 KB
/
multiSimplifier.cpp
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
#include "compiler.h"
void multiSimplifier() {
for (auto &inter:intermediateCodeInner) {
if (inter.size() == 5) {
function f0;
varType ty2 = whatIsThisShit(inter.at(2), f0);
varType ty4 = whatIsThisShit(inter.at(4), f0);
if (ty2 == INTEGER && ty4 == INTEGER && inter.at(3).at(0) == '*') {
int one = stoi(inter.at(2));
int two = stoi(inter.at(4));
int ans = one * two;
inter.pop_back();
inter.pop_back();
inter.pop_back();
inter.emplace_back(to_string(ans));
} else if (ty2 == INTEGER && ty4 == INTEGER && inter.at(3).at(0) == '/') {
int one = stoi(inter.at(2));
int two = stoi(inter.at(4));
int ans = one / two;
inter.pop_back();
inter.pop_back();
inter.pop_back();
inter.emplace_back(to_string(ans));
} else if (((ty2 == INTEGER && stoi(inter.at(2)) == 0)
|| (ty4 == INTEGER && stoi(inter.at(4)) == 0))
&& inter.at(3).at(0) == '*') {
int ans = 0;
inter.pop_back();
inter.pop_back();
inter.pop_back();
inter.emplace_back(to_string(ans));
} else if (ty2 == INTEGER && stoi(inter.at(2)) == 0 && inter.at(3).at(0) == '/') {
int ans = 0;
inter.pop_back();
inter.pop_back();
inter.pop_back();
inter.emplace_back(to_string(ans));
} else {
if (whatIsThisShit(inter.at(4), f0) == INTEGER
&& inter.at(3).at(0) == '*') {
int value = stoi(inter.at(4));
if (value >= 0) {
if (!(value & (value - 1)) && value != 0) {
value = log2(value);
inter.pop_back();
inter.pop_back();
inter.emplace_back("<<");
inter.emplace_back(to_string(value));
}
} else {
int v2 = -value;
if (!(v2 & (v2 - 1))) {
v2 = log2(v2);
inter.pop_back();
inter.pop_back();
inter.emplace_back("<<");
inter.emplace_back("-" + to_string(v2));
}
}
}
if (whatIsThisShit(inter.at(4), f0) == INTEGER
&& inter.at(3).at(0) == '/') {
int value = stoi(inter.at(4));
if (value > 0) {
if (!(value & (value - 1))) {
value = log2(value);
inter.pop_back();
inter.pop_back();
inter.emplace_back(">>");
inter.emplace_back(to_string(value));
}
} else {
int v2 = -value;
if (!(v2 & (v2 - 1))) {
v2 = log2(v2);
inter.pop_back();
inter.pop_back();
inter.emplace_back(">>");
inter.emplace_back("-" + to_string(v2));
}
}
}
}
}
}
}
int log2(int value) {
int v = value;
int x = 0;
while (v > 1) {
v >>= 1;
x++;
}
return x;
}