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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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){
  52. if (table->indexAvailableTop > 0){
  53. while(table->indexAvailableTop > 0){
  54. if (table->array[table->indexAvailableTop-1].symbole_depth == table->depth){
  55. table->indexAvailableTop--;
  56. } else {
  57. break;
  58. }
  59. }
  60. }
  61. //TODO: vérifier qu'il n'y a pas de varaibles temporarires au moment de changement de profondeur
  62. return 0;
  63. }
  64. void free_temp(Table_Symboles * table){
  65. table->indexAvailableBottom++;
  66. if (table->indexAvailableBottom >= TABLE_SIZE){
  67. printf("Huge error\n");
  68. table->indexAvailableBottom--;
  69. }
  70. }
  71. int prepare_function_call(Table_Symboles * table){
  72. prepare_argument_push(table);
  73. prepare_argument_push(table);
  74. prepare_argument_push(table);
  75. }
  76. int prepare_argument_push(Table_Symboles * table){
  77. Symbole symbole;
  78. symbole.addr = table->indexAvailableTop;
  79. symbole.symbole_depth = table->depth;
  80. if (table->indexAvailableTop < table->indexAvailableBottom){
  81. table->array[table->indexAvailableTop] = symbole;
  82. table->indexAvailableTop++;
  83. return (table->indexAvailableTop) - 1 ;
  84. }
  85. }
  86. int initialise_symbole(Table_Symboles * table, char * varName){
  87. int index = variable_exists(table, varName);
  88. if (index == -1){
  89. return -1;
  90. } else {
  91. table->array[index].init = INITIALISED;
  92. }
  93. }
  94. void print_symbole(Symbole * symbole){
  95. char * var = symbole->Variable_Name;
  96. int addr = symbole->addr;
  97. enum Symbole_Type type = symbole->type;
  98. char typeStr[20];
  99. if (type == TYPE_INT){
  100. strcpy(typeStr, "INT");
  101. } else if (type == TYPE_CONST_INT){
  102. strcpy(typeStr, "CONST_INT");
  103. } else if (type == TYPE_INT_PTR) {
  104. strcpy(typeStr, "INT_PTR");
  105. } else {
  106. strcpy(typeStr, "Error type");
  107. }
  108. enum Initialised_Variable init = symbole->init;
  109. char initStr[20];
  110. if (init == INITIALISED){
  111. strcpy(initStr,"INITIALISED");
  112. } else{
  113. strcpy(initStr,"NOT_INITIALISED");
  114. }
  115. int depth = symbole->symbole_depth;
  116. printf("%-20s\t\t %-12s\t\t %-12d\t %-20s\t %-12d\n", var, typeStr, addr, initStr, depth);
  117. }
  118. void print_table(Table_Symboles * table){
  119. printf("%-20s\t\t %-12s\t\t %-12s\t %-20s\t %-12s\n", "Variable Name", "Type", "Address", "Initialised", "Depth");
  120. int indexTop = table->indexAvailableTop;
  121. int indexBottom = table->indexAvailableBottom;
  122. Symbole symbole;
  123. for (int i = 0; i < indexTop; i++){
  124. symbole = table->array[i];
  125. print_symbole(&symbole);
  126. }
  127. if (table->indexAvailableBottom != TABLE_SIZE - 1){
  128. printf("%-20s\t\t %-12s\t\t %-12s\t %-20s\t %-12s\n", "...", "...", "...", "...", "...");
  129. for (int i = (indexBottom + 1); i < TABLE_SIZE; i++){
  130. symbole = table->array[i];
  131. print_symbole(&symbole);
  132. }
  133. }
  134. }