Advertisement
Guest User

symtab.c

a guest
Mar 18th, 2018
1,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "symtab.h"
  5.  
  6. void init_hash_table(){
  7.     int i;
  8.     hash_table = malloc(SIZE * sizeof(list_t*));
  9.     for(i = 0; i < SIZE; i++) hash_table[i] = NULL;
  10. }
  11.  
  12. unsigned int hash(char *key){
  13.     unsigned int hashval = 0;
  14.     for(;*key!='\0';key++) hashval += *key;
  15.     hashval += key[0] % 11 + (key[0] << 3) - key[0];
  16.     return hashval % SIZE;
  17. }
  18.  
  19. void insert(char *name, int len, int type, int lineno){
  20.     unsigned int hashval = hash(name);
  21.     list_t *l = hash_table[hashval];
  22.    
  23.     while ((l != NULL) && (strcmp(name,l->st_name) != 0)) l = l->next;
  24.    
  25.     /* variable not yet in table */
  26.     if (l == NULL){
  27.         l = (list_t*) malloc(sizeof(list_t));
  28.         strncpy(l->st_name, name, len);  
  29.         /* add to hashtable */
  30.         l->st_type = type;
  31.         l->scope = cur_scope;
  32.         l->lines = (RefList*) malloc(sizeof(RefList));
  33.         l->lines->lineno = lineno;
  34.         l->lines->next = NULL;
  35.         l->next = hash_table[hashval];
  36.         hash_table[hashval] = l;
  37.         printf("Inserted %s for the first time with linenumber %d!\n", name, lineno); // error checking
  38.     }
  39.     /* found in table, so just add line number */
  40.     else{
  41.         l->scope = cur_scope;
  42.         RefList *t = l->lines;
  43.         while (t->next != NULL) t = t->next;
  44.         /* add linenumber to reference list */
  45.         t->next = (RefList*) malloc(sizeof(RefList));
  46.         t->next->lineno = lineno;
  47.         t->next->next = NULL;
  48.         printf("Found %s again at line %d!\n", name, lineno);
  49.     }
  50. }
  51.  
  52. list_t *lookup(char *name){ /* return symbol if found or NULL if not found */
  53.     unsigned int hashval = hash(name);
  54.     list_t *l = hash_table[hashval];
  55.     while ((l != NULL) && (strcmp(name,l->st_name) != 0)) l = l->next;
  56.     return l; // NULL is not found
  57. }
  58.  
  59. list_t *lookup_scope(char *name, int scope){ /* return symbol if found or NULL if not found */
  60.     unsigned int hashval = hash(name);
  61.     list_t *l = hash_table[hashval];
  62.     while ((l != NULL) && (strcmp(name,l->st_name) != 0) && (scope != l->scope)) l = l->next;
  63.     return l; // NULL is not found
  64. }
  65.  
  66. void hide_scope(){ /* hide the current scope */
  67.     if(cur_scope > 0) cur_scope--;
  68. }
  69.  
  70. void incr_scope(){ /* go to next scope */
  71.     cur_scope++;
  72. }
  73.  
  74. /* print to stdout by default */
  75. void symtab_dump(FILE * of){  
  76.   int i;
  77.   fprintf(of,"------------ ------ ------------\n");
  78.   fprintf(of,"Name         Type   Line Numbers\n");
  79.   fprintf(of,"------------ ------ -------------\n");
  80.   for (i=0; i < SIZE; ++i){
  81.     if (hash_table[i] != NULL){
  82.         list_t *l = hash_table[i];
  83.         while (l != NULL){
  84.             RefList *t = l->lines;
  85.             fprintf(of,"%-12s ",l->st_name);
  86.             if (l->st_type == INT_TYPE) fprintf(of,"%-7s","int");
  87.             else if (l->st_type == REAL_TYPE) fprintf(of,"%-7s","real");
  88.             else if (l->st_type == STR_TYPE) fprintf(of,"%-7s","string");
  89.             else if (l->st_type == ARRAY_TYPE){
  90.                 fprintf(of,"array of ");
  91.                 if (l->inf_type == INT_TYPE)           fprintf(of,"%-7s","int");
  92.                 else if (l->inf_type  == REAL_TYPE)    fprintf(of,"%-7s","real");
  93.                 else if (l->inf_type  == STR_TYPE)     fprintf(of,"%-7s","string");
  94.                 else fprintf(of,"%-7s","undef");
  95.             }
  96.             else if (l->st_type == FUNCTION_TYPE){
  97.                 fprintf(of,"%-7s %s","function returns ");
  98.                 if (l->inf_type == INT_TYPE)           fprintf(of,"%-7s","int");
  99.                 else if (l->inf_type  == REAL_TYPE)    fprintf(of,"%-7s","real");
  100.                 else if (l->inf_type  == STR_TYPE)     fprintf(of,"%-7s","string");
  101.                 else fprintf(of,"%-7s","undef");
  102.             }
  103.             else fprintf(of,"%-7s","undef"); // if UNDEF or 0
  104.             while (t != NULL){
  105.                 fprintf(of,"%4d ",t->lineno);
  106.             t = t->next;
  107.             }
  108.             fprintf(of,"\n");
  109.             l = l->next;
  110.         }
  111.     }
  112.   }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement