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

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