-
Notifications
You must be signed in to change notification settings - Fork 0
/
metagen.cpp
386 lines (333 loc) · 7.06 KB
/
metagen.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* METAGEN
* Made with beer in hand
* Oleg Mingalev <[email protected]>, 2018
*/
#include "testlib.h"
#include<string>
#include<unordered_map>
#include<iostream>
#include<memory>
#include <cstddef>
#include <string>
#include <unordered_map>
#include <cstddef>
namespace metagen {
class Cmd {
public:
virtual ~Cmd() {}
virtual std::string operator() (char** &pos) = 0;
};
class Creator {
public:
Creator(const std::string& classname);
virtual ~Creator() {}
virtual Cmd* create() = 0;
};
template <class T>
class CreatorImpl : public Creator {
public:
CreatorImpl<T>(const std::string& classname) : Creator(classname) {}
virtual ~CreatorImpl<T>() {}
virtual Cmd* create() { return new T; }
};
class Factory {
public:
static Cmd* create(const std::string& classname);
static void registerit(const std::string& classname, Creator* creator);
private:
static std::unordered_map<std::string, Creator*>& get_table();
};
#define REGISTER(classname) \
private: \
static const CreatorImpl<classname> creator;
#define REGISTERIMPL(classname,repr) \
const CreatorImpl<classname> classname::creator = CreatorImpl<classname>(#repr);
Creator::Creator(const std::string& classname) {
Factory::registerit(classname, this);
}
void Factory::registerit(const std::string& classname, Creator* creator) {
get_table()[classname] = creator;
}
Cmd* Factory::create(const std::string& classname) {
std::unordered_map<std::string, Creator*>::iterator i;
i = get_table().find(classname);
if(i != get_table().end())
return i->second->create();
else
return (Cmd*)NULL;
}
std::unordered_map<std::string, Creator*>& Factory::get_table() {
static std::unordered_map<std::string, Creator*> table;
return table;
}
#define GLUE2(x,y) x##y
#define GLUE(x,y) GLUE2(x,y)
#define CMD2(name,repr) class name : public metagen::Cmd { \
REGISTER(name); \
public: \
virtual ~name() {} \
virtual string operator() (char** &pos);\
}; \
REGISTERIMPL(name,repr); \
string name::operator() (char** &pos)
#define CMD(name) CMD2(GLUE(mg_,name),name)
namespace utils {
std::string repeat(std::string str, const std::size_t n)
{
if (n == 0) {
str.clear();
str.shrink_to_fit();
return str;
} else if (n == 1 || str.empty()) {
return str;
}
const auto period = str.size();
if (period == 1) {
str.append(n - 1, str.front());
return str;
}
str.reserve(period * n);
std::size_t m {2};
for (; m < n; m *= 2) str += str;
str.append(str.c_str(), (n - (m / 2)) * period);
return str;
}
inline int bitcnt(long long n) {
int ret = 0;
while (n) {
ret += n & 1;
n >>= 1;
}
return ret;
}
inline int mod(int n, int x)
{
return ((n%x) + x) % x;
}
} // namespace metagen::utils
}; // namespace metagen
using namespace std;
string proceed(char** &cur, char stopword[]);
string next_token(char** &cur) {
string ret(*cur++);
bool only_op = true;
for (int i = 0; i < ret.size(); ++i) {
only_op &= ret[i] == '[';
}
if (!ret.size() || !only_op) {
return ret;
}
char endword[] = "]";
if (ret.size() == 1) {
return proceed(cur, endword);
}
ret.resize(ret.size() - 1);
return ret;
}
#define INTARG atoi(next_token(pos).c_str())
#define FLOATARG atof(next_token(pos).c_str())
#define STRARG next_token(pos)
string proceed(char** &cur, char stopword[] = nullptr) {
string ret;
while (*cur && (!stopword || strcmp(*cur, stopword))) {
string token = next_token(cur);
unique_ptr<metagen::Cmd> cmd(metagen::Factory::create(token));
if (!cmd) {
cerr << "Unknown command <" << token << ">" << endl;
exit(1);
}
ret.append((*cmd)(cur));
}
if (stopword) {
if (*cur) {
++cur;
} else {
cerr << "Expected stopword <" << stopword << "> not found" << endl;
exit(1);
}
}
return ret;
}
namespace metagen::cmds::core {
CMD(repeat) {
int cnt = INTARG;
char endword[] = "end";
string ret;
char** curpos = pos;
for (int i = 0; i < cnt; ++i) {
pos = curpos;
ret.append(proceed(pos, endword));
}
return ret;
}
CMD(comment) {
while (*pos) ++pos;
return "";
}
}
namespace metagen::cmds::testlib {
CMD(next) {
return rnd.next(STRARG);
}
CMD(inext) {
int r = INTARG;
return to_string(rnd.next(r));
}
CMD(inext2) {
int l = INTARG;
int r = INTARG;
return to_string(rnd.next(l, r));
}
CMD(fnext) {
float r = FLOATARG;
return to_string(rnd.next(r));
}
CMD(wnext) {
int l = INTARG;
int t = INTARG;
return to_string(rnd.wnext(l, t));
}
}
namespace metagen::cmds::variables {
unordered_map<string, string> vars;
CMD(inc) {
string name = STRARG;
int n = atoi(vars[name].c_str());
++n;
vars[name] = to_string(n);
return "";
}
CMD(var) {
return vars[STRARG];
}
CMD(set) {
string name = STRARG;
string val = STRARG;
vars[name] = val;
return "";
}
}
namespace metagen::cmds::strings {
CMD(thuemorse) {
int n = INTARG;
string a = STRARG;
string b = STRARG;
string ret;
for (int i = 0; i < n; ++i) {
ret.append((metagen::utils::bitcnt(i) % 2) ? b : a);
}
return ret;
}
CMD(rotate) {
int cnt = INTARG;
string ret = STRARG;
std::rotate(
ret.begin(),
ret.begin() + metagen::utils::mod(cnt, ret.size()),
ret.end()
);
return ret;
}
CMD(shuffle) {
string ret = STRARG;
shuffle(ret.begin(), ret.end());
return ret;
}
CMD(reverse) {
string ret = STRARG;
reverse(ret.begin(), ret.end());
return ret;
}
CMD(print) {
return STRARG;
}
CMD(lowercase) {
static string ret;
if (!ret.size()) {
for (int i = 0; i < 26; ++i) {
ret.push_back('a'+i);
}
}
return ret;
}
CMD(uppercase) {
static string ret;
if (!ret.size()) {
for (int i = 0; i < 26; ++i) {
ret.push_back('A'+i);
}
}
return ret;
}
CMD(digits) {
static string ret;
if (!ret.size()) {
for (int i = 0; i < 10; ++i) {
ret.push_back('0'+i);
}
}
return ret;
}
CMD(cycle) {
int cnt = INTARG;
string s = STRARG;
string ret;
ret.reserve(cnt);
for (int i = 0; i < cnt; ++i) {
ret.push_back(s[i % s.size()]);
}
return ret;
}
CMD(times) {
int cnt = INTARG;
string s = STRARG;
return metagen::utils::repeat(s, cnt);
}
CMD(shift) {
int cnt = INTARG;
string alphabet = STRARG;
string s = STRARG;
unordered_map<char, int> rev;
for (int i = 0; i < alphabet.size(); ++i) {
rev[alphabet[i]] = i;
}
std::rotate(
alphabet.begin(),
alphabet.begin() + metagen::utils::mod(cnt, alphabet.size()),
alphabet.end()
);
for (int i = 0; i < s.size(); ++i) {
unordered_map<char, int>::iterator it = rev.find(s[i]);
if (it != rev.end()) {
s[i] = alphabet[it->second];
}
}
return s;
}
CMD(leftpad) {
int cnt = INTARG;
string ch = STRARG;
if (ch.size() != 1) {
cerr << "Invalid 2nd argument on leftpad: <" << ch << "> (must be char)" << endl;
exit(1);
}
string s = STRARG;
cnt -= s.size();
string ret = metagen::utils::repeat(ch, max(0, cnt));
ret.append(s);
return ret;
}
CMD2(space,_) {
return " ";
}
CMD(endl) {
return "\n";
}
}
int main(int argc, char* argv[]) {
registerGen(argc, argv, 1);
char** cur = argv;
++cur;
cout << proceed(cur) << endl;
}