This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/python/pl2py.c

165 lines
3.8 KiB
C
Raw Normal View History

2016-07-31 16:09:21 +01:00
#include "python.h"
2017-02-20 14:37:26 +00:00
PyObject *py_Local, *py_Global;
PyObject *YE(term_t t, int line, const char *file, const char *code)
{
char buf[1024];
YAP_WriteBuffer(YAP_GetFromSlot(t), buf, 1023, 0);
fprintf(stderr, "**** Warning,%s@%s:%d: failed on expression %s\n", code, file, line, buf );
return NULL;
2016-10-16 23:18:51 +01:00
}
2016-07-31 16:09:21 +01:00
2017-02-20 14:37:26 +00:00
void YEM(const char * exp, int line, const char *file, const char *code)
{
fprintf(stderr, "**** Warning,%s@%s:%d: failed while executing %s\n", code, file, line, exp );
}
2016-07-31 16:09:21 +01:00
/**
2017-02-20 14:37:26 +00:00
* term_to_python translates and evaluates from Prolog to Python
*
* @param t handle to Prolog term
* @param t whether should try to evaluate evaluables.
*
* @return a Python object descriptor or NULL if failed
*/
PyObject *term_to_python(term_t t, bool eval, PyObject *o) {
// o≈
2016-07-31 16:09:21 +01:00
YAP_Term yt = YAP_GetFromSlot(t);
2017-02-20 14:37:26 +00:00
// Yap_DebugPlWriteln(yt);
2016-07-31 16:09:21 +01:00
switch (PL_term_type(t)) {
case PL_VARIABLE: {
2016-12-10 07:01:10 +00:00
YAP_Term i = YAP_MkIntTerm(t);
2017-02-20 14:37:26 +00:00
PyObject *rc = term_to_nametuple(
"H", 1, YAP_InitSlot(YAP_MkApplTerm(
YAP_MkFunctor(YAP_LookupAtom("H"), 1), 1, &i)));
return CHECKNULL( t, rc );
2016-07-31 16:09:21 +01:00
};
case PL_ATOM: {
YAP_Atom at = YAP_AtomOfTerm(yt);
const char *s;
s = YAP_AtomName(at);
2017-02-20 14:37:26 +00:00
if (eval) {
o = PythonLookup(s, o);
/* if (!o)
return o;
*/
} else
{
PyObject *o = PythonLookupSpecial(s);
2016-07-31 16:09:21 +01:00
}
2017-02-20 14:37:26 +00:00
if (o) {
Py_INCREF( o );
return CHECKNULL(t,o);
}
}
2016-07-31 16:09:21 +01:00
case PL_STRING: {
2017-02-20 14:37:26 +00:00
const char *s = NULL;
if (YAP_IsAtomTerm(yt)) {
s = YAP_AtomName(YAP_AtomOfTerm(yt));
} else if (YAP_IsStringTerm(yt)) {
s = YAP_StringOfTerm(yt);
} else {
return CHECKNULL(t, NULL);
2016-07-31 16:09:21 +01:00
}
#if PY_MAJOR_VERSION < 3
if (proper_ascii_string(s)) {
2017-02-20 14:37:26 +00:00
PyObject *o = PyString_FromStringAndSize(s, strlen(s)) ;
return CHECKNULL(t, o);
2016-07-31 16:09:21 +01:00
} else
#endif
2017-02-20 14:37:26 +00:00
{
PyObject *pobj = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
return CHECKNULL(t,pobj);
2016-10-16 23:18:51 +01:00
}
2016-07-31 16:09:21 +01:00
} break;
case PL_INTEGER: {
int64_t j;
if (!PL_get_int64_ex(t, &j))
2017-02-20 14:37:26 +00:00
return CHECKNULL(t,NULL);
2016-07-31 16:09:21 +01:00
#if PY_MAJOR_VERSION < 3
2017-02-20 14:37:26 +00:00
PyObject *o = PyInt_FromLong(j);
return CHECKNULL(t,o);
2016-07-31 16:09:21 +01:00
#else
2017-02-20 14:37:26 +00:00
PyObject *o = PyLong_FromLong(j);
return CHECKNULL(t,o);
2016-07-31 16:09:21 +01:00
#endif
}
case PL_FLOAT: {
2017-02-20 14:37:26 +00:00
PyObject *out;
2016-07-31 16:09:21 +01:00
double fl;
if (!PL_get_float(t, &fl))
2017-02-20 14:37:26 +00:00
return CHECKNULL(t,NULL);
out = PyFloat_FromDouble(fl);
return CHECKNULL(t, out);
2016-07-31 16:09:21 +01:00
}
default: {
term_t tail = PL_new_term_ref(), arg;
size_t len, i;
if (PL_skip_list(t, tail, &len) && PL_get_nil(tail)) {
PyObject *out;
arg = tail;
out = PyList_New(len);
if (!out)
2017-02-20 14:37:26 +00:00
return CHECKNULL(t,NULL);
2016-07-31 16:09:21 +01:00
for (i = 0; i < len; i++) {
if (!PL_get_list(t, arg, t)) {
2017-02-20 14:37:26 +00:00
PL_reset_term_refs(tail);
return CHECKNULL(t,NULL);
2016-07-31 16:09:21 +01:00
}
2017-02-20 14:37:26 +00:00
if (PyList_SetItem(out, i, term_to_python(arg, eval, o)) < 0) {
return CHECKNULL(t,NULL);
2016-07-31 16:09:21 +01:00
}
}
2017-02-20 14:37:26 +00:00
PL_reset_term_refs(tail);
return CHECKNULL(t,out);
2016-07-31 16:09:21 +01:00
} else {
functor_t fun;
2017-02-20 14:37:26 +00:00
PyObject *rc;
2016-07-31 16:09:21 +01:00
2017-02-20 14:37:26 +00:00
if (!PL_get_functor(t, &fun)) {
PL_reset_term_refs(tail);
return CHECKNULL(t,NULL);
}
2016-07-31 16:09:21 +01:00
if (eval)
2017-02-20 14:37:26 +00:00
rc = compound_to_pyeval(t, o);
else
rc = compound_to_pytree(t, o);
PL_reset_term_refs(tail);
return rc;
2016-07-31 16:09:21 +01:00
}
}
}
2017-02-20 14:37:26 +00:00
return CHECKNULL(t,NULL);
}
PyObject *yap_to_python(YAP_Term t, bool eval, PyObject *o) {
term_t yt = YAP_InitSlot(t);
o = term_to_python(yt, eval, o);
PL_reset_term_refs(yt);
return o;
2016-07-31 16:09:21 +01:00
}
PyObject *deref_term_to_python(term_t t) {
// Yap_DebugPlWrite(YAP_GetFromSlot(t)); fprintf(stderr, " here I
// am\n");
YAP_Term yt = YAP_GetFromSlot(t);
if (YAP_IsVarTerm(yt)) {
char s[32];
char *o = YAP_WriteBuffer(yt, s, 31, 0);
2017-02-20 14:37:26 +00:00
PyObject *p = PyUnicode_FromString(o);
return p;
2016-07-31 16:09:21 +01:00
}
2017-02-20 14:37:26 +00:00
return term_to_python(t, false, NULL);
2016-07-31 16:09:21 +01:00
}