16 char __d_extra_buf[100];
19 return (s1.context == s2.context)&&(s1.semtype == s2.semtype)&&(s1.id==s2.id);
25 SemanticAddr _d_get_def_addr(
T *def) {
30 T *definitions = __sem_get_defs(sem,semtype,c);
32 SemanticID sid = {c,semtype,_d_get_def_addr(def)};
38 if (!is_symbol(s)) raise_error(
"Bad symbol in %s def: semantic type not SEM_TYPE_SYMBOL",n);
39 if (is_sys_symbol(s) && (s.id == 0))
return;
40 T *symbols = _sem_get_defs(sem,s);
41 if (!
_t_child(symbols,s.id)) raise_error(
"Bad symbol in %s def: definition not found in context",n);
46 if (!is_structure(s)) raise_error(
"Bad structure in %s def: semantic type not SEM_TYPE_STRUCTURE",n);
47 if (is_sys_structure(s) && (s.id == 0))
return;
48 T *structures = _sem_get_defs(sem,s);
49 if(s.id && !
_t_child(structures,s.id)) {raise_error(
"Unknown structure <%d.%d.%d> in declaration of %s",s.context,s.semtype,s.id,n);}
56 if (!t) raise_error(
"def not found!");
57 T * structure_def =
_t_child(t,SymbolDefStructureIdx);
59 raise_error(
"Symbol already defined");
64 void __d_set_structure_def(
T *structures,
Structure s,
T *def) {
66 if (
_t_children(d) > 1) raise_error(
"Structure already defined");
84 __d_validate_structure(sem,s,label);
86 T *l = _t_newr(def,SYMBOL_LABEL);
87 _t_new_str(l,ENGLISH_LABEL,label);
88 _t_news(def,SYMBOL_STRUCTURE,s);
90 return _d_define(sem,def,SEM_TYPE_SYMBOL,c);
106 T *l = _t_newr(def,STRUCTURE_LABEL);
107 _t_new_str(l,ENGLISH_LABEL,label);
108 if (structure_def)
_t_add(def,structure_def);
109 return _d_define(sem,def,SEM_TYPE_STRUCTURE,c);
126 va_start(params,num_params);
127 T *def = _d_make_vstruc_def(sem,label,num_params,params);
133 T * _d_make_vstruc_def(
SemTable *sem,
char *label,
int num_params,va_list params) {
136 seq = _t_newr(0,STRUCTURE_SEQUENCE);
138 for(i=0;i<num_params;i++) {
140 __d_validate_symbol(sem,s,label);
141 p = _t_news(seq,STRUCTURE_SYMBOL,s);
143 return seq ? seq : p;
159 raise_error(
"Bad symbol:%d.%d.%d-- only %d symbols in decl list",s.context,s.semtype,s.id,
_t_children(symbols));
161 T *t =
_t_child(def,SymbolDefStructureIdx);
179 Structure st = _sem_get_symbol_structure(sem,s);
183 size_t _sys_structure_size(
int id,
void *surface) {
186 case NULL_STRUCTURE_ID:
return 0;
193 case INTEGER_ID:
return sizeof(int);
194 case INTEGER64_ID:
return sizeof(uint64_t);
195 case FLOAT_ID:
return sizeof(float);
196 case CSTRING_ID:
return strlen(surface)+1;
197 case XADDR_ID:
return sizeof(
Xaddr);
198 case UUID_ID:
return sizeof(
UUIDt);
202 return sizeof(
void *);
220 T *structures = _sem_get_defs(sem,s);
222 if (is_sys_structure(s)) {
223 size = _sys_structure_size(s.id,surface);
225 raise_error(
"DON'T HAVE A SIZE FOR STRUCTURE '%s' (%d)",
_sem_get_name(sem,s),s.id);
229 T *structure =
_t_child(structures,s.id);
231 if (semeq(
_t_symbol(parts),STRUCTURE_SEQUENCE)) {
234 if (!semeq(
_t_symbol(p),STRUCTURE_SYMBOL)) {
235 raise_error(
"CAN'T GET SIZE FOR VARIABLE STRUCTURES '%s' (%d)",
_sem_get_name(sem,s),s.id);
240 else if (semeq(
_t_symbol(parts),STRUCTURE_SYMBOL)) {
247 #define MAX_HASHES 10
249 void __d_tsig(
SemTable *sem,
T *code,
T *tsig,TreeHash *hashes) {
253 if (semeq(sym,FILL_FROM_MATCH))
return;
254 if (semeq(sym,SLOT)) {
259 if (c = _t_find(code,SLOT_CHILDREN)) {
263 __d_tsig(sem,sc,tsig,hashes);
270 while(hashes[i] && hashes[i]!=h) {
272 if (i == MAX_HASHES) raise_error(
"whoa! too many slots in code template");
278 c->contents.symbol = EXPECTED_SLOT;
285 DO_KIDS(code,__d_tsig(sem,
_t_child(code,i),tsig,hashes));
304 T *l = _t_newr(def,PROCESS_NAME);
305 _t_new_str(l,ENGLISH_LABEL,name);
306 _t_new(def,PROCESS_INTENTION,intention,strlen(intention)+1);
338 if (signature && code) {
340 TreeHash h[MAX_HASHES]={0,0,0,0,0,0,0,0,0,0};
341 __d_tsig(sem,code,tsig,h);
345 return _d_define(sem,def,SEM_TYPE_PROCESS,c);
359 return _d_define(sem,def,SEM_TYPE_PROTOCOL,c);
362 T *__d_build_def_semtrex(
SemTable *sem,
T *def,
T *stx) {
364 if (semeq(def_sym,STRUCTURE_SYMBOL)) {
366 if (!semeq(NULL_SYMBOL,sym))
369 else if (semeq(def_sym,STRUCTURE_SEQUENCE)) {
372 stx = _t_newr(stx,SEMTREX_SEQUENCE);
374 __d_build_def_semtrex(sem,
_t_child(def,i),stx);
378 else if (semeq(def_sym,STRUCTURE_OR)) {
381 stx = _t_newr(stx,SEMTREX_ZERO_OR_ONE);
382 __d_build_def_semtrex(sem,
_t_child(def,1),stx);
389 T *last = __d_build_def_semtrex(sem,
_t_child(def,c),NULL);
390 for(i=c-1;i>=1;i--) {
392 __d_build_def_semtrex(sem,
_t_child(def,i),or);
396 if (stx)
_t_add(stx,last);
400 else if (semeq(def_sym,STRUCTURE_ANYTHING)) {
401 stx = _t_newr(stx,SEMTREX_ZERO_OR_MORE);
402 stx = _t_newr(stx,SEMTREX_SYMBOL_ANY);
404 __d_build_def_semtrex(sem,
_t_child(def,1),stx);
406 else if (semeq(def_sym,STRUCTURE_ZERO_OR_MORE)) {
407 stx = _t_newr(stx,SEMTREX_ZERO_OR_MORE);
408 __d_build_def_semtrex(sem,
_t_child(def,1),stx);
410 else if (semeq(def_sym,STRUCTURE_ONE_OR_MORE)) {
411 stx = _t_newr(stx,SEMTREX_ONE_OR_MORE);
412 __d_build_def_semtrex(sem,
_t_child(def,1),stx);
415 raise_error(
"translation from %s not implemented\n",
_sem_get_name(sem,def_sym));
434 T *stx =
_sl(parent,s);
437 Structure st = _sem_get_symbol_structure(sem,s);
438 if (!semeq(st,NULL_STRUCTURE)) {
439 T *structure = _sem_get_def(sem,st);
440 T *def =
_t_child(structure,StructureDefDefIdx);
441 __d_build_def_semtrex(sem,def,stx);
460 T *l=_t_newr(def,RECEPTOR_LABEL);
461 _t_new_str(l,ENGLISH_LABEL,label);
480 T *definitions =
_t_child(def,ReceptorDefinitionDefsIdx);
482 bool vmhost_special_case = (c == SYS_CONTEXT) && (!sem->contexts);
486 raise_error(
"recursive receptor definition not yet implemented");
488 Context new_context = _sem_new_context(sem,definitions);
497 if (vmhost_special_case) {
503 return _d_define(sem,def,SEM_TYPE_RECEPTOR,c);
508 T *def = _sem_get_def(sem,r);
519 static char buf[100000];
521 return __t_dump(sem,t,indent,buf);
524 char *_indent_line(
int level,
char *buf) {
526 int j = (-level - 1)*3;
528 while(j--) *buf++ =
' ';
531 else if (level > 0) {
537 #include "ansicolor.h"
538 #define MAX_LEVEL 100
540 char * __t_dump(
SemTable *sem,
T *t,
int level,
char *buf) {
548 buf = _indent_line(level,buf);
550 if (level > MAX_LEVEL) {
551 raise_error(
"whoa, MAX_LEVEL exceeded!");
563 if (t && (t == G_cursor)) {sprintf(buf,KRED);buf += strlen(buf);}
566 sprintf(buf,
"(process:%s",n);
568 else if (is_receptor(s)) {
569 if (t->context.flags & (TFLAG_SURFACE_IS_TREE+TFLAG_SURFACE_IS_RECEPTOR)) {
571 sprintf(buf,
"(%s:{%s}",n,c);
574 raise_error(
"bad node flags for receptor semtype!");
577 else if (semeq(s,NULL_SYMBOL)) {
578 sprintf(buf,
"(NULL_SYMBOL");
581 Structure st = _sem_get_symbol_structure(sem,s);
582 if (!is_sys_structure(st)) {
585 sprintf(buf,
"(%s",n);
591 sprintf(buf,
"(%s:%s",n,(
char *)
_t_surface(t));
594 sprintf(buf,
"(%s:%ld-byte-blob",n,
_t_size(t));
597 sprintf(buf,
"(%s:'%c'",n,*(
char *)
_t_surface(t));
601 sprintf(buf,
"(%s:%d",n,*(
int *)
_t_surface(t));
604 sprintf(buf,
"(%s:%ld",n,*(uint64_t *)
_t_surface(t));
607 sprintf(buf,
"(%s:%f",n,*(
float *)
_t_surface(t));
615 sprintf(buf,
"(%s:%s",n,c?c:
"<unknown>");
622 sprintf(buf,
"(%s:%s.%d",n,
_sem_get_name(sem,x.symbol),x.addr);
634 if (t->context.flags & TFLAG_SURFACE_IS_TREE) {
636 sprintf(buf,
"(%s:{%s}",n,c);
639 raise_error(
"TREE struct without TREE node flags");
640 case RECEPTOR_SURFACE_ID:
641 raise_error(
"boink bad receptor struct");
643 if (t->context.flags & TFLAG_SURFACE_IS_SCAPE) {
649 if (semeq(s,SEMTREX_MATCH_CURSOR)) {
652 sprintf(buf,
"(%s:{%s}",n,c);
656 sprintf(buf,
"(<unknown:%d.%d.%d>",s.context,s.semtype,s.id);
658 sprintf(buf,
"(%s",n);
662 if (t&&(t == G_cursor)) {sprintf(buf+strlen(buf),KNRM);}
664 DO_KIDS(t,__t_dump(sem,
_t_child(t,i),level < 0 ? level-1 : level+1,buf+strlen(buf)));
665 sprintf(buf+strlen(buf),
")");
T * _t_new_root(Symbol symbol)
char * _sem_get_name(SemTable *sem, SemanticID s)
Process _d_define_process(SemTable *sem, T *code, char *name, char *intention, T *signature, T *link, Context c)
T * _t_detach_by_idx(T *t, int i)
header file for symbol and structure definition functions
Semantic tree regular expression header file.
TreeHash _t_hash(SemTable *sem, T *t)
SemanticID _d_define_receptor(SemTable *sem, char *label, T *definitions, Context c)
semantic trees header file
T * _t_child(T *t, int i)
streams abstraction header file
T * _d_build_def_semtrex(SemTable *sem, Symbol s, T *parent)
Structure _d_define_structure(SemTable *sem, char *label, T *structure_def, Context c)
size_t _d_get_symbol_size(SemTable *sem, Symbol s, void *surface)
Structure __d_get_symbol_structure(T *symbols, Symbol s)
size_t _d_get_structure_size(SemTable *sem, Structure s, void *surface)
Symbol _d_define_symbol(SemTable *sem, Structure s, char *label, Context c)
T * _d_make_process_def(T *code, char *name, char *intention, T *signature, T *link)
#define _sl(t, s)
macro to add a single symbol literal to semtrex tree
char * __t2s(SemTable *sem, T *t, int indent)
SemanticID __d_define_receptor(SemTable *sem, T *def, Context c)
Protocol _d_define_protocol(SemTable *sem, T *def, Context c)
Structure _d_define_structure_v(SemTable *sem, char *label, Context c, int num_params,...)
char * _t_sprint_path(int *fp, char *buf)