-
Notifications
You must be signed in to change notification settings - Fork 4
/
circuitparser.cpp
221 lines (200 loc) · 7.91 KB
/
circuitparser.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <regex>
#include <set>
using namespace std;
int main(int argc, char* argv[]) {
if(argc != 3){
cerr << "Usage: " << argv[0] << " <input file> <output file>" << endl;
return 1;
}
string inputFileName = argv[1];
string outputFileName = argv[2];
ifstream inputFile(inputFileName);
if (!inputFile.is_open()) {
cerr << "Unable to open input file" << endl;
return 1;
}
map<string, string> variableExpressions;
map<string, string> out_Expressions;
string line;
set<string> outOrderVariables; // Set to hold variables from OUTORDER line
// First, parse the OUTORDER line to get all the variables
bool insideOutOrderBlock = false;
int length_of_no_changed_line = 0;
while (std::getline(inputFile, line)) {
length_of_no_changed_line++;
size_t outOrderPos = line.find("OUTORDER");
if (outOrderPos != std::string::npos) {
insideOutOrderBlock = true;
line = line.substr(outOrderPos + 11); // Remove "OUTORDER" from the line
}
if (insideOutOrderBlock) {
std::stringstream ss(line); // Create a stringstream from the line
std::string variable;
while (ss >> variable) { // Read each variable from the stringstream
//remove `;` from the variable
if (variable.back() == ';') {
variable.pop_back();
}
outOrderVariables.insert(variable); // Insert the variable into the set
}
}
if (insideOutOrderBlock && line.back() == ';') {
// If we're inside the OUTORDER block and the line ends with a semicolon,
// we've reached the end of the OUTORDER block
insideOutOrderBlock = false;
break;
}
}
// print all the Order variables
// for (const auto& variable : outOrderVariables) {
// cout << variable << endl;
// }
//print how many variables are in the set
//cout << "Number of variables in the set: " << outOrderVariables.size() << endl;
// Then, use this set to parse the rest of the file
while (getline(inputFile, line)) {
size_t equalPos = line.find('=');
if (equalPos != string::npos) {
string variable = line.substr(0, equalPos);
string expression = line.substr(equalPos + 1);
expression.pop_back();
// Trim trailing spaces from variable
string tmp_var = variable;
tmp_var.erase(variable.find_last_not_of(" \n\r\t")+1);
//cout<< "variable: " << variable << endl;
if (variable.find("new_n") == 0) {
variableExpressions[variable] = expression;
}
if (outOrderVariables.find(tmp_var) != outOrderVariables.end()) { // Check if variable is in the OUTORDER set
out_Expressions[variable] = expression;
}
}
}
//print all in out_Expressions
// for (const auto& pair : variableExpressions) {
// cout << pair.first << "=" << pair.second << endl;
// }
// for (const auto& pair : out_Expressions) {
// cout <<pair.first << "=" << pair.second << endl;
// }
// for (const auto& pair : out_Expressions) {
// cout <<pair.first << "=" << pair.second << endl;
// }
//map<string, string> newExpressions; // Map to hold modified output expressions
vector<pair<string, string>> newExpressions;
for (const auto& pair : out_Expressions) {
string newExpression = pair.second;
bool replaced = true;
while (replaced) {
string oldExpression = newExpression;
size_t pos = 0;
while (pos < newExpression.length()) {
size_t startPos = newExpression.find("new_n", pos);
if (startPos == string::npos) {
break; // No more "new_n" found, exit loop
}
size_t endPos = newExpression.find('_', startPos);
if (endPos == string::npos) {
break; // Invalid format, exit loop
}
size_t secondEndPos = newExpression.find('_', endPos + 1);
if (secondEndPos == string::npos) {
break; // Invalid format, exit loop
}
string variable = newExpression.substr(startPos, secondEndPos - startPos + 1) + (" ");
if (variableExpressions.find(variable) != variableExpressions.end()) {
string tmp_var = variableExpressions[variable];
//remove the first space in tmp_var
//tmp_var.erase(tmp_var.find_last_not_of(" \n\r\t")+1);
newExpression.replace(startPos, secondEndPos - startPos + 1, "(" + variableExpressions[variable] + ")");
//newExpression.replace(startPos, secondEndPos - startPos + 1, "(" + tmp_var + ")");
} else {
pos = secondEndPos + 1; // Move to the next character
}
}
if (newExpression == oldExpression) {
replaced = false;
}
}
newExpressions.push_back(make_pair(pair.first, newExpression));
//newExpressions[pair.first] = newExpression; // Store the modified expression
}
ofstream outputFile(outputFileName);
ifstream inputFile1(inputFileName);
if (!inputFile1.is_open()) {
cerr << "Unable to open input file" << endl;
return 1;
}
getline(inputFile1, line);
outputFile << line << endl;
for (int i = 1; i < length_of_no_changed_line; ++i) {
string line;
getline(inputFile1, line);
//cout endl if meet `;`
if (line.back() == ';') {
outputFile << line << endl;
} else {
outputFile << line ;
}
}
// for all the pairs in newExpressions, if pair.second contains `!`, replace it as `! `
// for (auto& pair : newExpressions) {
// string tmp = pair.second;
// size_t pos = 0;
// while (pos < tmp.length()) {
// size_t startPos = tmp.find("!", pos);
// if (startPos == string::npos) {
// break; // No more "!" found, exit loop
// }
// tmp.replace(startPos, 1, "! ");
// pos = startPos + 2;
// }
// pos = 0;
// while (pos < tmp.length()) {
// size_t startPos = tmp.find("( ", pos);
// if (startPos == string::npos) {
// break; // No more "( " found, exit loop
// }
// tmp.replace(startPos, 2, "(");
// pos = startPos + 1;
// }
// newExpressions[pair.first] = tmp;
// }
for (auto& pair : newExpressions) {
string tmp = pair.second;
size_t pos = 0;
while (pos < tmp.length()) {
size_t startPos = tmp.find("!", pos);
if (startPos == string::npos) {
break; // No more "!" found, exit loop
}
tmp.replace(startPos, 1, "! ");
pos = startPos + 2;
}
pos = 0;
while (pos < tmp.length()) {
size_t startPos = tmp.find("( ", pos);
if (startPos == string::npos) {
break; // No more "( " found, exit loop
}
tmp.replace(startPos, 2, "(");
pos = startPos + 1;
}
pair.second = tmp;
}
// Write output expressions for each po
for (const auto& pair : newExpressions) {
outputFile << pair.first << " = (" << pair.second << ");" << endl;
}
// Write output expressions for each po
// for (const auto& pair : newExpressions) {
// outputFile << pair.first << " = (" << pair.second << ");" << endl;
// }
outputFile.close();
inputFile1.close();
return 0;
}