home *** CD-ROM | disk | FTP | other *** search
/ ftptest.leeds.ac.uk / 2015.02.ftptest.leeds.ac.uk.tar / ftptest.leeds.ac.uk / bionet / CAE-GROUP / SCL-WIN3x / FED_PLUS.EXE / DICT.C < prev    next >
C/C++ Source or Header  |  1994-07-23  |  7KB  |  244 lines

  1. static char rcsid[] = "$Id: dict.c,v 1.2 1993/10/15 18:49:55 libes Exp $";
  2.  
  3. /*
  4.  * This work was supported by the United States Government, and is
  5.  * not subject to copyright.
  6.  *
  7.  * $Log: dict.c,v $
  8.  * Revision 1.2  1993/10/15  18:49:55  libes
  9.  * CADDETC certified
  10.  *
  11.  * Revision 1.6  1993/02/22  21:41:39  libes
  12.  * enum fix
  13.  *
  14.  * Revision 1.5  1993/01/19  22:45:07  libes
  15.  * *** empty log message ***
  16.  *
  17.  * Revision 1.4  1992/08/18  17:16:22  libes
  18.  * rm'd extraneous error messages
  19.  *
  20.  * Revision 1.3  1992/06/08  18:08:05  libes
  21.  * prettied up interface to print_objects_when_running
  22.  *
  23.  * Revision 1.2  1992/05/31  08:37:18  libes
  24.  * multiple files
  25.  *
  26.  * Revision 1.1  1992/05/28  03:56:55  libes
  27.  * Initial revision
  28.  */
  29.  
  30. #define DICT_C
  31. #include "dict.h"
  32. #include "object.h"
  33. #include "expbasic.h"
  34.  
  35. static Error    ERROR_duplicate_decl;
  36. static Error    ERROR_duplicate_decl_diff_file;
  37.  
  38. void
  39. DICTprint(Dictionary dict)
  40. {
  41.     Element e;
  42.     DictionaryEntry de;
  43.  
  44.     HASHlistinit(dict,&de);
  45.  
  46.     while (0 != (e = (HASHlist(&de)))) {
  47.         printf("key <%s>  data <%x>  line <%d>  <\"%c\" %s>  <%s>\n",
  48.             e->key,e->data,e->symbol->line,e->type,
  49.             OBJget_type(e->type),e->symbol->filename);
  50.     }
  51. }
  52.  
  53. /*
  54. ** Procedure:    DICTinitialize
  55. ** Parameters:    -- none --
  56. ** Returns:    void
  57. ** Description:    Initialize the Dictionary module
  58. */
  59.  
  60. void
  61. DICTinitialize(void)
  62. {
  63.     ERROR_duplicate_decl = ERRORcreate(
  64. "Redeclaration of %s.  Previous declaration was on line %d.", SEVERITY_ERROR);
  65.     ERROR_duplicate_decl_diff_file = ERRORcreate(
  66. "Redeclaration of %s.  Previous declaration was on line %d in file %s.", SEVERITY_ERROR);
  67. }
  68.  
  69. /*
  70. ** Procedure:    DICTcreate
  71. ** Parameters:    Naming_Function func    - the naming function to be
  72. **                      used by the new dictionary
  73. **        Error*          errc    - buffer for error code
  74. ** Returns:    Dictionary        - a new dictionary with the
  75. **                      specified naming function
  76. */
  77.  
  78. /* now a macro */
  79.  
  80. /*
  81. ** Procedure:
  82. ** Parameters:    Scope  scope    - scope in which to define symbol
  83. **        Symbol symdef    - definition of new symbol
  84. **        Error* errc    - buffer for error code
  85. ** Returns:    returns 0 for success, 1 for failure.
  86. ** Description:    Define anything in a dictionary.  Generates an error
  87.         directly if there is a duplicate value.
  88. */
  89.  
  90. /*
  91. ** Procedure:    DICTdefine
  92. ** Parameters:    Dictionary dictionary    - dictionary to modify
  93. **        Generic    entry    - entry to be added
  94. **        Error*     errc        - buffer for error code
  95. ** Returns:    int lineno        - line # of previous definition
  96. OLD:
  97. ** Returns:    Generic            - the added entry, or NULL on failure
  98. ** Requires:    entry is of an appropriate type for the dictionary's
  99. **        naming function
  100. */
  101.  
  102. int
  103. DICTdefine(Dictionary dict, char *name, Generic obj, Symbol *sym,char type)
  104. {
  105.     struct Element new, *old;
  106.  
  107.     new.key = name;
  108.     new.data = obj;
  109.     new.symbol = sym;
  110.     new.type = type;
  111.  
  112.     if (0 == (old = HASHsearch(dict,&new,HASH_INSERT))) return(0);
  113.  
  114. #if 0
  115.     if ((type == OBJ_ENUM) && (old->type == OBJ_ENUM)) {
  116.         /* if we're adding an enum, but we've already seen one */
  117.         /* (and only one enum), mark it ambiguous */
  118.         DICTchange(old,obj,sym,OBJ_AMBIG_ENUM);
  119.     } else if ((type != OBJ_ENUM) || (!IS_ENUM(old->type))) {
  120.         /* if we're adding a non-enum, or we've already added a */
  121.         /* non-enum, complain */
  122.         if (sym->filename == old->symbol->filename) {
  123.             ERRORreport_with_symbol(ERROR_duplicate_decl,sym,name,old->symbol->line);
  124.         } else {
  125.             ERRORreport_with_symbol(ERROR_duplicate_decl_diff_file,sym,name,old->symbol->line,old->symbol->filename);
  126.         }
  127.     }
  128. #endif
  129.  
  130.     /* following code is a little tricky and accounts for the fact */
  131.     /* that enumerations are entered into their first scope of */
  132.     /* visibility but may be shadowed by other objects that are declared */
  133.     /* in that scope */
  134.     if (type == OBJ_ENUM) {
  135.         if (old->type == OBJ_ENUM)
  136.             DICTchange(old,obj,sym,OBJ_AMBIG_ENUM);
  137.     } else if (IS_ENUM(old->type)) {
  138.         DICTchange(old,obj,sym,type);
  139.     } else {
  140.         if (sym->filename == old->symbol->filename) {
  141.             ERRORreport_with_symbol(ERROR_duplicate_decl,sym,name,old->symbol->line);
  142.         } else {
  143.             ERRORreport_with_symbol(ERROR_duplicate_decl_diff_file,sym,name,old->symbol->line,old->symbol->filename);
  144.         }
  145.         errc = ERROR_subordinate_failed;
  146.         return(1);
  147.     }
  148.     return 0;
  149. }
  150.  
  151. /* this version is used for defining things within an enumeration scope */
  152. /* I.e., the only error it would pick up would be an error such as */
  153. /* ENUMERATION OF ( A, A ) which has happened! */
  154. /* This is the way DICTdefine used to look before enumerations gained */
  155. /* their unusual behavior with respect to scoping and visibility rules */
  156. int
  157. DICT_define(Dictionary dict, char *name, Generic obj, Symbol *sym,char type)
  158. {
  159.     struct Element e, *e2;
  160.  
  161.     e.key = name;
  162.     e.data = obj;
  163.     e.symbol = sym;
  164.     e.type = type;
  165.  
  166.     if (0 == (e2 = HASHsearch(dict,&e,HASH_INSERT))) return(0);
  167.  
  168.     if (sym->filename == e2->symbol->filename) {
  169.         ERRORreport_with_symbol(ERROR_duplicate_decl,sym,name,e2->symbol->line);
  170.     } else {
  171.         ERRORreport_with_symbol(ERROR_duplicate_decl_diff_file,sym,name,e2->symbol->line,e2->symbol->filename);
  172.     }
  173.     errc = ERROR_subordinate_failed;
  174.     return(1);
  175. }
  176.  
  177. /*
  178. ** Procedure:    DICTundefine
  179. ** Parameters:    Dictionary dictionary    - dictionary to modify
  180. **        char *     name        - name to remove
  181. ** Returns:    Generic            - the entry removed, NULL if not found
  182.     Changed to return void, since the hash code frees the element, there
  183.     is no way to return (without godawful casting) the generic itself.
  184. */
  185.  
  186. void
  187. DICTundefine(Dictionary dict, char * name)
  188. {
  189.     struct Element e;
  190.  
  191.     e.key = name;
  192.     HASHsearch(dict,&e,HASH_DELETE);
  193. }
  194.  
  195. /*
  196. ** Procedure:    DICTlookup
  197. ** Parameters:    Dictionary dictionary    - dictionary to look in
  198. **        char *     name        - name to look up
  199. ** Returns:    Generic            - the value found, NULL if not found
  200. */
  201.  
  202. Generic
  203. DICTlookup(Dictionary dictionary, char * name)
  204. {
  205.     struct Element e, *ep;
  206.  
  207.     if (!dictionary) return 0;
  208.  
  209.     e.key = name;
  210.     ep = HASHsearch(dictionary,&e,HASH_FIND);
  211.     if (ep) {
  212.         DICT_type = ep->type;
  213.         return(ep->data);
  214.     }
  215.         return(NULL);
  216. }
  217.  
  218. /* like DICTlookup but returns symbol, too */
  219. Generic
  220. DICTlookup_symbol(Dictionary dictionary, char * name, Symbol **sym)
  221. {
  222.     struct Element e, *ep;
  223.  
  224.     if (!dictionary) return 0;
  225.  
  226.     e.key = name;
  227.     ep = HASHsearch(dictionary,&e,HASH_FIND);
  228.     if (ep) {
  229.         DICT_type = ep->type;
  230.         *sym = ep->symbol;
  231.         return(ep->data);
  232.     }
  233.         return(NULL);
  234. }
  235.  
  236. Generic
  237. DICTdo(DictionaryEntry *dict_entry)
  238. {
  239.     if (0 == HASHlist(dict_entry)) return 0;
  240.  
  241.     DICT_type = dict_entry->e->type;    /* side-effect! */
  242.     return dict_entry->e->data;
  243. }
  244.