-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
377 lines (314 loc) · 8.9 KB
/
Copy pathparser.cpp
File metadata and controls
377 lines (314 loc) · 8.9 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "parser.h"
#include "types.h"
#define ERROR(name,error,tok) \
{ \
printError(name, error, tok); \
return false; \
}
#define ASSERT(str1,str2, t) \
{ \
if (str1 != str2) { \
std::cout << "Expected \"" << str1 << "\" "; \
std::cout << "but received \"" << str2 << "\" "; \
printTokenLine(t); \
return false; \
} \
}
Parser::Parser(Lexer *lexer) {
lx = lexer;
statements.clear();
}
Parser::~Parser() {
}
void Parser::printError(std::string name, std::string error, Token t) {
std::cout << "ErrorParse" << name << ": " << error << " ";
printTokenLine(t);
std::cout << " \"" << t.value << "\"" << std::endl;
}
void Parser::printTokenLine(Token t) {
std::cout << "at line " << t.row << ':' << t.col;
}
ExprStruct Parser::parseExpr() {
return parseExpr(false);
}
ExprStruct Parser::parseExpr(bool onlyAdditives) {
// First operand
Token operand1 = lx->read();
if (!isLoaded(operand1.type)) {
printError("Expr", "Expected literal or register", operand1);
return ExprStruct(false);
}
// Operation
Token operation = lx->peek();
// If this token isn't operator, we assume expression is a single number or register
if (operation.type != OP) {
ExprStruct res = ExprStruct(true);
res.operand1 = operand1;
return res;
} else if (onlyAdditives && !isAdditive(operation.value)) {
std::cout << "ErrorExpr: Only adding and substracting is allowed here " << std::endl;
printTokenLine(lx->read());
return ExprStruct(false);
}
lx->read();
// Second operand
Token operand2 = lx->read();
if (!isLoaded(operand2.type)) {
printError("Expr", "Expected literal or register", operand2);
return ExprStruct(false);
}
// Build structure and return
ExprStruct res = ExprStruct(true);
res.operand1 = operand1;
res.operand2 = operand2;
res.operation = operation.value;
return res;
}
bool Parser::parseJump() {
int line = lx->peek().row;
Token instruction = lx->read();
// First argument
Token arg1 = lx->read();
if (!isLoaded(arg1.type))
ERROR("Jump", "Only literals and registers are accepted when comparing", arg1);
Token punc = lx->read(); // Read ','
ASSERT(punc.value, ",", punc);
// Second argument
Token arg2 = lx->read();
if (!isLoaded(arg2.type))
ERROR("Jump", "Only literals and registers are accepted when comparing", arg2);
punc = lx->read(); // Read ','
ASSERT(punc.value, ",", punc);
Token label = lx->read();
// if label...
// Build structure and return
StatementStruct *res = new StatementStruct;
JmpStruct *ptr = new JmpStruct;
res->type = JUMP;
res->ptr = ptr;
res->line = line;
ptr->arg1 = arg1.value;
ptr->arg2 = arg2.value;
ptr->instruction = instruction.value;
ptr->label = label.value;
statements.push_back(res);
return true;
}
bool Parser::parseKeyword() {
int line = lx->peek().row;
StatementStruct *res = new StatementStruct();
res->type = KEYWORD;
KeyStruct *ptr = new KeyStruct;
Token instruction = lx->read();
ptr->instruction = instruction.value;
res->ptr = ptr;
res->line = line;
// If its call, get function name
if (instruction.value == "CALL") {
Token tok = lx->read();
ASSERT(tok.value, "<", tok);
tok = lx->read();
// if its identifier..
ptr->value = tok.value;
tok = lx->read();
ASSERT(tok.value, ">", tok);
}
statements.push_back(res);
return true;
}
int Parser::assignmentSize(Token t) {
if (t.type != OP) {
printError("Operand", "Expected assignment", t);
return -1;
}
std::string operand = t.value;
if (operand == "=")
return 4;
else {
if (operand.length() == 2) {
printError("Operand", "Operation size not specified", t);
return -1;
}
int load = operand[2] - '0';
if (operand.length() > 3 || (load != 1 && load != 2)) {
printError("Operand", "Operation size specifier must be 1('=.1') or 2('=.2')", t);
return -1;
}
return load;
}
return -1;
}
bool Parser::parseLoadConvert(std::string dest) {
int line = lx->peek().row;
// Read assign operand and check for errors
std::string operand = lx->read().value;
if (operand.length() > 1 && operand[1] == '.')
ERROR("Convert", "Load size mustn't be specified when converting", lx->peek());
// The conversion function
std::string conv = lx->read().value;
// Read source register and check for errors
Token source = lx->read();
if (source.type != REG)
ERROR("Convert", "Only registers can be used during conversions", source);
// Build structure and return
StatementStruct *res = new StatementStruct();
ConvertStruct *ptr = new ConvertStruct;
res->type = CONVERT;
res->ptr = ptr;
res->line = line;
ptr->destReg = dest;
ptr->conv = conv;
ptr->sourceReg = source.value;
statements.push_back(res);
return true;
}
bool Parser::parseLoadInstant(std::string dest) {
int line = lx->peek().row;
int size = assignmentSize(lx->read());
if (size == -1) return false; // Exit if we encountered error
ExprStruct expr = parseExpr();
/*
// If no size is specified, just parse the expression
if (size == 4)
expr
// Otherwise the source is number or register (no operation allowed)
else {
Token cur = lx->read();
Token next = lx->peek();
if (!isLoaded(cur.type))
ERROR("Load", "Expected literal, register or memory address", cur);
if (isLoaded(cur.type) && next.type == OP)
ERROR("Load", "When specifying load size, no operations are allowed", next);
expr = ExprStruct(true);
expr.operand1 = cur.value;
expr.operand2 = "";
expr.operation = "";
}*/
// Now ready up the variables
StatementStruct *res = new StatementStruct();
LoadStruct *ptr = new LoadStruct;
res->type = LOAD;
res->ptr = ptr;
res->line = line;
// Set structure variables
ptr->reg = dest;
ptr->load = size;
ptr->expr = expr;
ptr->reference = false;
// Save and return true for success
statements.push_back(res);
return true;
}
bool Parser::parseLoadReference(std::string dest) {
int line = lx->peek().row;
int size = assignmentSize(lx->read());
if (size == -1) return false; // Exit if we encountered error
lx->read(); // Read 'M'
Token tok = lx->read(); // Read '['
ASSERT("[", tok.value, tok);
ExprStruct expr = parseExpr(true);
if (!expr.built) return false;
tok = lx->read(); // Read ']'
ASSERT("]", tok.value, tok);
// Now ready up the variables
StatementStruct *res = new StatementStruct();
LoadStruct *ptr = new LoadStruct;
res->type = LOAD;
res->ptr = ptr;
res->line = line;
// Set structure variables
ptr->reg = dest;
ptr->load = size;
ptr->expr = expr;
ptr->reference = true;
statements.push_back(res);
return true;
}
bool Parser::parseLoad() {
std::string dest = lx->read().value; // Register in which we will be loading value
// Checking making sure that its followed by an assignment
Token tok = lx->peek();
if (tok.value[0] != '=')
ERROR("Load", "Expected assignment", tok);
// Grab next token and parse different variants of load
tok = lx->peekExtra();
if (tok.type == CONV) return parseLoadConvert(dest);
if (tok.type == REF) return parseLoadReference(dest);
if (isLoaded(tok.type)) return parseLoadInstant(dest);
std::cout << "UnexpectedErrorLoad" << std::endl;
return false;
}
bool Parser::parseStore() {
int line = lx->peek().row;
lx->read(); // Skip 'M'
Token tok = lx->read(); // Should be '['
ASSERT(tok.value, "[", tok);
// Parse the address
ExprStruct dest = parseExpr(true);
if (!dest.built) return false;
tok = lx->read(); // Should be ']'
ASSERT(tok.value, "]", tok);
Token assignment = lx->read();
int store = assignmentSize(assignment);
if (store == -1) return false;
// Value that will be stored in memory
Token val = lx->read();
if (!isLoaded(val.type))
ERROR("Store", "Expected literal or register", val);
if (lx->peek().value != ";")
ERROR("Store", "No additional operations are allowed here", lx->read());
// Build structure and return
StatementStruct *res = new StatementStruct();
StoreStruct *ptr = new StoreStruct();
res->ptr = ptr;
res->type = STORE;
res->line = line;
ptr->dest = dest;
ptr->store = store;
ptr->val = val;
statements.push_back(res);
return true;
}
std::vector<StatementStruct*> Parser::parse() {
while (true) {
Token first = lx->peek();
while (first.value == ";") {
lx->read();
first = lx->peek();
}
bool success;
switch (first.type) {
case JMP:
success = parseJump();
break;
case KW:
success = parseKeyword();
break;
case REG:
success = parseLoad();
break;
case REF:
success = parseStore();
break;
case END:
return statements;
default:
success = false;
printError("Unexpected", "Unexpected error", first);
if (first.value[0] == 'R')
std::cout << "Possible solution: Make sure to use registers up to 5: R[1-5]"
<< std::endl;
break;
}
if (success) {
if (first.type != KW && lx->peek().value != ";")
printError("", "Expected ';' symbol", lx->peek());
} else {
Token tok = lx->peek();
while (tok.value != ";") {
lx->read();
tok = lx->peek();
}
}
}
}