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 2.9KB

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