Adapting subtermUnify to have state.

This commit is contained in:
Cas Cremers 2018-12-24 14:29:02 +01:00
parent 499b4f8987
commit 602d9dc32e
3 changed files with 18 additions and 11 deletions

View File

@ -1012,7 +1012,7 @@ bind_existing_to_goal (const Binding b, const int run, const int index,
{ {
Term bigterm; Term bigterm;
int unifiesWithKeys (Termlist substlist, Termlist keylist) int unifiesWithKeys (Termlist substlist, Termlist keylist, void *state)
{ {
int old_length; int old_length;
int newgoals; int newgoals;
@ -1086,7 +1086,7 @@ bind_existing_to_goal (const Binding b, const int run, const int index,
} }
bigterm = roledef_shift (sys->runs[run].start, index)->message; bigterm = roledef_shift (sys->runs[run].start, index)->message;
subtermUnify (bigterm, b->term, NULL, NULL, unifiesWithKeys); subtermUnify (bigterm, b->term, NULL, NULL, unifiesWithKeys, NULL);
} }
@ -1451,7 +1451,7 @@ debug_send_candidate (const Protocol p, const Role r, const Roledef rd,
//! Dummy helper function for iterator; abort if sub-unification found //! Dummy helper function for iterator; abort if sub-unification found
int int
test_sub_unification (Termlist substlist, Termlist keylist) test_sub_unification (Termlist substlist, Termlist keylist, void *state)
{ {
// A unification exists; return the signal // A unification exists; return the signal
return false; return false;
@ -1555,7 +1555,8 @@ bind_this_role_send (Protocol p, Role r, Roledef rd, int index,
debug_send_candidate (p, r, rd, index); debug_send_candidate (p, r, rd, index);
if (!subtermUnify if (!subtermUnify
(rd->message, (bs->binding)->term, NULL, NULL, test_sub_unification)) (rd->message, (bs->binding)->term, NULL, NULL, test_sub_unification,
NULL))
{ {
// A good candidate // A good candidate
bs->found++; bs->found++;

View File

@ -292,7 +292,8 @@ unify (Term t1, Term t2, Termlist tl, int (*callback) (), void *state)
*/ */
struct su_kcb_state struct su_kcb_state
{ {
int (*callback) (Termlist, Termlist); void *oldstate;
int (*callback) (Termlist, Termlist, void *);
Termlist keylist; Termlist keylist;
}; };
@ -300,7 +301,8 @@ int
keycallback (Termlist tl, struct su_kcb_state *ptr_kcb_state) keycallback (Termlist tl, struct su_kcb_state *ptr_kcb_state)
{ {
assert (ptr_kcb_state != NULL); assert (ptr_kcb_state != NULL);
return ptr_kcb_state->callback (tl, ptr_kcb_state->keylist); return ptr_kcb_state->callback (tl, ptr_kcb_state->keylist,
ptr_kcb_state->oldstate);
} }
//! Subterm unification //! Subterm unification
@ -320,11 +322,12 @@ keycallback (Termlist tl, struct su_kcb_state *ptr_kcb_state)
*/ */
int int
subtermUnify (Term tbig, Term tsmall, Termlist tl, Termlist keylist, subtermUnify (Term tbig, Term tsmall, Termlist tl, Termlist keylist,
int (*callback) (Termlist, Termlist)) int (*callback) (Termlist, Termlist, void *), void *state)
{ {
int proceed; int proceed;
struct su_kcb_state kcb_state; struct su_kcb_state kcb_state;
kcb_state.oldstate = state;
kcb_state.callback = callback; kcb_state.callback = callback;
kcb_state.keylist = keylist; kcb_state.keylist = keylist;
@ -346,9 +349,11 @@ subtermUnify (Term tbig, Term tsmall, Termlist tl, Termlist keylist,
if (realTermTuple (tbig)) if (realTermTuple (tbig))
{ {
proceed = proceed proceed = proceed
&& subtermUnify (TermOp1 (tbig), tsmall, tl, keylist, callback); && subtermUnify (TermOp1 (tbig), tsmall, tl, keylist, callback,
state);
proceed = proceed proceed = proceed
&& subtermUnify (TermOp2 (tbig), tsmall, tl, keylist, callback); && subtermUnify (TermOp2 (tbig), tsmall, tl, keylist, callback,
state);
} }
// 3. unification with encryption needed // 3. unification with encryption needed
@ -357,7 +362,8 @@ subtermUnify (Term tbig, Term tsmall, Termlist tl, Termlist keylist,
// extend the keylist // extend the keylist
keylist = termlistAdd (keylist, tbig); keylist = termlistAdd (keylist, tbig);
proceed = proceed proceed = proceed
&& subtermUnify (TermOp (tbig), tsmall, tl, keylist, callback); && subtermUnify (TermOp (tbig), tsmall, tl, keylist, callback,
state);
// remove last item again // remove last item again
keylist = termlistDelTerm (keylist); keylist = termlistDelTerm (keylist);
} }

View File

@ -39,6 +39,6 @@ int checkRoletermMatch (const Term t1, const Term t2, const Termlist tl);
int unify (Term t1, Term t2, Termlist tl, int (*callback) (), void *state); int unify (Term t1, Term t2, Termlist tl, int (*callback) (), void *state);
int int
subtermUnify (Term tbig, Term tsmall, Termlist tl, Termlist keylist, subtermUnify (Term tbig, Term tsmall, Termlist tl, Termlist keylist,
int (*callback) (Termlist, Termlist)); int (*callback) (Termlist, Termlist, void *), void *state);
#endif #endif