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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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, 1};
  33. int profondeur = 0;
  34. int last_temp_var_size;
  35. const struct type_t integer = {INT, 0, 1};
  36. struct element_t {
  37. struct symbole_t symbole;
  38. struct element_t * suivant;
  39. };
  40. struct pile_t {
  41. int taille;
  42. struct element_t * first;
  43. };
  44. struct pile_t * pile;
  45. char * type_to_string(struct type_t type) {
  46. char * star = "*";
  47. char * resultat = malloc(sizeof(char)*20);
  48. for (int i = 0; i< type.pointeur_level; i++){
  49. strcat(resultat,star);
  50. }
  51. if (type.base == INT) {
  52. strcat(resultat,"int");
  53. } else {;
  54. strcat(resultat,"unknown");
  55. }
  56. return resultat;
  57. }
  58. void print_symbole(struct symbole_t symbole) {
  59. char * type = type_to_string(symbole.type);
  60. if (symbole.initialized) {
  61. printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:OUI, profondeur : %d, isTab : %d}\n", symbole.nom, symbole.adresse, type, symbole.profondeur,symbole.type.isTab);
  62. } else {
  63. printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:NON, profondeur : %d, isTab : %d}\n", symbole.nom, symbole.adresse, type,symbole.profondeur,symbole.type.isTab);
  64. }
  65. free(type);
  66. }
  67. void init (void) {
  68. pile = malloc(sizeof(struct pile_t));
  69. pile->first = NULL;
  70. pile->taille = 0;
  71. }
  72. int push(char * nom, int isInit, struct type_t type) {
  73. struct element_t * aux = malloc(sizeof(struct element_t));
  74. struct symbole_t symbole = {"", last_addr, type, isInit,profondeur};
  75. strcpy(symbole.nom,nom);
  76. aux->symbole = symbole;
  77. aux->suivant = pile->first;
  78. pile->first = aux;
  79. pile->taille++;
  80. int addr_var = last_addr;
  81. last_addr += (type.nb_blocs)*taille_types[type.base];
  82. return addr_var;
  83. }
  84. struct symbole_t pop() {
  85. struct symbole_t retour = {"", 0, UNKNOWN, 0, 0};
  86. struct element_t * aux;
  87. if (pile->taille > 0) {
  88. aux = pile->first;
  89. pile->first = pile->first->suivant;
  90. retour = aux->symbole;
  91. free(aux);
  92. pile->taille--;
  93. last_addr -= taille_types[retour.type.base];
  94. }
  95. return retour;
  96. }
  97. void multiple_pop(int n){
  98. for (int i =0; i<n; i++){
  99. pop();
  100. }
  101. }
  102. struct symbole_t * get_variable(char * nom){
  103. struct symbole_t * retour = NULL;
  104. struct element_t * aux = pile->first;
  105. int i;
  106. for (i=0; i < pile->taille; i++) {
  107. if (!strcmp(nom, aux->symbole.nom)) {
  108. retour = &aux->symbole;
  109. break;
  110. } else {
  111. aux = aux->suivant;
  112. }
  113. }
  114. return retour;
  115. }
  116. void print() {
  117. printf("Affichage de la Table des Symboles\n\tSize : %d\n\tContenu : \n", pile->taille);
  118. struct element_t * aux = pile->first;
  119. int i;
  120. for (i=0; i < pile->taille; i++) {
  121. if (aux->symbole.initialized) {
  122. printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:OUI, profondeur : %d, isTab : %d}\n", aux->symbole.nom, aux->symbole.adresse, type_to_string(aux->symbole.type), aux->symbole.profondeur, aux->symbole.type.isTab);
  123. } else {
  124. printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:NON, profondeur : %d, isTab : %d}\n", aux->symbole.nom, aux->symbole.adresse, type_to_string(aux->symbole.type), aux->symbole.profondeur, aux->symbole.type.isTab);
  125. }
  126. aux = aux->suivant;
  127. }
  128. }
  129. int get_last_addr(){
  130. return last_addr;
  131. }
  132. void inc_prof() {
  133. profondeur++;
  134. }
  135. void decrement_prof(){
  136. profondeur--;
  137. }
  138. int get_prof() {
  139. return profondeur;
  140. }
  141. void reset_prof(){
  142. printf("Profondeur dans reset : %d\n", profondeur);
  143. while (pile->first != NULL && pile->first->symbole.profondeur == profondeur){
  144. pop();
  145. }
  146. profondeur--;
  147. }