2016-07-31 16:09:21 +01:00
|
|
|
|
|
|
|
|
2017-06-12 18:00:47 +01:00
|
|
|
#include "py4yap.h"
|
2016-07-31 16:09:21 +01:00
|
|
|
|
2017-05-02 07:38:23 +01:00
|
|
|
extern PyObject *py_Local, *py_Global;
|
2017-02-20 14:37:26 +00:00
|
|
|
|
2017-06-18 11:14:55 +01:00
|
|
|
PyObject *YE(term_t t, int line, const char *file, const char *code) {
|
2017-02-20 14:37:26 +00:00
|
|
|
char buf[1024];
|
|
|
|
YAP_WriteBuffer(YAP_GetFromSlot(t), buf, 1023, 0);
|
2017-06-18 11:14:55 +01:00
|
|
|
fprintf(stderr, "**** Warning,%s@%s:%d: failed on expression %s\n", code,
|
|
|
|
file, line, buf);
|
2017-05-02 07:38:23 +01:00
|
|
|
|
2017-02-20 14:37:26 +00:00
|
|
|
return NULL;
|
2016-10-16 23:18:51 +01:00
|
|
|
}
|
2016-07-31 16:09:21 +01:00
|
|
|
|
2017-06-18 11:14:55 +01: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);
|
2017-02-20 14:37:26 +00:00
|
|
|
}
|
|
|
|
|
2017-06-18 11:14:55 +01:00
|
|
|
static PyObject *s_to_python(const char *s, bool eval, PyObject *p0) {
|
2017-05-27 22:54:00 +01:00
|
|
|
PyObject *o;
|
2017-06-18 11:14:55 +01:00
|
|
|
if (eval) {
|
|
|
|
o = PythonLookup(s, p0);
|
|
|
|
/* if (!o)
|
|
|
|
return o;
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
o = PythonLookupSpecial(s);
|
|
|
|
}
|
|
|
|
if (o) {
|
|
|
|
Py_INCREF(o);
|
|
|
|
return CHECKNULL(YAP_MkStringTerm(s), o);
|
|
|
|
} else {
|
|
|
|
PyObject *pobj = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
|
|
|
|
return pobj;
|
|
|
|
}
|
2017-05-27 22:54:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* obtain the object matching a certain string.
|
|
|
|
*
|
|
|
|
* @param t handle to Prolog term
|
|
|
|
* @param t whether should try to evaluate evaluables.
|
|
|
|
*
|
|
|
|
* @return a Python object descriptor or NULL if failed
|
|
|
|
*/
|
2017-06-18 11:14:55 +01:00
|
|
|
X_API PyObject *string_to_python(const char *s, bool eval, PyObject *p0) {
|
|
|
|
char *buf = malloc(strlen(s) + 1), *child;
|
|
|
|
while ((child = strchr(s, '.')) != NULL) {
|
2017-05-27 22:54:00 +01:00
|
|
|
size_t len = child - s;
|
2017-06-18 11:14:55 +01:00
|
|
|
strncpy(buf, s, len);
|
|
|
|
buf[len] = '\0';
|
2017-05-27 22:54:00 +01:00
|
|
|
p0 = s_to_python(buf, eval, p0);
|
2017-06-18 11:14:55 +01:00
|
|
|
s = child + 1;
|
2017-05-27 22:54:00 +01:00
|
|
|
}
|
|
|
|
return s_to_python(s, eval, p0);
|
|
|
|
}
|
2017-06-18 11:14:55 +01:00
|
|
|
|
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
|
|
|
|
*/
|
2017-06-18 11:14:55 +01:00
|
|
|
PyObject *term_to_python(term_t t, bool eval, PyObject *o) {
|
2017-02-20 14:37:26 +00:00
|
|
|
// o≈
|
2016-07-31 16:09:21 +01:00
|
|
|
YAP_Term yt = YAP_GetFromSlot(t);
|
2017-05-08 18:51:29 +01:00
|
|
|
// Yap_DebugPlWriteln(yt);
|
2017-06-18 11:14:55 +01:00
|
|
|
switch (PL_term_type(t)) {
|
|
|
|
case PL_VARIABLE: {
|
|
|
|
if (t == 0) {
|
2017-05-14 11:36:09 +01:00
|
|
|
Yap_ThrowError(SYSTEM_ERROR_INTERNAL, yt, "in term_to_python");
|
|
|
|
}
|
|
|
|
PyObject *out = PyTuple_New(1);
|
|
|
|
PyTuple_SET_ITEM(out, 0, PyLong_FromLong(t));
|
|
|
|
return term_to_nametuple("v", 1, out);
|
|
|
|
};
|
2017-06-18 11:14:55 +01:00
|
|
|
case PL_ATOM: {
|
2016-07-31 16:09:21 +01:00
|
|
|
YAP_Atom at = YAP_AtomOfTerm(yt);
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
s = YAP_AtomName(at);
|
2017-06-18 11:14:55 +01:00
|
|
|
if (eval) {
|
2017-02-20 14:37:26 +00:00
|
|
|
o = PythonLookup(s, o);
|
|
|
|
/* if (!o)
|
2017-06-18 11:14:55 +01:00
|
|
|
return o;
|
2017-02-20 14:37:26 +00:00
|
|
|
*/
|
2017-06-18 11:14:55 +01:00
|
|
|
} else {
|
2017-05-14 11:36:09 +01:00
|
|
|
o = PythonLookupSpecial(s);
|
|
|
|
}
|
2017-06-18 11:14:55 +01:00
|
|
|
if (o) {
|
2017-05-14 11:36:09 +01:00
|
|
|
Py_INCREF(o);
|
|
|
|
return CHECKNULL(t, o);
|
|
|
|
}
|
|
|
|
}
|
2017-06-18 11:14:55 +01:00
|
|
|
case PL_STRING: {
|
2017-02-20 14:37:26 +00:00
|
|
|
const char *s = NULL;
|
2017-06-18 11:14:55 +01:00
|
|
|
if (YAP_IsAtomTerm(yt)) {
|
2017-02-20 14:37:26 +00:00
|
|
|
s = YAP_AtomName(YAP_AtomOfTerm(yt));
|
2017-06-18 11:14:55 +01:00
|
|
|
} else if (YAP_IsStringTerm(yt)) {
|
2017-02-20 14:37:26 +00:00
|
|
|
s = YAP_StringOfTerm(yt);
|
2017-06-18 11:14:55 +01:00
|
|
|
} else {
|
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-06-18 11:14:55 +01:00
|
|
|
if (proper_ascii_string(s)) {
|
2017-05-14 11:36:09 +01:00
|
|
|
PyObject *o = PyString_FromStringAndSize(s, strlen(s));
|
2017-02-20 14:37:26 +00:00
|
|
|
return CHECKNULL(t, o);
|
2017-06-18 11:14:55 +01:00
|
|
|
} else
|
2016-07-31 16:09:21 +01:00
|
|
|
#endif
|
2017-05-14 11:36:09 +01:00
|
|
|
{
|
2017-06-22 10:41:41 +01:00
|
|
|
// char *p = malloc(strlen(s)+1);
|
|
|
|
//strcpy(p, s);
|
|
|
|
PyObject *pobj = PyUnicode_FromString(s);
|
|
|
|
Py_IncRef(pobj);
|
2017-05-14 11:36:09 +01:00
|
|
|
return CHECKNULL(t, pobj);
|
|
|
|
}
|
2017-06-18 11:14:55 +01:00
|
|
|
} break;
|
|
|
|
case PL_INTEGER: {
|
2016-07-31 16:09:21 +01:00
|
|
|
int64_t j;
|
|
|
|
if (!PL_get_int64_ex(t, &j))
|
2017-05-14 11:36:09 +01: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);
|
2017-05-14 11:36:09 +01:00
|
|
|
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);
|
2017-05-14 11:36:09 +01:00
|
|
|
return CHECKNULL(t, o);
|
2016-07-31 16:09:21 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-06-18 11:14:55 +01:00
|
|
|
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-05-14 11:36:09 +01:00
|
|
|
return CHECKNULL(t, NULL);
|
2017-02-20 14:37:26 +00:00
|
|
|
out = PyFloat_FromDouble(fl);
|
|
|
|
return CHECKNULL(t, out);
|
2016-07-31 16:09:21 +01:00
|
|
|
}
|
2017-06-18 11:14:55 +01:00
|
|
|
default: {
|
2016-07-31 16:09:21 +01:00
|
|
|
term_t tail = PL_new_term_ref(), arg;
|
|
|
|
size_t len, i;
|
2017-06-18 11:14:55 +01:00
|
|
|
if (PL_skip_list(t, tail, &len) && PL_get_nil(tail)) {
|
2017-05-02 07:38:23 +01:00
|
|
|
PyObject *out, *a;
|
2016-07-31 16:09:21 +01:00
|
|
|
|
|
|
|
arg = tail;
|
|
|
|
out = PyList_New(len);
|
2017-06-18 11:14:55 +01:00
|
|
|
if (!out) {
|
2017-07-03 22:12:31 +01:00
|
|
|
PL_reset_term_refs(tail);
|
|
|
|
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "list->python");
|
2017-06-18 11:14:55 +01:00
|
|
|
}
|
2016-07-31 16:09:21 +01:00
|
|
|
|
2017-06-18 11:14:55 +01:00
|
|
|
for (i = 0; i < len; i++) {
|
2017-07-03 22:12:31 +01:00
|
|
|
if (!PL_get_list(t, arg, t)) {
|
|
|
|
PL_reset_term_refs(tail);
|
|
|
|
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "list->python");
|
|
|
|
}
|
|
|
|
a = term_to_python(arg, eval, o);
|
|
|
|
if (a) {
|
|
|
|
if (PyList_SetItem(out, i, a) < 0) {
|
|
|
|
PL_reset_term_refs(tail);
|
|
|
|
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "list->python");
|
|
|
|
}
|
|
|
|
}
|
2016-07-31 16:09:21 +01:00
|
|
|
}
|
2017-02-20 14:37:26 +00:00
|
|
|
PL_reset_term_refs(tail);
|
2017-05-14 11:36:09 +01:00
|
|
|
return CHECKNULL(t, out);
|
2017-06-18 11:14:55 +01:00
|
|
|
} else {
|
2016-07-31 16:09:21 +01:00
|
|
|
functor_t fun;
|
2017-07-03 22:12:31 +01:00
|
|
|
atom_t name;
|
|
|
|
int arity;
|
2017-02-20 14:37:26 +00:00
|
|
|
PyObject *rc;
|
2016-07-31 16:09:21 +01:00
|
|
|
|
2017-06-18 11:14:55 +01:00
|
|
|
if (!PL_get_functor(t, &fun)) {
|
2017-07-03 22:12:31 +01:00
|
|
|
PL_reset_term_refs(tail);
|
|
|
|
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "list->python");
|
|
|
|
}
|
|
|
|
AOK(PL_get_name_arity(t, &name, &arity), NULL);
|
|
|
|
if (name == ATOM_t) {
|
|
|
|
int i;
|
|
|
|
rc = PyTuple_New(arity);
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
term_t arg = PL_new_term_ref();
|
|
|
|
if (!PL_get_arg(i+1, t, arg)) {
|
|
|
|
PL_reset_term_refs(arg);
|
|
|
|
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "t(...)->python");
|
|
|
|
}
|
|
|
|
PyObject *a = term_to_python(arg, eval, o);
|
|
|
|
if (a) {
|
|
|
|
if (PyTuple_SetItem(rc, i, a) < 0) {
|
|
|
|
PL_reset_term_refs(arg);
|
|
|
|
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "t(...)->python");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PL_reset_term_refs(arg);
|
|
|
|
}
|
2017-02-20 14:37:26 +00:00
|
|
|
}
|
2016-07-31 16:09:21 +01:00
|
|
|
if (eval)
|
2017-07-03 22:12:31 +01:00
|
|
|
rc = compound_to_pyeval(t, o);
|
2017-02-20 14:37:26 +00:00
|
|
|
else
|
2017-07-03 22:12:31 +01:00
|
|
|
rc = compound_to_pytree(t, o);
|
2017-02-20 14:37:26 +00:00
|
|
|
PL_reset_term_refs(tail);
|
|
|
|
return rc;
|
2016-07-31 16:09:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-03 22:12:31 +01:00
|
|
|
return Py_None;
|
2017-02-20 14:37:26 +00:00
|
|
|
}
|
|
|
|
|
2017-06-18 11:14:55 +01:00
|
|
|
PyObject *yap_to_python(YAP_Term t, bool eval, PyObject *o) {
|
2017-05-14 11:36:09 +01:00
|
|
|
if (t == 0)
|
2017-05-02 07:38:23 +01:00
|
|
|
return NULL;
|
2017-02-20 14:37:26 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-06-18 11:14:55 +01:00
|
|
|
PyObject *deref_term_to_python(term_t t) {
|
2016-07-31 16:09:21 +01:00
|
|
|
// Yap_DebugPlWrite(YAP_GetFromSlot(t)); fprintf(stderr, " here I
|
|
|
|
// am\n");
|
|
|
|
YAP_Term yt = YAP_GetFromSlot(t);
|
2017-06-18 11:14:55 +01:00
|
|
|
if (YAP_IsVarTerm(yt)) {
|
2016-07-31 16:09:21 +01:00
|
|
|
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
|
|
|
}
|
2017-07-03 22:12:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
void YAPPy_ThrowError__(const char *file, const char *function, int lineno,
|
|
|
|
yap_error_number type, term_t where, ...);
|