improve SWI interface compatibility

This commit is contained in:
Vitor Santos Costa 2010-01-26 12:21:06 +00:00
parent b112574dc5
commit 110f1c7977
3 changed files with 43 additions and 22 deletions

View File

@ -385,7 +385,7 @@ extern X_API int PL_unify_chars(term_t, int, size_t, const char *);
extern X_API int PL_unify_chars_diff(term_t, term_t, int, size_t, const char *);
extern X_API int PL_unify_wchars(term_t, int, size_t, const pl_wchar_t *);
extern X_API int PL_unify_wchars_diff(term_t, term_t, int, size_t, const pl_wchar_t *);
extern X_API int PL_chars_to_term(term_t,const char *);
extern X_API int PL_chars_to_term(const char *,term_t);
/* begin PL_is_* functions =============================*/
extern X_API int PL_is_atom(term_t);
extern X_API int PL_is_atomic(term_t);

View File

@ -441,7 +441,7 @@ extern X_API int PROTO(YAP_Halt,(int));
extern X_API YAP_Term *PROTO(YAP_TopOfLocalStack,(void));
/* int YAP_Predicate() */
extern X_API void *PROTO(YAP_Predicate,(YAP_Atom,unsigned long int,int));
extern X_API void *PROTO(YAP_Predicate,(YAP_Atom,unsigned long int,YAP_Term));
/* int YAP_Predicate() */
extern X_API void PROTO(YAP_PredicateInfo,(void *,YAP_Atom *,unsigned long int*,YAP_Module*));

View File

@ -99,6 +99,16 @@ SWIAtomToAtom(atom_t at)
return (Atom)at;
}
static inline Term
SWIModuleToModule(module_t m)
{
if (m)
return (CELL)m;
if (CurrentModule)
return CurrentModule;
return USER_MODULE;
}
static inline functor_t
FunctorToSWIFunctor(Functor at)
{
@ -171,11 +181,11 @@ PL_agc_hook(PL_agc_hook_t entry)
YAP: char* AtomName(Atom) */
X_API char* PL_atom_chars(atom_t a) /* SAM check type */
{
return AtomName(SWIAtomToAtom(a));
return RepAtom(SWIAtomToAtom(a))->StrOfAE;
}
X_API int
PL_chars_to_term(term_t term,const char *s) {
PL_chars_to_term(const char *s, term_t term) {
YAP_Term t,error;
if ( (t=YAP_ReadBuffer(s,&error))==0L ) {
Yap_PutInSlot(term, error);
@ -263,7 +273,7 @@ X_API int PL_get_intptr(term_t ts, intptr_t *a)
YAP: char* AtomName(Atom) */
X_API int PL_get_atom_chars(term_t ts, char **a) /* SAM check type */
{
YAP_Term t = Yap_GetFromSlot(ts);
Term t = Yap_GetFromSlot(ts);
if (!IsAtomTerm(t))
return 0;
*a = RepAtom(AtomOfTerm(t))->StrOfAE;
@ -1932,12 +1942,10 @@ PL_initialise(int myargc, char **myargv)
{
YAP_init_args init_args;
memset((void *)&init_args,0,sizeof(init_args));
init_args.Argv = myargv;
init_args.Argc = myargc;
init_args.SavedState = "startup.yss";
init_args.HeapSize = 0;
init_args.StackSize = 0;
init_args.TrailSize = 0;
init_args.YapLibDir = NULL;
init_args.YapPrologBootFile = NULL;
init_args.HaltAfterConsult = FALSE;
@ -1983,20 +1991,24 @@ X_API atom_t PL_module_name(module_t m)
X_API predicate_t PL_pred(functor_t f, module_t m)
{
Functor ff = SWIFunctorToFunctor(f);
Term mod = SWIModuleToModule(m);
if (IsAtomTerm((Term)f)) {
return YAP_Predicate(YAP_AtomOfTerm((Term)f),0,(YAP_Module)m);
return YAP_Predicate(YAP_AtomOfTerm((Term)f),0,mod);
} else {
return YAP_Predicate((YAP_Atom)NameOfFunctor(ff),ArityOfFunctor(ff),(YAP_Module)m);
return YAP_Predicate((YAP_Atom)NameOfFunctor(ff),ArityOfFunctor(ff),mod);
}
}
X_API predicate_t PL_predicate(const char *name, int arity, const char *m)
{
int mod;
if (m == NULL)
mod = YAP_CurrentModule();
else
Term mod;
if (m == NULL) {
mod = CurrentModule;
if (!mod) mod = USER_MODULE;
} else {
mod = MkAtomTerm(Yap_LookupAtom((char *)m));
}
return YAP_Predicate(YAP_LookupAtom((char *)name),
arity,
mod);
@ -2033,8 +2045,7 @@ X_API qid_t PL_open_query(module_t ctx, int flags, predicate_t p, term_t t0)
{
Atom yname;
unsigned long int arity;
Term m;
Term t[2];
Term t[2], m;
/* ignore flags and module for now */
if (execution.open != 0) {
@ -2043,14 +2054,14 @@ X_API qid_t PL_open_query(module_t ctx, int flags, predicate_t p, term_t t0)
execution.open=1;
execution.state=0;
PredicateInfo((PredEntry *)p, &yname, &arity, &m);
t[0] = YAP_ModuleName(m);
t[0] = SWIModuleToModule(ctx);
if (arity == 0) {
t[1] = MkAtomTerm(yname);
} else {
Functor f = Yap_MkFunctor(yname, arity);
t[1] = Yap_MkApplTerm(f,arity,Yap_AddressFromSlot(t0));
}
execution.g = Yap_MkApplTerm(Yap_MkFunctor(Yap_LookupAtom(":"),2),2,t);
execution.g = Yap_MkApplTerm(FunctorModule,2,t);
return &execution;
}
@ -2073,6 +2084,7 @@ X_API int PL_next_solution(qid_t qi)
X_API void PL_cut_query(qid_t qi)
{
if (qi->open != 1) return;
YAP_PruneGoal();
qi->open = 0;
}
@ -2102,11 +2114,20 @@ X_API int PL_toplevel(void)
X_API int PL_call(term_t tp, module_t m)
{
YAP_Term t[2], g;
t[0] = YAP_ModuleName((YAP_Module)m);
int out;
BACKUP_B();
BACKUP_H();
Term t[2], g;
t[0] = SWIModuleToModule(m);
t[1] = Yap_GetFromSlot(tp);
g = YAP_MkApplTerm(YAP_MkFunctor(YAP_LookupAtom(":"),2),2,t);
return YAP_RunGoal(g);
g = Yap_MkApplTerm(FunctorModule,2,t);
out = YAP_RunGoal(g);
RECOVER_H();
RECOVER_B();
return out;
}
X_API void PL_register_foreign_in_module(const char *module, const char *name, int arity, foreign_t (*function)(void), int flags)