-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
280 lines (252 loc) · 7.1 KB
/
main.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
#include <iostream>
#include <ios>
#include <string>
#include <utility>
#include <map>
#include <vector>
#include <memory>
std::string getStdin() {
std::string output;
std::string input;
std::cin >> std::noskipws;
while (std::getline(std::cin, input)) {
output += input + '\n';
}
return output;
}
template<class T>
struct is_contextual_convertible_to_bool {
template<class U, int = sizeof(static_cast<bool>(std::declval<U>()))>
static constexpr auto test(int) -> char;
template<class>
static constexpr auto test(...) -> char(&)[2];
static constexpr bool value = sizeof(test<T>(0)) == 1;
};
template<class T>
struct is_range {
template<class U, std::size_t = sizeof(std::declval<U>().begin(), std::declval<U>().end())>
static constexpr auto test(int) -> char;
template<class>
static constexpr auto test(...) -> char(&)[2];
static constexpr bool value = sizeof(test<T>(0)) == 1;
};
template<class T>
struct has_str {
template<class U, int = sizeof(std::to_string(std::declval<U>()))>
static constexpr auto test(int) -> char;
template<class>
static constexpr auto test(...) -> char(&)[2];
static constexpr bool value = sizeof(test<T>(0)) == 1;
};
class object;
class iterator {
struct holder;
template<class T> struct holder_impl;
std::shared_ptr<holder> holder_;
public:
template<class T> iterator(T it);
void operator++();
object operator*();
bool operator!=(iterator it) const;
};
class object {
struct holder {
virtual ~holder() { }
virtual bool cond() const = 0;
virtual iterator begin() = 0;
virtual iterator end() = 0;
virtual std::string str() const = 0;
};
template<class T>
struct holder_impl : holder {
T obj;
holder_impl(T obj) : obj(std::move(obj)) { }
template<class U>
bool cond_(int, typename std::enable_if<is_contextual_convertible_to_bool<U>::value>::type* = 0) const {
return static_cast<bool>(obj);
}
template<class>
bool cond_(...) const {
throw 0;
}
virtual bool cond() const override {
return cond_<T>(0);
}
template<class U>
iterator begin_(int, typename std::enable_if<is_range<U>::value>::type* = 0) {
return obj.begin();
}
template<class>
iterator begin_(...) {
throw 0;
}
virtual iterator begin() override {
return begin_<T>(0);
};
template<class U>
iterator end_(int, typename std::enable_if<is_range<U>::value>::type* = 0) {
return obj.end();
}
template<class>
iterator end_(...) {
throw 0;
}
virtual iterator end() override {
return end_<T>(0);
}
template<class U>
std::string str_(int, typename std::enable_if<has_str<U>::value>::type* = 0) const {
return std::to_string(obj);
}
template<class>
std::string str_(...) const {
throw 0;
}
virtual std::string str() const override {
return str_<T>(0);
}
};
std::shared_ptr<holder> holder_;
public:
object() = default;
object(const object&) = default;
object(object&&) = default;
object& operator=(const object&) = default;
object& operator=(object&&) = default;
template<class T>
object(T v) : holder_(new holder_impl<T>(std::move(v))) { }
template<class T>
void operator=(T v) { holder_.reset(new holder_impl<T>(std::move(v))); }
explicit operator bool() const { return holder_->cond(); }
iterator begin() { return holder_->begin(); }
iterator end() { return holder_->end(); }
std::string str() const { return holder_->str(); }
};
struct iterator::holder {
virtual ~holder() { }
virtual void operator++() = 0;
virtual object operator*() = 0;
virtual bool operator!=(iterator it) const = 0;
};
template<class T>
struct iterator::holder_impl : iterator::holder {
T it;
holder_impl(T it) : it(std::move(it)) { }
virtual void operator++() override {
++it;
}
virtual object operator*() override {
return *it;
}
virtual bool operator!=(iterator it) const override {
return this->it != std::static_pointer_cast<holder_impl<T>>(it.holder_)->it;
}
};
template<class T>
iterator::iterator(T it)
: holder_(new iterator::holder_impl<T>(std::move(it))) { }
void iterator::operator++() {
holder_->operator++();
}
object iterator::operator*() {
return holder_->operator*();
}
bool iterator::operator!=(iterator it) const {
return holder_->operator!=(it);
}
typedef std::map<std::string, object> tmpl;
class parser {
public:
std::string input;
int index;
parser(std::string input) : input(std::move(input)), index(0) { }
void read() {
if (index >= input.length()) throw 0;
input[index++];
}
char peek() const {
if (index >= input.length()) throw 0;
return input[index];
}
char next() const {
if (index + 1 >= input.length()) throw 0;
return input[index + 1];
}
};
void block(parser& p) {
char c = p.peek();
if (c != '$') {
std::cout << c;
p.read();
} else {
p.read();
char d = p.peek();
if (d == '$') {
p.read();
std::cout << d;
} else if (d == '#') {
p.read();
while (p.peek() != '\n')
p.read();
} else if (d == '{') {
p.read();
d = p.peek();
if (d == '{') {
p.read();
std::cout << "{{";
} else {
std::cout << "[variable(";
do {
std::cout << d;
p.read();
d = p.peek();
} while (d != '}' && d != ' ');
p.read();
std::cout << ")]";
}
} else if (d == '}') {
p.read();
d = p.peek();
if (d == '}') {
p.read();
std::cout << "}}";
} else {
throw 0;
}
} else {
std::string name;
do {
name.push_back(d);
p.read();
d = p.peek();
} while (d != ' ');
if (name == "for") {
std::cout << "$for block";
} else if (name == "if") {
std::cout << "$if block";
} else if (name == "elseif") {
std::cout << "$elseif block";
} else if (name == "else") {
std::cout << "$else block";
}
}
}
block(p);
}
void parse(std::string input) {
try {
parser p{std::move(input)};
block(p);
} catch (...) {
std::cout << std::endl;
}
}
int main() {
tmpl t;
t["test"] = std::vector<int>{ 1, 2, 3, 4 };
for (object v: t["test"]) {
std::cout << v.str() << std::endl;
}
std::string input = getStdin();
parse(input);
}