scopeStack.c

Description du code

Fonctions de gestion d'une pile de portées Compilateur LSD010

Code source ou contenu du fichier

  1. /*
  2.  * scopeStack.c : Implementation to manage a stack of scopes
  3.  * for a declaration symbol and his re-declarations
  4.  * Part of the compiler project for LSD10 language
  5.  * Gaudry Stéphane
  6.  * More information on http://www.gaudry.be/langages-lex-yacc-intro.html
  7.  * **********************************************************
  8.  */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #if(VERBOSE_LEVEL<=DEB_E)
  13. #include <errno.h>
  14. #endif
  15. #include "scopeStack.h"
  16. #include "common.h"
  17.  
  18.  
  19. /*
  20.  * **********************************************************
  21.  * Internal business
  22.  * **********************************************************
  23.  */
  24.  
  25.  
  26. /*
  27.  * **********************************************************
  28.  * Implementation of the header exposed items
  29.  * See scopeStack.h for these functions comments
  30.  * **********************************************************
  31.  */
  32.  
  33. ScopeStack* createScopeStack()
  34. {
  35. ScopeStack *newScopeStack = malloc( sizeof(*newScopeStack) );
  36. if(newScopeStack==NULL)
  37. {
  38. #if(VERBOSE_LEVEL<=DEB_E)
  39. {
  40. printMsg(DEB_E,"Allocation Error(ScopeStack)", __FILE__, __LINE__);
  41. printMsg(DEB_E, (char *)strerror(errno), __FILE__, __LINE__);
  42. }
  43. #endif
  44. //Failure of the compiler behavior, independent of the parsed code
  45. exit(EXIT_FAILURE);
  46. }
  47. newScopeStack->parentPtr=NULL;
  48. newScopeStack->usage=VAR_USAGE_NEVER;
  49. newScopeStack->declarationNode = NULL;
  50. newScopeStack->functionNode=NULL;
  51. return newScopeStack;
  52. }
  53.  
  54. void finalizeScopeStack(ScopeStack **scopeStackPtr)
  55. {
  56. while(*scopeStackPtr)
  57. {
  58. popScopeStack(scopeStackPtr);
  59. }
  60. }
  61.  
  62. void pushScopesStack(ScopeStack **scopeStackPtr, int scope, AstNode *declarationNode)
  63. {
  64. ScopeStack *tempScopeStackPtr = createScopeStack();
  65.  
  66. // char msg[1024];//todo: minimize length
  67. // sprintf(
  68. // msg,
  69. // "Push scope : %d [15%p] => %s (compiler %s,%d)",
  70. // scope, tempScopeStackPtr, declarationNode==NULL?"node null":declarationNode->info->name,
  71. // __FILE__,
  72. // __LINE__
  73. // );
  74. // printMsg(DEB_EXEC, msg, __FILE__, __LINE__);
  75.  
  76. tempScopeStackPtr->declarationNode = declarationNode;
  77. if(declarationNode!=NULL)
  78. {
  79. declarationNode->info->scopeId = scope;
  80. declarationNode->info->scopeDepth = scopeHelperGetCurrentDepth();
  81. }
  82. tempScopeStackPtr->parentPtr=*scopeStackPtr;
  83. *scopeStackPtr = tempScopeStackPtr;
  84. }
  85.  
  86. AstNode* popScopeStack(ScopeStack **scopeStackPtr)
  87. {
  88. if(!*scopeStackPtr)
  89. {
  90. return NULL;
  91. }
  92.  
  93. // char msg[1024];//todo: minimize length
  94. // sprintf(
  95. // msg,
  96. // "\tBefore Pop scope : %d [15%p] => %s (compiler %s,%d)",
  97. // (*scopeStackPtr)->scopeId,
  98. // (*scopeStackPtr),
  99. // returnVal==NULL?"node null":returnVal->info->name,
  100. // __FILE__,
  101. // __LINE__
  102. // );
  103. // printMsg(DEB_EXEC, msg, __FILE__, __LINE__);
  104.  
  105. ScopeStack *top = *scopeStackPtr;
  106. AstNode *returnVal = top->declarationNode;
  107. *scopeStackPtr = top->parentPtr;
  108. free(top);
  109.  
  110. // sprintf(
  111. // msg,
  112. // "\tAfter Pop scope : %d [15%p] => %s (compiler %s,%d)",
  113. // (*scopeStackPtr)==NULL?INITIAL_INT:(*scopeStackPtr)->scopeId,
  114. // (*scopeStackPtr),
  115. // returnVal==NULL?"node null":returnVal->info->name,
  116. // __FILE__,
  117. // __LINE__
  118. // );
  119. // printMsg(DEB_EXEC, msg, __FILE__, __LINE__);
  120.  
  121. return returnVal;
  122. }
  123.  
  124. void setScopesStackDepth(ScopeStack *scopeStack, int scopeDepth)
  125. {
  126. scopeStack->declarationNode->info->scopeDepth=scopeDepth;
  127. }
  128. int getScopeDepth(ScopeStack *scopeStack)
  129. {
  130. if(scopeStack==NULL)
  131. {
  132. // printf("\n; scopeStack null (compiler %s, %d)\n", __FILE__, __LINE__);
  133. return ERROR_INT;
  134. }
  135. if(scopeStack->declarationNode==NULL)
  136. {
  137. // printf("\n; declaration node null (compiler %s, %d)\n", __FILE__, __LINE__);
  138. return ERROR_INT;
  139. }
  140. return scopeStack->declarationNode->info->scopeId;
  141. }

Autres extraits de codes en c

  • DisquetteDispo Vérifier la disponibilité du lecteur de disquette
  • Suite de Fibonacci Exemple d'itération en C
  • Suite de Fibonacci Exemple de récursion en C
  • astDataRepresentation.h Représentation de données de l'arbre syntaxique abstrait Compilateur LSD010
  • ast.h Arbre syntaxique abstrait Compilateur LSD010
  • ast.c Arbre syntaxique abstrait Compilateur LSD010
  • symbolsTableDataRepresentation.h Représentation de données de la table des symboles Compilateur LSD010
  • symbolsTable.h Fonctions de gestion de la table des symboles Compilateur LSD010
  • symbolsTable.c Fonctions de gestion de la table des symboles Compilateur LSD010
  • hashCode.h Fonctions de hachage Compilateur LSD010
  • hashCode.c Fonctions de hachage Compilateur LSD010
  • scopeStack.h Fonctions de gestion d'une pile de portées Compilateur LSD010
  • scopeStack.c Fonctions de gestion d'une pile de portées Compilateur LSD010
  • scopeHelper.h Fonctions de gestion de la portée courante Compilateur LSD010
  • console.h Fonctions d'affichage Compilateur LSD010
  • console.c Fonctions d'affichage Compilateur LSD010
  • graphVizHelper.h Génération d'une image d'un arbre syntaxique abstrait.
    Classe d'intégration de l'outil GraphViz. Compilateur LSD010
  • graphVizHelper.c Génération d'une image d'un arbre syntaxique abstrait.
    Classe d'intégration de l'outil GraphViz. Compilateur LSD010
  • common.h Définition des constantes et variables communes Compilateur LSD010
  • pcode.c Génération de p-code Compilateur LSD010
  • pcode.h Génération de p-code Compilateur LSD010
  • Tous les extraits

Document créé le 05/10/2009, dernière modification le 28/10/2018
Source du document imprimé : https://www.gaudry.be/sniplet-rf-lsd010/project/source/scopeStack.c.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.