forked from cparse/cparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackToken.cpp
298 lines (269 loc) · 7.5 KB
/
packToken.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
#include <sstream>
#include <string>
#include <iostream>
#include "./shunting-yard.h"
#include "./packToken.h"
#include "./shunting-yard-exceptions.h"
const packToken& packToken::None() {
static packToken none = packToken(TokenNone());
return none;
}
packToken::strFunc_t& packToken::str_custom() {
static strFunc_t func = 0;
return func;
}
packToken::packToken(const TokenMap& map) : base(new TokenMap(map)) {}
packToken::packToken(const TokenList& list) : base(new TokenList(list)) {}
packToken& packToken::operator=(const packToken& t) {
delete base;
base = t.base->clone();
return *this;
}
bool packToken::operator==(const packToken& token) const {
if (NUM & token.base->type & base->type) {
return token.asDouble() == asDouble();
}
if (token.base->type != base->type) {
return false;
} else {
// Compare strings to simplify code
return token.str() == str();
}
}
bool packToken::operator!=(const packToken& token) const {
return !(*this == token);
}
TokenBase* packToken::operator->() const {
return base;
}
std::ostream& operator<<(std::ostream &os, const packToken& t) {
return os << t.str();
}
packToken& packToken::operator[](const std::string& key) {
if (base->type != MAP) {
throw bad_cast(
"The Token is not a map!");
}
return (*static_cast<TokenMap*>(base))[key];
}
const packToken& packToken::operator[](const std::string& key) const {
if (base->type != MAP) {
throw bad_cast(
"The Token is not a map!");
}
return (*static_cast<TokenMap*>(base))[key];
}
packToken& packToken::operator[](const char* key) {
if (base->type != MAP) {
throw bad_cast(
"The Token is not a map!");
}
return (*static_cast<TokenMap*>(base))[key];
}
const packToken& packToken::operator[](const char* key) const {
if (base->type != MAP) {
throw bad_cast(
"The Token is not a map!");
}
return (*static_cast<TokenMap*>(base))[key];
}
bool packToken::asBool() const {
switch (base->type) {
case REAL:
return static_cast<Token<double>*>(base)->val != 0;
case INT:
return static_cast<Token<int64_t>*>(base)->val != 0;
case BOOL:
return static_cast<Token<uint8_t>*>(base)->val != 0;
case STR:
return static_cast<Token<std::string>*>(base)->val != std::string();
case MAP:
case FUNC:
return true;
case NONE:
return false;
case TUPLE:
case STUPLE:
return static_cast<Tuple*>(base)->list().size() != 0;
default:
throw bad_cast("Token type can not be cast to boolean!");
}
}
double packToken::asDouble() const {
switch (base->type) {
case REAL:
return static_cast<Token<double>*>(base)->val;
case INT:
return static_cast<Token<int64_t>*>(base)->val;
case BOOL:
return static_cast<Token<uint8_t>*>(base)->val;
default:
if (!(base->type & NUM)) {
throw bad_cast(
"The Token is not a number!");
} else {
throw bad_cast(
"Unknown numerical type, can't convert it to double!");
}
}
}
int64_t packToken::asInt() const {
switch (base->type) {
case REAL:
return static_cast<Token<double>*>(base)->val;
case INT:
return static_cast<Token<int64_t>*>(base)->val;
case BOOL:
return static_cast<Token<uint8_t>*>(base)->val;
default:
if (!(base->type & NUM)) {
throw bad_cast(
"The Token is not a number!");
} else {
throw bad_cast(
"Unknown numerical type, can't convert it to integer!");
}
}
}
std::string& packToken::asString() const {
if (base->type != STR && base->type != VAR && base->type != OP) {
throw bad_cast(
"The Token is not a string!");
}
return static_cast<Token<std::string>*>(base)->val;
}
TokenMap& packToken::asMap() const {
if (base->type != MAP) {
throw bad_cast(
"The Token is not a map!");
}
return *static_cast<TokenMap*>(base);
}
TokenList& packToken::asList() const {
if (base->type != LIST) {
throw bad_cast(
"The Token is not a list!");
}
return *static_cast<TokenList*>(base);
}
Tuple& packToken::asTuple() const {
if (base->type != TUPLE) {
throw bad_cast(
"The Token is not a tuple!");
}
return *static_cast<Tuple*>(base);
}
STuple& packToken::asSTuple() const {
if (base->type != STUPLE) {
throw bad_cast(
"The Token is not an special tuple!");
}
return *static_cast<STuple*>(base);
}
Function* packToken::asFunc() const {
if (base->type != FUNC) {
throw bad_cast(
"The Token is not a function!");
}
return static_cast<Function*>(base);
}
std::string packToken::str(uint32_t nest) const {
return packToken::str(base, nest);
}
std::string packToken::str(const TokenBase* base, uint32_t nest) {
std::stringstream ss;
TokenMap_t* tmap;
TokenMap_t::iterator m_it;
TokenList_t* tlist;
TokenList_t::iterator l_it;
const Function* func;
bool first, boolval;
std::string name;
if (!base) return "undefined";
if (base->type & REF) {
base = static_cast<const RefToken*>(base)->resolve();
name = static_cast<const RefToken*>(base)->key.str();
}
/* * * * * Check for a user defined functions: * * * * */
if (packToken::str_custom()) {
std::string result = packToken::str_custom()(base, nest);
if (result != "") {
return result;
}
}
/* * * * * Stringify the token: * * * * */
switch (base->type) {
case NONE:
return "None";
case UNARY:
return "UnaryToken";
case OP:
return static_cast<const Token<std::string>*>(base)->val;
case VAR:
return static_cast<const Token<std::string>*>(base)->val;
case REAL:
ss << static_cast<const Token<double>*>(base)->val;
return ss.str();
case INT:
ss << static_cast<const Token<int64_t>*>(base)->val;
return ss.str();
case BOOL:
boolval = static_cast<const Token<uint8_t>*>(base)->val;
return boolval ? "True" : "False";
case STR:
return "\"" + static_cast<const Token<std::string>*>(base)->val + "\"";
case FUNC:
func = static_cast<const Function*>(base);
if (func->name().size()) return "[Function: " + func->name() + "]";
if (name.size()) return "[Function: " + name + "]";
return "[Function]";
case TUPLE:
case STUPLE:
if (nest == 0) return "[Tuple]";
ss << "(";
first = true;
for (const packToken token : static_cast<const Tuple*>(base)->list()) {
if (!first) {
ss << ", ";
} else {
first = false;
}
ss << str(token.token(), nest-1);
}
if (first) {
// Its an empty tuple:
// Add a `,` to make it different than ():
ss << ",)";
} else {
ss << ")";
}
return ss.str();
case MAP:
if (nest == 0) return "[Map]";
tmap = &(static_cast<const TokenMap*>(base)->map());
if (tmap->size() == 0) return "{}";
ss << "{";
for (m_it = tmap->begin(); m_it != tmap->end(); ++m_it) {
ss << (m_it == tmap->begin() ? "" : ",");
ss << " \"" << m_it->first << "\": " << m_it->second.str(nest-1);
}
ss << " }";
return ss.str();
case LIST:
if (nest == 0) return "[List]";
tlist = &(static_cast<const TokenList*>(base)->list());
if (tlist->size() == 0) return "[]";
ss << "[";
for (l_it = tlist->begin(); l_it != tlist->end(); ++l_it) {
ss << (l_it == tlist->begin() ? "" : ",");
ss << " " << l_it->str(nest-1);
}
ss << " ]";
return ss.str();
default:
if (base->type & IT) {
return "[Iterator]";
}
return "unknown_type";
}
}