python support

This commit is contained in:
Vitor Santos Costa 2016-07-31 11:36:51 -05:00
parent 37ddd18a9d
commit 6163533208

View File

@ -3,11 +3,93 @@
// Language independent exception handler // Language independent exception handler
%include exception.i %include exception.i
%include stdint.i
%ignore *::operator[];
class YAPPredicate; class YAPPredicate;
class YAPEngine; class YAPEngine;
#define arity_t uintptr_t
#ifdef SWIGPYTHON #ifdef SWIGPYTHON
%typemap(out) YAPTerm {
if ($1.handle() == 0) {
return NULL;
}
}
%extend(out) YAPTerm {
YAPTerm & __getitem__ (size_t i) {
Term t0 = $self->term();
if (IsApplTerm(t0)) {
Functor f = FunctorOfTerm(t0);
if (!IsExtensionFunctor(f))
return *new YAPTerm(ArgOfTerm(i + 1, t0));
} else if (IsPairTerm(t0)) {
if (i==0)
return * new YAPTerm(HeadOfTerm(t0));
else if (i==1)
return * new YAPTerm(TailOfTerm(t0));
}
return * new YAPTerm();
}
}
%typemap(out) YAPIntegerTerm {
Term t = $1.term();
Int j = IntegerOfTerm(t);
#if PY_MAJOR_VERSION < 3
return PyInt_FromLong(j);
#else
return PyLong_FromLong(j);
#endif
}
%typemap(out) YAPFloatTerm {
Term t = $1.term();
Int double j = FloatOfTerm(t);
return PyFloat_FromDouble(j);
}
// translate well-known names and existing
// Python symbols
// Everthing else let wrapped.
// as a term
%typemap(out) YAPAtomTerm {
const char *s = RepAtom(AtomOfTerm($1.term()))->StrOfAE;
PyObject *p;
if ((p = AtomToPy( s))) {
return p;
}
}
// translate lists as Python Lists
// Python symbols
// Everthing else let wrapped.
// as a term
%typemap(out) YAPListTerm {
Term l =$1.term(), *end;
PyObject *list;
Int len = Yap_SkipList( & l, &end );
$result = list = PyList_New( len );
for (Int i = 0 ; i < len ; i++) {
Term a = HeadOfTerm(l);
YAPTerm *argp1 = new YAPTerm( a );
PyObject *obj0 =
SWIG_NewPointerObj(SWIG_as_voidptr(argp1), SWIGTYPE_p_YAPTerm, 0 | 0 );
l = TailOfTerm(l);
PyList_SetItem( list, i, obj0 );
}
return list;
}
%exception YAPPredicate { %exception YAPPredicate {
try { try {
$action $action
@ -18,6 +100,7 @@
} }
#endif #endif
%exception query { %exception query {
try { try {
$action $action
@ -88,3 +171,11 @@
} }
} }
#endif #endif
%init %{
#ifdef SWIGPYTHON
init_python();
#endif
%}