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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 init(struct pile_t * pile) {
  45. pile->first = NULL;
  46. pile->taille = 0;
  47. }
  48. void push(struct symbole_t symbole, struct pile_t * pile) {
  49. struct element_t * aux = malloc(sizeof(struct element_t));
  50. aux->symbole = symbole;
  51. aux->suivant = pile->first;
  52. pile->first = aux;
  53. pile->taille++;
  54. }
  55. struct symbole_t pop(struct pile_t * pile) {
  56. struct symbole_t retour = {"", 0, UNKNOWN, -1};
  57. struct element_t * aux;
  58. if (pile->taille > 0) {
  59. aux = pile->first;
  60. pile->first = pile->first->suivant;
  61. retour = aux->symbole;
  62. free(aux);
  63. pile->taille--;
  64. }
  65. return retour;
  66. }
  67. char status(char * nom, struct pile_t pile) {
  68. char retour = 0;
  69. struct element_t * aux = pile.first;
  70. int i;
  71. for (i=0; i < pile.taille; i++) {
  72. if (!strcmp(nom, aux->symbole.nom)) {
  73. if (aux->symbole.initialized) {
  74. retour = 1;
  75. } else {
  76. retour = 2;
  77. }
  78. break;
  79. } else {
  80. aux = aux->suivant;
  81. }
  82. }
  83. return retour;
  84. }
  85. void print(struct pile_t pile) {
  86. printf("Affichage de la Table des Symboles\n\tSize : %d\n\tContenu : \n", pile.taille);
  87. struct element_t * aux = pile.first;
  88. int i;
  89. for (i=0; i < pile.taille; i++) {
  90. if (aux->symbole.initialized) {
  91. 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));
  92. } else {
  93. 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));
  94. }
  95. aux = aux->suivant;
  96. }
  97. }