/
token.h
293 lines (260 loc) · 6.66 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
// vim:ts=4:sw=4:
//
// Interactive BASIC Compiler Project
// File: token.h - token class header file
// Copyright (C) 2012-2015 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 "ibcp.h"
#include "table.h"
class Token
{
public:
// constructor for codes
Token(Code code) : m_column{-1}, m_length{-1}, m_string{}, m_code{code},
m_reference{}, m_subCode{} {}
Token(TableEntry *entry, int column, int length,
const std::string string = {}) : m_column{column}, m_length{length},
m_string{string}, m_code{entry->code()}, m_reference{}, m_subCode{} {}
// constructor for codes with operands
Token(TableEntry *entry, DataType dataType, int column, int length,
const std::string string = {}, bool reference = {},
SubCode subCode = {}) : m_column{column}, m_length{length},
m_string{string}, m_code{entry->alternate(dataType)->code()},
m_reference{reference}, m_subCode{subCode} {}
// constructor for integer constants
Token(DataType dataType, int column, int length, const std::string string,
int value) : m_column{column}, m_length{length}, m_string{string},
m_code{Table::entry(Const_Code, dataType == DataType::Double
? dataType : DataType::Integer)->code()}, m_reference{}, m_subCode{},
m_value(value), /*convert*/ m_valueInt{value} {}
// constructor for double constants
Token(DataType dataType, int column, int length, const std::string string,
double value);
// constructor for string constants
Token(int column, int length, const std::string string) :
m_column{column}, m_length{length}, m_string{string},
m_code{Table::entry(Const_Code, DataType::String)->code()},
m_reference{}, m_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;
}
// table access functions
TableEntry *table() const
{
return Table::entry(m_code);
}
Type type(void) const
{
return table()->type();
}
bool isType(Type type) const
{
return type == table()->type();
}
bool hasFlag(unsigned flag) const
{
return table()->hasFlag(flag);
}
int precedence() const
{
return table()->precedence();
}
// data type access functions
DataType dataType() const
{
return table()->returnDataType();
}
bool isDataType(DataType dataType) const
{
return dataType == table()->returnDataType();
}
bool isDataTypeCompatible(DataType dataType)
{
// check if token data type is compatible with desired data type
return dataType == table()->returnDataType()
|| (dataType == DataType::Number
&& table()->returnDataType() != DataType::String)
|| dataType == DataType::Any || dataType == DataType::None;
}
// string access function
std::string string(void) const
{
return m_string;
}
std::string stringWithDataType() const;
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 isLastOperand(int operandIndex)
{
return operandIndex == table()->operandCount() - 1;
}
void setFirstAlternate(int operandIndex)
{
m_code = table()->alternate(operandIndex)->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(SubCode subCode) const
{
return m_subCode & subCode;
}
void addSubCode(SubCode subCode)
{
m_subCode |= subCode;
}
void removeSubCode(SubCode subCode)
{
m_subCode &= ~subCode;
}
uint16_t 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 isNull(void) const
{
return !m_code;
}
// other functions
TableEntry *convert(TokenPtr &operandToken, int operandIndex);
TableEntry *convertCodeEntry(DataType dataType);
void changeConstantIgnoreError(DataType toDataType) noexcept;
bool changeConstant(DataType toDataType);
private:
// instance members
int m_column; // start column of token
int m_length; // length 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
uint16_t 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
};
using TokenPtr = std::shared_ptr<Token>;
// 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