-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_generator.cpp
More file actions
executable file
·336 lines (335 loc) · 18.2 KB
/
Copy pathfunction_generator.cpp
File metadata and controls
executable file
·336 lines (335 loc) · 18.2 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
328
329
330
331
332
333
334
335
336
/**
* @author 2024 (c) LittleYang0531
* @attention 目前该程序只支持最大一层 group,如果后面官方出了很多层的 group 再写
* @attention 还要注意官方的变量定义中有没有出现 C++ 关键字,有的话请添加在 keywords 数组里
* @date 2024/12/13
* @link <https://github.com/Sonolus/runtime-metadata/blob/main/Runtime/Functions.json>
*/
#include<bits/stdc++.h>
using namespace std;
#include"include/json.h"
vector<string> keywords = {
"default"
};
set<string> modified;
map<string, string> calls;
map<string, int> callParamCnt;
int calcHash(string s) {
int64_t res = 0;
for (int i = 0; i < s.size(); i++) res *= 55331, res += s[i], res %= 998244353;
return res;
}
vector<string> explode(string seperator, string source) {
string src = source; vector<string> res;
while (src.find(seperator) != string::npos) {
int wh = src.find(seperator);
res.push_back(src.substr(0, src.find(seperator)));
src = src.substr(wh + string(seperator).size());
} res.push_back(src);
return res;
}
int main(int argc, char** argv) {
if (argc < 3) {
cout << "Usage: " << argv[0] << " Functions.json functions" << endl;
return 1;
}
ifstream fin; fin.open(argv[1]);
fin.seekg(0, ios::end);
int len = fin.tellg();
char *ch = new char[len];
fin.seekg(0, ios::beg);
fin.read(ch, len);
string s = string(ch, len);
Json::Value json = json_decode(s);
fin.close();
ofstream fout2; fout2.open("functions/.Functions.List.h");
fout2 << "/**" << endl;
fout2 << " * This file is generated by program. Do not modify this file!" << endl;
fout2 << " * Please modify \".Function.Extra.h\" instead. All the modification in this file will be cleared!" << endl;
fout2 << " */" << endl;
fout2 << endl;
fout2 << "#ifndef Functions_H" << endl;
fout2 << "#define Functions_H" << endl;
fout2 << endl;
// for (int i = 0; i < keywords.size(); i++)
// fout << "#define " << keywords[i] << " " << keywords[i] << "_value" << endl;
// fout << endl;
for (int i = 0; i < keywords.size(); i++)
fout2 << "#define " << keywords[i] << " " << keywords[i] << "_value" << endl;
fout2 << endl;
for (int i = 0; i < json.size(); i++) {
auto func = json[i];
for (int j = 0; j < func["parameters"].size(); j++) {
auto p = func["parameters"][j];
if (p["type"] == "group") {
fout2 << "struct " << func["name"].asString() << "_Group_";
for (int k = 0; k < p["items"].size(); k++) {
auto item = p["items"][k];
fout2 << item["name"].asString();
if (k != p["items"].size() - 1) fout2 << "_";
}
fout2 << "{" << endl;
for (int k = 0; k < p["items"].size(); k++) {
auto item = p["items"][k];
fout2 << "\tdouble " << item["name"].asString() << ";" << endl;
}
fout2 << "};" << endl;
}
}
// ofstream fout; fout.open("functions/" + func["name"].asString() + ".h");
stringstream ss;
ss << "#ifndef Functions_" << func["name"].asString() << "_H" << endl;
ss << "#define Functions_" << func["name"].asString() << "_H" << endl;
ss << endl;
ss << "double " << func["name"].asString() << "(";
stringstream callCommand; int pt = 0; bool isFirst = true;
callCommand << func["name"].asString() << "(";
stringstream notImplemented;
for (int j = 0; j < func["parameters"].size(); j++) {
auto p = func["parameters"][j];
notImplemented << "\tcerr";
if (p["type"] == "group") {
string argName = "";
for (int k = 0; k < p["items"].size(); k++) {
auto item = p["items"][k];
argName += item["name"].asString();
if (k != p["items"].size() - 1) argName += "_";
}
if (p.isMember("minCount")) {
ss << "const vector<" << func["name"].asString() << "_Group_" << argName << "> &" << argName;
if (isFirst) isFirst = false;
else callCommand << ", ", notImplemented << " << \", \"";
pt = -1;
// Implement manually
} else if (p.isMember("count")) {
for (int k = 0; k < p["count"].asInt(); k++)
ss << func["name"].asString() << "_Group_" << argName << " " << argName << k << (k != p["count"].asInt() - 1 ? ", " : "");
for (int k = 0; k < p["count"].asInt(); k++) {
if (isFirst) isFirst = false;
else callCommand << ", ", notImplemented << " << \", \"";
callCommand << "{ ";
for (int l = 0; l < p["items"].size(); l++) callCommand << (l ? ", " : "") << "node.values[" << pt++ << "]";
callCommand << " }";
notImplemented << " << \"" << argName << k << ": \" << \"{ \"";
for (int l = 0; l < p["items"].size(); l++)
notImplemented << (l ? " << \", \"" : "") << " << \"." << p["items"][l]["name"].asString() << " = \" << " << argName << k << "." << p["items"][l]["name"].asString();
notImplemented << " << \" }\"";
}
} else if (p.isMember("forEach")) {
for (int k = 0; k < p["forEach"].size(); k++)
ss << func["name"].asString() << "_Group_" << argName << " " << p["forEach"][k].asString() << (k != p["forEach"].size() - 1 ? ", " : "");
for (int k = 0; k < p["forEach"].size(); k++) {
if (isFirst) isFirst = false;
else callCommand << ", ", notImplemented << " << \", \"";
callCommand << "{ ";
for (int l = 0; l < p["items"].size(); l++) callCommand << (l ? ", " : "") << "node.values[" << pt++ << "]";
callCommand << " }";
notImplemented << " << \"" << p["forEach"][k].asString() << ": \" << \"{ \"";
for (int l = 0; l < p["items"].size(); l++)
notImplemented << (l ? " << \", \"" : "") << " << \"." << p["items"][l]["name"].asString() << " = \" << " << p["forEach"][k].asString() << "." << p["items"][l]["name"].asString();
notImplemented << " << \" }\"";
}
} else {
ss << func["name"].asString() << "_Group_" << argName << " " << argName;
if (isFirst) isFirst = false;
else callCommand << ", ", notImplemented << " << \", \"";
callCommand << "{ ";
for (int l = 0; l < p["items"].size(); l++) callCommand << (l ? ", " : "") << "node.values[" << pt++ << "]";
callCommand << " }";
notImplemented << " << \"" << argName << ": \" << \"{ \"";
for (int l = 0; l < p["items"].size(); l++)
notImplemented << (l ? " << \", \"" : "") << " << \"." << p["items"][l]["name"].asString() << " = \" << " << argName << "." << p["items"][l]["name"].asString();
notImplemented << " << \" }\"";
}
} else {
if (p.isMember("minCount")) {
ss << "const vector<double> &" << p["name"].asString();
if (isFirst) isFirst = false;
else callCommand << ", ", notImplemented << " << \", \"";
if (pt) callCommand << "vector<double>(node.values.begin() + " << pt << ", node.values.end())";
else callCommand << "node.values";
notImplemented << " << \"" << p["name"].asString() << ": \" << \"{ \"; ";
notImplemented << "for (int i = 0; i < " << p["name"].asString() << ".size(); i++) cerr << (i ? \", \" : \"\") << \"[\" << i << \"] = \" << " << p["name"].asString() << "[i]; ";
notImplemented << "cerr << \" }\"";
pt = -1;
} else if (p.isMember("count")) {
for (int k = 0; k < p["count"].asInt(); k++)
ss << "double " << p["name"].asString() << k << (k != p["count"].asInt() - 1 ? ", " : "");
for (int k = 0; k < p["count"].asInt(); k++) {
if (isFirst) isFirst = false;
else callCommand << ", ", notImplemented << " << \", \"";
callCommand << "node.values[" << pt++ << "]";
notImplemented << " << \"" << p["name"].asString() << k << ": \" << " << p["name"].asString() << k;
}
} else if (p.isMember("forEach")) {
for (int k = 0; k < p["forEach"].size(); k++)
ss << "double " << p["forEach"][k].asString() << (k != p["forEach"].size() - 1 ? ", " : "");
for (int k = 0; k < p["forEach"].size(); k++) {
if (isFirst) isFirst = false;
else callCommand << ", ", notImplemented << " << \", \"";
callCommand << "node.values[" << pt++ << "]";
notImplemented << " << \"" << p["forEach"][k].asString() << ": \" << " << p["forEach"][k].asString();
}
} else {
ss << "double " << p["name"].asString();
if (isFirst) isFirst = false;
else callCommand << ", ", notImplemented << " << \", \"";
callCommand << "node.values[" << pt++ << "]";
notImplemented << " << \"" << p["name"].asString() << ": \" << " << p["name"].asString();
}
}
if (j != func["parameters"].size() - 1) ss << ", ";
notImplemented << ";" << endl;
}
ss << ") {";
string head = ss.str();
ss.str("");
// ss << "\tcerr << \"\\e[31mCalled not implemented function \\\"" << func["name"].asString() << "()\\\"!\\e[0m\" << endl;" << endl;
ss << "\tcerr << \"\\e[31mCalled not implemented function \\\"" << func["name"].asString() << "(\";" << endl;
ss << notImplemented.str() << "\tcerr << \")\\\"!\\e[0m\" << endl;" << endl;
ss << "\treturn 0;" << endl;
ss << "}" << endl;
ss << endl;
ss << "#endif" << endl;
string oldcontent = ss.str();
ss.str("");
ss << "\tcerr << \"\\e[31mCalled not implemented function \\\"" << func["name"].asString() << "(\";" << endl;
ss << notImplemented.str() << "\tcerr << \")\\\"!\\e[0m\" << endl;" << endl;
ss << "\treturn 0;" << endl;
ss << "}" << endl;
ss << endl;
ss << "#endif" << endl;
string newcontent = ss.str();
callCommand << ")";
callParamCnt[func["name"].asString()] = pt;
calls[func["name"].asString()] = callCommand.str();
ifstream fin("functions/" + func["name"].asString() + ".h");
if (fin) {
fin.seekg(0, ios::end);
int len = fin.tellg();
fin.seekg(0, ios::beg);
char* ch = new char[len];
fin.read(ch, len);
string s = string(ch, len);
vector<string> lines = explode("\n", s);
vector<string> headers = explode("\n", head);
string currcontent = "";
for (int i = headers.size(); i < lines.size(); i++)
currcontent += lines[i] + (i == lines.size() - 1 ? "" : "\n");
fin.close();
ofstream fout; fout.open("functions/" + func["name"].asString() + ".h");
if (currcontent != oldcontent) {
cout << "\"" << func["name"].asString() + ".h\" was modified! (" << modified.size() + 1 << " / " << json.size() << ", " << 100.0 * (modified.size() + 1) / json.size() << "%)" << endl;
modified.insert(func["name"].asString());
fout << head << endl << currcontent;
} else {
fout << head << endl << newcontent;
}
} else {
fin.close();
ofstream fout; fout.open("functions/" + func["name"].asString() + ".h");
fout << head << endl << newcontent;
}
}
for (int i = 0; i < json.size(); i++) {
auto func = json[i];
fout2 << "double " << func["name"].asString() << "(";
for (int j = 0; j < func["parameters"].size(); j++) {
auto p = func["parameters"][j];
if (p["type"] == "group") {
string argName = "";
for (int k = 0; k < p["items"].size(); k++) {
auto item = p["items"][k];
argName += item["name"].asString();
if (k != p["items"].size() - 1) argName += "_";
}
if (p.isMember("minCount")) {
fout2 << "const vector<" << func["name"].asString() << "_Group_" << argName << "> &" << argName;
} else if (p.isMember("count")) {
for (int k = 0; k < p["count"].asInt(); k++)
fout2 << func["name"].asString() << "_Group_" << argName << " " << argName << k << (k != p["count"].asInt() - 1 ? ", " : "");
} else if (p.isMember("forEach")) {
for (int k = 0; k < p["forEach"].size(); k++)
fout2 << func["name"].asString() << "_Group_" << argName << " " << p["forEach"][k].asString() << (k != p["forEach"].size() - 1 ? ", " : "");
} else {
fout2 << func["name"].asString() << "_Group_" << argName << " " << argName;
}
} else {
if (p.isMember("minCount")) {
fout2 << "const vector<double> &" << p["name"].asString();
} else if (p.isMember("count")) {
for (int k = 0; k < p["count"].asInt(); k++)
fout2 << "double " << p["name"].asString() << k << (k != p["count"].asInt() - 1 ? ", " : "");
} else if (p.isMember("forEach")) {
for (int k = 0; k < p["forEach"].size(); k++)
fout2 << "double " << p["forEach"][k].asString() << (k != p["forEach"].size() - 1 ? ", " : "");
} else {
fout2 << "double " << p["name"].asString();
}
}
if (j != func["parameters"].size() - 1) fout2 << ", ";
}
fout2 << ");" << endl;
}
calls["Switch"] = "Switch(node.values[0], [](vector<double> v){ vector<Switch_Group_test_consequent> res; for (int i = 0; i < v.size(); i += 2) res.push_back({ v[i], v[i + 1] }); return res; }(vector<double>(node.values.begin() + 1, node.values.end())))";
calls["SwitchIntegerWithDefault"] = "SwitchIntegerWithDefault(node.values[0], vector<double>(node.values.begin() + 1, node.values.end() - 1), node.values.back())";
calls["SwitchWithDefault"] = "SwitchWithDefault(node.values[0], [](vector<double> v){ vector<SwitchWithDefault_Group_test_consequent> res; for (int i = 0; i < v.size(); i += 2) res.push_back({ v[i], v[i + 1] }); return res; }(vector<double>(node.values.begin() + 1, node.values.end() - 1)), node.values.back())";
callParamCnt["SwitchIntegerWithDefault"] = -1;
callParamCnt["SwitchWithDefault"] = -1;
fout2 << "double RunCode(int nodeId);" << endl;
for (int i = 0; i < json.size(); i++) {
auto func = json[i];
fout2 << "#include\"" << func["name"].asString() << ".h" << "\"";
if (modified.count(func["name"].asString())) fout2 << " // Modified";
fout2 << endl;
}
fout2 << endl;
for (int i = 0; i < keywords.size(); i++)
fout2 << "#undef " << keywords[i] << endl;
fout2 << endl;
fout2 << "double RunCode(int nodeId) {" << endl;
fout2 << "\tbeforeRunCode(nodeId);" << endl;
fout2 << "\tDataNode &node = nodes[nodeId];" << endl;
fout2 << "\tif (node.isValue) return node.value;" << endl;
fout2 << "\tint hash = node.hash;" << endl;
// fout2 << "\tif (set<string>({ ";
// bool isFirst = true;
// for (int i = 0; i < json.size(); i++) {
// auto func = json[i];
// if (func["controlFlow"].asBool())
// fout2 << (isFirst ? "" : ", ") << "\"" << func["name"].asString() << "\"", isFirst = false;
// }
// fout2 << " }).count(node.name)) {" << endl;
// fout2 << "\t\tvector<double> value;" << endl;
fout2 << "\tfor (int i = 0; i < node.param.size(); i++) node.values[i] = node.param[i];" << endl;
for (int i = 0; i < json.size(); i++) {
auto func = json[i];
if (func["controlFlow"].asBool())
fout2 << "\tif (hash == " << calcHash(func["name"].asString()) << ") { "
<< (callParamCnt[func["name"].asString()] == -1 ? "" : "node.values.resize(" + to_string(callParamCnt[func["name"].asString()]) + ", 0); ")
<< "double res = " << calls[func["name"].asString()] << "; "
<< "afterRunCode(nodeId); "
<< "return res; "
<< "}" << endl;
}
// fout2 << "\t} else {" << endl;
fout2 << "\tfor (int i = 0; i < node.param.size(); i++) {" << endl;
fout2 << "\t\tnode.values[i] = RunCode(node.param[i]);" << endl;
fout2 << "\t\tif (breakCount) return 0;" << endl;
fout2 << "\t}" << endl;
for (int i = 0; i < json.size(); i++) {
auto func = json[i];
if (!func["controlFlow"].asBool())
fout2 << "\tif (hash == " << calcHash(func["name"].asString()) << ") { "
<< (callParamCnt[func["name"].asString()] == -1 ? "" : "node.values.resize(" + to_string(callParamCnt[func["name"].asString()]) + ", 0); ")
<< "double res = " << calls[func["name"].asString()] << "; "
<< "afterRunCode(nodeId); "
<< "return res; "
<< "}" << endl;
}
// fout2 << "\t}" << endl;
fout2 << "}" << endl;
fout2 << endl;
fout2 << "#endif" << endl;
cout << "Finished " << modified.size() << " / " << json.size() << " (" << 100.0 * modified.size() / json.size() << "%)" << endl;
}