/
token.h
342 lines (309 loc) · 6.55 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// 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 <QCoreApplication>
#include <QStack>
#include <QString>
#include <QStringList>
#include "ibcp.h"
// changes to TokenType may require changes to ibcp.cpp: Token::initialize()
enum TokenType
{
Command_TokenType,
Operator_TokenType,
IntFuncN_TokenType,
IntFuncP_TokenType,
Constant_TokenType,
DefFuncN_TokenType,
DefFuncP_TokenType,
NoParen_TokenType,
Paren_TokenType,
Error_TokenType,
sizeof_TokenType
};
class Token
{
Q_DECLARE_TR_FUNCTIONS(Token)
int m_id; // private ID (index) for detecting token leaks
int m_column; // start column of token
int m_length; // length of token
TokenType m_type; // type of the token
DataType m_dataType; // data type of token
QString 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
public:
explicit Token(int column = -1)
{
m_column = column;
m_length = 1;
m_reference = false;
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);
}
// overload new and delete operators for memory management
void *operator new(size_t size);
void operator delete(void *ptr);
// 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;
}
void addLengthToColumn(void)
{
m_column += m_length;
}
// type access functions
TokenType type(void) const
{
return m_type;
}
void setType(TokenType type)
{
m_type = type;
}
bool isType(TokenType type) const
{
return type == m_type;
}
// data type access functions
DataType dataType(bool actual = false) const
{
return !actual && hasSubCode(Double_SubCode)
? Double_DataType : 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
QString string(void) const
{
return m_string;
}
void setString(const QString &string)
{
m_string = string;
}
void setString(int pos, QChar character)
{
m_string[pos] = character;
}
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;
}
Code nextCode(void)
{
return ++m_code;
}
bool isCode(Code code) const
{
return hasTableEntry() && code == m_code;
}
// reference access functions
bool reference(void) const
{
return m_reference;
}
void setReference(bool reference = true)
{
m_reference = reference;
}
// sub-code access functions
int hasSubCode(int subCode) const
{
return m_subCode & subCode;
}
void addSubCode(int subCode)
{
m_subCode |= subCode;
}
void removeSubCode(int subCode)
{
m_subCode &= ~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;
}
// set error functions
void setError(const QString &msg, DataType dataType = Double_DataType)
{
m_length = 1;
m_type = Error_TokenType;
m_dataType = dataType;
m_string = msg;
}
void setError(int column, const QString &msg)
{
m_length = -column; // assume length=1, specifies alternate column
m_type = Error_TokenType;
m_dataType = Double_DataType;
m_string = msg;
}
void setError(const QString &msg, int len)
{
m_length = len;
m_type = Error_TokenType;
m_dataType = Double_DataType;
m_string = msg;
}
// token information functions
bool hasParen(void) const
{
return s_paren[m_type];
}
int precedence(void) const
{
return s_prec[m_type];
}
int hasTableEntry(void) const
{
return s_table[m_type];
}
bool isNull(void) const
{
return hasTableEntry() && m_code == Null_Code;
}
// set length to include second token
Token *setThrough(Token *token2)
{
m_length = token2->m_column - m_column + token2->m_length;
return this;
}
// recreate text for token
QString text(bool withIndex = false);
private:
QString textOperand(bool withIndex);
// static members
static bool s_paren[sizeof_TokenType];
static int s_prec[sizeof_TokenType];
static bool s_table[sizeof_TokenType];
static const QString s_messageArray[sizeof_TokenStatus];
class FreeStack : public QStack<Token *>
{
public:
~FreeStack(void);
};
static FreeStack s_freeStack;
class UsedVector : public QVector<Token *>
{
public:
~UsedVector(void);
void reportErrors(void);
};
static UsedVector s_used;
class DeletedList : public QStringList
{
public:
~DeletedList(void);
void reportErrors(void);
};
static DeletedList s_deleted;
public:
// static member functions
static void initialize(void);
static const QString message(TokenStatus status)
{
return s_messageArray[status];
}
static void reportErrors(void)
{
s_used.reportErrors();
s_deleted.reportErrors();
}
};
// stack to hold tokens
class TokenStack : public QStack<Token *>
{
public:
~TokenStack(void)
{
// delete any tokens left in stack when stack goes out of scope
while (!isEmpty())
{
delete pop();
}
}
};
#endif // TOKEN_H