-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreconstructor.cpp
More file actions
327 lines (313 loc) · 11.7 KB
/
reconstructor.cpp
File metadata and controls
327 lines (313 loc) · 11.7 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <cstdlib>
#include <cctype>
// Simple AST node structure.
struct Node {
std::string type;
std::string value;
std::vector<Node*> children;
};
// Helper: trim spaces from both ends.
std::string trim(const std::string &s) {
size_t start = s.find_first_not_of(" \t");
if(start == std::string::npos) return "";
size_t end = s.find_last_not_of(" \t");
return s.substr(start, end - start + 1);
}
// Parse a single line into node type and value.
// Expected format: "Identifier (x)" or "Declaration" (if no value)
void parseLine(const std::string &line, std::string &nodeType, std::string &nodeValue) {
std::string s = trim(line);
size_t pos = s.find(" (");
if(pos != std::string::npos) {
nodeType = s.substr(0, pos);
size_t end = s.find(")", pos);
if(end != std::string::npos)
nodeValue = s.substr(pos+2, end - pos - 2);
else
nodeValue = "";
} else {
nodeType = s;
nodeValue = "";
}
}
// Recursively parse AST from lines using indentation (2 spaces per level)
Node* parseAST(const std::vector<std::string> &lines, size_t &index, int currIndent) {
if (index >= lines.size())
return nullptr;
// Count indent (each level = 2 spaces)
int indent = 0;
while(indent < lines[index].size() && lines[index][indent]==' ')
indent++;
if(indent < currIndent)
return nullptr;
std::string nodeType, nodeValue;
parseLine(lines[index].substr(currIndent), nodeType, nodeValue);
Node* node = new Node{nodeType, nodeValue, {}};
index++;
// Process children: while next line indent > current indent
while(index < lines.size()) {
int nextIndent = 0;
while(nextIndent < lines[index].size() && lines[index][nextIndent]==' ')
nextIndent++;
if(nextIndent <= currIndent) break;
Node* child = parseAST(lines, index, currIndent + 2);
if(child)
node->children.push_back(child);
}
return node;
}
// Recursively generate C++ code from the AST tree.
std::string generateCode(Node* node) {
if (!node)
return "";
if(node->type == "Program") {
std::string code;
for(auto child : node->children) {
code += generateCode(child) + "\n";
}
return code;
}
else if(node->type == "Declaration") {
// expecting two children: first is the type and second is identifier or AssignmentExpr.
std::string dataType = generateCode(node->children[0]);
std::string rest = generateCode(node->children[1]);
return dataType + " " + rest + ";";
}
else if(node->type == "AssignmentExpr") {
std::string left = generateCode(node->children[0]);
std::string right = generateCode(node->children[1]);
return left + " = " + right;
}
else if(node->type == "IfStatement") {
if(node->value == "if") {
std::string condition = generateCode(node->children[0]);
std::string thenBlock = generateCode(node->children[1]);
std::string code = "if(" + condition + ") {\n" + thenBlock + "\n}";
if(node->children.size() > 2)
code += " else {\n" + generateCode(node->children[2]) + "\n}";
return code;
}
else if(node->value == "else") {
return generateCode(node->children[0]);
}
}
else if(node->type == "WhileStatement") {
std::string condition = generateCode(node->children[0]);
std::string block = generateCode(node->children[1]);
return "while(" + condition + ") {\n" + block + "\n}";
}
else if(node->type == "ExpressionStatement") {
return generateCode(node->children[0]) + ";";
}
else if(node->type == "BinaryExpr") {
std::string left = generateCode(node->children[0]);
std::string right = generateCode(node->children[1]);
if(node->value == "<<" || node->value == ">>")
return left + node->value + right; // no extra spaces for insert/extract operators
else
return left + " " + node->value + " " + right;
}
else if(node->type == "FunctionCall" ) {
std::string code = node->value + "(";
for(size_t i = 0; i < node->children.size(); i++) {
code += generateCode(node->children[i]);
if(i < node->children.size()-1)
code += ", ";
}
code += ")";
return code;
}
else if(node->type == "CustomFunction") {
if(node->value == "max_of_three") {
std::string x = generateCode(node->children[0]);
std::string y = generateCode(node->children[1]);
std::string z = generateCode(node->children[2]);
return "([=]() {\n"
" int temp_max;\n"
" if(" + x + " > " + y + " && " + x + " > " + z + ") {\n"
" temp_max = " + x + ";\n"
" } else if(" + y + " > " + z + ") {\n"
" temp_max = " + y + ";\n"
" } else {\n"
" temp_max = " + z + ";\n"
" }\n"
" return temp_max;\n"
"}())";
}
else if(node->value == "min") {
std::string a = generateCode(node->children[0]);
std::string b = generateCode(node->children[1]);
return "(( " + a + " < " + b + " ) ? " + a + " : " + b + ")";
}
else if(node->value == "power") {
std::string base = generateCode(node->children[0]);
std::string exp = generateCode(node->children[1]);
return "([=]() {\n"
" int result = 1;\n"
" int b = " + base + ";\n"
" int e = " + exp + ";\n"
" while(e > 0) {\n"
" if(e % 2 == 1) result *= b;\n"
" b *= b;\n"
" e /= 2;\n"
" }\n"
" return result;\n"
"}())";
}
else if(node->value == "sqrt") {
std::string arg = generateCode(node->children[0]);
return "sqrt(" + arg + ")";
}
else if(node->value == "max") {
std::string a = generateCode(node->children[0]);
std::string b = generateCode(node->children[1]);
return "(( " + a + " > " + b + " ) ? " + a + " : " + b + ")";
}
else if(node->value == "gcd") {
std::string a = generateCode(node->children[0]);
std::string b = generateCode(node->children[1]);
return "([=]() {\n"
" int x = " + a + ", y = " + b + ";\n"
" while(y != 0) {\n"
" int temp = y;\n"
" y = x % y;\n"
" x = temp;\n"
" }\n"
" return x;\n"
"}())";
}
else if(node->value == "fast_exp") {
std::string base = generateCode(node->children[0]);
std::string exp = generateCode(node->children[1]);
return "([=]() {\n"
" int result = 1;\n"
" int b = " + base + ";\n"
" int e = " + exp + ";\n"
" while(e > 0) {\n"
" if(e & 1) result *= b;\n"
" b *= b;\n"
" e >>= 1;\n"
" }\n"
" return result;\n"
"}())";
}
else if(node->value == "lcm") {
std::string a = generateCode(node->children[0]);
std::string b = generateCode(node->children[1]);
return "(( " + a + " * " + b + " ) / ( [=]() {\n"
" int x = " + a + ", y = " + b + ";\n"
" while(y != 0) {\n"
" int temp = y;\n"
" y = x % y;\n"
" x = temp;\n"
" }\n"
" return x;\n"
"}() ))";
}
else if(node->value == "binary_search") {
std::string code = node->value + "(";
for(size_t i = 0; i < node->children.size(); i++) {
code += generateCode(node->children[i]);
if(i < node->children.size() - 1)
code += ", ";
}
code += ")";
return code;
}
else if(node->value == "factorial") {
std::string arg = generateCode(node->children[0]);
return "([=]() {\n"
" int n = " + arg + ";\n"
" int result = 1;\n"
" for(int i = 1; i <= n; i++) result *= i;\n"
" return result;\n"
"}())";
}
else if(node->value == "sieve") {
std::string arg = generateCode(node->children[0]);
return "sieve(" + arg + ")";
}
else {
std::string code = node->value + "(";
for(size_t i = 0; i < node->children.size(); i++) {
code += generateCode(node->children[i]);
if(i < node->children.size() - 1)
code += ", ";
}
code += ")";
return code;
}
}
else if(node->type == "FunctionDeclaration") {
// children: [0]: return type, [1]: parameter list, [2]: body
std::string returnType = generateCode(node->children[0]);
std::string parameters = generateCode(node->children[1]);
std::string body = generateCode(node->children[2]);
if(node->children[2]->value == "Block") {
body = "{\n" + body + "\n}";
}
return returnType + " " + node->value + "(" + parameters + ") " + body;
}
else if(node->type == "Literal") {
// New handling for directives.
if(node->value == "#include") {
return "#include <" + generateCode(node->children[0]) + ">";
}
else if(node->value == "using namespace") {
return "using namespace " + generateCode(node->children[0]) + ";";
}
else {
return node->value;
}
}
else if(node->type == "Identifier") {
return node->value;
}
// Fallback.
return node->value;
}
void freeTree(Node* node) {
if(!node) return;
for(auto child : node->children)
freeTree(child);
delete node;
}
int main() {
// Read the entire parseroutput.txt file line-by-line.
std::ifstream inFile("parseroutput.txt");
if(!inFile) {
std::cerr << "Unable to open parseroutput.txt" << std::endl;
return EXIT_FAILURE;
}
std::vector<std::string> lines;
std::string line;
while(std::getline(inFile, line)) {
if(!line.empty())
lines.push_back(line);
}
inFile.close();
size_t index = 0;
Node* root = parseAST(lines, index, 0);
if (!root) {
std::cerr << "Error parsing AST" << std::endl;
return EXIT_FAILURE;
}
// Generate C++ code from AST.
std::string cppCode = generateCode(root);
freeTree(root);
// Write the generated code to reconstructor.txt.
std::ofstream outFile("reconstructor.txt");
if (!outFile) {
std::cerr << "Unable to open reconstructor.txt for writing." << std::endl;
return EXIT_FAILURE;
}
outFile << "// Generated C++ Code\n" << cppCode;
outFile.close();
std::cout << "Reconstruction completed. C++ code written to reconstructor.txt" << std::endl;
return EXIT_SUCCESS;
}