ceptr
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
sys_defs.c
Go to the documentation of this file.
1 
11 #include "tree.h"
12 #include "def.h"
13 #include "receptor.h"
14 
15 #include "base_defs.h"
16 #include <stdarg.h>
17 #include <glob.h>
18 
19 #include "util.h"
20 #include "debug.h"
21 
22 const Symbol NULL_SYMBOL = {0,SEM_TYPE_SYMBOL,0};
23 const Structure NULL_STRUCTURE = {0,SEM_TYPE_STRUCTURE,0};
24 const Process NULL_PROCESS = {0,SEM_TYPE_PROCESS,0};
25 
26 #define _sd(s,c,t,i); s.context = c;s.semtype=t;s.id=i;
27 
28 SemTable *G_sem;
29 
30 SemTable *def_sys() {
31 
32  SemTable *sem = _sem_new();
33 
34  // bootstrap the hard-coded definitions that we need to even be able to make
35  // definitions
36 
37  _sd(DEFINITIONS,SYS_CONTEXT,SEM_TYPE_SYMBOL,DEFINITIONS_ID);
38  _sd(STRUCTURES,SYS_CONTEXT,SEM_TYPE_SYMBOL,STRUCTURES_ID);
39  _sd(STRUCTURE_DEFINITION,SYS_CONTEXT,SEM_TYPE_SYMBOL,STRUCTURE_DEFINITION_ID);
40  _sd(STRUCTURE_LABEL,SYS_CONTEXT,SEM_TYPE_SYMBOL,STRUCTURE_LABEL_ID);
41  _sd(STRUCTURE_SEQUENCE,SYS_CONTEXT,SEM_TYPE_SYMBOL,STRUCTURE_SEQUENCE_ID);
42  _sd(STRUCTURE_SYMBOL,SYS_CONTEXT,SEM_TYPE_SYMBOL,STRUCTURE_SYMBOL_ID);
43  _sd(STRUCTURE_OR,SYS_CONTEXT,SEM_TYPE_SYMBOL,STRUCTURE_OR_ID);
44  _sd(STRUCTURE_ZERO_OR_MORE,SYS_CONTEXT,SEM_TYPE_SYMBOL,STRUCTURE_ZERO_OR_MORE_ID);
45  _sd(STRUCTURE_ONE_OR_MORE,SYS_CONTEXT,SEM_TYPE_SYMBOL,STRUCTURE_ONE_OR_MORE_ID);
46  _sd(STRUCTURE_ZERO_OR_ONE,SYS_CONTEXT,SEM_TYPE_SYMBOL,STRUCTURE_ZERO_OR_ONE_ID);
47  _sd(STRUCTURE_STRUCTURE,SYS_CONTEXT,SEM_TYPE_SYMBOL,STRUCTURE_STRUCTURE_ID);
48  _sd(SYMBOLS,SYS_CONTEXT,SEM_TYPE_SYMBOL,SYMBOLS_ID);
49  _sd(PROCESSES,SYS_CONTEXT,SEM_TYPE_SYMBOL,PROCESSES_ID);
50  _sd(PROTOCOLS,SYS_CONTEXT,SEM_TYPE_SYMBOL,PROTOCOLS_ID);
51  _sd(RECEPTORS,SYS_CONTEXT,SEM_TYPE_SYMBOL,RECEPTORS_ID);
52  _sd(SCAPES,SYS_CONTEXT,SEM_TYPE_SYMBOL,SCAPES_ID);
53  _sd(SYMBOL_DEFINITION,SYS_CONTEXT,SEM_TYPE_SYMBOL,SYMBOL_DEFINITION_ID);
54  _sd(SYMBOL_STRUCTURE,SYS_CONTEXT,SEM_TYPE_SYMBOL,SYMBOL_STRUCTURE_ID);
55  _sd(SYMBOL_LABEL,SYS_CONTEXT,SEM_TYPE_SYMBOL,SYMBOL_LABEL_ID);
56  _sd(RECEPTOR_DEFINITION,SYS_CONTEXT,SEM_TYPE_SYMBOL,RECEPTOR_DEFINITION_ID);
57  _sd(RECEPTOR_LABEL,SYS_CONTEXT,SEM_TYPE_SYMBOL,RECEPTOR_LABEL_ID);
58  _sd(ENGLISH_LABEL,SYS_CONTEXT,SEM_TYPE_SYMBOL,ENGLISH_LABEL_ID);
59 
60  base_contexts(sem);
61 
62  // this has to happen after the _sd declarations so that the basic Symbols will be valid
63  base_defs(sem);
64 
65  _r_defineClockReceptor(sem);
66  return sem;
67 }
68 
69 
70 void sys_free(SemTable *sem) {
71  _t_free(_t_root(sem->stores[0].definitions));
72  _sem_free(sem);
73 }
74 
75 Context G_ctx;
76 char * G_label;
77 
78 T *sT_(SemTable *sem,Symbol sym,int num_params,...){
79  va_list params;
80  T *set = _t_newr(0,sym);
81  va_start(params,num_params);
82  int i;
83  for(i=0;i<num_params;i++) {
84  T * t = va_arg(params,T *);
85  if (semeq(_t_symbol(t),STRUCTURE_SYMBOL)) {
86  Symbol ss = *(Symbol *)_t_surface(t);
87  if (is_structure(ss)) {
88  T *structures = _sem_get_defs(sem,ss);
89  T *st = _t_child(structures,ss.id);
90  if (!st) {
91  raise_error("Structure used in %s definition is undefined!",G_label);
92  }
93  else {
94  _t_free(t);
95  t = _t_clone(_t_child(st,2));
96  }
97  }
98  else if (ss.id == -1) {raise_error("Symbol used in %s definition is undefined!",G_label);}
99  }
100  _t_add(set,t);
101  }
102  va_end(params);
103  return set;
104 }
105 
106 void load_contexts(SemTable *sem) {
107  glob_t paths;
108  int x;
109  char **p;
110 
111  /* Find all ".c" files in given directory*/
112  x = glob("contexts/*.cptr", 0, NULL, &paths);
113 
114  if (x == 0)
115  {
116  Receptor *r = _r_new(sem,SYS_RECEPTOR);
117  for (p=paths.gl_pathv; *p != NULL; ++p)
118  load_context(*p,r);
119  globfree(&paths);
120  _r_free(r);
121  }
122 }
123 
124 void load_context(char *path, Receptor *parent) {
125 
126  debug(D_BOOT,"loading %s\n",path);
127  // get just the file name from the path
128  size_t l = strlen(path);
129  int i;
130  i = l-1;
131  char *s = &path[i];
132  while (i-- > 0 && *s != '/' ) s--;
133  if (i > 0) s++;
134  char name[255];
135  strcpy(name,s);
136  name[strlen(name)-5]=0;
137 
138  // see if the file is an existing context that we should just add to
139  // or if we will use its name to create a new context
140  SemTable *sem = parent->sem;
141  SemanticID rsid;
142  Receptor *r = 0;
143 
144  char *code_text = readFile(path,&l);
145  char *start,*t,c;
146  char p = 0;
147 
148  char ctx[255];
149 
150  s = code_text;
151  while(l>0) {
152  start = s;
153  // look for a line that starts with a '-' which mark indicates that we should
154  // stop reading an interpret what came before
155  ctx[0] = 0;
156  while(l>0) {
157  l--; c = *s++;
158  if (p == '\n' && c == '-') {
159  *(s-1) = 0;
160  t = ctx;
161  // look for a name on that line that we load into ctx
162  while((c=*s++) && c != '\n') *t++ = c;
163  *t = 0;
164  break;
165  }
166  p = c;
167  }
168  // if ctx isn't the same as the name, then it names the context we are to load
169  // this (and subsequent) code chunks into, so copy ctx into name and cleanup the
170  // last receptor instance if needed
171  if (ctx[0] && strcmp(ctx,name)) {
172  strcpy(name,ctx);
173  if (r) {_r_free(r);r = 0;};
174  }
175  // set up a receptor instance for the named context and create a the context if needed.
176  if (!r) {
177  if (_sem_get_by_label(sem,name,&rsid)) {
178  debug(D_BOOT,"found existing context: %s\n",name);
179  if (!is_receptor(rsid)) raise_error("%s is not a receptor!",name);
180  }
181  else {
182  debug(D_BOOT,"creating context: %s\n",name);
183  T *def = _t_parse(sem,0,"(RECEPTOR_DEFINITION (RECEPTOR_LABEL %) (DEFINITIONS (STRUCTURES) (SYMBOLS) (PROCESSES) (RECEPTORS) (PROTOCOLS) (SCAPES)))",_t_new_str(0,ENGLISH_LABEL,name));
184  rsid = __d_define_receptor(sem,def,parent->context);
185  }
186  r = _r_new(sem,rsid);
187  }
188  debug(D_BOOT,"executing into %s:\n%s\n",name,start);
189  T *code = _t_parse(sem,0,start);
190  if (code) {
191  Q *q = r->q;
192  T *run_tree = __p_build_run_tree(code,0);
193  _t_free(code);
194  Qe *e = _p_addrt2q(q,run_tree);
195  _p_reduceq(q);
196  debug(D_BOOT,"results in: %s\n",_t2s(sem,run_tree));
197  }
198  }
199  if (r) _r_free(r);
200 }
Definition: ceptr_types.h:114
Definition: ceptr_types.h:206
header file for symbol and structure definition functions
T * _t_clone(T *t)
Definition: tree.c:589
void _r_free(Receptor *r)
Definition: receptor.c:186
Error _p_reduceq(Q *q)
Definition: process.c:2126
semantic trees header file
T * _t_root(T *t)
Definition: tree.c:1272
Symbol _t_symbol(T *t)
Definition: tree.c:1228
T * _t_child(T *t, int i)
Definition: tree.c:1251
Context context
the context this receptor's definition creates
Definition: ceptr_types.h:240
receptor implementation header file
Q * q
process queue
Definition: ceptr_types.h:250
void * _t_surface(T *t)
Definition: tree.c:1215
Receptor * _r_new(SemTable *sem, SemanticID r)
Creates a new receptor.
Definition: receptor.c:88
SemTable * sem
pointer back to the genotype table for this receptor's vmhost instance
Definition: ceptr_types.h:242
SemanticID __d_define_receptor(SemTable *sem, T *def, Context c)
Definition: def.c:478
T * _t_parse(SemTable *sem, T *parent, char *s,...)
Definition: tree.c:919
void _t_add(T *t, T *c)
Definition: tree.c:261
auto-generated system definitions
void _t_free(T *t)
Definition: tree.c:526