home *** CD-ROM | disk | FTP | other *** search
/ ftp.parl.clemson.edu / 2015-02-07.ftp.parl.clemson.edu.tar / ftp.parl.clemson.edu / pub / coven / coven-utils-1.1.tgz / coven-utils-1.1.tar / utils / coven-language / symbol.c < prev    next >
C/C++ Source or Header  |  2003-01-28  |  7KB  |  299 lines

  1. /*
  2.  * (C) 2001 Clemson University
  3.  *
  4.  * See COPYING in top-level directory.
  5.  */
  6.  
  7.  
  8. #ifndef lint
  9. static char symbol_c_rcsid[] = "$Header: /projects/cvsroot/coven-language/symbol.c,v 1.3 2003/01/28 14:55:09 ndebard Exp $";
  10. #endif
  11.  
  12. /* $Log: symbol.c,v $
  13. /* Revision 1.3  2003/01/28 14:55:09  ndebard
  14. /* New GPL.
  15. /*
  16. /* Revision 1.2  2003/01/23 13:06:54  ndebard
  17. /* GPLed this dir.
  18. /*
  19. /* Revision 1.1.1.1  2003/01/23 11:47:48  ndebard
  20. /* Imported source - from Coven project, reorganizing
  21. /*
  22. /* Revision 1.4  2002/11/05 23:46:41  ndebard
  23. /* Working on the language.  It should handle everything up to dropping the
  24. /* output for module calls right now.
  25. /*
  26. /* Revision 1.3  2002/07/31 14:44:48  ndebard
  27. /* Lots of changes while in Scotland.  This now parses the MODULES and
  28. /* VARIABLES sections correctly (it is believed).  The backend of the Coven
  29. /* driver needs to be redesigned now to handle some new data types before
  30. /* the PROGRAM section can be completed.
  31. /*
  32. /* Revision 1.2  2002/07/03 20:45:53  walt
  33. /* initial added symbol table stuff
  34. /*
  35. /* Revision 1.1  2002/07/03 18:55:12  walt
  36. /* First set of files for Coven compiler - WBL
  37. /*
  38.  * 
  39.  * Revision 1.1  90/02/02  10:19:51  walt
  40.  * Initial revision
  41.  * 
  42.  */
  43.  
  44. /*
  45. *    symbol --- symbol table and mapping routines
  46. */
  47.  
  48. #include <stdio.h>
  49. #include "symbol.h"
  50. #include "stringtbl.h"
  51. #include "y.tab.h"
  52. extern int line;
  53.  
  54. /*
  55.  *    MAXHASH --- determines the hash table width.
  56.  *    symtab --- the symbol table structure
  57.  */
  58.  
  59. #define MAXHASH    311
  60.  
  61. static sym_ent_p symtab[MAXHASH];
  62.  
  63. /*
  64.  *    external routine declarations
  65.  */
  66.  
  67. int strcmp();
  68.  
  69. /*
  70.  *    hash --- scramble a name (hopefully) uniformly to fit in a table
  71.  */
  72. static unsigned int hash(char *name)
  73. {
  74.     register unsigned int h = 0;
  75.     while (*name)
  76.     {
  77.         h <<= 4;
  78.         h ^= *name++;
  79.     }
  80.     return(h % MAXHASH);
  81. }
  82.  
  83. /*
  84.  *    symenter --- enter a name into the symbol table
  85.  */
  86. sym_ent_p symenter(char *name, sym_ent_p context, int type)
  87. {
  88.     register sym_ent_p p;
  89.     unsigned int h;
  90.  
  91.     /* first check for conflicts */
  92.     p = symlook(name, context, type);
  93.     if(p != NULL) {
  94.         yyerror("Symbol name conflict when adding name to symbol table.");
  95.         exit(1);
  96.     }
  97.  
  98.     if(type == TYPE_TYPE || 
  99.        type == TYPE_VARIABLE ||
  100.        type == TYPE_CONST ||
  101.        (context == NULL && type == TYPE_MODULE)
  102.       ) 
  103.     {
  104.         /* create an entry and insert it at the front of the table */
  105.         h = hash(name);
  106.         p = (sym_ent_p)emalloc(sizeof(sym_ent));
  107.         p->name = name;
  108.         p->next = symtab[h];
  109.         p->type = type;
  110.         switch(type) {
  111.           case TYPE_MODULE:
  112.             p->value.mod = (module_ent_p)emalloc(sizeof(module_ent));
  113.             p->value.mod->args = NULL;
  114.         break;
  115.           case TYPE_VARIABLE:
  116.         p->value.var = (variable_ent_p)emalloc(sizeof(variable_ent));
  117.         break;
  118.           case TYPE_CONST:
  119.         p->value.con = (const_ent_p)emalloc(sizeof(const_ent));
  120.         break;
  121.           case TYPE_TYPE:
  122.         /* we don't do anything here, yet */
  123.         break;
  124.         }
  125.         symtab[h] = p;
  126.     }
  127.     else {
  128.         p = (sym_ent_p)emalloc(sizeof(sym_ent));
  129.         p->name = name;
  130.         p->next = NULL;
  131.         p->type = type;
  132.     }
  133.  
  134.     return(p);
  135. }
  136.  
  137. /*
  138.  *    symlook --- lookup a symbol in the symbol table
  139.  *
  140.  *        symlook scans the symbol table and returns a pointer to
  141.  *        the symbol table entry
  142.  */
  143. sym_ent_p symlook(char *name, sym_ent_p context, int type)
  144. {
  145.     register sym_ent_p p;
  146.     unsigned int h;
  147.     
  148.     if(context == NULL) {
  149.         h = hash(name);
  150.         for (p = symtab[h]; p != 0; p = p->next)
  151.         if(strcmp(p->name, name) == 0)
  152.             break;
  153.     }
  154.     else {
  155.         if(type == TYPE_MODULE)
  156.             p = context->value.mod->args;
  157.         else
  158.                p = context;
  159.         while(p != NULL) {
  160.             if(strcmp(p->name, name) == 0)
  161.             break;
  162.             p = p->next;
  163.         }
  164.     }
  165.  
  166.     return(p);
  167. }
  168.  
  169. sym_ent_p lookup_type(char *name)
  170. {
  171.     sym_ent_p p;
  172.     p = symlook(name, NULL, TYPE_TYPE);
  173.     if(p != NULL && p->type == TYPE_TYPE) return p;
  174.     return NULL;
  175. }
  176.  
  177. void init_symbol_table()
  178. {
  179.     bzero(symtab, MAXHASH * sizeof(sym_ent_p));
  180. }
  181.  
  182. void sym_print (sym_ent_p sym, char *prepend)
  183. {
  184.       printf(prepend);
  185.     printf("SYMBOL [%s]\n", sym->name);
  186.     switch(sym->type) {
  187.       case TYPE_MODULE:
  188.            printf(prepend);
  189.         printf("\ttype: MODULE\n");
  190.            printf(prepend);
  191.         printf("\tmodule path: %s\n", sym->value.mod->path);
  192.         {
  193.             sym_ent_p cur = sym->value.mod->args;
  194.         while(cur != NULL) {
  195.             sym_print(cur, "\t");
  196.             cur = cur->next;
  197.         }
  198.         }
  199.         break;
  200.       case TYPE_VARIABLE:
  201.            printf(prepend);
  202.         printf("\ttype: VARIABLE\n");
  203.         switch(sym->value.var->vclass) {
  204.           case BUFFER:
  205.                printf(prepend);
  206.         printf("\tclass: BUFFER\n");
  207.         break;
  208.           case PARAMETER:
  209.                printf(prepend);
  210.         printf("\tclass: PARAMETER\n");
  211.         break;
  212.           case -1:
  213.         printf(prepend);
  214.         printf("\tclass: NONE DEFINED\n");
  215.         break;
  216.           default:
  217.                printf(prepend);
  218.         printf("\tclass: * UNDEFINED * ERROR!\n");
  219.         break;
  220.         }
  221.         printf(prepend);
  222.         printf("\ttype: %s\n", sym->value.var->type->name);
  223.         break;
  224.       case TYPE_CONST:
  225.            printf(prepend);
  226.         printf("\ttype: CONST\n");
  227.         printf(prepend);
  228.         printf("\ttype: %s\n", sym->value.con->type->name);
  229.         printf(prepend);
  230.         printf("\tvalue (as a string): %s\n", sym->value.con->cvalue);
  231.         break;
  232.       case TYPE_TYPE:
  233.         printf(prepend);
  234.         printf("\ttype: TYPE\n");
  235.         break;
  236.       case TYPE_ARGUMENT:
  237.            printf(prepend);
  238.         printf("\ttype: ARGUMENT\n");
  239.         switch(sym->value.arg->direction) {
  240.           case INPUT:
  241.                printf(prepend);
  242.           printf("\tdirection: INPUT\n");
  243.         break;
  244.           case OUTPUT:
  245.                printf(prepend);
  246.         printf("\tdirection: OUTPUT\n");
  247.         break;
  248.           case INOUT:
  249.                printf(prepend);
  250.         printf("\tdirection: INOUT\n");
  251.         break;
  252.           case STATIC:
  253.                printf(prepend);
  254.         printf("\tdirection: STATIC\n");
  255.         break;
  256.           default:
  257.                printf(prepend);
  258.         printf("\tdirection: * UNDEFINED * ERROR!\n");
  259.         break;
  260.         }
  261.         switch(sym->value.arg->mclass) {
  262.           case BUFFER:
  263.                printf(prepend);
  264.         printf("\tclass: BUFFER\n");
  265.         break;
  266.           case PARAMETER:
  267.                printf(prepend);
  268.         printf("\tclass: PARAMETER\n");
  269.         break;
  270.           default:
  271.                printf(prepend);
  272.         printf("\tclass: * UNDEFINED * ERROR!\n");
  273.         break;
  274.         }
  275.            printf(prepend);
  276.         printf("\ttype: %s\n", sym->value.arg->type->name);
  277.         break;
  278.       default:
  279.            printf(prepend);
  280.         printf("\ttype: * UNDEFINED * ERROR!\n");
  281.         break;
  282.     }
  283. }
  284.  
  285. void symtab_print(void)
  286. {
  287.     int i;
  288.        for(i=0; i<MAXHASH; i++) {
  289.         sym_ent_p cur = symtab[i];
  290.         if(cur != NULL)
  291.             printf("*** [ HASH VALUE ] *** %d\n", i);
  292.         while(cur != NULL) {
  293.            sym_print(cur, "");
  294.            cur = cur->next;
  295.         }
  296.     }
  297. }
  298.  
  299.