-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.yy
535 lines (487 loc) · 25.1 KB
/
parser.yy
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
%{
#include "parser.tab.hh"
#include "scanner.hh"
#include <cstdarg>
#define yylex driver.scanner_->yylex
%}
%code requires
{
#include <iostream>
#include <iomanip>
#include "ast.hh"
#include "driver.hh"
#include "location.hh"
#include <string>
#include <sstream>
#include <algorithm>
#include <unordered_map>
namespace kabsa {
Node *create_integer_number_node(int value);
Node *create_double_number_node(double value);
Node *create_identifier_node(const location& l, std::string key, identifierEnum identifier_type = VARIABLE_TYPE, bool assignment = true);
std::string identifierTypeAsString(identifierEnum identifier_type);
Node *get_identifier_node(const location& l, std::string key, std::vector<identifierEnum> valid_identifier_types);
Node *create_operation_node(int operation_token, int num_of_operands, ...);
void generate(Node *node);
bool write_symbol_table_file();
bool isValidType(identifierEnum identifier_type, std::vector<identifierEnum> valid_identifier_types);
static int last_used_label = 0;
static std::stringstream assembly_ss;
static std::unordered_map<std::string, IdentifierNode*> symbol_table;
}
}
%code provides {
namespace kabsa {
class Driver;
inline void yyerror (const location& l, const char* msg) {
std::cerr << l << ": " << msg << std::endl; exit(1);
}
}
}
%require "3.2"
%language "C++"
%locations
%defines
%debug
%define api.namespace {kabsa}
%define api.parser.class {Parser}
%define api.value.type variant
%parse-param {Driver &driver}
%lex-param {Driver &driver}
%define parse.error verbose
%token TOK_EOF 0
%token <int> CONSTANT WHILE IF ENUM DO FOR SWITCH FUNCTION ELSE GE LE EQ NE RETURN CASE BREAK DEFAULT PLUS MINUS MULTIPLY DIVIDE SEMICOLON COLON LEFT_PARENTHESIS RIGHT_PARENTHESIS LEFT_BRACES RIGHT_BRACES ASSIGN GT LT COMMA AND OR NOT CALL PUSH_ARGS
%token <int> INTEGER TRUE FALSE
%token <double> DOUBLE
%token <std::string> IDENTIFIER
%nterm <Node *> main_program program statements statement expression boolean_expression arguments enum_specifier enum_list variable_assignment constant_assignment parameters function_declaration function_call constant_expression labeled_statement labeled_statements variable_declaration constant_declaration
%nonassoc IFX
%nonassoc ELSE
%left OR
%left AND
%left EQ NE
%left LT LE GT GE
%left PLUS MINUS
%left MULTIPLY DIVIDE
%right NOT
%nonassoc UMINUS
%%
main_program
:program { driver.write_outfile(assembly_ss); write_symbol_table_file(); exit(0); }
;
program
: %empty {}
| program function_declaration { generate($2); }
;
function_declaration
: FUNCTION IDENTIFIER LEFT_PARENTHESIS parameters RIGHT_PARENTHESIS statement { $$ = create_operation_node(kabsa::Parser::token::FUNCTION, 3, create_identifier_node(*driver.location_, $2, FUNCTION_TYPE), $4, $6); }
;
statement
: SEMICOLON { $$ = create_operation_node(kabsa::Parser::token::SEMICOLON, 2, NULL, NULL); }
| RETURN expression SEMICOLON { $$ = create_operation_node(kabsa::Parser::token::RETURN, 1, $2); }
| RETURN SEMICOLON { $$ = create_operation_node(kabsa::Parser::token::RETURN, 0); }
| variable_assignment SEMICOLON
| constant_assignment SEMICOLON
| variable_declaration
| enum_specifier SEMICOLON
| constant_declaration
| WHILE LEFT_PARENTHESIS boolean_expression RIGHT_PARENTHESIS statement { $$ = create_operation_node(kabsa::Parser::token::WHILE, 2, $3, $5); }
| DO statement WHILE LEFT_PARENTHESIS boolean_expression RIGHT_PARENTHESIS SEMICOLON { $$ = create_operation_node(kabsa::Parser::token::DO, 2, $5, $2); }
| IF LEFT_PARENTHESIS boolean_expression RIGHT_PARENTHESIS statement %prec IFX { $$ = create_operation_node(kabsa::Parser::token::IF, 2, $3, $5); }
| IF LEFT_PARENTHESIS boolean_expression RIGHT_PARENTHESIS statement ELSE statement { $$ = create_operation_node(kabsa::Parser::token::IF, 3, $3, $5, $7); }
| FOR LEFT_PARENTHESIS variable_assignment SEMICOLON boolean_expression SEMICOLON variable_assignment RIGHT_PARENTHESIS statement { $$ = create_operation_node(kabsa::Parser::token::FOR, 4, $3, $5, $7, $9); }
| SWITCH LEFT_PARENTHESIS expression RIGHT_PARENTHESIS LEFT_BRACES labeled_statements RIGHT_BRACES { $$ = create_operation_node(kabsa::Parser::token::SWITCH, 2, $3, $6); }
| LEFT_BRACES statements RIGHT_BRACES { $$ = $2; }
;
function_call
: IDENTIFIER LEFT_PARENTHESIS arguments RIGHT_PARENTHESIS { $$ = create_operation_node(kabsa::Parser::token::CALL, 2, get_identifier_node(*driver.location_, $1, std::vector<identifierEnum> {FUNCTION_TYPE}), $3); }
;
variable_assignment
: IDENTIFIER ASSIGN expression { $$ = create_operation_node(kabsa::Parser::token::ASSIGN, 2, create_identifier_node(*driver.location_, $1, VARIABLE_TYPE), $3); }
;
constant_assignment
: CONSTANT IDENTIFIER ASSIGN expression { $$ = create_operation_node(kabsa::Parser::token::ASSIGN, 2, create_identifier_node(*driver.location_, $2, CONSTANT_TYPE), $4); }
;
variable_declaration
: IDENTIFIER SEMICOLON { $$ = create_identifier_node(*driver.location_, $1, VARIABLE_TYPE, false); }
;
constant_declaration
: CONSTANT IDENTIFIER SEMICOLON { $$ = create_identifier_node(*driver.location_, $2, CONSTANT_TYPE, false); }
;
statements
: statement
| statements statement { $$ = create_operation_node(kabsa::Parser::token::SEMICOLON, 2, $1, $2); }
;
parameters
: %empty {}
| IDENTIFIER { $$ = create_identifier_node(*driver.location_, $1, FUNCTION_PARAMETER_TYPE); }
| parameters COMMA IDENTIFIER { $$ = create_operation_node(kabsa::Parser::token::COMMA, 2, $1, create_identifier_node(*driver.location_, $3, FUNCTION_PARAMETER_TYPE)); }
;
arguments
: %empty {}
| expression
| arguments COMMA expression { $$ = create_operation_node(kabsa::Parser::token::PUSH_ARGS, 2, $1, $3); }
;
expression
: INTEGER { $$ = create_integer_number_node($1); }
| DOUBLE { $$ = create_double_number_node($1); }
| boolean_expression
| function_call
| IDENTIFIER { $$ = get_identifier_node(*driver.location_, $1, std::vector<identifierEnum> {CONSTANT_TYPE, VARIABLE_TYPE, FUNCTION_PARAMETER_TYPE, ENUM_TYPE}); }
| MINUS expression %prec UMINUS { $$ = create_operation_node(kabsa::Parser::token::UMINUS, 1, $2); }
| expression PLUS expression { $$ = create_operation_node(kabsa::Parser::token::PLUS, 2, $1, $3); }
| expression MINUS expression { $$ = create_operation_node(kabsa::Parser::token::MINUS, 2, $1, $3); }
| expression MULTIPLY expression { $$ = create_operation_node(kabsa::Parser::token::MULTIPLY, 2, $1, $3); }
| expression DIVIDE expression { $$ = create_operation_node(kabsa::Parser::token::DIVIDE, 2, $1, $3); }
| LEFT_PARENTHESIS expression RIGHT_PARENTHESIS { $$ = $2; }
;
labeled_statements
: labeled_statement
| labeled_statements labeled_statement { $$ = create_operation_node(kabsa::Parser::token::SEMICOLON, 2, $1, $2); }
;
labeled_statement
: CASE constant_expression COLON statement { $$ = create_operation_node(kabsa::Parser::token::CASE, 2, $2, $4); }
| DEFAULT COLON statement { $$ = create_operation_node(kabsa::Parser::token::DEFAULT, 1, $3); }
| DEFAULT COLON { $$ = create_operation_node(kabsa::Parser::token::DEFAULT, 0); }
| BREAK SEMICOLON { $$ = create_operation_node(kabsa::Parser::token::BREAK, 0); }
;
constant_expression
: INTEGER { $$ = create_integer_number_node($1); }
| IDENTIFIER { $$ = get_identifier_node(*driver.location_, $1, std::vector<identifierEnum> {CONSTANT_TYPE, ENUM_TYPE}); }
;
enum_specifier
: ENUM LEFT_BRACES enum_list RIGHT_BRACES { $$ = create_operation_node(kabsa::Parser::token::ENUM, 1, $3); }
| ENUM IDENTIFIER LEFT_BRACES enum_list RIGHT_BRACES { $$ = create_operation_node(kabsa::Parser::token::ENUM, 2, $4, create_identifier_node(*driver.location_, $2, ENUM_SPECIFIER)); }
| ENUM IDENTIFIER LEFT_BRACES RIGHT_BRACES { $$ = create_operation_node(kabsa::Parser::token::ENUM, 0); }
;
enum_list
: IDENTIFIER { $$ = create_identifier_node(*driver.location_, $1, ENUM_TYPE); }
| enum_list COMMA IDENTIFIER { $$ = create_operation_node(kabsa::Parser::token::COMMA, 2, $1, create_identifier_node(*driver.location_, $3, ENUM_TYPE)); }
;
boolean_expression
: TRUE { $$ = create_integer_number_node(1); }
| FALSE { $$ = create_integer_number_node(0); }
| expression LT expression { $$ = create_operation_node(kabsa::Parser::token::LT, 2, $1, $3); }
| expression GT expression { $$ = create_operation_node(kabsa::Parser::token::GT, 2, $1, $3); }
| expression GE expression { $$ = create_operation_node(kabsa::Parser::token::GE, 2, $1, $3); }
| expression LE expression { $$ = create_operation_node(kabsa::Parser::token::LE, 2, $1, $3); }
| expression NE expression { $$ = create_operation_node(kabsa::Parser::token::NE, 2, $1, $3); }
| expression EQ expression { $$ = create_operation_node(kabsa::Parser::token::EQ, 2, $1, $3); }
| expression AND expression { $$ = create_operation_node(kabsa::Parser::token::AND, 2, $1, $3); }
| expression OR expression { $$ = create_operation_node(kabsa::Parser::token::OR, 2, $1, $3); }
| NOT expression { $$ = create_operation_node(kabsa::Parser::token::NOT, 1, $2); }
;
%%
namespace kabsa
{
void Parser::error(const location& l, const std::string& m) {
std::cerr << *driver.location_ << ": " << m << std::endl;
}
Node *create_integer_number_node(int value) {
NumberNode<int> *node = new NumberNode<int>(value);
return node;
}
Node *create_double_number_node(double value) {
NumberNode<double> *node = new NumberNode<double>(value);
return node;
}
Node *create_identifier_node(const location& l, std::string key, identifierEnum identifier_type, bool assignment) {
auto iterator = symbol_table.find(key);
if (identifier_type == VARIABLE_TYPE && iterator != symbol_table.end()) {
if (iterator->second->getIdentifierType() == CONSTANT_TYPE) {
yyerror(l, "Cannot reassign value to constant variable");
}
if (!assignment) {
std::cerr << l << ": " << identifierTypeAsString(iterator->second->getIdentifierType()) + " redeclaration" << std::endl; exit(1);
}
}
IdentifierNode *node = new IdentifierNode(key, identifier_type, assignment);
symbol_table[key] = node;
return node;
}
bool isValidType(identifierEnum identifier_type, std::vector<identifierEnum> valid_identifier_types) {
auto iterator = std::find(valid_identifier_types.begin(), valid_identifier_types.end(), identifier_type);
if (iterator != valid_identifier_types.end()) {
return true;
}
return false;
}
std::string identifierTypeAsString(identifierEnum identifier_type) {
switch(identifier_type) {
case CONSTANT_TYPE:
return "constant variable";
case FUNCTION_TYPE:
return "function";
case ENUM_TYPE:
return "enumerator";
case ENUM_SPECIFIER:
return "enumerator specifier";
default:
return "variable";
}
}
Node *get_identifier_node(const location& l, std::string key, std::vector<identifierEnum> valid_identifier_types) {
auto iterator = symbol_table.find(key);
if (iterator != symbol_table.end()) {
IdentifierNode *node = iterator->second;
if (not isValidType(node->getIdentifierType(), valid_identifier_types)) {
std::string valid_types_as_strings;
std::string separator = " or as a ";
std::string last_variable_type_string = "";
for(identifierEnum valid_type : valid_identifier_types) {
std::string type_string = identifierTypeAsString(valid_type);
if (last_variable_type_string != type_string) {
valid_types_as_strings = valid_types_as_strings + type_string + separator;
}
last_variable_type_string = type_string;
}
valid_types_as_strings = valid_types_as_strings.substr(0, valid_types_as_strings.length() - separator.length());
std::cerr << l << ": " << "Cannot use " << identifierTypeAsString(node->getIdentifierType()) << " as a " << valid_types_as_strings << std::endl; exit(1);
}
if (!node->isInitialized()){
std::cerr << l << ": " << "Uninitialized " + identifierTypeAsString(node->getIdentifierType()) + " used" << std::endl; exit(1);
}
return node;
}
else {
if (isValidType(FUNCTION_TYPE, valid_identifier_types)) {
std::cerr << l << ": " << "Undeclared " << identifierTypeAsString(FUNCTION_TYPE) << " used" << std::endl; exit(1);
}
else if (isValidType(CONSTANT_TYPE, valid_identifier_types) && !isValidType(VARIABLE_TYPE, valid_identifier_types)) {
std::cerr << l << ": " << "Undeclared " << identifierTypeAsString(CONSTANT_TYPE) << " used" << std::endl; exit(1);
}
else {
std::cerr << l << ": " << "Undeclared " << identifierTypeAsString(VARIABLE_TYPE) << " used" << std::endl; exit(1);
}
}
return NULL;
}
Node *create_operation_node(int operation_token, int num_of_operands, ...) {
va_list arguments_list;
OperationNode *node = new OperationNode(operation_token);
va_start(arguments_list, num_of_operands);
for (int i = 0; i < num_of_operands; i++)
node->addOperandNode(va_arg(arguments_list, Node *));
va_end(arguments_list);
return node;
}
void generate(Node *node) {
int label_1, label_2;
if (!node) return;
switch(node->getNodeType()) {
case NUMBER_TYPE: {
NumberNode<int> *integer_number_node = dynamic_cast<NumberNode<int> *>(node);
NumberNode<double> *double_number_node = dynamic_cast<NumberNode<double> *>(node);
if (integer_number_node == NULL) {
assembly_ss<< "\tPUSH\t" << double_number_node->getValue() << std::endl;
}
else {
assembly_ss << "\tPUSH\t" << integer_number_node->getValue() << std::endl;
}
} break;
case IDENTIFIER_TYPE: {
IdentifierNode *identifier_node = dynamic_cast<IdentifierNode *>(node);
switch(identifier_node->getIdentifierType()) {
case FUNCTION_PARAMETER_TYPE: {
assembly_ss << "\tPOP\t" << identifier_node->getKey() << std::endl;
} break;
default:
assembly_ss << "\tPUSH\t" << identifier_node->getKey() << std::endl;
}
} break;
case OPERATION_TYPE: {
OperationNode *operation_node = dynamic_cast<OperationNode *>(node);
switch(operation_node->getOperatorToken()) {
case kabsa::Parser::token::WHILE: {
label_1 = last_used_label++;
assembly_ss << 'L' << std::setfill('0') << std::setw(4) << label_1 << ':' << std::endl;
generate(operation_node->getOperandNode(0));
label_2 = last_used_label++;
assembly_ss << "\tJNZ\tL" << std::setfill('0') << std::setw(4) << label_2 << std::endl;
generate(operation_node->getOperandNode(1));
assembly_ss << "\tJMP\tL" << std::setfill('0') << std::setw(4) << label_1 << std::endl;
assembly_ss << 'L' << std::setfill('0') << std::setw(4) << label_2 << ':' << std::endl;
} break;
case kabsa::Parser::token::DO: {
label_1 = last_used_label++;
assembly_ss << 'L' << std::setfill('0') << std::setw(4) << label_1 << ':' << std::endl;
generate(operation_node->getOperandNode(1));
generate(operation_node->getOperandNode(0));
assembly_ss << "\tJZ\tL" << std::setfill('0') << std::setw(4) << label_1 << std::endl;
} break;
case kabsa::Parser::token::FOR: {
generate(operation_node->getOperandNode(0));
label_1 = last_used_label++;
assembly_ss << 'L' << std::setfill('0') << std::setw(4) << label_1 << ':' << std::endl;
generate(operation_node->getOperandNode(1));
label_2 = last_used_label++;
assembly_ss << "\tJNZ\tL" << std::setfill('0') << std::setw(4) << label_2 << std::endl;
generate(operation_node->getOperandNode(3));
generate(operation_node->getOperandNode(2));
assembly_ss << "\tJMP\tL" << std::setfill('0') << std::setw(4) << label_1 << std::endl;
assembly_ss << 'L' << std::setfill('0') << std::setw(4) << label_2 << ':' << std::endl;
} break;
case kabsa::Parser::token::IF: {
generate(operation_node->getOperandNode(0));
if (operation_node->getNumberOfOperands() > 2) {
label_1 = last_used_label++;
assembly_ss << "\tJNZ\tL" << std::setfill('0') << std::setw(4) << label_1 << std::endl;
generate(operation_node->getOperandNode(1));
label_2 = last_used_label++;
assembly_ss << "\tJMP\tL" << std::setfill('0') << std::setw(4) << label_2 << std::endl;
assembly_ss << 'L' << std::setfill('0') << std::setw(4) << label_1 << ':' << std::endl;
generate(operation_node->getOperandNode(2));
assembly_ss << 'L' << std::setfill('0') << std::setw(4) << label_2 << ':' << std::endl;
} else {
label_1 = last_used_label++;
assembly_ss << "\tJNZ\tL" << std::setfill('0') << std::setw(4) << label_1 << std::endl;
generate(operation_node->getOperandNode(1));
assembly_ss << 'L' << std::setfill('0') << std::setw(4) << label_1 << ':' << std::endl;
}
} break;
case kabsa::Parser::token::FUNCTION: {
IdentifierNode *function_identifier_node = dynamic_cast<IdentifierNode *>(operation_node->getOperandNode(0));
assembly_ss << "\tPROC\t" << function_identifier_node->getKey() << std::endl;
generate(operation_node->getOperandNode(1));
generate(operation_node->getOperandNode(2));
assembly_ss << "\tENDP\t" << function_identifier_node->getKey() << std::endl;
} break;
case kabsa::Parser::token::RETURN: {
if (operation_node->getNumberOfOperands() > 0) {
generate(operation_node->getOperandNode(0));
}
assembly_ss << "\tRET" << std::endl;
} break;
case kabsa::Parser::token::CALL: {
IdentifierNode *function_identifier_node = dynamic_cast<IdentifierNode *>(operation_node->getOperandNode(0));
generate(operation_node->getOperandNode(1));
assembly_ss << "\tCALL\t" << function_identifier_node->getKey() << std::endl;
} break;
case kabsa::Parser::token::ASSIGN: {
generate(operation_node->getOperandNode(1));
IdentifierNode *identifier_operand = dynamic_cast<IdentifierNode *>(operation_node->getOperandNode(0));
assembly_ss << "\tPOP\t" << identifier_operand->getKey() << std::endl;
} break;
case kabsa::Parser::token::UMINUS: {
generate(operation_node->getOperandNode(0));
assembly_ss << "\tNEG" << std::endl;
} break;
case kabsa::Parser::token::PUSH_ARGS: {
generate(operation_node->getOperandNode(1));
generate(operation_node->getOperandNode(0));
} break;
case kabsa::Parser::token::SWITCH: {
std::vector<OperationNode *> labeled_statements;
std::vector<int> switch_labels;
OperationNode *labeled_statement = dynamic_cast<OperationNode *>(operation_node->getOperandNode(1));
while (labeled_statement->getOperatorToken() == kabsa::Parser::token::SEMICOLON) {
labeled_statements.push_back(dynamic_cast<OperationNode *>(labeled_statement->getOperandNode(1)));
labeled_statement = dynamic_cast<OperationNode *>(labeled_statement->getOperandNode(0));
}
labeled_statements.push_back(labeled_statement);
std::reverse(labeled_statements.begin(), labeled_statements.end());
for (int i = 0; i < labeled_statements.size(); i++) {
switch(labeled_statements[i]->getOperatorToken()) {
case kabsa::Parser::token::CASE: {
generate(operation_node->getOperandNode(0));
generate(labeled_statements[i]->getOperandNode(0));
assembly_ss << "\tCMPEQ" << std::endl;
label_1 = last_used_label++;
assembly_ss << "\tJZ\tL" << std::setfill('0') << std::setw(4) << label_1 << std::endl;
switch_labels.push_back(label_1);
} break;
}
}
label_1 = last_used_label++;
assembly_ss << "\tJMP\tL" << std::setfill('0') << std::setw(4) << label_1 << std::endl;
switch_labels.push_back(label_1);
int break_label = last_used_label++;
int label_index = 0;
for (int i = 0; i < labeled_statements.size(); i++) {
switch(labeled_statements[i]->getOperatorToken()) {
case kabsa::Parser::token::CASE: {
assembly_ss << 'L' << std::setfill('0') << std::setw(4) << switch_labels[label_index++] << ':' << std::endl;
generate(labeled_statements[i]->getOperandNode(1));
} break;
case kabsa::Parser::token::DEFAULT: {
assembly_ss << 'L' << std::setfill('0') << std::setw(4) << switch_labels[label_index++] << ':' << std::endl;
if (labeled_statements[i]->getNumberOfOperands() > 0) {
generate(labeled_statements[i]->getOperandNode(0));
}
} break;
case kabsa::Parser::token::BREAK: {
assembly_ss << "\tJMP\tL" << std::setfill('0') << std::setw(4) << break_label << std::endl;
} break;
}
}
assembly_ss << 'L' << std::setfill('0') << std::setw(4) << break_label << ':' << std::endl;
} break;
case kabsa::Parser::token::ENUM: {
if (operation_node->getNumberOfOperands() > 0) {
std::vector<IdentifierNode *> enumerator_nodes;
int enum_value = 0;
OperationNode *enum_list_op_node = dynamic_cast<OperationNode *>(operation_node->getOperandNode(0));
Node *backup_node = operation_node->getOperandNode(0);
while (enum_list_op_node != NULL) {
enumerator_nodes.push_back(dynamic_cast<IdentifierNode *>(enum_list_op_node->getOperandNode(1)));
backup_node = enum_list_op_node->getOperandNode(0);
enum_list_op_node = dynamic_cast<OperationNode *>(backup_node);
}
if (enum_list_op_node == NULL) {
IdentifierNode *enum_list_id_node = dynamic_cast<IdentifierNode *>(backup_node);
if (enum_list_id_node != NULL) {
enumerator_nodes.push_back(enum_list_id_node);
}
}
std::reverse(enumerator_nodes.begin(), enumerator_nodes.end());
for(IdentifierNode *enumerator_node : enumerator_nodes) {
generate(create_operation_node(kabsa::Parser::token::ASSIGN, 2, enumerator_node, create_integer_number_node(enum_value++)));
}
}
} break;
default:
generate(operation_node->getOperandNode(0));
generate(operation_node->getOperandNode(1));
switch(operation_node->getOperatorToken()) {
case kabsa::Parser::token::PLUS: assembly_ss << "\tADD" << std::endl; break;
case kabsa::Parser::token::MINUS: assembly_ss << "\tSUB" << std::endl; break;
case kabsa::Parser::token::MULTIPLY: assembly_ss << "\tMUL" << std::endl; break;
case kabsa::Parser::token::DIVIDE: assembly_ss << "\tDIV" << std::endl; break;
case kabsa::Parser::token::LT: assembly_ss << "\tCMPLT" << std::endl; break;
case kabsa::Parser::token::GT: assembly_ss << "\tCMPGT" << std::endl; break;
case kabsa::Parser::token::GE: assembly_ss << "\tCMPGE" << std::endl; break;
case kabsa::Parser::token::LE: assembly_ss << "\tCMPLE" << std::endl; break;
case kabsa::Parser::token::NE: assembly_ss << "\tCMPNE" << std::endl; break;
case kabsa::Parser::token::EQ: assembly_ss << "\tCMPEQ" << std::endl; break;
case kabsa::Parser::token::AND: assembly_ss << "\tAND" << std::endl; break;
case kabsa::Parser::token::OR: assembly_ss << "\tOR" << std::endl; break;
case kabsa::Parser::token::NOT: assembly_ss << "\tNOT" << std::endl; break;
}
}
} break;
}
}
bool write_symbol_table_file() {
// std::ofstream outfile(output_directory + "/" + filename + "_symbol_table.txt");
// if(!outfile.is_open()) {
// std::cout<< "Unable to create file at the given directory" << std::endl;
// return false;
// }
// std::stringstream ss;
std::cout<<"----------------------------------------------------------------------------------------\n";
std::cout<<"| Variable | Type | Initialized |\n";
std::cout<<"----------------------------------------------------------------------------------------\n";
for (std::pair<std::string, IdentifierNode*> element : symbol_table)
{
//std::cout << element.first << " :: " << element.second << std::endl;
std::cout << "|"<<std::setfill(' ') << std::setw(15) << element.first << std::setfill(' ') << std::setw(14) << "|" <<
std::setfill(' ') << std::setw(18) << identifierTypeAsString(element.second->getIdentifierType()) << std::setfill(' ') << std::setw(11) << "|"
<< std::setfill(' ') << std::setw(15) << element.second->isInitialized() << std::setfill(' ') << std::setw(14) << "|" << std::endl;
std::cout<<"----------------------------------------------------------------------------------------\n";
}
// outfile<< ss.rdbuf();
// outfile.close();
// std::cout<< "Symbol table file created at: \""<< output_directory << "\", with name: \"" << filename << "_symbol_table.txt\"" << std::endl;
return true;
}
}