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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. struct element_t {
  30. struct symbole_t symbole;
  31. struct element_t * suivant;
  32. };
  33. struct pile_t {
  34. int taille;
  35. struct element_t * first;
  36. };
  37. char * type_to_string(enum type_t type) {
  38. if (type == INT) {
  39. return "int";
  40. } else {
  41. return "unknown";
  42. }
  43. }
  44. void print_symbole(struct symbole_t symbole) {
  45. if (symbole.initialized) {
  46. printf("\t\t{nom:%s, adresse:%p, type:%s, initialized:OUI}\n", symbole.nom, (void *)(symbole.adresse), type_to_string(symbole.type));
  47. } else {
  48. printf("\t\t{nom:%s, adresse:%p, type:%s, initialized:NON}\n", symbole.nom, (void *)(symbole.adresse), type_to_string(symbole.type));
  49. }
  50. }
  51. void init (void) {
  52. pile = malloc(sizeof(struct pile_t));
  53. pile->first = NULL;
  54. pile->taille = 0;
  55. }
  56. void push(struct symbole_t symbole, struct pile_t * pile) {
  57. struct element_t * aux = malloc(sizeof(struct element_t));
  58. aux->symbole = symbole;
  59. aux->suivant = pile->first;
  60. pile->first = aux;
  61. pile->taille++;
  62. }
  63. struct symbole_t pop(struct pile_t * pile) {
  64. struct symbole_t retour = {"", 0, UNKNOWN, 0};
  65. struct element_t * aux;
  66. if (pile->taille > 0) {
  67. aux = pile->first;
  68. pile->first = pile->first->suivant;
  69. retour = aux->symbole;
  70. free(aux);
  71. pile->taille--;
  72. }
  73. return retour;
  74. }
  75. char status(char * nom, struct pile_t * pile) {
  76. char retour = 0;
  77. struct element_t * aux = pile->first;
  78. int i;
  79. for (i=0; i < pile->taille; i++) {
  80. if (!strcmp(nom, aux->symbole.nom)) {
  81. if (aux->symbole.initialized) {
  82. retour = 1;
  83. } else {
  84. retour = 2;
  85. }
  86. break;
  87. } else {
  88. aux = aux->suivant;
  89. }
  90. }
  91. return retour;
  92. }
  93. void print(struct pile_t * pile) {
  94. printf("Affichage de la Table des Symboles\n\tSize : %d\n\tContenu : \n", pile->taille);
  95. struct element_t * aux = pile->first;
  96. int i;
  97. for (i=0; i < pile->taille; i++) {
  98. if (aux->symbole.initialized) {
  99. printf("\t\t{nom:%s, adresse:%p, type:%s, initialized:OUI}\n", aux->symbole.nom, (void *)(aux->symbole.adresse), type_to_string(aux->symbole.type));
  100. } else {
  101. printf("\t\t{nom:%s, adresse:%p, type:%s, initialized:NON}\n", aux->symbole.nom, (void *)(aux->symbole.adresse), type_to_string(aux->symbole.type));
  102. }
  103. aux = aux->suivant;
  104. }
  105. }