ceptr
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
semtable.c
Go to the documentation of this file.
1 
11 #include "semtable.h"
12 #include "def.h"
13 
14 SemTable *_sem_new() {
15  SemTable * sem= malloc(sizeof(SemTable));
16  memset(sem,0,sizeof(SemTable));
17 }
18 
19 int _sem_new_context(SemTable *sem,T *definitions) {
20 
21  if (sem->contexts >= MAX_CONTEXTS-1) raise_error("no more room in semtable");
22  int idx = sem->contexts++;
23  // sem->stores[idx].table = NULL;
24  sem->stores[idx].definitions = definitions;
25  return idx;
26 }
27 
28 void _sem_free(SemTable *sem) {
29  free(sem);
30 }
31 
32 ContextStore *__sem_context(SemTable *sem,Context c) {
33  return &sem->stores[c];
34 }
35 
36 void _sem_free_context(SemTable *sem,Context c) {
37  ContextStore *ctx = __sem_context(sem,c);
38  // definition tree belong to the receptors that allocated them so
39  // we never free them.
40  ctx->definitions = NULL;
41  //if (ctx->table) lableTableFree(ctx->table);
42 
43  if ((c+1) == sem->contexts)
44  sem->contexts--;
45 }
46 
47 char G_ctx_buf[20];
48 
49 char *_sem_ctx2s(SemTable *sem,Context c) {
50  switch(c) {
51  case SYS_CONTEXT:return "SYS_CONTEXT";
52  case COMPOSITORY_CONTEXT:return "COMPOSITORY_CONTEXT";
53  case DEV_COMPOSITORY_CONTEXT:return "DEV_COMPOSITORY_CONTEXT";
54  case TEST_CONTEXT:return "TEST_CONTEXT";
55  default:
56  sprintf(G_ctx_buf,"%d",c);
57  return G_ctx_buf;
58  }
59 }
60 
61 // get the definitions for the semantic type of the semid
62 T *__sem_get_defs(SemTable *sem,SemanticType semtype,Context c) {
63  ContextStore *ctx = __sem_context(sem,c);
64  if (!ctx->definitions) raise_error("no definitions in context %s",_sem_ctx2s(sem,c));
65  T *defs = _t_child(ctx->definitions,semtype);
66  if (!defs) raise_error("no defs for semtype %d in context %s",semtype,_sem_ctx2s(sem,c));
67  return defs;
68 }
69 
70 T *__sem_get_def(SemTable *sem,SemanticType semtype,Context c,SemanticAddr i) {
71  T *defs = __sem_get_defs(sem,semtype,c);
72  return _t_child(defs,i);
73 }
74 
86  if (s.id == 0) {
87  if (s.context == SYS_CONTEXT) {
88  switch (s.semtype) {
89  case SEM_TYPE_STRUCTURE: return "NULL_STRUCTURE";
90  case SEM_TYPE_SYMBOL: return "NULL_SYMBOL";
91  case SEM_TYPE_PROCESS: return "NULL_PROCESS";
92  case SEM_TYPE_RECEPTOR: return "SYS_RECEPTOR";
93  default: raise_error("bad semtype");
94  }
95  }
96  else {
97  raise_error("unexpected semantic NULL id!");
98  }
99  }
100  T *def = _sem_get_def(sem,s);
101  char *n = NULL;
102  if (def) {
103  int path[] ={DefLabelIdx,1,TREE_PATH_TERMINATOR};
104  T *t = _t_get(def,path);
105  if (!t) raise_error("missing label!");
106  n = (char *)_t_surface(t);
107  }
108  return n;
109 }
110 
124 T * _sem_get_label(SemTable *sem,SemanticID s,Symbol label_type) {
125  if (s.id == 0) {
126  raise_error("semantic NULL have no defs!");
127  }
128  T *def = _sem_get_def(sem,s);
129  T *label = NULL;
130  if (def) {
131  T *labels = _t_child(def,DefLabelIdx);
132  label = _t_find(labels,label_type);
133  if (!label) label = _t_child(labels,1);
134  }
135  return label;
136 }
148 void _sem_add_label(SemTable *sem,SemanticID s,Symbol label_type,char *label) {
149  T *def = _sem_get_def(sem,s);
150  T *labels = _t_child(def,DefLabelIdx);
151  _t_new_str(labels,label_type,label);
152 }
153 
154 Structure _sem_get_symbol_structure(SemTable *sem,Symbol s){
155  if (!is_symbol(s)) raise_error("Bad symbol: semantic type not SEM_TYPE_SYMBOL");
156  return __d_get_symbol_structure(_sem_get_defs(sem,s),s);
157 }
158 
159 // @todo, convert this to hash table label table!!
160 bool __sem_get_by_label(SemTable *sem,char *label,SemanticID *sid,Context c) {
161  ContextStore *ctx = __sem_context(sem,c);
162  T *d = ctx->definitions;
163  if (!d) raise_error("no definitions in context %s",_sem_ctx2s(sem,c));
164  int i,j;
165  for(i=1;i<=_t_children(d);i++) {
166  T *defs = _t_child(d,i);
167  for(j=1;j<=_t_children(defs);j++) {
168  T *def = _t_child(defs,j);
169  if (strcmp(label,(char *)_t_surface(_t_child(_t_child(def,DefLabelIdx),1)))==0) {
170  sid->semtype = i;
171  sid->id = j;
172  sid->context = c;
173  return true;
174  }
175  }
176  }
177  return false;
178 }
179 
180 bool _sem_get_by_label(SemTable *sem,char *label,SemanticID *sid) {
181  int i;
182  if (!strcmp(label,"NULL_SYMBOL")) {*sid = NULL_SYMBOL; return true;}
183  for(i=0;i<sem->contexts;i++)
184  if (__sem_get_by_label(sem,label,sid,i)) return true;
185  return false;
186 }
187 
char * _sem_get_name(SemTable *sem, SemanticID s)
Definition: semtable.c:85
Definition: ceptr_types.h:114
T * _t_get(T *t, int *p)
Definition: tree.c:1441
header file for symbol and structure definition functions
T * _t_child(T *t, int i)
Definition: tree.c:1251
T * _sem_get_label(SemTable *sem, SemanticID s, Symbol label_type)
Definition: semtable.c:124
Structure __d_get_symbol_structure(T *symbols, Symbol s)
Definition: def.c:156
void * _t_surface(T *t)
Definition: tree.c:1215
header file for table for coordinating meaning across a vmhost
void _sem_add_label(SemTable *sem, SemanticID s, Symbol label_type, char *label)
Definition: semtable.c:148
int _t_children(T *t)
Definition: tree.c:1205