hashCode.c

Description du code

Fonctions de hachage Compilateur LSD010

Code source ou contenu du fichier

  1. /*
  2.  * hashCode.c : Implementation to get int value from a node
  3.  * Part of the compiler project for LSD10 language
  4.  * Gaudry Stéphane
  5.  * More information on http://www.gaudry.be/langages-table-des-symboles.html
  6.  * **********************************************************
  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 "common.h"
  16.  
  17. /*
  18.  * **********************************************************
  19.  * Internal business declarations
  20.  * **********************************************************
  21.  */
  22.  
  23. int maxVal = -1;
  24.  
  25. /*
  26.  * **********************************************************
  27.  * Internal business
  28.  * **********************************************************
  29.  */
  30. void checkMaxVal(char *file, int line)
  31. {
  32. if(maxVal==-1)
  33. {
  34. #if(VERBOSE_LEVEL<=DEB_E)
  35. {
  36. printMsg(DEB_E,"Upper boundary undefined, you must call \"setHashUpperBoundary\" before", file, line);
  37. printMsg(DEB_E, (char *)strerror(errno), file, line);
  38. }
  39. #endif
  40. //Failure of the compiler behavior, independent of the parsed code
  41. exit(EXIT_FAILURE);
  42. }
  43. }
  44.  
  45. /*
  46.  * **********************************************************
  47.  * Internal business hashcode algorithms alternatives
  48.  * **********************************************************
  49.  */
  50.  
  51. int getDragonHash(const char *cle)
  52. {
  53. // printf("\n; Compute hash for %s (%s,%d)", cle, __FILE__, __LINE__);
  54. const char *ptr = cle;
  55. int val = 0;
  56. while(*ptr!='\0')
  57. {
  58. int tmp;
  59. val = (val<<4)+(*ptr);
  60. if(tmp = (val & 0xF0000000))
  61. {
  62. val = val ^ (tmp >>24);
  63. val = val ^ tmp;
  64. }
  65. ptr++;
  66. }
  67. // printf("\n; Hash for %s = %d(%s,%d)", cle, val, __FILE__ ,__LINE__);
  68. return val;//%maxVal; don't use % because this is an internal function and the % is called once on the result
  69. }
  70. unsigned long getBernsteinHash(unsigned char *str)
  71. {
  72. unsigned long hash = 5381;
  73. while(*str!='\0')
  74. {
  75. int c = *str;
  76. hash = (hash << 5 ) + c;
  77. str++;
  78. }
  79. return hash;
  80. }
  81. //unsigned int getDJBHash(const char *str)
  82. //{
  83. // unsigned int hash = 5381; // DJB Hash
  84. // const char *s;
  85. // for (s = str; *s; s++) {
  86. // hash = ((hash << 5) + hash) + tolower(*s);
  87. // }
  88. // return (hash & 0x7FFFFFFF)%maxVal;
  89. //}
  90.  
  91. /*
  92.  * **********************************************************
  93.  * Implementation of the header exposed items
  94.  * See hashCode.h for these functions comments
  95.  * **********************************************************
  96.  */
  97. void setHashUpperBoundary(int _maxVal)
  98. {
  99. if(_maxVal==-1)
  100. {
  101. #if(VERBOSE_LEVEL<=DEB_E)
  102. {
  103. printMsg(DEB_E,"Upper boundary must be > 0", __FILE__, __LINE__);
  104. printMsg(DEB_E, (char *)strerror(errno), __FILE__, __LINE__);
  105. }
  106. #endif
  107. //Failure of the compiler behavior, independent of the parsed code
  108. exit(EXIT_FAILURE);
  109. }
  110. maxVal = _maxVal;
  111. }
  112. int getHashCode(char *identifier)
  113. {
  114. checkMaxVal(__FILE__, __LINE__);
  115. return getDragonHash(identifier)%maxVal;
  116. }
  117. int getASTHashCode(AstNode *astNodePtr)
  118. {
  119. checkMaxVal(__FILE__, __LINE__);
  120. int hash = getDragonHash(astNodePtr->info->name);
  121. switch(astNodePtr->type)
  122. {
  123. /* functions overloading is not allowed for LSD010
  124. * ref: LSD010_7.2.6
  125. * otherwise, we may use something like that:
  126. * for each argument, add the int value of the argument type
  127. * astNodePtr =
  128. * fctHash += astNodePtr->info->type;
  129. */
  130.  
  131. /*
  132. * The only one allowed case of same names is the set of {function, var} for {declarationNode, existingNode}
  133. * ref: LSD010_7.2.5
  134. * Thus, adding 1 to the result produces different hash for vars and functions
  135. */
  136. case NODE_TYPE_FUNCTION:
  137. case NODE_TYPE_FUNCTION_CALL:
  138. hash+=1;
  139. break;
  140. }
  141. #if(VERBOSE_LEVEL<=DEB_SYM)
  142. "\n;Hash for %s=%d[final %d] (compiler %s, %d)",
  143. astNodePtr->info->name,
  144. hash,
  145. (hash<0)?(maxVal-hash)%maxVal:hash%maxVal,
  146. __FILE__,
  147. __LINE__
  148. );
  149. #endif
  150. if(hash<0)
  151. {
  152. hash=maxVal-hash;
  153. }
  154. return hash%maxVal;
  155. }

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

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 05/10/2009, last modified the 28/10/2018
Source of the printed document:https://www.gaudry.be/en/sniplet-rf-lsd010/project/source/hashCode.c.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.