-
Notifications
You must be signed in to change notification settings - Fork 0
/
sli_token.h
312 lines (259 loc) · 5.4 KB
/
sli_token.h
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
#ifndef SLI_TOKEN_H
#define SLI_TOKEN_H
#include <iostream>
#include <string>
#include "sli_type.h"
#include "sli_exceptions.h"
/**
* Struct Datum is the smnallest memory unit. The linked type object decides
* how this memory unit is interpreted.
* The interpreter manages all memory for datums.
* Compound objects are represented as follows:
* array: array_type 1 datum with pointer to array_rep;
* array_rep, string_rep
* n_ref ! len ! [ d1,...,d_len]
* s_expr (for lists)
* n_ref ! lchild ! r_child
*/
namespace sli3
{
class SLIType;
class TokenArray;
class SLIFunction;
class Dictionary;
class SLIString;
class SLIistream;
class SLIostream;
class TypeNode;
class Token
{
public:
Token();
Token(SLIType *);
Token(const Token &);
~Token();
/**
* Initialize from a Token. This function assumes that the object is uninitialized.
*/
Token & init(const Token &t);
/**
* Initialize by assignment operation.
*/
Token & operator=(const Token &t);
/**
* Move contents of t to this token.
* The after assigning the contents of t, t is cleared.
*/
Token & move(Token &t);
/**
* Exchange the contents of t and this token.
*/
Token & swap(Token &t);
/**
* Clear contents of token.
*
*/
void clear();
operator int() const;
operator long() const;
operator long&();
operator double() const;
operator double&();
operator bool() const;
operator bool&();
operator std::string() const;
operator std::string&();
operator std::vector<long>() const;
operator std::vector<double>() const;
operator std::istream&();
operator std::ostream&();
operator TokenArray&();
bool is_valid() const;
refcount_t references() const;
refcount_t add_reference() const;
void remove_reference();
bool operator==(const Token&) const;
bool operator==(Name) const;
bool operator==(int) const;
bool operator==(unsigned int) const;
bool operator==(long) const;
bool operator==(unsigned long) const;
bool operator==(double) const;
bool operator==(bool) const;
bool operator!=(const Token&) const;
bool is_of_type(unsigned int) const;
void require_type(unsigned int) const;
Name get_typename() const;
void execute();
bool is_executable() const
{
return (type_ and type_->is_executable());
}
std::ostream & print(std::ostream &) const;
std::ostream & pprint(std::ostream &) const;
SLIType *type_; //!< If NULL, the datum is unused.
union value
{
double double_val;
long long_val;
bool bool_val;
size_t name_val;
TokenArray *array_val;
SLIFunction *func_val;
Dictionary *dict_val;
SLIString *string_val;
SLIistream *istream_val;
SLIostream *ostream_val;
TypeNode *trie_val;
SLIType *type_val;
} data_;
};
std::ostream & operator<<(std::ostream& , const Token&);
inline
Token::Token()
:type_(0), data_()
{}
inline
Token::Token(SLIType *t)
:type_(t), data_()
{}
inline
Token::Token(Token const& s)
:type_(s.type_),
data_(s.data_)
{
if(type_ != 0)
type_->add_reference(*this);
}
inline
Token::~Token()
{
remove_reference();
}
inline
void Token::clear()
{
remove_reference();
type_=0;
//data_=value();
}
inline
Token& Token::init(const Token&t)
{
t.add_reference();
type_=t.type_;
data_=t.data_;
return *this;
}
inline
Token& Token::move( Token&t)
{
clear();
init(t);
t.clear();
return *this;
}
inline
Token& Token::swap(Token&t)
{
SLIType *tmp_type=type_;
value tmp_data=data_;
type_=t.type_;
data_=t.data_;
t.type_=tmp_type;
t.data_=tmp_data;
return *this;
}
inline
Token& Token::operator=(const Token &t)
{
clear();
return init(t);
}
inline
bool Token::is_valid() const
{
return type_ !=0;
}
inline
refcount_t Token::references() const
{
return (is_valid()) ? type_->references(*this):1;
}
inline
refcount_t Token::add_reference() const
{
return (is_valid()) ? type_->add_reference(*this) : 1;
}
inline
void Token::remove_reference()
{
if(is_valid())
type_->remove_reference(*this);
}
inline
Token::operator int() const
{
require_type(sli3::integertype);
return data_.long_val;
}
inline
Token::operator long() const
{
require_type(sli3::integertype);
return data_.long_val;
}
inline
Token::operator long&()
{
require_type(sli3::integertype);
return data_.long_val;
}
inline
Token::operator double&()
{
require_type(sli3::doubletype);
return data_.double_val;
}
inline
Token::operator double() const
{
require_type(sli3::doubletype);
return data_.double_val;
}
inline
Token::operator bool() const
{
require_type(sli3::booltype);
return data_.bool_val;
}
inline
Token::operator bool&()
{
require_type(sli3::booltype);
return data_.bool_val;
}
inline
bool Token::is_of_type(unsigned int id) const
{
return type_ and type_->is_type(id);
}
inline
void Token::require_type(unsigned int id) const
{
if(not type_)
throw InvalidToken();
type_->require_type(id);
}
inline
void Token::execute()
{
if(type_)
{
type_->execute(*this);
return;
}
throw InvalidToken();
}
}
#endif