13 #include "ceptr_error.h"
23 void __t_append_child(
T *t,
T *c) {
24 if (t->structure.child_count == 0) {
25 t->structure.children = malloc(
sizeof(
T *)*TREE_CHILDREN_BLOCK);
26 }
else if (!(t->structure.child_count % TREE_CHILDREN_BLOCK)){
27 int b = t->structure.child_count/TREE_CHILDREN_BLOCK + 1;
28 t->structure.children = realloc(t->structure.children,
sizeof(
T *)*(TREE_CHILDREN_BLOCK*b));
31 t->structure.children[t->structure.child_count++] = c;
34 T * __t_init(
T *parent,
Symbol symbol,
bool is_run_node) {
35 T *t = malloc(is_run_node ?
sizeof(
rT) :
sizeof(
T));
36 t->structure.child_count = 0;
37 t->structure.parent = parent;
38 t->contents.symbol = symbol;
41 ((
rT *)t)->cur_child = RUN_TREE_NOT_EVAULATED;
42 t->context.flags |= TFLAG_RUN_NODE;
45 __t_append_child(parent,t);
60 T *t = __t_init(parent,symbol,is_run_node);
61 if (is_run_node) t->context.flags |= TFLAG_RUN_NODE;
62 if (size && surface) {
64 if (size <=
sizeof(
void *)) {
65 dst = &t->contents.surface;
68 t->context.flags |= TFLAG_ALLOCATED;
69 dst = t->contents.surface = malloc(size);
71 memcpy(dst,surface,size);
73 t->contents.size = size;
86 return __t_new(parent,symbol,&surface,
sizeof(
char),is_run_node);
98 return __t_new(parent,symbol,&surface,
sizeof(
int),is_run_node);
110 return __t_new(parent,symbol,&surface,
sizeof(
long),is_run_node);
134 T *t = __t_init(parent,symbol,
false);
135 *((
T **)&t->contents.surface) = surface;
136 t->contents.size =
sizeof(
T *);
138 t->context.flags |= TFLAG_SURFACE_IS_TREE;
151 return __t_new(parent,symbol,surface,strlen(surface)+1,is_run_node);
161 return _t_new(0,symbol,0,0);
172 return __t_new(parent,symbol,0,0,is_run_node);
179 T *__t_new_special(
T *parent,
Symbol symbol,
void *s,
int flag,
bool is_run_node) {
180 T *t = __t_init(parent,symbol,is_run_node);
181 t->contents.surface = s;
182 t->contents.size =
sizeof(
void *);
184 t->context.flags |= flag;
185 if (is_run_node) t->context.flags |= TFLAG_RUN_NODE;
205 T *t = __t_new_special(parent,symbol,r,TFLAG_SURFACE_IS_TREE+TFLAG_SURFACE_IS_RECEPTOR,0);
223 return __t_new_special(parent,symbol,s,TFLAG_SURFACE_IS_SCAPE,0);
240 return __t_new_special(parent,symbol,s,TFLAG_SURFACE_IS_CPTR+TFLAG_REFERENCE,0);
252 return _t_news(parent,symbol,surface);
263 c->structure.parent = t;
264 __t_append_child(t,c);
296 t->structure.child_count--;
297 if (t->structure.child_count == 0) {
298 free(t->structure.children);
301 t->structure.children[i-1] = t->structure.children[i];
307 if (c) c->structure.parent = 0;
326 t->contents.size = size;
327 if (t->context.flags & TFLAG_ALLOCATED) {
328 free(t->contents.surface);
332 t->contents.surface = malloc(size);
333 memcpy(t->contents.surface,surface,size);
334 t->context.flags = TFLAG_ALLOCATED;
338 memcpy(&t->contents.surface,surface,size);
339 t->context.flags = 0;
342 t->contents.symbol = s;
374 if (!c) {raise_error(
"tree doesn't have child %d",i);}
376 t->structure.children[i-1] = r;
377 r->structure.parent = t;
393 if ((t->context.flags & TFLAG_RUN_NODE) != (r->context.flags & TFLAG_RUN_NODE)) {
394 raise_error(
"runnode mismatch");
397 t->contents = r->contents;
398 t->structure.child_count = r->structure.child_count;
399 t->structure.children = r->structure.children;
400 t->context = r->context;
403 DO_KIDS(t,
_t_child(t,i)->structure.parent = t);
421 if (!c) {raise_error(
"tree doesn't have child %d",i);}
422 t->structure.children[i-1] = r;
423 r->structure.parent = t;
424 c->structure.parent = NULL;
444 raise_error(
"Can't insert into the root!");
454 T **tp = &p->structure.children[l-1];
470 raise_error(
"Can't insert into the root!");
481 raise_error(
"Path must lead to an existing node or one after last child.");
487 void __t_free_children(
T *t) {
488 int c = t->structure.child_count;
491 _t_free(t->structure.children[c]);
493 free(t->structure.children);
495 t->structure.child_count = 0;
499 void __t_free(
T *t) {
500 __t_free_children(t);
501 if (!(t->context.flags & TFLAG_REFERENCE)) {
502 if (t->context.flags & TFLAG_ALLOCATED)
503 free(t->contents.surface);
504 else if (t->context.flags & TFLAG_SURFACE_IS_TREE) {
505 if (t->context.flags & TFLAG_SURFACE_IS_RECEPTOR)
510 else if (t->context.flags & TFLAG_SURFACE_IS_SCAPE)
512 else if (t->context.flags & TFLAG_SURFACE_IS_CPTR) {
513 raise_error(
"WHAAA!");
531 T *__t_clone(
T *t,
T *p) {
533 uint32_t flags = t->context.flags;
537 if (flags & (TFLAG_SURFACE_IS_RECEPTOR+TFLAG_SURFACE_IS_SCAPE+TFLAG_SURFACE_IS_CPTR)) {
539 nt->context.flags |= TFLAG_REFERENCE;
541 else if (flags & TFLAG_SURFACE_IS_TREE) {
548 DO_KIDS(t,__t_clone(
_t_child(t,i),nt));
554 T *__t_rclone(
T *t,
T *p) {
556 uint32_t flags = t->context.flags;
557 if (flags & TFLAG_SURFACE_IS_RECEPTOR) {
558 raise_error(
"can't rclone receptors");
562 else if (flags & (TFLAG_SURFACE_IS_RECEPTOR+TFLAG_SURFACE_IS_SCAPE+TFLAG_SURFACE_IS_CPTR)) {
564 nt->context.flags |= TFLAG_REFERENCE;
566 else if (flags & TFLAG_SURFACE_IS_TREE) {
573 ((
rT *)nt)->cur_child = RUN_TREE_NOT_EVAULATED;
574 DO_KIDS(t,__t_rclone(
_t_child(t,i),nt));
590 return __t_clone(t,0);
594 return __t_rclone(t,0);
598 if (is_process(param)) {
599 *defP = _sem_get_def(sem,param);
600 return PROCESS_SIGNATURE;
606 Structure st = _sem_get_symbol_structure(sem,param);
608 if (semeq(st,NULL_STRUCTURE)) {
612 T *st_def = _sem_get_def(sem,st);
624 enum buildStates {bReadSymbol,bPop,bAddRoot,bReadBase};
637 va_start (ap, parent);
644 int state = bReadSymbol;
651 param = va_arg(ap,
Symbol);
652 if (semeq(param,NULL_SYMBOL)) {debug(D_TREE,
"read NULL_SYMBOL\n");state=bPop;
break;}
654 type = _getBuildType(sem,param,&st,&def);
657 if (semeq(type,PROCESS_SIGNATURE)) {
660 else if (semeq(type,STRUCTURE_SYMBOL)) {
668 else if (semeq(type,NULL_STRUCTURE)) {
673 else if (semeq(type,STRUCTURE_OR)) {
676 else if (semeq(type,STRUCTURE_ZERO_OR_MORE) || semeq(type,STRUCTURE_ANYTHING) || semeq(type,STRUCTURE_SEQUENCE) || semeq(type,STRUCTURE_ONE_OR_MORE)) {
681 raise_error(
"type: %s unimplemented in bReadSymbol",
_sem_get_name(sem,type));
686 debug(D_TREE,
"building sys structure %s\n",
_sem_get_name(sem,st));
687 if (semeq(st,PROCESS) || semeq(st,SYMBOL) || semeq(st,STRUCTURE) || semeq(st,PROTOCOL)) {
690 if (semeq(param,SEMTREX_GROUP)) {
695 else if (semeq(st,INTEGER) || semeq(st,BIT)) {
696 t = _t_newi(t,param,va_arg(ap,
int));
698 else if (semeq(st,CSTRING)) {
699 t = _t_new_str(t,param,va_arg(ap,
char *));
701 else if (semeq(st,CHAR)) {
702 int i = va_arg(ap,
int);
703 t = _t_newc(t,param,i);
705 else if (semeq(st,FLOAT)) {
706 double d = va_arg(ap,
double);
708 t = _t_new(t,param,&f,
sizeof(
float));
710 else if (semeq(st,TREE_PATH)) {
713 while((path[j]=va_arg(ap,
int)) != TREE_PATH_TERMINATOR) {
715 if (j==100) raise_error(
"path too deep!");
717 t = _t_new(t,param,path,
sizeof(
int)*(j+1));
720 raise_error(
"unimplemented surface type:%s",
_sem_get_name(sem,st));
726 if (p == parent) {done =
true;
break;}
729 type = _getBuildType(sem,param,&st,&def);
731 if (semeq(type,STRUCTURE_ZERO_OR_MORE) || semeq(type,STRUCTURE_ANYTHING) || semeq(type,STRUCTURE_SEQUENCE) || semeq(type,STRUCTURE_ONE_OR_MORE)) {
734 else if (semeq(type,PROCESS_SIGNATURE)) {
737 else if (semeq(type,STRUCTURE_OR)) {
740 else if (semeq(type,STRUCTURE_SYMBOL)) {
747 raise_error(
"type: %s unimplemented in bPop",
_sem_get_name(sem,type));
752 t = _t_newr(t,param);
773 va_start (ap, parent);
781 param = va_arg(ap,
Symbol);
782 if (semeq(param,STX_OP)) {
785 if (!semeq(node,NULL_SYMBOL)) type = _getBuildType(sem,node,&st,&def);
786 if (semeq(type,STRUCTURE_SYMBOL) && semeq(*(
Symbol *)
_t_surface(def),NULL_SYMBOL)) {
787 debug(D_TREE,
"building sys structure %s\n",
_sem_get_name(sem,st));
788 if (semeq(st,PROCESS) || semeq(st,SYMBOL) || semeq(st,STRUCTURE) || semeq(st,PROTOCOL)) {
791 else if (semeq(st,INTEGER) || semeq(st,BIT)) {
792 t = _t_newi(t,node,va_arg(ap,
int));
794 else if (semeq(st,CSTRING)) {
795 t = _t_new_str(t,node,va_arg(ap,
char *));
797 else if (semeq(st,CHAR)) {
798 int x = va_arg(ap,
int);
799 t = _t_newc(t,node,x);
801 else if (semeq(st,TREE_PATH)) {
804 while((path[j]=va_arg(ap,
int)) != TREE_PATH_TERMINATOR) {
806 if (j==100) raise_error(
"path too deep!");
808 t = _t_new(t,node,path,
sizeof(
int)*(j+1));
811 raise_error(
"unimplemented surface type:%s",
_sem_get_name(sem,st));
818 else if (semeq(param,STX_CP)) {
820 if (p == parent) {done =
true;
break;}
824 raise_error(
"expecting open or close! got: %s",
_sem_get_name(sem,param));
830 #define test_buffer_overrun if (i == 999) raise_error("buf overrun\n");
832 T *__t_tokenize(
char *s) {
837 if (isspace(c)) {s++;
continue;}
838 if (c ==
'(') _t_newr(t,P_OP);
839 else if (c ==
')') _t_newr(t,P_CP);
840 else if (c ==
':') _t_newr(t,P_COLON);
841 else if (c ==
'%') _t_newr(t,P_INTERPOLATE);
842 else if (c ==
'\'') {
844 if (!c) raise_error(
"expecting char value, got end of string");
846 if (!x) raise_error(
"expecting ', got end of string");
847 if (x !=
'\'') raise_error(
"expecting ' got %c\n",x);
848 _t_newc(t,P_VAL_C,c);
852 while((c=buf[i]=*++s) && c !=
'"' && i<999) i++;
854 if (!c) raise_error(
"no closing quote found");
856 _t_new_str(t,P_VAL_S,buf);
858 else if (isdigit(c) || c ==
'.') {
859 bool is_float =
false;
861 while((c=buf[i]=*s) && (isdigit(c) || (c ==
'.' && !is_float)) && i<999) {
862 if (c==
'.') is_float =
true;
868 _t_new(t,P_VAL_F,&f,
sizeof(
float));
870 else _t_newi(t,P_VAL_I,atoi(buf));
872 if (c==
'.') raise_error(
"unexpected . in number\n");
882 while((c=buf[i]=*s) && isdigit(c) && i<999) {
886 if (i==0 && c!=
')') raise_error(
"expecting a number");
889 path[j++] = atoi(buf);
890 if (j==99) raise_error(
"path too deep in parse");
893 path[j++]=TREE_PATH_TERMINATOR;
894 _t_new(t,P_VAL_PATH,path,
sizeof(
int)*j);
899 while((c=buf[i]=*s) && (isalnum(c) || c ==
'_') && i<999) {i++; s++;}
902 _t_new_str(t,P_LABEL,buf);
920 T *tokens = __t_tokenize(s);
926 while ((tok =
_t_child(tokens,idx++))) {
927 if (semeq(P_INTERPOLATE,
_t_symbol(tok))) {
933 if (!tok) raise_error(
"unexpected end of tokens!");
935 raise_error(
"expecting symbol LABEL");
938 if (!_sem_get_by_label(sem,label,&node)) {
939 raise_error(
"unknown semantic id:%s",label);
944 Symbol type = NULL_SYMBOL;
946 if (!semeq(node,NULL_SYMBOL)) type = _getBuildType(sem,node,&st,&def);
949 if (semeq(type,STRUCTURE_SYMBOL) && semeq(*(
Symbol *)
_t_surface(def),NULL_SYMBOL)) {
951 if (!tok) raise_error(
"unexpected end of tokens! expecting P_COLON");
953 raise_error(
"expecting P_COLON got %s",_t2s(sem,tok));
955 if (!tok) raise_error(
"unexpected end of tokens! expecting a value token");
957 if (!(semeq(v,P_VAL_S)||semeq(v,P_VAL_C)||semeq(v,P_VAL_I)||semeq(v,P_VAL_F)||semeq(v,P_VAL_PATH)||semeq(v,P_LABEL) )) raise_error(
"expecting value symbol got: %s",_t2s(sem,tok));
959 if (semeq(st,PROCESS) || semeq(st,SYMBOL) || semeq(st,STRUCTURE) || semeq(st,PROTOCOL)) {
960 if (!semeq(v,P_LABEL)) raise_error(
"expecting a label for the value of a SemanticID");
962 if (!_sem_get_by_label(sem,label,&v)) {
963 raise_error(
"unknown semantic id:%s",label);
965 t = _t_news(t,node,v);
967 else if (semeq(st,INTEGER) || semeq(st,BIT)) {
968 if (!semeq(v,P_VAL_I)) raise_error(
"expecting a P_VAL_I got %s",_t2s(sem,tok));
971 else if (semeq(st,FLOAT)) {
972 if (!semeq(v,P_VAL_F)) raise_error(
"expecting a P_VAL_F got %s",_t2s(sem,tok));
973 t = _t_new(t,node,(
float *)
_t_surface(tok),
sizeof(
float));
975 else if (semeq(st,CSTRING)) {
976 if (!semeq(v,P_VAL_S)) raise_error(
"expecting a P_VAL_S got %s",_t2s(sem,tok));
977 t = _t_new_str(t,node,(
char *)
_t_surface(tok));
979 else if (semeq(st,CHAR)) {
980 if (!semeq(v,P_VAL_C)) raise_error(
"expecting a P_VAL_C got %s",_t2s(sem,tok));
982 t = _t_newc(t,node,x);
984 else if (semeq(st,TREE_PATH)) {
985 if (!semeq(v,P_VAL_PATH)) raise_error(
"expecting a P_VAL_PATH got %s",_t2s(sem,tok));
989 raise_error(
"unimplemented surface type:%s",
_sem_get_name(sem,st));
998 if (p == parent) {
break;}
1002 raise_error(
"expecting open or close paren! got: %s\n",_t2s(sem,tok));
1005 if (idx <
_t_children(tokens)) raise_error(
"found ending close paren but some tokens still remain: %s",_t2s(sem,tokens));
1014 T *__t_find_actual(
T *sem_map,
Symbol actual_kind,
T *replacement_kind) {
1017 T *l =
_sl(stx,SEMANTIC_LINK);
1018 T *seq = _t_newr(l,SEMTREX_SEQUENCE);
1019 T *x = _t_newr(seq,SEMTREX_VALUE_LITERAL);
1022 x =
_sl(seq,REPLACEMENT_VALUE);
1023 T *g = _t_news(x,SEMTREX_GROUP,actual_kind);
1024 x =
_sl(g,actual_kind);
1026 debug(D_TREE,
" trying to find a %s in sem_map\n",t2s(replacement_kind));
1028 result = _stx_get_matched_node(actual_kind,mr,sem_map,NULL);
1029 debug(D_TREE,
" re-mapping %s ->",t2s(replacement_kind));
1030 debug(D_TREE,
"%s\n",t2s(result));
1032 }
else {debug(D_TREE,
" failed!\n");}
1050 if (!
template)
return false;
1051 debug(D_TREE,
"filling template:\n%s\n",
__t2s(G_sem,
template,INDENT));
1052 debug(D_TREE,
"from sem_map:\n%s\n\n",
__t2s(G_sem,sem_map,INDENT));
1054 bool is_run_node = (
template->context.flags |= TFLAG_RUN_NODE) || as_run_node;
1056 T *t =
_t_child(
template,SlotSemanticRefIdx);
1059 T *v =
_t_child(
template,SlotValueOfIdx);
1064 if (semeq(vsym,SLOT_IS_VALUE_OF))
1066 else if (semeq(vsym,SLOT_CHILDREN)) {
1073 raise_error(
"expecting SLOT_IS_VALUE_OF or SLOT_CHILDREN, got %s",
_sem_get_name(G_sem,vsym));
1076 debug(D_TREE,
"replacing %s\n",t2s(
template));
1082 T *ref =
_t_child(m,SemanticMapSemanticRefIdx);
1086 debug(D_TREE,
" yes!)\n");
1087 debug(D_TREE,
"with %s\n",t2s(m));
1090 if (semeq(sym,GOAL) && !v) {
1092 if (!is_process(p)) {
1093 if (semeq(ACTUAL_PROCESS,p)) {
1096 else if (semeq(GOAL,p)) {
1099 T *x = __t_find_actual(sem_map,ACTUAL_PROCESS,replacement_value);
1101 raise_error(
"unable to find actual for %s",t2s(m));
1105 raise_error(
"expecting GOAL or ACTUAL_PROCESS for replacement values. got: %s",t2s(m));
1108 r =
__t_new(0,p,0,0,is_run_node);
1110 ((
rT *)r)->cur_child = RUN_TREE_NOT_EVAULATED;
1114 debug(D_TREE,
"replacement value: %s\n",t2s(replacement_value));
1116 if (semeq(rsid,ROLE)) {
1117 T *x = __t_find_actual(sem_map,ACTUAL_RECEPTOR,replacement_value);
1118 if (x) replacement_value = x;
1120 else if (semeq(rsid,USAGE)) {
1121 T *x = __t_find_actual(sem_map,ACTUAL_SYMBOL,replacement_value);
1122 if (x) replacement_value = x;
1129 r = _t_rclone(replacement_value);
1132 r->contents.symbol = valof;
1141 if (semeq(rsid,ACTUAL_SYMBOL)) {
1143 T *ac = _t_news(0,ACTUAL_SYMBOL,acsym);
1144 T *x = __t_find_actual(sem_map,ACTUAL_VALUE,ac);
1149 else replacement_value = temp =
_t_new_root(acsym);
1151 else if (semeq(rsid,ACTUAL_VALUE)) {
1152 replacement_value =
_t_child(replacement_value,1);
1154 if (semeq(NULL_SYMBOL,
_t_symbol(replacement_value))) {
1155 replacement_value = NULL;
1157 if (replacement_value) {
1159 r = _t_rclone(replacement_value);
1176 if (!p) raise_error(
"not expecting a root node!");
1183 else { debug(D_TREE,
" nope)\n");}
1191 if (_t_fill_template(t,sem_map)) i--;
1194 debug(D_TREE,
"results in:\n%s\n\n",
template ?
__t2s(G_sem,
template,INDENT) :
"<nothing>");
1195 return template == NULL;
1206 return t->structure.child_count;
1216 if (t->context.flags & (TFLAG_ALLOCATED|TFLAG_SURFACE_IS_TREE|TFLAG_SURFACE_IS_SCAPE|TFLAG_SURFACE_IS_CPTR))
1217 return t->contents.surface;
1219 return &t->contents.surface;
1229 return t->contents.symbol;
1239 return t->contents.size;
1252 if (i>t->structure.child_count || i < 1)
return 0;
1253 return t->structure.children[i-1];
1263 return t->structure.parent;
1291 if (p->structure.children[i] == t) {
1313 if (p->structure.children[i] == t) {
1315 return i<c ? p->structure.children[i] : 0;
1332 for(i = start_child;i<=c;i++) {
1351 while(*p1 != TREE_PATH_TERMINATOR && *p2 != TREE_PATH_TERMINATOR)
1352 if (*(p1++) != *(p2++))
return 0;
1353 return *p1 == TREE_PATH_TERMINATOR && *p2 == TREE_PATH_TERMINATOR;
1367 while(*p++ != TREE_PATH_TERMINATOR) i++;
1385 if (!t)
return NULL;
1388 int s =
sizeof(int)*20;
1397 s*=2;p=realloc(p,s);}
1411 p[l]= TREE_PATH_TERMINATOR;
1425 while(*src_p != TREE_PATH_TERMINATOR) {
1426 *dst_p++ = *src_p++;
1428 *dst_p = TREE_PATH_TERMINATOR;
1444 if (i == TREE_PATH_TERMINATOR)
1447 if (!(t->context.flags & TFLAG_SURFACE_IS_TREE)) {
1448 raise_error(
"surface is not a tree!");
1454 if (c == NULL )
return NULL;
1455 if (*p == TREE_PATH_TERMINATOR)
return c;
1475 while((p[i] = va_arg(ap,
int)) != TREE_PATH_TERMINATOR) {
1476 if (i++ == 100) raise_error(
"tree path to deep");
1494 if (c == NULL)
return NULL;
1514 sprintf(b,
"/%d",fp[i]);
1546 if (*pathP == NULL) {
1555 *lenP =
sizeof(int)*(d+1);
1556 *pathP = p = malloc(*lenP);
1557 for(i=0;i<d;i++) {p[i]=1;}
1558 p[d]=TREE_PATH_TERMINATOR;
1568 if (d == 0)
return NULL;
1573 int cur_idx = p[d-1];
1585 int new_depth_size = (i+d+1)*
sizeof(
int);
1586 if ( new_depth_size > *lenP) {
1587 *pathP = p = realloc(*pathP,*lenP=new_depth_size);
1589 while(i--) p[d++]=1;
1590 p[d]=TREE_PATH_TERMINATOR;
1595 p[d-1] = TREE_PATH_TERMINATOR;
1618 struct {
Symbol s;TreeHash h;} h;
1624 h.h = hashfn((
char *)surface,l);
1627 result = hashfn((
char *)&h,
sizeof(h));
1630 size_t l =
sizeof(TreeHash)*c+
sizeof(
Symbol);
1631 TreeHash *h,*hashes = malloc(l);
1637 result = hashfn((
char *)hashes,l);
1655 UUIDt __uuid_gen() {
1658 clock_gettime(CLOCK_MONOTONIC, &c);
1659 u.time = ((c.tv_sec * (1000000)) + (c.tv_nsec / 1000));
1665 return memcmp(u1,u2,
sizeof(
UUIDt))==0;
1670 #define SWRITE(type,value) type * type##P = (type *)(*bufferP +offset); *type##P=value;offset += sizeof(type);
1691 while ((offset+l+
sizeof(
Symbol)) > current_size) {
1693 *bufferP = realloc(*bufferP,current_size);
1724 size_t buf_size = 1000;
1725 *surfaceP = malloc(buf_size);
1727 *surfaceP = realloc(*surfaceP,*lengthP);
1731 #define SREAD(type,var_name) type var_name = *(type *)*surfaceP;*lengthP -= sizeof(type);*surfaceP += sizeof(type);
1732 #define _SREAD(type,var_name) var_name = *(type *)*surfaceP;*lengthP -= sizeof(type);*surfaceP += sizeof(type);
1735 T * _t_unserialize(
SemTable *sem,
void **surfaceP,
size_t *lengthP,
T *t) {
1743 Structure st = _sem_get_symbol_structure(sem,s);
1745 if (is_sys_structure(st)) {
1746 size = _sys_structure_size(st.id,*surfaceP);
1747 if (size == -1) {raise_error(
"BANG!");}
1752 if (semeq(st,INTEGER))
1753 t = _t_newi(t,s,*(
int *)*surfaceP);
1755 t = _t_new(t,s,*surfaceP,size);
1765 _t_unserialize(sem,surfaceP,lengthP,t);
1771 #define _add_char2buf(c,buf) *buf=c;buf++;*buf=0
1773 #define _add_sem(buf,s) sprintf(buf,"{ \"ctx\":%d,\"type\":%d,\"id\":%d }",s.context,s.semtype,s.id);
1794 buf = _indent_line(level,buf);
1796 sprintf(buf,
"{\"sem\":");
1800 if (is_symbol(s) && !semeq(s,NULL_SYMBOL)) {
1801 Structure st = _sem_get_symbol_structure(sem,s);
1803 if (is_sys_structure(st)) {
1808 sprintf(buf,
",\"surface\":\"\%s\"",(
char *)
_t_surface(t));
1813 sprintf(buf,
",\"surface\":\"\\\"\"");
1815 sprintf(buf,
",\"surface\":\"%c\"",cr);
1819 sprintf(buf,
",\"surface\":%s",(*(
int *)
_t_surface(t)) ?
"1" :
"0");
1822 raise_error(
"not implemented");
1825 sprintf(buf,
",\"surface\":%d",*(
int *)
_t_surface(t));
1828 sprintf(buf,
",\"surface\":%ld",*(uint64_t *)
_t_surface(t));
1831 sprintf(buf,
",\"surface\":%f",*(
float *)
_t_surface(t));
1840 sprintf(buf,
",\"surface\":");
1850 sprintf(buf,
",\"surface\":{ \"symbol\":\"%s\",\"addr\":%d }",
_sem_get_name(sem,x.symbol),x.addr);
1853 sprintf(buf,
",\"surface\":\"%p\"",
_t_surface(t));
1856 if (t->context.flags & TFLAG_SURFACE_IS_TREE) {
1858 sprintf(buf,
",\"surface\":%s",c);
1861 case RECEPTOR_SURFACE_ID:
1862 if (t->context.flags & (TFLAG_SURFACE_IS_TREE+TFLAG_SURFACE_IS_RECEPTOR)) {
1864 sprintf(buf,
",\"surface\":%s",c);
1868 if (t->context.flags & TFLAG_SURFACE_IS_SCAPE) {
1871 raise_error(
"not-implemented");
1877 if (st.id >
_t_children(__sem_get_defs(sem,SEM_TYPE_STRUCTURE,st.context)))
1878 raise_error(
"don't know how to convert surface of %s, structure id %d seems invalid",
_sem_get_name(sem,s),st.id);
1886 sprintf(buf,
",\"children\":[");
1892 _add_char2buf(
',',buf);
1895 _add_char2buf(
']',buf);
1897 _add_char2buf(
'}',buf);
1921 buf = _indent_line(level,buf);
1923 if (is_process(s)) {
1924 sprintf(buf,
"{ \"type\":\"process\",\"name\" :\"%s\"",
_sem_get_name(sem,s));
1928 Structure st = _sem_get_symbol_structure(sem,s);
1929 sprintf(buf,
"{ \"symbol\":{ \"context\":%d,\"id\":%d },",s.context,s.id);
1932 if (!is_sys_structure(st)) {
1935 sprintf(buf,
"\"type\":\"%s\",\"name\":\"%s\"",
_sem_get_name(sem,st),n);
1943 sprintf(buf,
"\"type\":\"CSTRING\",\"name\":\"%s\",\"surface\":\"\%s\"",n,(
char *)
_t_surface(t));
1948 sprintf(buf,
"\"type\":\"CHAR\",\"name\":\"%s\",\"surface\":\"\\\"\"",n);
1950 sprintf(buf,
"\"type\":\"CHAR\",\"name\":\"%s\",\"surface\":\"%c\"",n,cr);
1954 sprintf(buf,
"\"type\":\"BIT\",\"name\":\"%s\",\"surface\":%s",n,(*(
int *)
_t_surface(t)) ?
"1" :
"0");
1957 raise_error(
"not implemented");
1960 sprintf(buf,
"\"type\":\"INTEGER\",\"name\":\"%s\",\"surface\":%d",n,*(
int *)
_t_surface(t));
1963 sprintf(buf,
"\"type\":\"INTEGER64\",\"name\":\"%s\",\"surface\":%ld",n,*(uint64_t*)
_t_surface(t));
1966 sprintf(buf,
"\"type\":\"FLOAT\",\"name\":\"%s\",\"surface\":%f",n,*(
float *)
_t_surface(t));
1970 sprintf(buf,
"\"type\":\"SYMBOL\",\"name\":\"%s\",\"surface\":\"%s\"",n,c?c:
"<unknown>");
1974 sprintf(buf,
"\"type\":\"STRUCTURE\",\"name\":\"%s\",\"surface\":\"%s\"",n,c?c:
"<unknown>");
1978 sprintf(buf,
"\"type\":\"PROCESS\",\"name\":\"%s\",\"surface\":\"%s\"",n,c?c:
"<unknown>");
1982 sprintf(buf,
"\"type\":\"PROTOCOL\",\"name\":\"%s\",\"surface\":\"%s\"",n,c?c:
"<unknown>");
1986 sprintf(buf,
"\"type\":\"RECEPTOR\",\"name\":\"%s\",\"surface\":\"%s\"",n,c?c:
"<unknown>");
1993 sprintf(buf,
"\"type\":\"XADDR\",\"name\":\"%s\",\"surface\":{ \"symbol\":\"%s\",\"addr\":%d }",n,
_sem_get_name(sem,x.symbol),x.addr);
1996 sprintf(buf,
"\"type\":\"CPOINTER\",\"name\":\"%s\",\"surface\":\"%p\"",n,
_t_surface(t));
1999 if (t->context.flags & TFLAG_SURFACE_IS_TREE) {
2001 sprintf(buf,
"\"type\":\"TREE\",\"name\":\"%s\",\"surface\":%s",n,c);
2004 case RECEPTOR_SURFACE_ID:
2005 if (t->context.flags & (TFLAG_SURFACE_IS_TREE+TFLAG_SURFACE_IS_RECEPTOR)) {
2007 sprintf(buf,
"\"type\":\"RECEPTOR\",\"name\":\"%s\",\"surface\":%s",n,c);
2011 if (t->context.flags & TFLAG_SURFACE_IS_SCAPE) {
2018 if (semeq(s,SEMTREX_MATCH_CURSOR)) {
2021 sprintf(buf,
"(%s:{%s}",n,c);
2025 sprintf(buf,
"(<unknown:%d.%d.%d>",s.context,s.semtype,s.id);
2028 sprintf(buf,
"\"type\":\"%s\",\"name\":\"%s\"",c,n);
2036 sprintf(buf,
",\"children\":[");
2042 _add_char2buf(
',',buf);
2045 _add_char2buf(
']',buf);
2047 _add_char2buf(
'}',buf);
2052 int __t_writeln(
T *t,
Stream *stream) {
2069 if (semeq(sym,LINE)) {
2070 err = __t_writeln(t,stream);
2072 else if (semeq(sym,LINES)) {
2074 err = __t_writeln(
_t_child(t,i),stream);
2075 if (err == 0)
return 0;
2080 Structure struc = _sem_get_symbol_structure(sem,sym);
2081 if (semeq(struc,CSTRING)) {
2087 err =
_st_write(stream,str,strlen(str));
T * _t_new_root(Symbol symbol)
char * _sem_get_name(SemTable *sem, SemanticID s)
T * _t_next_sibling(T *t)
T * _t_new_scape(T *parent, Symbol symbol, Scape *s)
int _st_write(Stream *st, char *buf, size_t len)
void _t_morph(T *dst, T *src)
T * _t_build2(SemTable *sem, T *parent,...)
T * __t_newi(T *parent, Symbol symbol, int surface, bool is_run_node)
int _t_write(SemTable *sem, T *t, Stream *stream)
T * _t_path_walk(T *t, int **pathP, int *lenP)
bool __t_fill_template(T *template, T *sem_map, bool as_run_node)
int _t_hash_equal(TreeHash h1, TreeHash h2)
T * _t_detach_by_idx(T *t, int i)
header file for symbol and structure definition functions
int _t_path_depth(int *p)
Semantic tree regular expression header file.
T * _t_new_receptor(T *parent, Symbol symbol, Receptor *r)
TreeHash _t_hash(SemTable *sem, T *t)
void _r_free(Receptor *r)
semantic trees header file
T * __t_newr(T *parent, Symbol symbol, bool is_run_node)
void _t_serialize(SemTable *sem, T *t, void **surfaceP, size_t *lengthP)
T * __t_new(T *parent, Symbol symbol, void *surface, size_t size, bool is_run_node)
T * _t_child(T *t, int i)
T * _t_newp(T *parent, Symbol symbol, Process surface)
T * _t_swap(T *t, int i, T *r)
void _t_pathcpy(int *dst_p, int *src_p)
void _t_insert_at(T *t, int *path, T *i)
size_t __t_serialize(SemTable *sem, T *t, void **bufferP, size_t offset, size_t current_size, int compact)
receptor implementation header file
size_t _d_get_symbol_size(SemTable *sem, Symbol s, void *surface)
T * __t_new_str(T *parent, Symbol symbol, char *surface, bool is_run_node)
T * _t_newt(T *parent, Symbol symbol, T *surface)
void _t_replace_node(T *t, T *r)
SState * state(StateType type, int *statesP, int level)
char * _t2rawjson(SemTable *sem, T *t, int level, char *buf)
#define SREAD(type, var_name)
macro to read typed date from the surface and update length and surface values
#define SWRITE(type, value)
macro to write data by type into *bufferP and increment offset by the size of the type ...
int _t_matchr(T *semtrex, T *t, T **rP)
void * _t_get_surface(T *t, int *p)
T * __t_find(T *t, SemanticID sym, int start_child)
#define _sl(t, s)
macro to add a single symbol literal to semtrex tree
char * __t2s(SemTable *sem, T *t, int indent)
T * __t_newi64(T *parent, Symbol symbol, long surface, bool is_run_node)
T * _t_parse(SemTable *sem, T *parent, char *s,...)
int _t_path_equal(int *p1, int *p2)
char * _t2json(SemTable *sem, T *t, int level, char *buf)
void __t_morph(T *t, Symbol s, void *surface, size_t size, int allocate)
void _t_replace(T *t, int i, T *r)
void _t_detach_by_ptr(T *t, T *c)
T * __t_news(T *parent, Symbol symbol, SemanticID surface, bool is_run_node)
int _st_writeln(Stream *stream, char *str)
T * _t_build(SemTable *sem, T *parent,...)
T * __t_newc(T *parent, Symbol symbol, char surface, bool is_run_node)
T * _t_new_cptr(T *parent, Symbol symbol, void *s)
char * _t_sprint_path(int *fp, char *buf)