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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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, 0};
  13. // Constante pour les pointeurs
  14. const struct type_t pointer = {ADDR, 0, 1, 0, 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. strncat(resultat,star,20);
  33. }
  34. if (type.base == INT) {
  35. strncat(resultat,"int",20);
  36. } else {;
  37. strncat(resultat,"unknown",20);
  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, isConst : %d}\n", symbole.nom, symbole.adresse, type, symbole.profondeur,symbole.type.isTab, symbole.type.isConst);
  46. } else {
  47. printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:NON, profondeur : %d, isTab : %d, isConst : %d}\n", symbole.nom, symbole.adresse, type,symbole.profondeur,symbole.type.isTab, symbole.type.isConst);
  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. printf("Push de %s avec une taille de type de %d et %d blocs a l'adresse %d\n", nom, taille_types[type.base],type.nb_blocs,last_addr);
  68. last_addr += (type.nb_blocs)*taille_types[type.base];
  69. return addr_var;
  70. }
  71. // Fonction renvoyant et supprimant le premier symbole
  72. struct symbole_t pop() {
  73. struct symbole_t retour = {"", 0, UNKNOWN, 0, 0};
  74. struct element_t * aux;
  75. if (pile->taille > 0) {
  76. aux = pile->first;
  77. pile->first = pile->first->suivant;
  78. retour = aux->symbole;
  79. free(aux);
  80. pile->taille--;
  81. last_addr -= taille_types[retour.type.base];
  82. }
  83. return retour;
  84. }
  85. // Fonction supprimant les n premiers symboles
  86. void multiple_pop(int n){
  87. for (int i =0; i<n; i++){
  88. pop();
  89. }
  90. }
  91. // Fonction d'accès a un symbole par son nom
  92. struct symbole_t * get_variable(char * nom){
  93. struct symbole_t * retour = NULL;
  94. struct element_t * aux = pile->first;
  95. int i;
  96. for (i=0; i < pile->taille; i++) {
  97. if (!strcmp(nom, aux->symbole.nom)) {
  98. retour = &aux->symbole;
  99. break;
  100. } else {
  101. aux = aux->suivant;
  102. }
  103. }
  104. return retour;
  105. }
  106. // Fonction d'affichage de la pile
  107. void print() {
  108. printf("Affichage de la Table des Symboles\n\tSize : %d\n\tTop : %d\n\tContenu : \n", pile->taille, last_addr);
  109. struct element_t * aux = pile->first;
  110. int i;
  111. for (i=0; i < pile->taille; i++) {
  112. print_symbole(aux->symbole);
  113. aux = aux->suivant;
  114. }
  115. }
  116. // Getteur sur la première adresse dispo (utile pour le CALL)
  117. int get_last_addr(){
  118. return last_addr;
  119. }
  120. /********************************/
  121. /*** GESTION DE LA PROFONDEUR ***/
  122. /********************************/
  123. void inc_prof() {
  124. profondeur++;
  125. }
  126. void decrement_prof(){
  127. profondeur--;
  128. }
  129. int get_prof() {
  130. return profondeur;
  131. }
  132. void reset_prof(){
  133. while (pile->first != NULL && pile->first->symbole.profondeur == profondeur){
  134. pop();
  135. }
  136. profondeur--;
  137. }