hashCode.c
Description du code
Fonctions de hachage Compilateur LSD010Code source ou contenu du fichier
Code c (hashCode.c) (156 lignes)
/* * hashCode.c : Implementation to get int value from a node * Part of the compiler project for LSD10 language * Gaudry Stéphane * More information on http://www.gaudry.be/langages-table-des-symboles.html * ********************************************************** */ #include <stdio.h> #include <stdlib.h> #include <string.h> #if(VERBOSE_LEVEL<=DEB_E) #include <errno.h> #endif #include "common.h" /* * ********************************************************** * Internal business declarations * ********************************************************** */ int maxVal = -1; /* * ********************************************************** * Internal business * ********************************************************** */ void checkMaxVal(char *file, int line) { if(maxVal==-1) { #if(VERBOSE_LEVEL<=DEB_E) { printMsg(DEB_E,"Upper boundary undefined, you must call \"setHashUpperBoundary\" before", file, line); } #endif //Failure of the compiler behavior, independent of the parsed code } } /* * ********************************************************** * Internal business hashcode algorithms alternatives * ********************************************************** */ int getDragonHash(const char *cle) { // printf("\n; Compute hash for %s (%s,%d)", cle, __FILE__, __LINE__); const char *ptr = cle; int val = 0; while(*ptr!='\0') { int tmp; val = (val<<4)+(*ptr); if(tmp = (val & 0xF0000000)) { val = val ^ (tmp >>24); val = val ^ tmp; } ptr++; } // printf("\n; Hash for %s = %d(%s,%d)", cle, val, __FILE__ ,__LINE__); return val;//%maxVal; don't use % because this is an internal function and the % is called once on the result } unsigned long getBernsteinHash(unsigned char *str) { unsigned long hash = 5381; while(*str!='\0') { int c = *str; hash = (hash << 5 ) + c; str++; } return hash; } //unsigned int getDJBHash(const char *str) //{ // unsigned int hash = 5381; // DJB Hash // const char *s; // for (s = str; *s; s++) { // hash = ((hash << 5) + hash) + tolower(*s); // } // return (hash & 0x7FFFFFFF)%maxVal; //} /* * ********************************************************** * Implementation of the header exposed items * See hashCode.h for these functions comments * ********************************************************** */ void setHashUpperBoundary(int _maxVal) { if(_maxVal==-1) { #if(VERBOSE_LEVEL<=DEB_E) { printMsg(DEB_E,"Upper boundary must be > 0", __FILE__, __LINE__); } #endif //Failure of the compiler behavior, independent of the parsed code } maxVal = _maxVal; } int getHashCode(char *identifier) { checkMaxVal(__FILE__, __LINE__); return getDragonHash(identifier)%maxVal; } int getASTHashCode(AstNode *astNodePtr) { checkMaxVal(__FILE__, __LINE__); int hash = getDragonHash(astNodePtr->info->name); switch(astNodePtr->type) { /* functions overloading is not allowed for LSD010 * ref: LSD010_7.2.6 * otherwise, we may use something like that: * for each argument, add the int value of the argument type * astNodePtr = * fctHash += astNodePtr->info->type; */ /* * The only one allowed case of same names is the set of {function, var} for {declarationNode, existingNode} * ref: LSD010_7.2.5 * Thus, adding 1 to the result produces different hash for vars and functions */ case NODE_TYPE_FUNCTION: case NODE_TYPE_FUNCTION_CALL: hash+=1; break; } #if(VERBOSE_LEVEL<=DEB_SYM) "\n;Hash for %s=%d[final %d] (compiler %s, %d)", astNodePtr->info->name, hash, (hash<0)?(maxVal-hash)%maxVal:hash%maxVal, __FILE__, __LINE__ ); #endif if(hash<0) { hash=maxVal-hash; } return hash%maxVal; }
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.