le compilateur super performant
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

symbol_table.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "symbol_table.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. void init_table(SymbolTable *table) {
  6. table->index = 0;
  7. table->temp_index = SYMBOL_TABLE_SIZE - 1;
  8. }
  9. void init_symbol_item(SymbolItem *symbol_item, size_t name_size) {
  10. symbol_item->type = 0;
  11. symbol_item->name = malloc(name_size);
  12. symbol_item->address = 0;
  13. symbol_item->init = 0;
  14. }
  15. int has_symbol(SymbolTable* table, char *name) {
  16. for (int i = 0; i < table->index; i++) {
  17. if (strcmp(table->table[i].name, name) == 0) {
  18. return 1;
  19. }
  20. }
  21. for (int i = SYMBOL_TABLE_SIZE -1; i > table->temp_index; i--) {
  22. if (strcmp(table->table[i].name, name) == 0) {
  23. return 1;
  24. }
  25. }
  26. return 0;
  27. }
  28. int add_symbol(SymbolTable *table, enum Type type, char *name) {
  29. if (table->index > table->temp_index) {
  30. return -2;
  31. }
  32. if (has_symbol(table, name)) {
  33. return -1;
  34. }
  35. SymbolItem newItem;
  36. init_symbol_item(&newItem, strlen(name));
  37. newItem.type = type;
  38. newItem.name = name;
  39. newItem.address = table->index;
  40. newItem.init = 0;
  41. table->table[table->index] = newItem;
  42. table->index++;
  43. return 0;
  44. }
  45. const SymbolItem* add_temp_symbol(SymbolTable* table, enum Type type) {
  46. if (table->temp_index < table->index) {
  47. return NULL;
  48. }
  49. SymbolItem *newItem = &table->table[table->temp_index];
  50. init_symbol_item(newItem, 10);
  51. newItem->type = type;
  52. sprintf(newItem->name, "_temp%d", table->temp_index);
  53. newItem->address = table->temp_index;
  54. newItem->init = 0;
  55. table->temp_index--;
  56. return newItem;
  57. }
  58. int mark_symbol_initialized(SymbolTable *table, char *name) {
  59. struct SymbolItem *item = get_symbol_item(table, name);
  60. if (item != NULL) {
  61. if (item->init == 1) {
  62. return -2;
  63. }
  64. item->init = 1;
  65. return 0;
  66. }
  67. return -1;
  68. }
  69. char* type_to_string(enum Type type) {
  70. if (type == TYPE_CONST) {
  71. return "const";
  72. } else {
  73. return " int ";
  74. }
  75. }
  76. void print_table(SymbolTable *table) {
  77. printf("Variables:\n");
  78. printf("%10s | %5s | %3s | %1s\n", "name", "type", "@", "I");
  79. for (int i = 0; i < table->index; i++) {
  80. print_item(&table->table[i]);
  81. }
  82. printf("Temporary variables:\n");
  83. for (int i = SYMBOL_TABLE_SIZE - 1; i > table->temp_index; i--) {
  84. print_item(&table->table[i]);
  85. }
  86. }
  87. void print_item(struct SymbolItem *item) {
  88. if (item == NULL) {
  89. printf("NULL");
  90. }
  91. else {
  92. printf("%10s | %5s | %3d | %1d\n",
  93. item->name,
  94. type_to_string(item->type),
  95. item->address,
  96. item->init);
  97. }
  98. }
  99. SymbolItem *get_symbol_item(SymbolTable *table, const char *name) {
  100. for (int i = 0; i < table->index; i++) {
  101. if (strcmp(table->table[i].name, name) == 0) {
  102. return &table->table[i];
  103. }
  104. }
  105. for (int i = SYMBOL_TABLE_SIZE -1; i > table->temp_index; i--) {
  106. if (strcmp(table->table[i].name, name) == 0) {
  107. return &table->table[i];
  108. }
  109. }
  110. return NULL;
  111. }