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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "table_symboles.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. // Première adresse disponible
  6. int last_addr = 0;
  7. // Tableau indexé sur les types donnant leur tailles
  8. int taille_types[] = {-1, 1, 1};
  9. // Pronfondeur dans le code (pour la visibilité des variables)
  10. int profondeur = 0;
  11. // Constante pour les entiers
  12. const struct type_t integer = {INT, 0, 1, 0};
  13. // Constante pour les pointeurs
  14. const struct type_t pointer = {ADDR, 0, 1, 0};
  15. // Structure chainant les symboles
  16. struct element_t {
  17. struct symbole_t symbole;
  18. struct element_t * suivant;
  19. };
  20. // Structure de la pile des symboles
  21. struct pile_t {
  22. int taille;
  23. struct element_t * first;
  24. };
  25. // Pile des symboles
  26. struct pile_t * pile;
  27. // Fonction d'affichage pour un type
  28. char * type_to_string(struct type_t type) {
  29. char * star = "*";
  30. char * resultat = malloc(sizeof(char)*20);
  31. for (int i = 0; i< type.pointeur_level; i++){
  32. strcat(resultat,star);
  33. }
  34. if (type.base == INT) {
  35. strcat(resultat,"int");
  36. } else {;
  37. strcat(resultat,"unknown");
  38. }
  39. return resultat;
  40. }
  41. // Fonction d'affichage pour un symbole
  42. void print_symbole(struct symbole_t symbole) {
  43. char * type = type_to_string(symbole.type);
  44. if (symbole.initialized) {
  45. 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);
  46. } else {
  47. 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);
  48. }
  49. free(type);
  50. }
  51. // Initialisation de la pile
  52. void init (void) {
  53. pile = malloc(sizeof(struct pile_t));
  54. pile->first = NULL;
  55. pile->taille = 0;
  56. }
  57. // Fonction d'ajout d'un symbole à la pile
  58. int push(char * nom, int isInit, struct type_t type) {
  59. struct element_t * aux = malloc(sizeof(struct element_t));
  60. struct symbole_t symbole = {"", last_addr, type, isInit,profondeur};
  61. strcpy(symbole.nom,nom);
  62. aux->symbole = symbole;
  63. aux->suivant = pile->first;
  64. pile->first = aux;
  65. pile->taille++;
  66. int addr_var = last_addr;
  67. last_addr += (type.nb_blocs)*taille_types[type.base];
  68. return addr_var;
  69. }
  70. // Fonction renvoyant et supprimant le premier symbole
  71. struct symbole_t pop() {
  72. struct symbole_t retour = {"", 0, UNKNOWN, 0, 0};
  73. struct element_t * aux;
  74. if (pile->taille > 0) {
  75. aux = pile->first;
  76. pile->first = pile->first->suivant;
  77. retour = aux->symbole;
  78. free(aux);
  79. pile->taille--;
  80. last_addr -= taille_types[retour.type.base];
  81. }
  82. return retour;
  83. }
  84. // Fonction supprimant les n premiers symboles
  85. void multiple_pop(int n){
  86. for (int i =0; i<n; i++){
  87. pop();
  88. }
  89. }
  90. // Fonction d'accès a un symbole par son nom
  91. struct symbole_t * get_variable(char * nom){
  92. struct symbole_t * retour = NULL;
  93. struct element_t * aux = pile->first;
  94. int i;
  95. for (i=0; i < pile->taille; i++) {
  96. if (!strcmp(nom, aux->symbole.nom)) {
  97. retour = &aux->symbole;
  98. break;
  99. } else {
  100. aux = aux->suivant;
  101. }
  102. }
  103. return retour;
  104. }
  105. // Fonction d'affichage de la pile
  106. void print() {
  107. printf("Affichage de la Table des Symboles\n\tSize : %d\n\tContenu : \n", pile->taille);
  108. struct element_t * aux = pile->first;
  109. int i;
  110. for (i=0; i < pile->taille; i++) {
  111. if (aux->symbole.initialized) {
  112. 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);
  113. } else {
  114. 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);
  115. }
  116. aux = aux->suivant;
  117. }
  118. }
  119. // Getteur sur la première adresse dispo (utile pour le CALL)
  120. int get_last_addr(){
  121. return last_addr;
  122. }
  123. /********************************/
  124. /*** GESTION DE LA PROFONDEUR ***/
  125. /********************************/
  126. void inc_prof() {
  127. profondeur++;
  128. }
  129. void decrement_prof(){
  130. profondeur--;
  131. }
  132. int get_prof() {
  133. return profondeur;
  134. }
  135. void reset_prof(){
  136. printf("Profondeur dans reset : %d\n", profondeur);
  137. while (pile->first != NULL && pile->first->symbole.profondeur == profondeur){
  138. pop();
  139. }
  140. profondeur--;
  141. }