/
token.h
310 lines (274 loc) · 6.76 KB
/
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
// vim:ts=4:sw=4:
//
// Interactive BASIC Compiler Project
// File: token.h - token class header file
// Copyright (C) 2012-2013 Thunder422
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// For a copy of the GNU General Public License,
// see <http://www.gnu.org/licenses/>.
//
//
// Change History:
//
// 2012-11-02 initial version (parts removed from ibcp.h)
#ifndef TOKEN_H
#define TOKEN_H
#include <memory>
#include <string>
#include <unordered_map>
#include "ibcp.h"
class Token;
using TokenUniquePtr = std::unique_ptr<Token>;
using TokenPtr = std::shared_ptr<Token>;
class Token
{
public:
// changes to Type may require changes to initializers
// for s_hasParen and s_precedence maps
enum class Type
{
Command = 1,
Operator,
IntFuncN,
IntFuncP,
Constant,
DefFuncN,
DefFuncP,
NoParen,
Paren
};
explicit Token(int column = -1, int length = 1) : m_column{column},
m_length{length}, m_type{}, m_code{Invalid_Code}, m_reference{},
m_subCode{None_SubCode} {}
// constructor for codes
Token(int column, int length, Type type, DataType dataType, Code code,
const std::string string = {}) : m_column{column}, m_length{length},
m_type{type}, m_dataType{dataType}, m_string{string}, m_code{code},
m_reference{}, m_subCode{None_SubCode} {}
// constructor for identifiers
Token(int column, int length, Type type, DataType dataType,
const std::string &string) : m_column{column}, m_length{length},
m_type{type}, m_dataType{dataType}, m_string{string},
m_code{Invalid_Code}, m_reference{}, m_subCode{None_SubCode} {}
// constructor for integer constants
Token(int column, int length, const std::string string, int value) :
m_column{column}, m_length{length}, m_type{Token::Type::Constant},
m_dataType{DataType::Integer}, m_string{string}, m_code{Invalid_Code},
m_reference{}, m_subCode{None_SubCode}, m_valueInt{value}
{
m_value = value; // convert to double in case needed
}
// constructor for double constants
Token(int column, int length, const std::string string, double value,
bool decimal);
// constructor for string constants
Token(int column, int length, const std::string string) : m_column{column},
m_length{length}, m_type{Token::Type::Constant},
m_dataType{DataType::String}, m_string{string}, m_code{Invalid_Code},
m_reference{}, m_subCode{None_SubCode} {}
Token(const Token &token) // copy constructor
{
*this = token;
}
~Token(void) {}
bool operator==(const Token &other) const;
bool operator!=(const Token &other) const
{
return !(*this == other);
}
// column and length access functions
int column(void) const
{
return m_column;
}
int length(void) const
{
return m_length;
}
void setLength(int length)
{
m_length = length;
}
// type access functions
Type type(void) const
{
return m_type;
}
void setType(Type type)
{
m_type = type;
}
bool isType(Type type) const
{
return type == m_type;
}
// data type access functions
DataType dataType(bool actual = false) const
{
return !actual && hasSubCode(Double_SubCode)
? DataType::Double : m_dataType;
}
void setDataType(DataType dataType)
{
m_dataType = dataType;
}
bool isDataType(DataType dataType) const
{
return dataType == m_dataType;
}
void setDataType(void); // set default data type if not already set
// string access function
std::string string(void) const
{
return m_string;
}
void setString(const std::string &string)
{
m_string = string;
}
int stringLength(void) const
{
return m_string.length();
}
// code access functions
Code code(void) const
{
return m_code;
}
void setCode(Code code)
{
m_code = code;
}
bool isCode(Code code) const
{
return code == m_code;
}
bool hasValidCode(void) const
{
return m_code != Invalid_Code;
}
// reference access functions
bool reference(void) const
{
return m_reference;
}
void setReference(bool reference = true)
{
m_reference = reference;
}
// sub-code access functions
bool hasSubCode() const
{
return m_subCode;
}
bool hasSubCode(int subCode) const
{
return m_subCode & subCode;
}
void addSubCode(int subCode)
{
m_subCode |= subCode;
}
void removeSubCode(int subCode)
{
m_subCode &= ~subCode;
}
int subCodes(void) const
{
return m_subCode;
}
// value access functions
double value(void) const
{
return m_value;
}
int valueInt(void) const
{
return m_valueInt;
}
void setValue(double value)
{
m_value = value;
}
void setValue(int value)
{
m_valueInt = value;
}
// index access functions
int index(void)
{
return m_index;
}
void setIndex(int index)
{
m_index = index;
}
// token information functions
bool hasParen(void) const
{
return s_hasParen[m_type];
}
int precedence(void) const
{
return s_precendence[m_type];
}
bool isNull(void) const
{
return m_code == Null_Code;
}
// other functions
Code convertCode(DataType toDataType) const;
private:
// static members
static std::unordered_map<Type, bool, EnumClassHash> s_hasParen;
static std::unordered_map<Type, int, EnumClassHash> s_precendence;
// instance members
int m_column; // start column of token
int m_length; // length of token
Type m_type; // type of the token
DataType m_dataType; // data type of token
std::string m_string; // pointer to string of token
Code m_code; // internal code of token (index of TableEntry)
bool m_reference; // token is a reference flag
int m_subCode; // sub-code flags of token
double m_value; // double value for constant token
int m_valueInt; // integer value for constant token (if possible)
int m_index; // index within encoded program code line
};
// structure for holding information about an error exception
struct TokenError
{
TokenError(Status status, int column, int length) : m_status {status},
m_column {column}, m_length {length} {}
TokenError(Status status, const TokenPtr &token) :
TokenError {status, token->column(), token->length()} {}
// convenience function for getting error status
Status operator()()
{
return m_status;
}
// convenience function for checking for a particular error status
bool operator()(Status status)
{
return status == m_status;
}
// convenience function for setting error status
void operator=(Status status)
{
m_status = status;
}
Status m_status; // status of error
int m_column; // column of error
int m_length; // length of error
};
#endif // TOKEN_H