/
table.h
269 lines (237 loc) · 7 KB
/
table.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
// vim:ts=4:sw=4:
//
// Interactive BASIC Compiler Project
// File: table.h - table 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-03 initial version (parts removed from ibcp.h)
#ifndef TABLE_H
#define TABLE_H
#include <array>
#include <memory>
#include <unordered_map>
#include "ibcp.h"
#include "utility.h"
class Token;
using TokenPtr = std::shared_ptr<Token>;
// TODO temporary until code type enumeration implemented
enum class Type
{
Command = 1,
Operator,
IntFunc,
Constant,
DefFunc,
NoParen,
Paren
};
// bit definitions for flags field
enum TableFlag : unsigned
{
// table entry flags (each must have unique bit set)
Multiple_Flag = 1u << 0, // function has multiple forms
Reference_Flag = 1u << 1, // code requires a reference
SubStr_Flag = 1u << 2, // code supports sub-string assignments
Hidden_Flag = 1u << 3, // code is hidden operator/function
Print_Flag = 1u << 4, // print-only function
UseConstAsIs_Flag = 1u << 5, // use constant data type as is
Keep_Flag = 1u << 6, // sub-string keep assignment
Two_Flag = 1u << 7, // code can have two words or characters
Command_Flag = 1u << 8, // operator code is really a command
EndStmt_Flag = 1u << 31 // end statement
};
struct ExprInfo;
class Translator;
class ProgramModel;
class Recreator;
class RpnItem;
using RpnItemPtr = std::shared_ptr<RpnItem>;
typedef void (*TranslateFunction)(Translator &translator);
typedef uint16_t (*EncodeFunction)(ProgramModel *programUnit,
const TokenPtr &token);
typedef const std::string (*OperandTextFunction)
(const ProgramModel *programUnit, uint16_t operand, SubCode subCode);
typedef void (*RemoveFunction)(ProgramModel *programUnit, uint16_t operand);
typedef void (*RecreateFunction)(Recreator &recreator, RpnItemPtr &rpnItem);
// expression information for operators and internal functions
struct ExprInfo
{
DataType m_returnDataType; // return data type of operator/function
short m_operandCount; // number of operands (operators/functions)
const DataType *m_operandDataType; // data type of each operand
ExprInfo(DataType returnDataType = DataType::None,
const std::initializer_list<const DataType> &operands = {}) :
m_returnDataType {returnDataType},
m_operandCount(operands.size()),
m_operandDataType {operands.begin()}
{
}
};
// table entry structure with information for a single code
class TableEntry
{
public:
TableEntry(Type type, const std::string name, const std::string name2,
const std::string option, unsigned flags, int precedence,
ExprInfo *exprInfo, TranslateFunction _translate,
EncodeFunction _encode, OperandTextFunction _operandText,
RemoveFunction _remove, RecreateFunction _recreate) :
m_type {type},
m_name {name},
m_name2 {name2},
m_option {option},
m_flags {flags},
m_precedence {precedence},
m_exprInfo {exprInfo},
translate {_translate},
encode {_encode},
operandText {_operandText},
remove {_remove},
recreate {_recreate}
{}
int index() const;
bool isCode(Code code)
{
return code == this->index();
}
Type type() const
{
return m_type;
}
std::string name() const
{
return m_name;
}
const std::string name2() const
{
return m_name2;
}
std::string commandName() const;
const std::string debugName() const
{
return m_name + m_name2;
}
const std::string optionName() const
{
return m_option;
}
bool hasFlag(unsigned flag) const
{
return m_flags & flag ? true : false;
}
int precedence() const
{
return m_precedence;
}
DataType returnDataType() const
{
return m_exprInfo->m_returnDataType;
}
int operandCount() const
{
return m_exprInfo->m_operandCount;
}
DataType operandDataType(int operandIndex) const
{
return m_exprInfo->m_operandDataType[operandIndex];
}
DataType expectedDataType();
bool isUnaryOperator() const
{
return m_type == Type::Operator && m_exprInfo->m_operandCount == 1;
}
bool isUnaryOrBinaryOperator() const
{
return m_type == Type::Operator && m_exprInfo->m_operandCount > 0;
}
TableEntry *alternate(DataType returnDataType);
int alternateCount(int operandIndex);
TableEntry *alternate(int operandIndex = 0);
TableEntry *alternate(int operandIndex, DataType operandDataType);
TranslateFunction translateFunction() const
{
return translate;
}
EncodeFunction encodeFunction() const
{
return encode;
}
bool hasOperand() const
{
return operandText;
}
OperandTextFunction operandTextFunction() const
{
return operandText;
}
RemoveFunction removeFunction() const
{
return remove;
}
RecreateFunction recreateFunction() const
{
return recreate;
}
friend class Table;
private:
Type m_type; // type of token for entry
const std::string m_name; // name for table entry
const std::string m_name2; // name of second word of command
const std::string m_option; // name of option sub-code
unsigned m_flags; // flags for entry
int m_precedence; // precedence of code
ExprInfo *m_exprInfo; // expression info pointer (NULL for none)
TranslateFunction translate; // pointer to translate function
EncodeFunction encode; // pointer to encode function
OperandTextFunction operandText;// pointer to operand text function
RemoveFunction remove; // pointer to remove function
RecreateFunction recreate; // pointer to recreate function
};
class Table
{
public:
Table(TableEntry *entry, int entryCount);
// TABLE SPECIFIC FUNCTIONS
static TableEntry *entry(int index);
static TableEntry *entry(int index, DataType dataType);
static TableEntry *find(const std::string &string);
static TableEntry *find(const std::string &word1, const std::string &word2)
{
return find(word1 + ' ' + word2);
}
friend class TableEntry;
private:
// these functions private to prevent multiple instances
Table(void) {}
Table(Table const &) {}
Table &operator=(Table const &) {return *this;}
static void addExpectedDataType(TableEntry *entry, DataType dataType);
void add(TableEntry &entry);
TableEntry *m_entry; // pointer to table entries
// case insensitive unordered map alias
using NameMap = std::unordered_map<std::string, TableEntry *,
CaseOptionalHash, CaseOptionalEqual>;
static NameMap s_nameToEntry; // name to code table map
// entry to alternate entries arrays
using EntryVectorArray = std::array<std::vector<TableEntry *>, 3>;
static std::unordered_map<TableEntry *, EntryVectorArray> s_alternate;
// entry to expected data type map
static std::unordered_map<TableEntry *, DataType> s_expectedDataType;
};
#endif // TABLE_H