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 / SCHEMA.C < prev    next >
C/C++ Source or Header  |  1994-07-23  |  7KB  |  292 lines

  1. static char rcsid[] = "$Id: schema.c,v 1.9 1993/10/15 18:48:48 libes Exp $";
  2.  
  3. /************************************************************************
  4. ** Module:    Schema
  5. ** Description:    This module implements the Schema abstraction, which
  6. **    basically amounts to a named symbol table.
  7. ** Constants:
  8. **    SCHEMA_NULL    - the null schema
  9. **
  10. ************************************************************************/
  11.  
  12. /*
  13.  * This code was developed with the support of the United States Government,
  14.  * and is not subject to copyright.
  15.  *
  16.  * $Log: schema.c,v $
  17.  * Revision 1.9  1993/10/15  18:48:48  libes
  18.  * CADDETC certified
  19.  *
  20.  * Revision 1.8  1993/02/22  21:48:53  libes
  21.  * removed old ifdeffed code
  22.  *
  23.  * Revision 1.7  1993/01/19  22:16:43  libes
  24.  * *** empty log message ***
  25.  *
  26.  * Revision 1.6  1992/08/27  23:42:20  libes
  27.  * *** empty log message ***
  28.  *
  29.  * Revision 1.5  1992/08/18  17:13:43  libes
  30.  * rm'd extraneous error messages
  31.  *
  32.  * Revision 1.4  1992/06/08  18:06:57  libes
  33.  * prettied up interface to print_objects_when_running
  34.  */
  35.  
  36. #define SCHEMA_C
  37. #include "expbasic.h"
  38. #include "schema.h"
  39. #include "object.h"
  40. #include "resolve.h"
  41. #include <stdlib.h>
  42.  
  43. Symbol *
  44. RENAME_get_symbol(Generic r)
  45. {
  46.     return(((Rename *)r)->old);
  47. }
  48.  
  49. /*
  50. ** Procedure:    SCHEMAinitialize
  51. ** Parameters:    -- none --
  52. ** Returns:    void
  53. ** Description:    Initialize the Schema module.
  54. */
  55.  
  56. void
  57. SCHEMAinitialize(void)
  58. {
  59.     OBJcreate(OBJ_RENAME,RENAME_get_symbol,"rename clause",OBJ_UNUSED_BITS);
  60.     MEMinitialize(&REN_fl,sizeof(struct Rename),30,30);
  61.     MEMinitialize(&SCHEMA_fl,sizeof(struct Schema),40,20);
  62. }
  63.  
  64. /*
  65. ** Procedure:    SCOPEcreate
  66. ** Parameters:    Scope  scope    - next higher scope
  67. **        Error* errc    - buffer for error code
  68. ** Returns:    Scope        - the scope created
  69. ** Description:    Create and return an empty scope inside a parent scope.
  70. */
  71.  
  72. Scope
  73. SCOPEcreate(char type)
  74. {
  75.     Scope d = SCOPE_new();
  76.     d->symbol_table = DICTcreate(50);
  77.     d->type = type;
  78.     return d;
  79. }
  80.  
  81. Scope
  82. SCOPEcreate_tiny(char type)
  83. {
  84.     Scope d = SCOPE_new();
  85.     d->symbol_table = DICTcreate(1);
  86.     d->type = type;
  87.     return d;
  88. }
  89.  
  90. /* create a scope without a symbol table */
  91. /* used for simple types */
  92. Scope
  93. SCOPEcreate_nostab(char type)
  94. {
  95.     Scope d = SCOPE_new();
  96.     d->type = type;
  97.     return d;
  98. }
  99.  
  100. /*
  101. ** Procedure:    SCHEMAcreate
  102. ** Parameters:    String name    - name of schema to create
  103. **        Scope  scope    - local scope for schema
  104. **        Error* errc    - buffer for error code
  105. ** Returns:    Schema        - the schema created
  106. ** Description:    Create and return a schema as specified.
  107. */
  108.  
  109. Schema
  110. SCHEMAcreate(void)
  111. {
  112.     Scope s = SCOPEcreate(OBJ_SCHEMA);
  113.     s->u.schema = SCHEMA_new();
  114.     return s;
  115. }
  116.  
  117. /*
  118. ** Procedure:    SCHEMAget_name
  119. ** Parameters:    Schema schema    - schema to examine
  120. ** Returns:    String        - schema name
  121. ** Description:    Retrieve the name of a schema.
  122. */
  123.  
  124. /* this function is implemented as a macro in schema.h */
  125.  
  126. /*
  127. ** Procedure:    SCHEMAdump
  128. ** Parameters:    Schema schema    - schema to dump
  129. **        FILE*  file    - file to dump to
  130. ** Returns:    void
  131. ** Description:    Dump a schema to a file.
  132. **
  133. ** Notes:    This function is provided for debugging purposes.
  134. */
  135.  
  136. #if 0
  137. void
  138. SCHEMAdump(Schema schema, FILE* file)
  139. {
  140.     fprintf(file, "SCHEMA %s:\n", SCHEMAget_name(schema));
  141.     SCOPEdump(schema, file);
  142.     fprintf(file, "END SCHEMA %s\n\n", SCHEMAget_name(schema));
  143. }
  144. #endif
  145.  
  146. #if 0
  147. SYMBOLprint(Symbol *s)
  148. {
  149.     printf("%s (r:%d #:%d f:%s)\n",s->name,s->resolved,s->line,s->filename);
  150. }
  151. #endif
  152.  
  153. void
  154. SCHEMAadd_reference(Schema cur_schema, Symbol *ref_schema, Symbol *old, Symbol *new)
  155. {
  156.     Rename *r = REN_new();
  157.     r->schema_sym = ref_schema;
  158.     r->old = old;
  159.     r->new = new;
  160.     r->rename_type = ref;
  161.  
  162.     if (!cur_schema->u.schema->refdict) cur_schema->u.schema->refdict = DICTcreate(20);
  163.     DICTdefine(cur_schema->u.schema->refdict,
  164.         (new?new->name:old->name),
  165.         (Generic)r,r->old,OBJ_RENAME);
  166. }
  167.  
  168. void
  169. SCHEMAadd_use(Schema cur_schema, Symbol *ref_schema, Symbol *old, Symbol *new)
  170. {
  171.     Rename *r = REN_new();
  172.     r->schema_sym = ref_schema;
  173.     r->old = old;
  174.     r->new = new;
  175.     r->rename_type = use;
  176.  
  177.     if (!cur_schema->u.schema->usedict) cur_schema->u.schema->usedict = DICTcreate(20);
  178.     DICTdefine(cur_schema->u.schema->usedict,
  179.         (new?new->name:old->name),
  180.         (Generic)r,r->old,OBJ_RENAME);
  181. }
  182.  
  183. static void
  184. SCHEMA_get_entities_use(Scope scope,Linked_List result)
  185. {
  186.     DictionaryEntry    de;
  187.     Rename *rename;
  188.  
  189.     if (scope->search_id == __SCOPE_search_id) return;
  190.     scope->search_id = __SCOPE_search_id;
  191.  
  192.     /* fully USE'd schema */
  193.     LISTdo(scope->u.schema->uselist,schema,Schema )
  194.         SCOPE_get_entities(schema,result);
  195.         SCHEMA_get_entities_use(schema,result);
  196.     LISTod
  197.  
  198.     /* partially USE'd schema */
  199.     if (scope->u.schema->usedict) {
  200.         DICTdo_init(scope->u.schema->usedict,&de);
  201.         while (0 != (rename = (Rename *)DICTdo(&de))) {
  202.             LISTadd(result,rename->object);
  203.         }
  204.     }
  205. }
  206.  
  207. /* return use'd entities */
  208. Linked_List
  209. SCHEMAget_entities_use(Scope scope)
  210. {
  211.     Linked_List result = LISTcreate();
  212.  
  213.     __SCOPE_search_id++;
  214.     ENTITY_MARK++;
  215.  
  216.     SCHEMA_get_entities_use(scope,result);
  217.     return(result);
  218. }
  219.  
  220. /* return ref'd entities */
  221. static
  222. void
  223. SCHEMA_get_entities_ref(Scope scope,Linked_List result)
  224. {
  225.     Rename *rename;
  226.     DictionaryEntry    de;
  227.  
  228.     if (scope->search_id == __SCOPE_search_id) return;
  229.     scope->search_id = __SCOPE_search_id;
  230.  
  231.     ENTITY_MARK++;
  232.  
  233.     /* fully REF'd schema */
  234.     LISTdo(scope->u.schema->reflist,schema,Schema )
  235.         SCOPE_get_entities(schema,result);
  236.         /* don't go down remote schema's reflist */
  237.     LISTod
  238.  
  239.     /* partially REF'd schema */
  240.     DICTdo_init(scope->u.schema->refdict,&de);
  241.     while (0 != (rename = (Rename *)DICTdo(&de))) {
  242.         if (DICT_type == OBJ_ENTITY) {
  243.             LISTadd(result,rename->object);
  244.         }
  245.     }
  246. }
  247.  
  248. /* return ref'd entities */
  249. Linked_List 
  250. SCHEMAget_entities_ref(Scope scope)
  251. {
  252.     Linked_List result = LISTcreate();
  253.  
  254.     __SCOPE_search_id++;
  255.     ENTITY_MARK++;
  256.  
  257.     result = LISTcreate();
  258.     SCHEMA_get_entities_ref(scope,result);
  259.     return(result);
  260. }
  261.  
  262. /* look up an attribute reference */
  263. /* if strict false, anything can be returned, not just attributes */
  264. Variable
  265. VARfind(Scope scope,char *name, int strict)
  266. {
  267.     Variable result;
  268.  
  269.     /* first look up locally */
  270.     result = (Variable)DICTlookup(scope->symbol_table,name);
  271.     if (result) {
  272.         if (strict && (DICT_type != OBJ_VARIABLE)) {
  273.             printf("press ^C now to trap to debugger\n");
  274.             exit(0);
  275.         }
  276.         return result;
  277.     }
  278.     switch (scope->type) {
  279.     case OBJ_ENTITY:
  280.         LISTdo(scope->u.entity->supertypes,e,Entity)
  281.             result = VARfind(e,name,strict);
  282.             if (result) return(result);
  283.         LISTod;
  284.         break;
  285.     case OBJ_INCREMENT:
  286.     case OBJ_QUERY:
  287.     case OBJ_ALIAS:
  288.         return(VARfind(scope->superscope,name,strict));
  289.     }
  290.     return 0;
  291. }
  292.