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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. }
  75. int prepare_argument_push(Table_Symboles * table){
  76. Symbole symbole;
  77. symbole.addr = table->indexAvailableTop;
  78. symbole.symbole_depth = table->depth;
  79. if (table->indexAvailableTop < table->indexAvailableBottom){
  80. table->array[table->indexAvailableTop] = symbole;
  81. table->indexAvailableTop++;
  82. return (table->indexAvailableTop) - 1 ;
  83. }
  84. }
  85. int initialise_symbole(Table_Symboles * table, char * varName){
  86. int index = variable_exists(table, varName);
  87. if (index == -1){
  88. return -1;
  89. } else {
  90. table->array[index].init = INITIALISED;
  91. }
  92. }
  93. void print_symbole(Symbole * symbole){
  94. char * var = symbole->Variable_Name;
  95. int addr = symbole->addr;
  96. enum Symbole_Type type = symbole->type;
  97. char typeStr[20];
  98. if (type == TYPE_INT){
  99. strcpy(typeStr, "INT");
  100. } else if (type == TYPE_CONST_INT){
  101. strcpy(typeStr, "CONST_INT");
  102. } else if (type == TYPE_INT_PTR) {
  103. strcpy(typeStr, "INT_PTR");
  104. } else {
  105. strcpy(typeStr, "Error type");
  106. }
  107. enum Initialised_Variable init = symbole->init;
  108. char initStr[20];
  109. if (init == INITIALISED){
  110. strcpy(initStr,"INITIALISED");
  111. } else{
  112. strcpy(initStr,"NOT_INITIALISED");
  113. }
  114. int depth = symbole->symbole_depth;
  115. printf("%-20s\t\t %-12s\t\t %-12d\t %-20s\t %-12d\n", var, typeStr, addr, initStr, depth);
  116. }
  117. void print_table(Table_Symboles * table){
  118. printf("%-20s\t\t %-12s\t\t %-12s\t %-20s\t %-12s\n", "Variable Name", "Type", "Address", "Initialised", "Depth");
  119. int indexTop = table->indexAvailableTop;
  120. int indexBottom = table->indexAvailableBottom;
  121. Symbole symbole;
  122. for (int i = 0; i < indexTop; i++){
  123. symbole = table->array[i];
  124. print_symbole(&symbole);
  125. }
  126. if (table->indexAvailableBottom != TABLE_SIZE - 1){
  127. printf("%-20s\t\t %-12s\t\t %-12s\t %-20s\t %-12s\n", "...", "...", "...", "...", "...");
  128. for (int i = (indexBottom + 1); i < TABLE_SIZE; i++){
  129. symbole = table->array[i];
  130. print_symbole(&symbole);
  131. }
  132. }
  133. }