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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* TABLE DES SYMBOLE DU COMPILATEUR (PILE)
  2. -----------------------------------------------------
  3. | symbole | adresse | type | initialisé |
  4. -----------------------------------------------------
  5. | | | | |
  6. | | | | |
  7. | | | | |
  8. | i | 0x777756b8 | int | false |
  9. | size | 0x777756b8 | int | true |
  10. -----------------------------------------------------
  11. Types pour l'implémentation :
  12. - enum type_t : [int]
  13. - struct symbole : {
  14. char nom[30];
  15. uintptr_t adresse;
  16. enum type_t type;
  17. char initialized;
  18. }
  19. Opérations possible :
  20. - init -> pile * -> void
  21. - push -> symbole -> pile * -> void
  22. - pop -> pile * -> symbole
  23. - exist -> pile * -> symbole -> char
  24. - initialized -> pile * -> symbole -> char */
  25. #include "table_symboles.h"
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <stdio.h>
  29. #define MAXADDR 1024*5
  30. int last_addr = 0;
  31. int temp_addr = MAXADDR;
  32. int taille_types[] = {-1, 4};
  33. int profondeur = 0;
  34. int last_temp_var_size;
  35. struct element_t {
  36. struct symbole_t symbole;
  37. struct element_t * suivant;
  38. };
  39. struct pile_t {
  40. int taille;
  41. struct element_t * first;
  42. };
  43. struct pile_t * pile;
  44. char * type_to_string(struct type_t type) {
  45. char * star = "*";
  46. char * resultat = malloc(sizeof(char)*20);
  47. for (int i = 0; i< type.pointeur_level; i++){
  48. strcat(resultat,star);
  49. }
  50. if (type.base == INT) {
  51. strcat(resultat,"int");
  52. } else {;
  53. strcat(resultat,"unknown");
  54. }
  55. return resultat;
  56. }
  57. void print_symbole(struct symbole_t symbole) {
  58. char * type = type_to_string(symbole.type);
  59. if (symbole.initialized) {
  60. printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:OUI, profondeur : %d}\n", symbole.nom, symbole.adresse, type, symbole.profondeur);
  61. } else {
  62. printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:NON, profondeur : %d}\n", symbole.nom, symbole.adresse, type,symbole.profondeur);
  63. }
  64. free(type);
  65. }
  66. void init (void) {
  67. pile = malloc(sizeof(struct pile_t));
  68. pile->first = NULL;
  69. pile->taille = 0;
  70. }
  71. int push(char * nom, int isInit, struct type_t type) {
  72. struct element_t * aux = malloc(sizeof(struct element_t));
  73. struct symbole_t symbole = {"", last_addr, type, isInit,profondeur};
  74. strcpy(symbole.nom,nom);
  75. aux->symbole = symbole;
  76. aux->suivant = pile->first;
  77. pile->first = aux;
  78. pile->taille++;
  79. int addr_var = last_addr;
  80. last_addr += taille_types[type.base];
  81. return addr_var;
  82. }
  83. struct symbole_t pop() {
  84. struct symbole_t retour = {"", 0, UNKNOWN, 0, 0};
  85. struct element_t * aux;
  86. if (pile->taille > 0) {
  87. aux = pile->first;
  88. pile->first = pile->first->suivant;
  89. retour = aux->symbole;
  90. free(aux);
  91. pile->taille--;
  92. last_addr -= taille_types[retour.type.base];
  93. }
  94. return retour;
  95. }
  96. struct symbole_t decl_tab(char * name, struct type_t type, int nb_blocs){
  97. push(name,0,type);
  98. last_addr += (nb_blocs-1)*taille_types[type.base];
  99. }
  100. char status(char * nom) {
  101. char retour = 0;
  102. struct element_t * aux = pile->first;
  103. int i;
  104. for (i=0; i < pile->taille; i++) {
  105. if (!strcmp(nom, aux->symbole.nom)) {
  106. if (aux->symbole.initialized) {
  107. retour = 1;
  108. } else {
  109. retour = 2;
  110. }
  111. break;
  112. } else {
  113. aux = aux->suivant;
  114. }
  115. }
  116. return retour;
  117. }
  118. struct symbole_t * get_variable(char * nom){
  119. struct symbole_t * retour = NULL;
  120. struct element_t * aux = pile->first;
  121. int i;
  122. for (i=0; i < pile->taille; i++) {
  123. if (!strcmp(nom, aux->symbole.nom)) {
  124. retour = &aux->symbole;
  125. break;
  126. } else {
  127. aux = aux->suivant;
  128. }
  129. }
  130. return retour;
  131. }
  132. void print() {
  133. printf("Affichage de la Table des Symboles\n\tSize : %d\n\tContenu : \n", pile->taille);
  134. struct element_t * aux = pile->first;
  135. int i;
  136. for (i=0; i < pile->taille; i++) {
  137. if (aux->symbole.initialized) {
  138. printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:OUI, profondeur : %d}\n", aux->symbole.nom, aux->symbole.adresse, type_to_string(aux->symbole.type), aux->symbole.profondeur);
  139. } else {
  140. printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:NON, profondeur : %d}\n", aux->symbole.nom, aux->symbole.adresse, type_to_string(aux->symbole.type), aux->symbole.profondeur);
  141. }
  142. aux = aux->suivant;
  143. }
  144. }
  145. int get_last_addr(){
  146. return last_addr;
  147. }
  148. int allocate_mem_temp_var(enum base_type_t type){
  149. struct type_t type_bis = {type,0};
  150. int addr = push("",1,type_bis);
  151. return addr;
  152. }
  153. void reset_pronf(){
  154. printf("Profondeur dans reset : %d\n", profondeur);
  155. while (pile->first != NULL && pile->first->symbole.profondeur == profondeur){
  156. pop();
  157. }
  158. }
  159. void decrement_temp_var(){
  160. pop();
  161. }