-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.hpp
305 lines (294 loc) · 7.12 KB
/
parser.hpp
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
#ifndef PARSER_HPP
#define PARSER_HPP
#include "lexer.hpp"
#include <list>
#include <string>
#include <unordered_map>
#include <vector>
#include <array>
using namespace std;
// define the parser module here
enum memWordType {
Word_pos0,
Word_pos1,
Word_pos2,
Word_pos3
};
enum myTokenType {
MY_EOL,
MY_SEP,
MY_EQUAL,
MY_OPEN,
MY_CLOSE,
STRING_AL_DI,
STRING_AL_DI_COL,
STRING_DEL,
STRING_STRING,
STRING_LAYOUT,
STRING_INT_LO,
STRING_INT,
STRING_CONST,
EOT
};
enum testTokenType {
REGISTER,
OFFSET,
TEST_EOL,
TEST_SEP,
TEST_EQUAL,
TEST_OPEN,
TEST_CLOSE,
TEST_LABEL,
TEST_SOURCE,
TEST_IMMID,
TEST_EOT
};
enum memrefType {
MEMREF_S0,
MEMREF_S1,
MEMREF_S2,
MEMREF_S3,
MEMREF_S4,
MEMREF_S5,
MEMREF_S6,
MEMREF_S7,
MEMREF_HALT,
MEMREF_ERROR
};
enum myDeclarSubType {
LAYOUT,
LABEL,
CONST,
DECLAR_EOT,
DECLAR_EOL
};
enum layoutType {
S0,
S1,
S2,
S3,
S4,
S5,
S6,
S7,
S8,
LAYOUT_HALT,
LAYOUT_ERROR
};
enum labelType {
LABEL_S0,
LABEL_S1,
// LABEL_S2,
// LABEL_S3,
// LABEL_S4,
LABEL_HALT,
LABEL_ERROR
};
enum constType {
CONST_S0,
CONST_S1,
CONST_S2,
CONST_S3,
// CONST_S4,
// CONST_S5,
// CONST_S6,
CONST_HALT,
CONST_ERROR
};
enum declarType {
DECLAR_S0,
DECLAR_S1,
DECLAR_S2,
DECLAR_S3,
DECLAR_S4,
DECLAR_HALT,
DECLAR_ERROR
};
enum operation {
LoadWord,
LoadImmid,
LoadAdd,
StoreWord,
Move,
MoveFromHi,
MoveFromLo,
AddS,
AddU,
SubS,
SubU,
MultS,
MultU,
Div2S,
Div2U,
And,
Nor,
Not,
Or,
Xor,
Jump,
BranchEq,
BranchNe,
BranchLt,
BranchLe,
BranchGt,
BranchGe,
NOP,
NONE
};
typedef std::vector<Token> Argument;
struct instruction {
operation op;
Argument args;
size_t line;
};
struct immidiate {
string name;
size_t value;
};
typedef std::unordered_map<std::string, size_t> LabelMapType;
typedef std::vector<Token> TextLabelType;
typedef std::vector<uint8_t> MemoryType;
typedef std::vector<instruction> instructionSet;
typedef std::vector<immidiate> immidiateSet;
//template <typename T>
class parser {
public:
parser(TokenList alist);
~parser();
size_t getFirstTextLine();
void getInstructionLine(std::string &inputfile);
const string get_error();
const string getlexer_error();
MemoryType getMemory();
LabelMapType getLabelMap();
instructionSet getInstructions();
immidiateSet getImmidiates();
TextLabelType getTextLabels();
vector<immidiate> getMainLabels();
bool isInstruct(TokenList&alist);
testTokenType test_classify(TokenList::const_iterator it, TokenList::const_iterator end);
bool isMemref(TokenList::const_iterator it, TokenList::const_iterator end);
void isMemref0(memrefType & state, testTokenType input);
void isMemref1(memrefType & state, testTokenType input);
void isMemref2(memrefType & state, testTokenType input);
void isMemref3(memrefType & state, testTokenType input);
void isMemref4(memrefType & state, testTokenType input);
void isMemref5(memrefType & state, testTokenType input);
void isMemref6(memrefType & state, testTokenType input);
//text
bool isNOP(TokenList &alist_text);//add
bool isLdword(TokenList &alist_text);//add
bool isLdhalf(TokenList &alist_text);
bool isLdbyte(TokenList &alist_text);
bool isLdimm(TokenList &alist_text);//add
bool isLdadd(TokenList &alist_text);//add
bool isLdsw(TokenList &alist_text);//add
bool isLdsh(TokenList &alist_text);
bool isLdsb(TokenList &alist_text);
bool isLdmove(TokenList &alist_text);//add
bool isLdmfhi(TokenList &alist_text);//add
bool isLdmflo(TokenList &alist_text);//add
bool isLdmthi(TokenList &alist_text);
bool isLdmtlo(TokenList &alist_text);
bool isadd(TokenList &alist_text);//add
bool isaddu(TokenList &alist_text);//add
bool issub(TokenList &alist_text);//add
bool issubu(TokenList &alist_text);//add
bool ismul(TokenList &alist_text);
bool ismulo(TokenList &alist_text);
bool ismulou(TokenList &alist_text);
bool ismult(TokenList &alist_text);//add
bool ismultu(TokenList &alist_text);//add
bool isdiv_3(TokenList &alist_text);
bool isdivu_3(TokenList &alist_text);
bool isdiv_2(TokenList &alist_text);//add
bool isdivu_2(TokenList &alist_text);//add
bool isrem(TokenList &alist_text);
bool isremu(TokenList &alist_text);
bool isabs(TokenList &alist_text);
bool isneg(TokenList &alist_text);
bool isnegu(TokenList &alist_text);
bool isand(TokenList &alist_text);//add
bool isnor(TokenList &alist_text);//add
bool isnot(TokenList &alist_text);//add
bool isor(TokenList &alist_text);//add
bool isxor(TokenList &alist_text);//add
bool isjump(TokenList &alist_text);//add
bool isbeq(TokenList &alist_text);//add
bool isbne(TokenList &alist_text);//add
bool isblt(TokenList &alist_text);//add
bool isble(TokenList &alist_text);//add
bool isbgt(TokenList &alist_text);//add
bool isbge(TokenList &alist_text);//add
//data
bool isInt(Token atoken);
bool isString_LO(Token atoken);
bool isInt_LO(Token atoken);
bool isLayout();
bool isLabel();
bool isConst();
bool isDeclara(TokenList alist);
bool parseToken();
void isLayout0(layoutType & state, myTokenType input);
void isLayout1(layoutType & state, myTokenType input);
void isLayout2(layoutType & state, myTokenType input);
void isLayout3(layoutType & state, myTokenType input);
void isLayout4(layoutType & state, myTokenType input);
void isLayout5(layoutType & state, myTokenType input);
void isLayout6(layoutType & state, myTokenType input);
void isLayout7(layoutType & state, myTokenType input);
void isLayout8(layoutType & state, myTokenType input);
void isLabel0(labelType & state, myTokenType input);
void isLabel1(labelType & state, myTokenType input);
void isConstant0(constType & state, myTokenType input);
void isConstant1(constType & state, myTokenType input);
void isConstant2(constType & state, myTokenType input);
void isConstant3(constType & state, myTokenType input);
void isDeclar0(declarType & state, myDeclarSubType input);
void isDeclar1(declarType & state, myDeclarSubType input);
void isDeclar2(declarType & state, myDeclarSubType input);
void isDeclar3(declarType & state, myDeclarSubType input);
void isDeclar4(declarType & state, myDeclarSubType input);
myTokenType classify(TokenList::const_iterator it, TokenList::const_iterator end);
myDeclarSubType declar_classify(TokenList::const_iterator it, TokenList::const_iterator end);
void gettextLabel(TokenList alist);
bool isRegister(Token atoken);
bool isLabel_fromData(Token atoken);
bool isTextLabel(TokenList alist);
bool is_text_label(Token atoken);
bool isLexerError();
private:
size_t firstLine;
Argument arguments;
size_t instructLine;
instruction instructValues;
size_t var = 0;
TokenList mylist;
TokenList::iterator myit;
TokenList::iterator lexit;
TokenList declarList;
//TokenList instructList;
//TokenList labelList;
list<string> variables;
list<string> constList;
list<string> labelList;
LabelMapType DataLabel;
size_t textLine;
size_t documentLine;
vector<immidiate> mainVec;
MemoryType memoryData;
instructionSet instructions;
immidiateSet immidiateLabels;
TextLabelType textLabels;
size_t layoutCount;
size_t constCount;
size_t memrefCount;
bool isLexErr;
bool isChar(Token atoken);
bool isAl_di(Token atoken);
bool isAl_di_col();
bool isDigit(char & str);
bool isImmid(Token atoken);
bool isSource(Token atoken);
};
#endif