Skip to content

Commit

Permalink
wip #8 fixing _d_build_def_semtrex to handle structure optionality
Browse files Browse the repository at this point in the history
  • Loading branch information
zippy committed May 27, 2016
1 parent 478904b commit 5398691
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
8 changes: 6 additions & 2 deletions spec/def_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ void testDefProcessTemplate() {
//! [testDefProcessTemplate]
}



void testDefSemtrex() {
//! [testDefSemtrex]
Receptor *r = _r_new(G_sem,TEST_RECEPTOR);
Expand All @@ -180,6 +178,12 @@ void testDefSemtrex() {
__t_dump(r->sem,stx,0,buf);
spec_is_str_equal(buf,"(SEMTREX_SYMBOL_LITERAL (SEMTREX_SYMBOL:house_loc) (SEMTREX_SEQUENCE (SEMTREX_SYMBOL_LITERAL (SEMTREX_SYMBOL:lat)) (SEMTREX_SYMBOL_LITERAL (SEMTREX_SYMBOL:lon))))");

T *t = _t_parse(G_sem,0,"(house_loc (lat:22.2) (lon:43.9))");
spec_is_true(_t_match(stx,t));
_t_free(t);
t = _t_parse(G_sem,0,"(house_loc (lat:22.2) (lat:43.9))");
spec_is_false(_t_match(stx,t));
_t_free(t);
_t_free(stx);
_r_free(r);
//! [testDefSemtrex]
Expand Down
59 changes: 49 additions & 10 deletions src/def.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,52 @@ Protocol _d_define_protocol(SemTable *sem,T *def,Context c) {
return _d_define(sem,def,SEM_TYPE_PROTOCOL,c);
}

T *__d_build_def_semtrex(SemTable *sem,T *def,T *stx) {
Symbol def_sym = _t_symbol(def);
if (semeq(def_sym,STRUCTURE_SYMBOL)) {
stx = _d_build_def_semtrex(sem,*(Symbol *)_t_surface(def),stx);
}
else if (semeq(def_sym,STRUCTURE_SEQUENCE)) {
int i,c = _t_children(def);
if (c > 0) {
stx = _t_newr(stx,SEMTREX_SEQUENCE);
for(i=1;i<=c;i++) {
__d_build_def_semtrex(sem,_t_child(def,i),stx);
}
}
}
else if (semeq(def_sym,STRUCTURE_OR)) {
int i,c = _t_children(def);
if (c == 1) {
stx = _t_newr(stx,SEMTREX_ZERO_OR_ONE);
__d_build_def_semtrex(sem,_t_child(def,1),stx);
}
else if (c > 1) {
// semtrex OR is binary whereas structure def or is a set,
// so we have to build up a binary try OR structure
// so, we build it up from the bottom first, building from the
// last child backwards.
T *last = __d_build_def_semtrex(sem,_t_child(def,c),NULL);
for(i=c-1;i>=1;i--) {
T *or = _t_new_root(SEMTREX_OR);
__d_build_def_semtrex(sem,_t_child(def,c),or);
_t_add(or,last);
last = or;
}
if (stx) _t_add(stx,last);
stx = last;
}
}
else if (semeq(def_sym,STRUCTURE_ZERO_OR_MORE)) {
stx = _t_newr(stx,SEMTREX_ZERO_OR_MORE);
__d_build_def_semtrex(sem,_t_child(def,1),stx);
}
else {
raise_error("translation from %s not implemented\n",_sem_get_name(sem,def_sym));
}
return stx;
}

/**
* Walks the definition of a symbol to build a semtrex that would match that definiton
*
Expand All @@ -377,16 +423,9 @@ T * _d_build_def_semtrex(SemTable *sem,Symbol s,T *parent) {

Structure st = _sem_get_symbol_structure(sem,s);
if (!(is_sys_structure(st))) {
T *structure = _d_get_structure_def(_sem_get_defs(sem,st),st);
T *parts = _t_child(structure,2);
int i,c = _t_children(parts);
if (c > 0) {
T *seq = _t_newr(stx,SEMTREX_SEQUENCE);
for(i=1;i<=c;i++) {
T *p = _t_child(parts,i);
_d_build_def_semtrex(sem,*(Symbol *)_t_surface(p),seq);
}
}
T *structure = _sem_get_def(sem,st);
T *def = _t_child(structure,StructureDefDefIdx);
__d_build_def_semtrex(sem,def,stx);
}
return stx;
}
Expand Down
2 changes: 1 addition & 1 deletion src/def.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// @todo figure out a better way to handle this... like put defs like these
// into the symbol gen code (talk about a semantic muddle! If only I had ceptr...)
// Note, that I tried that and it's hard!
enum {StructureDefDefIdx=1};
enum {ReceptorInstanceInstOfIdx=1,ReceptorInstanceContextNumIdx,ReceptorInstanceParentContextIdx,ReceptorInstanceStateIdx};
enum {ReceptorDefinitionLabelIdx=1,ReceptorDefinitionDefsIdx};
enum {ReceptorFluxIdx=1,ReceptorPendingSignalsIdx,ReceptorPendingResponsesIdx,ReceptorConversationsIdx,ReceptorElapsedTimeIdx};
Expand All @@ -40,6 +39,7 @@ enum {ExpectRoleIdx=1,ExpectSourceIdx,ExpectPatternIdx,ExpectActionIdx,ExpectPar
enum {InitiateRoleIdx=1,InitiateDestinationIdx,InitiateActionIdx};
enum {SourceRoleIdx=1};
enum {DefLabelIdx=1,SymbolDefStructureIdx};
enum {StructureDefLabelIdx=1,StructureDefDefIdx};
enum {ProcessDefNameIdx=1,ProcessDefIntentionIdx,ProcessDefCodeIdx,ProcessDefSignatureIdx,ProcessDefLinkIdx};
enum {SignatureOutputSigIdx=1};
enum {InputSigLabelIdx=1,InputSigSemVariantsIdx,InputSigOptionalIdx};
Expand Down
4 changes: 2 additions & 2 deletions src/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ int _p_transcode(SemTable *sem, T* src,Symbol to_sym, Structure to_s,T **result)
Symbol tdef_sym = _t_symbol(tdef);

T *k;
Symbol k_sym = _t_symbol(k = _t_child(tdef,StructureDefDefIdx));
Symbol k_sym = _t_symbol(k = _t_child(tdef,1));
bool tsym_isa_simple_list = (semeq(tdef_sym,STRUCTURE_ZERO_OR_MORE)||
semeq(tdef_sym,STRUCTURE_ZERO_OR_ONE)||
semeq(tdef_sym,STRUCTURE_ONE_OR_MORE)) &&
Expand All @@ -223,7 +223,7 @@ int _p_transcode(SemTable *sem, T* src,Symbol to_sym, Structure to_s,T **result)
if (tsym_isa_simple_list) {
Symbol to_list_of_sym = *(Symbol *)_t_surface(k);
if (semeq(tdef_sym,sdef_sym) &&
semeq(_t_symbol( _t_child(sdef,StructureDefDefIdx)),STRUCTURE_SYMBOL)) {
semeq(_t_symbol( _t_child(sdef,1)),STRUCTURE_SYMBOL)) {
debug(D_TRANSCODE,"transcoding elements of simple list\n");
x = __t_newr(0,to_sym,true);
T *m,*r;
Expand Down

0 comments on commit 5398691

Please sign in to comment.