-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoken_struct.c
1485 lines (1216 loc) · 46.2 KB
/
token_struct.c
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "utils.h"
#include "token_struct.h"
#include "codegen_functions.h"
/*------------ Token Structure Methods ------------ */
/**
* Creates a new token node.
*
* @param token_type Type of the Token;
* @return A new token node.
*/
TokenNode* newTokenNode(int token_type){
TokenNode *_new_token_node;
/* Malloc new token node */
_new_token_node = mallocIt(_new_token_node);
/* Give a fatal error if malloc has errors */
if(_new_token_node == NULL){
printFatalError("FATAL ERROR CANNOT ALLOCATE A NEW TOKEN!");
exit(EXIT_FAILURE);
}
/* Initialize new token node */
_new_token_node->root_token = NULL;
_new_token_node->token_type = token_type;
_new_token_node->token_str = NULL;
_new_token_node->lex_str = NULL;
_new_token_node->child_list = newTokenList();
/* Return pointer to the new token */
return _new_token_node;
}
/**
* Destroy an already allocated token node.
*
* @param token_node that will be destroyed.
* @param deleteChilds if this flag is true it will destroy all childs related to this token.
* @return true if there's no error on execution and false otherwise.
*/
bool deleteTokenNode(ptrTokenNode *token_node, bool deleteChilds){
/* Check if token node exists */
if(token_node == NULL){
printError("CANNOT DESTROY A NULL TOKEN VARIABLE NODE!");
return false;
}
/* Check if token node exists */
if((*token_node) == NULL){
printError("CANNOT DESTROY A NULL TOKEN NODE!");
return false;
}
/* Free token_str if it exists */
if((*token_node)->token_str != NULL){
secureFree((*token_node)->token_str);
}
/* Free lex_str if it exists */
if((*token_node)->lex_str != NULL){
secureFree((*token_node)->lex_str);
}
/* Delete child list, and childs if destroyChild is true */
deleteTokenList(&(*token_node)->child_list, deleteChilds);
/* Free tokne_node pointer */
secureFree((*token_node));
/* Return success */
return true;
}
/**
* Add Token String.
*
* @param token_node Token which lex_str will be appended.
* @param token_str Token string value to be copied to TokenNode.
* @return true if there's no error on execution and false otherwise.
*/
bool nodeAddTokenStr(TokenNode *token_node, char *token_str){
char *_new_token_str;
/* Check if token_node isn't null */
if(token_node == NULL){
printError("TOKEN NODE IS NULL!");
return false;
}
/* Allocate new token_str */
_new_token_str = mallocItString(_new_token_str, token_str);
/* Check if malloc has succeed */
if(_new_token_str == NULL){
printError("FATAL ERROR CANNOT ALLOCATE STRING FOR TOKEN!");
return false;
}
/* Check if this token already has token str */
if(token_node->token_str != NULL){
secureFree(token_node->token_str);
}
/* Store the new token_str array pointer */
token_node->token_str = _new_token_str;
/* Copy content from token_str to the new token */
secureStringCopy(token_node->token_str, token_str);
/* Return success */
return true;
}
/**
* Add Lexical String.
*
* @param token_node Token which lex_str will be appended.
* @param lex_str Lex string value to be copied to TokenNode.
* @return true if there's no error on execution and false otherwise.
*/
bool nodeAddLexStr(TokenNode *token_node, char *lex_str){
char *_new_lex_str;
/* Check if token_node isn't null */
if(token_node == NULL){
printError("TOKEN NODE IS NULL!");
return false;
}
/* Allocate new lex_str */
_new_lex_str = mallocItString(_new_lex_str, lex_str);
/* Check if malloc has succeed */
if(_new_lex_str == NULL){
printError("FATAL ERROR CANNOT ALLOCATE STRING FOR TOKEN!");
return false;
}
/* Check if this token already has token str */
if(token_node->lex_str != NULL){
secureFree(token_node->lex_str);
}
/* Store the new lex_str array pointer */
token_node->lex_str = _new_lex_str;
/* Copy content from lex_str to the new token */
secureStringCopy(token_node->lex_str, lex_str);
/* Return success */
return true;
}
/**
* Add Root Token.
*
* @param token_node Token which lex_str will be appended.
* @param token_root Token root pointer value to be copied to TokenNode.
* @return true if there's no error on execution and false otherwise.
*/
bool nodeAddRootToken(TokenNode *token_node, TokenNode *root_token){
/* Check if token_node isn't null */
if(token_node == NULL){
printError("TOKEN NODE IS NULL!");
return false;
}
/* Check if token_node isn't null */
if(root_token == NULL){
printInfo("ROOT TOKEN IS NULL!");
}
/* Store the new token_str array pointer */
token_node->root_token = root_token;
/* Return success */
return true;
}
/* --------------- Token List Methods ------------- */
/**
* Initialize a list of tokens.
*
* @return Return a new token list.
*/
TokenList* newTokenList(){
TokenList *_new_token_list;
TokenNode **_items_list;
/* Try allocate a new token list */
_new_token_list = mallocIt(_new_token_list);
/* Give a fatal error if malloc has errors */
if(_new_token_list == NULL){
printFatalError("FATAL ERROR CANNOT ALLOCATE TOKEN LIST!");
exit(EXIT_FAILURE);
}
/* Try to allocate an array of tokens */
_items_list = mallocItArray(_items_list, DEFAULT_BLOCK_SIZE);
/* Give a fatal error if malloc has errors */
if(_items_list == NULL){
printFatalError("FATAL ERROR CANNOT ALLOCATE ITEMS OF THE TOKEN LIST!");
exit(EXIT_FAILURE);
}
/* Initialize token list */
_new_token_list->length = 0;
_new_token_list->size = DEFAULT_BLOCK_SIZE;
_new_token_list->items = _items_list;
/* Return the new token list */
return _new_token_list;
}
/**
* Destroy an already allocated token list.
*
* @param token_list pointer to the token list that will be destroyed.
* @param deleteChilds if this flag is true it will delete childs recursively.
* @return true if there's no error on execution and false otherwise.
*/
bool deleteTokenList(ptrTokenList *token_list, bool deleteChilds){
int i;
TokenNode *token_child;
/* Check if token node exists */
if(token_list == NULL){
printError("CANNOT DESTROY A NULL TOKEN LIST VARIABLE NODE!");
return false;
}
/* Check if token node exists */
if((*token_list) == NULL){
printError("CANNOT DESTROY AN ALREADY NULL TOKEN LIST NODE!");
return false;
}
/* If deleteChild is set to true it will delete all token childs related to this token */
if(deleteChilds){
for(i = 1; i <= (*token_list)->length; i++){
/* Get the actual node */
token_child = listGetTokenByIndex((*token_list), i);
/* Check if for some reason token child is null */
if(token_child == NULL){
printWarning("CHILD TOKEN IS NULL!");
continue;
}
/* Delete all childs recursively */
deleteTokenNode(&token_child, deleteChilds);
}
}
/* Free array */
secureFree((*token_list)->items);
/* Free array */
secureFree((*token_list));
/* Null the pointer apponted by token_list */
(*token_list) = NULL;
/* Return success */
return true;
}
/**
* Add an item to a list of tokens.
*
* @param token_list A pointer to a token list structure.
* @param token Token to be added.
* @return true if there's no error on execution and false otherwise.
*/
bool listAddToken(TokenList *token_list, TokenNode *token){
TokenNode **_reallocated_items;
/* Check if token isn't null */
if(token_list == NULL){
printError("TOKEN LIST IS NULL!");
return false;
}
/* Check if token isn't null */
if(token == NULL){
printError("TOKEN IS NULL!");
return false;
}
/* If array is full, add more empty spaces */
if((token_list->length + 1) == token_list->size){
/* Increase structure size */
token_list->size += DEFAULT_BLOCK_SIZE;
/* Try reallocate array of token_list items */
_reallocated_items = reallocItArray(token_list->items, token_list->size);
/* Give a fatal error if malloc has errors */
if(_reallocated_items == NULL){
printFatalError("FATAL ERROR CANNOT REALLOCATE TOKEN LIST ITEMS!");
exit(EXIT_FAILURE);
}
/* Store the pointer to the new items list array */
token_list->items = _reallocated_items;
}
/* Store pointer of the new token on items list */
token_list->items[token_list->length] = token;
/* Increment list length */
token_list->length += 1;
/* Return success */
return true;
}
/**
* Retrieve a token by it's given index, this function assume that valid index is on range from 1 to n.
*
* @param token_list A pointer to a token list structure.
* @param index Index of the item retrieved.
* @return Pointer to the token at the given index.
*/
TokenNode* listGetTokenByIndex(TokenList *token_list, int index){
/* Check if either token or token_list isn't null */
if(token_list == NULL){
printError("TOKEN LIST IS NULL!");
return NULL;
}
/* Invalid index used */
if(index <= 0){
printError("INVALID INDEX!");
return NULL;
}
/* Out of bounds access */
if(index > token_list->length){
printError("INDEX OUT OF BOUNDS!");
return NULL;
}
/* return Token pointer */
return token_list->items[index - 1];
}
/**
* Retrieve a list of tokens by it's given type.
*
* @param token_list A pointer to a token list structure.
* @param token_type Type of the wanted tokens.
* @return Pointer to a generated list of tokens of a given type.
*/
TokenList* listGetTokensByType(TokenList *token_list, int token_type){
int i;
TokenList *_tokens_found;
TokenNode *_token_i;
/* Create a new token list */
_tokens_found = newTokenList();
/* Search for the given types */
for(i = 1; i <= token_list->length; i++){
/* Pick the token at index i */
_token_i = listGetTokenByIndex(token_list, i);
/* Check if token_list item has the type wanted, and add this item to the found list */
if(_token_i->token_type == token_type){
listAddToken(_tokens_found, _token_i);
}
}
/* Return token founds */
return _tokens_found;
}
/* -------------- Symbol Node Methods ------------- */
/**
* Create a new symbol node, if symbol value it's null the symbol is of type nil.
*
* @param root_symbol_table Root symbol table of the given node.
* @param symbol_name Name of the given symbol.
* @param symbol_value Value of the symbol, use NULL to store nil value.
* @return A new symbol, or fatal erro cause it fails.
*/
SymbolNode* newSymbolNode(SymbolTable *root_symbol_table, char *symbol_name, int symbol_address, int symbol_type){
SymbolNode *_new_symbol_node;
char *_new_symbol_name;
/* Check if root symbol table is valid */
if(root_symbol_table == NULL){
printError("INVALID ROOT SYMBOL TABLE!");
return NULL;
}
/* Try allocate this new symbol */
_new_symbol_node = mallocIt(_new_symbol_node);
/* Give a fatal error if malloc has errors */
if(_new_symbol_node == NULL){
printFatalError("FATAL ERROR CANNOT ALLOCATE NEW SYMBOL!");
exit(EXIT_FAILURE);
}
/* Try allocate the name of the new symbol */
_new_symbol_name = mallocItString(_new_symbol_name, symbol_name);
/* Give a fatal error if malloc has errors */
if(_new_symbol_name == NULL){
printFatalError("FATAL ERROR CANNOT ALLOCATE NEW SYMBOL!");
exit(EXIT_FAILURE);
}
/* Copy content of the symbol name to this new structure */
secureStringCopy(_new_symbol_name, symbol_name);
/* Set the new symbol node */
_new_symbol_node->symbol_name = _new_symbol_name;
_new_symbol_node->symbol_address = symbol_address;
_new_symbol_node->symbol_type = symbol_type;
_new_symbol_node->symbol_size = DEFAULT_SYMBOL_SIZE;
_new_symbol_node->symbol_type_size = DEFAULT_SYMBOL_TYPE_SIZE;
_new_symbol_node->root_symbol_table = root_symbol_table;
/* Return pointer to this new node */
return _new_symbol_node;
}
/**
* Destroy an already allocated symbol node.
*
* @param symbol_node pointer to the symbol node that will be destroyed.
* @return true if there's no error on execution and false otherwise.
*/
bool deleteSymbolNode(ptrSymbolNode *symbol_node){
/* Check if token node exists */
if(symbol_node == NULL){
printError("CANNOT DESTROY A NULL TOKEN LIST VARIABLE NODE!");
return false;
}
/* Check if token node exists */
if((*symbol_node) == NULL){
printError("CANNOT DESTROY AN ALREADY NULL TOKEN LIST NODE!");
return false;
}
/* Check if there are a symbol name allocated and remove it */
if((*symbol_node)->symbol_name != NULL){
secureFree((*symbol_node)->symbol_name);
}
/* Free token list */
secureFree((*symbol_node));
/* Return success */
return true;
}
/**
* Compare symbol with a symbol name.
*
* @param symbol Symbol which will be compared.
* @param symbol_name Name of the symbol to check.
* @return true if symbol's are equal and false otherwise.
*/
bool symbolEqualsName(SymbolNode *symbol, char *symbol_name){
/* Check if symbol is not NULL */
if(symbol == NULL){
printError("SYMBOL IS NULL!");
return false;
}
/* Check if symbol_name is not NULL */
if(symbol_name == NULL){
printError("SYMBOL NAME IS NULL!");
return false;
}
/* Compare symbol name of symbol with symbol_name */
return (strcmp(symbol->symbol_name, symbol_name) == 0);
}
/**
* Get relative address of a symbol node.
*
* @param symbol_node Symbol node pointer structure.
* @return Integer relative stack address pointer;
*/
int symbolNodeGetSymbolAdress(SymbolNode *symbol_node){
/* Check if symbol node is not NULL */
if(symbol_node == NULL){
printError("ERROR SYMBOL NODE IS NULL!");
return -1;
}
/* Return symbol relative address */
return symbol_node->symbol_address;
}
/**
* Get symbol size of a given symbol node.
*
* @param symbol_node The node which size is wanted.
* @return Size in bytes of a given symbol node.
*/
int symbolNodeGetSymbolSize(SymbolNode *symbol_node){
/* Check if symbol node is not NULL */
if(symbol_node == NULL){
printError("ERROR SYMBOL NODE IS NULL!");
return -1;
}
/* Return symbol relative address */
return symbol_node->symbol_size;
}
/**
* Get define a symbol type into a register instruction.
*
* @param symbol_node The node which instruction is wanted.
* @return Pointer to a instruction node with the apropriated load instruction.
*/
InstructionNode* symbolNodeGetDefineInstruction(SymbolNode *symbol_node){
InstructionNode *_new_instruction_node;
/* Check if symbol node is not NULL */
if(symbol_node == NULL){
printError("ERROR SYMBOL NODE IS NULL!");
return NULL;
}
/* Check if this node have a valid root_symbol_table */
if(symbol_node->root_symbol_table == NULL){
printError("INVALID ROOT SYMBOL TABLE!");
return NULL;
}
/* Select which type of reference ponter this node have */
switch(symbol_node->root_symbol_table->register_type){
case REGISTER_TYPE_GP:
/* Global Pointer Register Type */
_new_instruction_node = newInstructionNode(formatedInstruction(mips_global_define, symbol_node->symbol_name), false);
break;
case REGISTER_TYPE_SP:
case REGISTER_TYPE_FP:
/* Stack Pointer and Frame Pointer Register Type */
_new_instruction_node = newInstructionNode(formatedInstruction(mips_local_define, symbol_node->symbol_address), false);
break;
default:
printError("UNKNOW REGISTER TYPE!");
return NULL;
}
/* Return the formated instruction */
return _new_instruction_node;
}
/**
* Get load a symbol into a register instruction.
*
* @param symbol_node The node which instruction is wanted.
* @return Pointer to a instruction node with the apropriated load instruction.
*/
InstructionNode* symbolNodeGetLoadInstruction(SymbolNode *symbol_node){
InstructionNode *_new_instruction_node;
/* Check if symbol node is not NULL */
if(symbol_node == NULL){
printError("ERROR SYMBOL NODE IS NULL!");
return NULL;
}
/* Check if this node have a valid root_symbol_table */
if(symbol_node->root_symbol_table == NULL){
printError("INVALID ROOT SYMBOL TABLE!");
return NULL;
}
/* Select which type of reference ponter this node have */
switch(symbol_node->root_symbol_table->register_type){
case REGISTER_TYPE_GP:
/* Global Pointer Register Type */
_new_instruction_node = newInstructionNode(formatedInstruction(mips_global_load, symbol_node->symbol_name), false);
break;
case REGISTER_TYPE_SP:
/* Stack Pointer Register Type */
_new_instruction_node = newInstructionNode(formatedInstruction(mips_local_load, (symbol_node->symbol_address), 's'), false);
break;
case REGISTER_TYPE_FP:
/* Frame Pointer Register Type */
_new_instruction_node = newInstructionNode(formatedInstruction(mips_local_load, (symbol_node->symbol_address), 'f'), false);
break;
default:
printError("UNKNOW REGISTER TYPE!");
return NULL;
}
/* Return the formated instruction */
return _new_instruction_node;
}
/**
* Get store a symbol into a register instruction.
*
* @param symbol_node The node which instruction is wanted.
* @return Pointer to a instruction node with the apropriated store instruction.
*/
InstructionNode* symbolNodeGetStoreInstruction(SymbolNode *symbol_node){
InstructionNode *_new_instruction_node;
/* Check if symbol node is not NULL */
if(symbol_node == NULL){
printError("ERROR SYMBOL NODE IS NULL!");
return NULL;
}
/* Check if this node have a valid root_symbol_table */
if(symbol_node->root_symbol_table == NULL){
printError("INVALID ROOT SYMBOL TABLE!");
return NULL;
}
/* Select which type of reference ponter this node have */
switch(symbol_node->root_symbol_table->register_type){
case REGISTER_TYPE_GP:
/* Global Pointer Register Type */
_new_instruction_node = newInstructionNode(formatedInstruction(mips_global_store, symbol_node->symbol_name), false);
break;
case REGISTER_TYPE_SP:
/* Stack Pointer Register Type */
_new_instruction_node = newInstructionNode(formatedInstruction(mips_local_store, symbol_node->symbol_address, 's'), false);
break;
case REGISTER_TYPE_FP:
/* Frame Pointer Register Type */
_new_instruction_node = newInstructionNode(formatedInstruction(mips_local_store, symbol_node->symbol_address, 'f'), false);
break;
default:
printError("UNKNOW REGISTER TYPE!");
return NULL;
}
/* Return the formated instruction */
return _new_instruction_node;
}
/**
* Get load a symbol type into a register instruction.
*
* @param symbol_node The node which instruction is wanted.
* @return Pointer to a instruction node with the apropriated load instruction.
*/
InstructionNode* symbolNodeGetLoadTypeInstruction(SymbolNode *symbol_node){
/* Check if symbol node is not NULL */
if(symbol_node == NULL){
printError("ERROR SYMBOL NODE IS NULL!");
return NULL;
}
/* Print a todo message */
printTodo("TODO GET STORE TYPE!");
/* Return the formated instruction */
return newInstructionNode(formatedInstruction("# TODO - LOAD TYPE\n"), false);
}
/**
* Get store a symbol type into a register instruction.
*
* @param symbol_node The node which instruction is wanted.
* @return Pointer to a instruction node with the apropriated store instruction.
*/
InstructionNode* symbolNodeGetStoreTypeInstruction(SymbolNode *symbol_node){
/* Check if symbol node is not NULL */
if(symbol_node == NULL){
printError("ERROR SYMBOL NODE IS NULL!");
return NULL;
}
/* Print a todo message */
printTodo("TODO GET STORE TYPE!");
/* Return the formated instruction */
return newInstructionNode(formatedInstruction("# TODO - STORE TYPE\n"), false);
}
/* ------------- Symbol Table Methods ------------- */
/**
* Create a new SymbolTable.
*
* @param previous_scope Previous symbol table, define symbol scope.
* @return An empty symbol table.
*/
SymbolTable* newSymbolTable(SymbolTable *previous_scope, int register_type){
SymbolTable *_new_symbol_table;
SymbolNode **_items_table;
/* Allocate a new symbol table */
_new_symbol_table = mallocIt(_new_symbol_table);
/* Give a fatal error if cannot allocate new symbol table */
if(_new_symbol_table == NULL){
printFatalError("FATAL ERROR CANNOT ALLOCATE NEW SYMBOL TABLE!");
exit(EXIT_FAILURE);
}
/* Allocate item symbols */
_items_table = mallocItArray(_items_table, DEFAULT_BLOCK_SIZE);
/* Give a fatal error if cannot allocate items of the table */
if(_items_table == NULL){
printFatalError("FATAL ERROR CANNOT ALLOCATE ARRAY OF SYMBOLS!");
exit(EXIT_FAILURE);
}
/* If there are no previous scope */
if(previous_scope == NULL){
_new_symbol_table->start_address = 0;
}
else{
_new_symbol_table->start_address = previous_scope->start_address + previous_scope->shift_address;
}
/* Set the new symbol table */
_new_symbol_table->size = DEFAULT_BLOCK_SIZE;
_new_symbol_table->length = 0;
_new_symbol_table->shift_address = 0;
_new_symbol_table->brother_table = NULL;
_new_symbol_table->items = _items_table;
_new_symbol_table->register_type = register_type;
_new_symbol_table->previous_scope = previous_scope;
/* Return pointer of the new symbol table */
return _new_symbol_table;
}
/**
* Create a new global Symbol Table.
*
* @return A new global symbol table.
*/
SymbolTable* newGlobalSymbolTable(){
SymbolTable *_new_global_symbol_table;
/* Allocate a new symbol table */
_new_global_symbol_table = newSymbolTable(NULL, REGISTER_TYPE_GP);
/* Give a fatal error if cannot allocate new symbol table */
if(_new_global_symbol_table == NULL){
printFatalError("FATAL ERROR CANNOT ALLOCATE NEW GLOBAL SYMBOL TABLE!");
exit(EXIT_FAILURE);
}
/* Return pointer of the new symbol table */
return _new_global_symbol_table;
}
/**
* Destroy an already allocated symbol table.
*
* @param symbol_table pointer to the symbol table that will be destroyed.
* @return true if there's no error on execution and false otherwise.
*/
bool deleteSymbolTable(ptrSymbolTable *symbol_table){
int i;
/* Check if token node exists */
if(symbol_table == NULL){
printError("CANNOT DESTROY A NULL SYMBOL TABLE VARIABLE!");
return false;
}
/* Check if token node exists */
if((*symbol_table) == NULL){
printError("CANNOT DESTROY AN ALREADY NULL SYMBOL TABLE!");
return false;
}
/* Check if there are items on this symbol table */
if((*symbol_table)->items != NULL){
/* Delete every symbol node on this table */
for(i = 0; i < (*symbol_table)->length; i++){
deleteSymbolNode(&(*symbol_table)->items[i]);
}
/* Remove this array */
secureFree((*symbol_table)->items);
}
/* Free symbol_table */
secureFree((*symbol_table));
/* Return success */
return true;
}
/* Increase or decrease size of all vars in a symbol table */
void symbolTableUpdateNodes(SymbolTable *symbol_table, int symbol_exp){
int i;
SymbolTable *_actual_symbol_table;
/* Assign the first value of actual symbol table */
_actual_symbol_table = symbol_table;
/* Update all variables in this symbol table structure */
while(_actual_symbol_table != NULL){
/* Check if symbol table already has a symbol with this name */
for(i = 0; i < _actual_symbol_table->length; i++){
fprintf(stderr, "%s :: %d\n", _actual_symbol_table->items[i]->symbol_name, _actual_symbol_table->items[i]->symbol_address);
/* Change the node address based on the new value */
_actual_symbol_table->items[i]->symbol_address += symbol_exp;
fprintf(stderr, "%s :: %d\n", _actual_symbol_table->items[i]->symbol_name, _actual_symbol_table->items[i]->symbol_address);
}
/* Get the previous symbol table */
_actual_symbol_table = _actual_symbol_table->previous_scope;
}
}
/**
* Add a brother symbol table to a given symbol table.
*
* @param symbol_table Table of symbols.
* @param brother_table Brother table of symbols.
* @return true if there's no error on execution and false otherwise.
*/
bool symbolTableAddBrother(SymbolTable *symbol_table, SymbolTable *brother_table){
/* Check if symbol table is not null */
if(symbol_table == NULL){
printError("INVALID TABLE OF SYMBOLS!");
return false;
}
/* Check if brother table is null, and show a warning about a redundant operation */
if(brother_table == NULL){
printWarning("BROTHER TABLE IS ALREADY NULL, THERE'S NO NECESSITY TO CHANGE THIS AGAIN!");
}
/* Assign brother_table to symbol table node */
symbol_table->brother_table = brother_table;
/* Return success */
return true;
}
/**
* Add a new symbol to symbol table.
*
* @param symbol_table Table of symbols.
* @param symbol_name Name of the symbol which will be appended to symbol table.
* @param symbol_type Type of the symbol which will be appended to symbol table.
* @return true if there's no error on execution and false otherwise.
*/
bool symbolTableAddSymbol(SymbolTable *symbol_table, char *symbol_name, int symbol_type){
SymbolNode *_new_symbol_node;
SymbolNode **_reallocated_items;
/* Check if symbol name is null or incorrect */
if((symbol_name == NULL) || (strlen(symbol_name) == 0)){
printError("SYMBOL NAME EMPTY OR INVALID !");
return false;
}
/* Check if there are space left on symbol table */
if((symbol_table->length + 1) == symbol_table->size){
/* Increase structure size */
symbol_table->size += DEFAULT_BLOCK_SIZE;
/* Try reallocate array of symbol_table items */
_reallocated_items = reallocItArray(symbol_table->items, symbol_table->size);
/* Give a fatal error if malloc has errors */
if(_reallocated_items == NULL){
printFatalError("FATAL ERROR CANNOT REALLOCATE SYMBOL TABLE ITEMS!");
exit(EXIT_FAILURE);
}
/* Store the pointer to the new items list array */
symbol_table->items = _reallocated_items;
}
/* Try allocate the new symbol node */
//_new_symbol_node = newSymbolNode(symbol_table, symbol_name, symbol_table->start_address + BYTE_VARIABLE_SIZE, symbol_type);
_new_symbol_node = newSymbolNode(symbol_table, symbol_name, BYTE_VARIABLE_SIZE, symbol_type);
/* Check if we can't allocate a new symbol node has returned no errors */
if(_new_symbol_node == NULL){
printFatalError("CANNOT CREATE NEW SYMBOL NODE!");
exit(EXIT_FAILURE);
}
/* Increase address of previous nodes */
symbolTableUpdateNodes(symbol_table, 4);
fprintf(stderr, "%s :: %d\n", symbol_name, symbol_table->shift_address);
/* Increase shift */
symbol_table->shift_address += BYTE_VARIABLE_SIZE;
fprintf(stderr, "%s :: %d\n", symbol_name, symbol_table->shift_address);
/* Add the new symbol */
symbol_table->items[symbol_table->length] = _new_symbol_node;
/* Increase number of symbols */
symbol_table->length += 1;
/* Return success */
return true;
}
/**
* Add a new logic symbol to symbol table.
*
* @param symbol_table Table of symbols.
* @return true if there's no error on execution and false otherwise.
*/
bool symbolTableAddLogicSymbol(SymbolTable *symbol_table){
SymbolNode *_new_symbol_node;
/* Check if symbol table is not NULL */
if(symbol_table == NULL){
printError("SYMBOL TABLE IS NULL!");
return false;
}
/* Try allocate the new symbol node */
_new_symbol_node = newSymbolNode(symbol_table, "$$logic$$", BYTE_VARIABLE_SIZE, VOID_TYPE);
/* Check if we had problems allocating this new symbol node */
if(_new_symbol_node == NULL){
printFatalError("CANNOT CREATE NEW SYMBOL NODE!");
exit(EXIT_FAILURE);
}
/* Add the new logic symbol */
symbol_table->items[0] = _new_symbol_node;
/* Increase number of symbols */
symbol_table->length += 1;
/* Return success */
return true;
}
/**
* Check if a symbol of a given name is present on the table.
*
* @param symbol_table Table of symbols.
* @param symbol_name symbol which is being searched.
* @return Return true if symbol name exist on table and false otherwise.
*/
bool symbolTableContains(SymbolTable *symbol_table, char *symbol_name){
int i;
SymbolTable *_actual_symbol_table;
/* Check if symbol table is not NULL */
if(symbol_table == NULL){
printError("SYMBOL TABLE IS NULL!");
return false;
}
/* Check if symbol_name is not NULL */
if(symbol_name == NULL){
printError("SYMBOL NAME IS NULL!");
return false;
}
/* Get the actual symbl table */
_actual_symbol_table = symbol_table;
/* Search for the variable in the actual symbol table */
while(_actual_symbol_table != NULL){
/* Check if the symbol table have elements and them search for */
if(symbol_table->length != 0){
/* Check if symbol table already has a symbol with this name */
for(i = 0; i < symbol_table->length; i++){