-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver_generator.cpp
More file actions
283 lines (254 loc) · 12.1 KB
/
Copy pathdriver_generator.cpp
File metadata and controls
283 lines (254 loc) · 12.1 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
#include "driver_generator.h"
MethodSchema MethodSchema::fromJson(const QJsonObject& obj) {
MethodSchema schema;
schema.name = obj["name"].toString();
schema.returnType = obj["returnType"].toString();
QJsonArray paramsArray = obj["params"].toArray();
for (int i = 0; i < paramsArray.size(); ++i) {
QJsonObject pObj = paramsArray[i].toObject();
schema.params.append({pObj["name"].toString(), pObj["type"].toString()});
}
return schema;
}
// Map schema types to C types
static QString toCType(const QString& type) {
if (type == "int") return "int";
if (type == "double") return "double";
if (type == "string") return "char*";
if (type.startsWith("vector<")) return "int*"; // int arrays via int*
return "int";
}
QString DriverGenerator::generateTemplate(const MethodSchema& schema, const QString& languageId) {
QString lang = languageId.toLower();
if (lang == "c" || lang == "cpp") {
// Pure C99 — TCC is a C-only compiler. No classes, no std::, no templates.
QString paramsStr;
for (int i = 0; i < schema.params.size(); ++i) {
if (i > 0) paramsStr += ", ";
const auto& p = schema.params[i];
if (p.type.startsWith("vector<")) {
paramsStr += "int* " + p.name + ", int " + p.name + "_size";
} else {
paramsStr += toCType(p.type) + " " + p.name;
}
}
QString tpl = "#include <stdio.h>\n#include <stdlib.h>\n\n";
tpl += toCType(schema.returnType) + " " + schema.name + "(" + paramsStr + ") {\n";
tpl += " /* Your code here */\n";
if (schema.returnType == "int") tpl += " return 0;\n";
else if (schema.returnType == "double") tpl += " return 0.0;\n";
tpl += "}\n";
return tpl;
}
else if (lang == "python") {
QString paramsStr = "self";
for (const auto& p : schema.params) {
QString type = p.type;
if (type == "int") type = "int";
else if (type == "vector<int>") type = "List[int]";
else if (type == "string") type = "str";
else if (type == "vector<string>") type = "List[str]";
paramsStr += ", " + p.name + ": " + type;
}
QString pyReturn = schema.returnType;
if (pyReturn == "vector<int>") pyReturn = "List[int]";
else if (pyReturn == "vector<string>") pyReturn = "List[str]";
QString tpl = "from typing import List\n\nclass Solution:\n";
tpl += " def " + schema.name + "(" + paramsStr + ") -> " + pyReturn + ":\n";
tpl += " # Your code here\n";
tpl += " pass\n";
return tpl;
}
else if (lang == "javascript" || lang == "js") {
QString paramsStr;
for (int i = 0; i < schema.params.size(); ++i) {
if (i > 0) paramsStr += ", ";
paramsStr += schema.params[i].name;
}
QString tpl = "/**\n";
for (const auto& p : schema.params) {
tpl += " * @param {" + p.type + "} " + p.name + "\n";
}
tpl += " * @return {" + schema.returnType + "}\n";
tpl += " */\n";
tpl += "var " + schema.name + " = function(" + paramsStr + ") {\n";
tpl += " // Your code here\n";
tpl += "\n};\n";
return tpl;
}
return "// Unsupported language template\n";
}
QString DriverGenerator::generateDriver(const MethodSchema& schema, const QString& languageId) {
if (!schema.isValid()) return "";
QString lang = languageId.toLower();
if (lang == "c" || lang == "cpp") return generateCppDriver(schema);
if (lang == "python") return generatePythonDriver(schema);
if (lang == "javascript" || lang == "js") return generateJsDriver(schema);
return "";
}
QString DriverGenerator::generateHeader(const MethodSchema& schema, const QString& languageId) {
if (!schema.isValid()) return "";
QString lang = languageId.toLower();
if (lang == "c") {
QString header = "#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\n#include <string.h>\n";
header += "\n// --- SYNTAXFLOW INJECTED DEFINITIONS ---\n";
header += "struct ListNode {\n int val;\n struct ListNode *next;\n};\n\n";
header += "struct TreeNode {\n int val;\n struct TreeNode *left;\n struct TreeNode *right;\n};\n\n";
header += "// ---------------------------------------\n\n";
return header;
} else if (lang == "cpp" || lang == "c++") {
QString header = "#include <iostream>\n#include <vector>\n#include <string>\n#include <algorithm>\n";
header += "\nusing namespace std;\n";
header += "\n// --- SYNTAXFLOW INJECTED DEFINITIONS ---\n";
header += "struct ListNode {\n int val;\n ListNode *next;\n";
header += " ListNode() : val(0), next(nullptr) {}\n";
header += " ListNode(int x) : val(x), next(nullptr) {}\n";
header += " ListNode(int x, ListNode *next) : val(x), next(next) {}\n};\n\n";
header += "struct TreeNode {\n int val;\n TreeNode *left;\n TreeNode *right;\n";
header += " TreeNode() : val(0), left(nullptr), right(nullptr) {}\n";
header += " TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n";
header += " TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n};\n\n";
header += "// ---------------------------------------\n\n";
return header;
}
return "";
}
QString DriverGenerator::generateCppDriver(const MethodSchema& schema) {
// Pure C99 driver — TCC is a C compiler only.
// Entry point is _sf_main (not main) so TccRunner can use tcc_get_symbol
// without triggering tcc_run()'s internal libtcc1.a loading crash.
QString out = "\n/* --- SYNTAXFLOW DRIVER --- */\n";
out += "int main() {\n";
QString callArgs;
for (int i = 0; i < schema.params.size(); ++i) {
const auto& p = schema.params[i];
if (i > 0) callArgs += ", ";
if (p.type == "int") {
out += " int _" + p.name + ";\n";
out += " scanf(\"%d\", &_" + p.name + ");\n";
callArgs += "_" + p.name;
} else if (p.type == "double") {
out += " double _" + p.name + ";\n";
out += " scanf(\"%lf\", &_" + p.name + ");\n";
callArgs += "_" + p.name;
} else if (p.type.startsWith("vector<")) {
out += " int _size_" + p.name + ";\n";
out += " scanf(\"%d\", &_size_" + p.name + ");\n";
out += " int* _" + p.name + " = (int*)malloc(_size_" + p.name + " * sizeof(int));\n";
out += " for(int i=0; i<_size_" + p.name + "; ++i) scanf(\"%d\", &_" + p.name + "[i]);\n";
callArgs += "_" + p.name + ", _size_" + p.name;
}
}
if (schema.returnType == "void") {
out += " " + schema.name + "(" + callArgs + ");\n";
} else if (schema.returnType == "int") {
out += " int _result = " + schema.name + "(" + callArgs + ");\n";
out += " printf(\"%d\\n\", _result);\n";
} else if (schema.returnType == "double") {
out += " double _result = " + schema.name + "(" + callArgs + ");\n";
out += " printf(\"%g\\n\", _result);\n";
} else if (schema.returnType.startsWith("vector<")) {
out += " int _result_size = 0;\n";
out += " int* _result = " + schema.name + "(" + callArgs + ", &_result_size);\n";
out += " for(int i=0; i<_result_size; ++i) {\n";
out += " if(i>0) printf(\" \");\n";
out += " printf(\"%d\", _result[i]);\n";
out += " }\n";
out += " printf(\"\\n\");\n";
}
out += " return 0;\n}\n";
return out;
}
QString DriverGenerator::generatePythonDriver(const MethodSchema& schema) {
QString out = "\n# --- SYNTAXFLOW DRIVER WRAPPER ---\n";
out += "if __name__ == '__main__':\n";
out += " import sys\n";
out += " _tokens = sys.stdin.read().split()\n";
out += " _tok_idx = 0\n";
out += " def _next_token():\n";
out += " global _tok_idx\n";
out += " if _tok_idx >= len(_tokens): return ''\n";
out += " res = _tokens[_tok_idx]\n";
out += " _tok_idx += 1\n";
out += " return res\n\n";
QString callArgs;
for (int i = 0; i < schema.params.size(); ++i) {
const auto& p = schema.params[i];
if (i > 0) callArgs += ", ";
callArgs += "_" + p.name;
if (p.type == "int") {
out += " _" + p.name + " = int(_next_token())\n";
} else if (p.type == "double") {
out += " _" + p.name + " = float(_next_token())\n";
} else if (p.type == "string") {
out += " _" + p.name + " = _next_token()\n";
} else if (p.type.startsWith("vector<")) {
QString baseTypeStr = p.type.mid(7, p.type.length() - 8);
QString castFunc = (baseTypeStr == "int") ? "int" : ((baseTypeStr == "double") ? "float" : "str");
out += " _size_" + p.name + " = int(_next_token())\n";
out += " _" + p.name + " = [" + castFunc + "(_next_token()) for _ in range(_size_" + p.name + ")]\n";
}
}
out += " _sol = Solution()\n";
if (schema.returnType == "void") {
out += " _sol." + schema.name + "(" + callArgs + ")\n";
} else {
out += " _result = _sol." + schema.name + "(" + callArgs + ")\n";
if (schema.returnType == "int" || schema.returnType == "double" || schema.returnType == "string") {
out += " print(_result)\n";
} else if (schema.returnType.startsWith("vector<")) {
out += " print(' '.join(map(str, _result)))\n";
}
}
return out;
}
QString DriverGenerator::generateJsDriver(const MethodSchema& schema) {
QString out = "\n// --- SYNTAXFLOW DRIVER WRAPPER ---\n";
out += "(function() {\n";
out += " let _inputStr = '';\n";
out += " while (true) {\n";
out += " let t = readline();\n";
out += " if (t === null) break;\n";
out += " _inputStr += t + ' ';\n";
out += " }\n";
out += " let _tokens = _inputStr.trim().split(/\\s+/);\n";
out += " let _tok_idx = 0;\n";
out += " function _nextToken() { return _tok_idx < _tokens.length ? _tokens[_tok_idx++] : ''; }\n\n";
QString callArgs;
for (int i = 0; i < schema.params.size(); ++i) {
const auto& p = schema.params[i];
if (i > 0) callArgs += ", ";
callArgs += "_" + p.name;
if (p.type == "int") {
out += " let _" + p.name + " = parseInt(_nextToken());\n";
} else if (p.type == "double") {
out += " let _" + p.name + " = parseFloat(_nextToken());\n";
} else if (p.type == "string") {
out += " let _" + p.name + " = _nextToken();\n";
} else if (p.type.startsWith("vector<")) {
QString baseTypeStr = p.type.mid(7, p.type.length() - 8);
QString castFunc = (baseTypeStr == "int") ? "parseInt" : ((baseTypeStr == "double") ? "parseFloat" : "");
out += " let _size_" + p.name + " = parseInt(_nextToken());\n";
out += " let _" + p.name + " = [];\n";
out += " for(let i=0; i<_size_" + p.name + "; ++i) {\n";
if (castFunc.isEmpty()) {
out += " _" + p.name + ".push(_nextToken());\n";
} else {
out += " _" + p.name + ".push(" + castFunc + "(_nextToken()));\n";
}
out += " }\n";
}
}
if (schema.returnType == "void") {
out += " " + schema.name + "(" + callArgs + ");\n";
} else {
out += " let _result = " + schema.name + "(" + callArgs + ");\n";
if (schema.returnType == "int" || schema.returnType == "double" || schema.returnType == "string") {
out += " print(_result);\n";
} else if (schema.returnType.startsWith("vector<")) {
out += " print(_result.join(' '));\n";
}
}
out += "})();\n";
return out;
}