No Description
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.

table_symboles.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "table_symboles.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. void initialise_table(Table_Symboles * table){
  5. table->indexAvailableBottom = TABLE_SIZE - 1;
  6. table->indexAvailableTop = 0;
  7. table->depth = 0;
  8. }
  9. int variable_exists(Table_Symboles * table, char * varName){
  10. for (int i = 0; i < table->indexAvailableTop; i++){
  11. if (strcmp(varName, table->array[i].Variable_Name) == 0){
  12. return i;
  13. }
  14. }
  15. for (int i = (table->indexAvailableBottom + 1); i < TABLE_SIZE; i++){
  16. if (strcmp(varName, table->array[i].Variable_Name) == 0){
  17. return i;
  18. }
  19. }
  20. return 0;
  21. }
  22. int add_symbole_top(Table_Symboles * table, char * varName, enum Symbole_Type type, enum Initialised_Variable init, int depth){
  23. Symbole symbole;
  24. strcpy(symbole.Variable_Name, varName);
  25. symbole.addr = table->indexAvailableTop;
  26. symbole.init = init;
  27. symbole.type = type;
  28. symbole.symbole_depth = table->depth;
  29. if (table->indexAvailableTop >= table->indexAvailableBottom){
  30. return -1;
  31. } else if (variable_exists(table, varName) != 0){
  32. return -2;
  33. } else {
  34. table->array[table->indexAvailableTop] = symbole;
  35. table->indexAvailableTop++;
  36. }
  37. return 0;
  38. }
  39. int add_symbole_bottom(Table_Symboles * table){
  40. Symbole symbole;
  41. symbole.addr = table->indexAvailableBottom;
  42. //symbole.symbole_depth = -1;
  43. if (table->indexAvailableTop >= table->indexAvailableBottom){
  44. return -1;
  45. } else {
  46. table->array[table->indexAvailableBottom] = symbole;
  47. table->indexAvailableBottom--;
  48. }
  49. return 0;
  50. }
  51. int remove_symboles(Table_Symboles * table, int depth){
  52. int index;
  53. if (depth < table->depth){
  54. return -1;
  55. } else {
  56. index = table->indexAvailableTop;
  57. while(table->array[index].symbole_depth == depth && index >=0){
  58. //remove
  59. table->indexAvailableTop--;
  60. index--;
  61. }
  62. //TODO: vérifier qu'il n'y a pas de varaibles temporarires au moment de changement de profondeur
  63. }
  64. return 0;
  65. }
  66. void free_temp(Table_Symboles * table){
  67. table->indexAvailableBottom++;
  68. }
  69. int initialise_symbole(Table_Symboles * table, char * varName){
  70. int index = variable_exists(table, varName);
  71. if (index == -1){
  72. return -1;
  73. } else {
  74. table->array[index].init = INITIALISED;
  75. }
  76. }
  77. void print_symbole(Symbole * symbole){
  78. char * var = symbole->Variable_Name;
  79. int addr = symbole->addr;
  80. enum Symbole_Type type = symbole->type;
  81. char typeStr[20];
  82. if (type == TYPE_INT){
  83. strcpy(typeStr, "INT");
  84. } else{
  85. strcpy(typeStr, "CONST_INT");
  86. }
  87. enum Initialised_Variable init = symbole->init;
  88. char initStr[20];
  89. if (init == INITIALISED){
  90. strcpy(initStr,"INITIALISED");
  91. } else{
  92. strcpy(initStr,"NOT_INITIALISED");
  93. }
  94. int depth = symbole->symbole_depth;
  95. printf("%-20s\t\t %-12s\t\t %-12d\t %-20s\t %-12d\n", var, typeStr, addr, initStr, depth);
  96. }
  97. void print_table(Table_Symboles * table){
  98. printf("%-20s\t\t %-12s\t\t %-12s\t %-20s\t %-12s\n", "Variable Name", "Type", "Address", "Initialised", "Depth");
  99. int indexTop = table->indexAvailableTop;
  100. int indexBottom = table->indexAvailableBottom;
  101. Symbole symbole;
  102. for (int i = 0; i < indexTop; i++){
  103. symbole = table->array[i];
  104. print_symbole(&symbole);
  105. }
  106. if (table->indexAvailableBottom != TABLE_SIZE - 1){
  107. printf("%-20s\t\t %-12s\t\t %-12s\t %-20s\t %-12s\n", "...", "...", "...", "...", "...");
  108. for (int i = (indexBottom + 1); i < TABLE_SIZE; i++){
  109. symbole = table->array[i];
  110. print_symbole(&symbole);
  111. }
  112. }
  113. }