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

454 lines
12 KiB
C
Raw Normal View History

2016-07-31 16:09:21 +01:00
2018-08-19 14:39:12 +01:00
2018-07-21 12:08:09 +01:00
#include "Yap.h"
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
2018-07-31 15:18:56 +01:00
2018-07-27 11:11:04 +01:00
PyObject *YEC(PyObject *f, PyObject *a, PyObject *d, int line, const char *file, const char *code) {
fprintf(stderr, "**** Warning,%s@%s:%d: failed on Python call \n", code,
file, line);
if (f)
PyObject_Print(f, stderr, 0);
else
fprintf(stderr,"<null>");
if (a)
PyObject_Print(a, stderr, 0);
2018-07-31 15:18:56 +01:00
if (d)
PyObject_Print(d, stderr, 0);
fprintf(stderr,"\n");
return NULL;
}
PyObject *YED2(PyObject *f, PyObject *a, PyObject *d, int line, const char *file, const char *code) {
fprintf(stderr, "**** Warning,%s@%s:%d: failed on Python call \n", code,
file, line);
if (f)
PyObject_Print(f, stderr, 0);
else
fprintf(stderr,"<null>");
fprintf(stderr,"(");
2018-07-27 11:11:04 +01:00
if (a)
PyObject_Print(a, stderr, 0);
2018-07-31 15:18:56 +01:00
fprintf(stderr,",");
if (d)
PyObject_Print(d, stderr, 0);
fprintf(stderr,")\n");
return NULL;
}
PyObject *YED1(PyObject *f, PyObject *a, int line, const char *file, const char *code) {
fprintf(stderr, "**** Warning,%s@%s:%d: failed on Python call \n", code,
file, line);
if (f)
PyObject_Print(f, stderr, 0);
else
fprintf(stderr,"<null>");
fprintf(stderr,"(");
if (a)
PyObject_Print(a, stderr, 0);
fprintf(stderr,")\n");
2018-07-27 11:11:04 +01:00
return NULL;
2018-07-31 15:18:56 +01:00
2018-07-27 11:11:04 +01:00
}
2018-07-31 15:18:56 +01:00
void YEM(const char *exp, int line, const char *file, const char *code) {
2017-06-18 11:14:55 +01:00
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 {
2017-08-21 12:36:48 +01:00
// char *ns = Py_Malloc(strlen(s)+1);
/// strcpy(ns,s);
2017-07-05 21:47:10 +01:00
PyObject *pobj = PyUnicode_FromString(s);
2018-03-12 15:11:59 +00:00
Py_INCREF(pobj);
2017-06-18 11:14:55 +01:00
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) {
2018-03-12 15:11:59 +00:00
2017-06-18 11:14:55 +01:00
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
2018-08-21 03:01:03 +01:00
static bool entry_to_dictionary(PyObject *dict, Term targ,
2017-08-21 12:36:48 +01:00
bool eval, bool cvt) {
2018-08-21 03:01:03 +01:00
PyObject *lhs = NULL, *rhs;
Term t1, t2;
const char *s;
t1 = ArgOfTerm(1,targ);
if (YAP_IsAtomTerm(t1)) {
s = YAP_AtomName(YAP_AtomOfTerm(t1));
} else if (IsStringTerm(t1)) {
s = YAP_StringOfTerm(t1);
} else if (IsIntegerTerm(t1)) {
lhs = PyLong_FromLong(IntegerOfTerm(t1));
} else {
return false;
2017-08-21 12:36:48 +01:00
}
2018-08-21 03:01:03 +01:00
2017-08-21 12:36:48 +01:00
if (lhs == NULL) {
2018-08-21 03:01:03 +01:00
lhs = PyUnicode_FromString(s);
2017-08-21 12:36:48 +01:00
}
2018-08-21 03:01:03 +01:00
t2 = ArgOfTerm(2,targ);
rhs = term_to_python(Yap_InitSlot(t2), eval, NULL, cvt);
2017-08-21 12:36:48 +01:00
if (rhs == NULL) {
PyErr_Print();
return false;
}
2018-08-21 03:01:03 +01:00
return PyDict_SetItem(dict, lhs, rhs) == 0;
2017-08-21 12:36:48 +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-08-21 12:36:48 +01:00
PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
2018-07-21 12:08:09 +01:00
//
2017-06-18 11:14:55 +01:00
switch (PL_term_type(t)) {
case PL_VARIABLE: {
2018-12-21 20:57:53 +00:00
if (t == 0) {
Yap_ThrowError(SYSTEM_ERROR_INTERNAL, t, "in term_to_python");
2017-05-14 11:36:09 +01:00
}
PyObject *out = PyTuple_New(1);
2017-08-21 12:36:48 +01:00
PyTuple_SET_ITEM(out, 0, PyLong_FromLong((YAP_Int)YAP_GetFromSlot(t)));
if (!cvt)
return out;
2017-05-14 11:36:09 +01:00
return term_to_nametuple("v", 1, out);
};
2017-06-18 11:14:55 +01:00
case PL_ATOM: {
2018-12-21 20:57:53 +00:00
YAP_Term yt = YAP_GetFromSlot(t);
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-08-21 12:36:48 +01:00
}
if (!o) {
o = PyUnicode_FromString(s);
2017-05-14 11:36:09 +01:00
}
2017-06-18 11:14:55 +01:00
if (o) {
2018-07-21 12:08:09 +01:00
// PyDict_SetItemString(py_Atoms, s, Py_None);
2017-05-14 11:36:09 +01:00
Py_INCREF(o);
2017-08-21 12:36:48 +01:00
return o;
2017-05-14 11:36:09 +01:00
}
}
2017-06-18 11:14:55 +01:00
case PL_STRING: {
2018-12-21 20:57:53 +00:00
YAP_Term yt = YAP_GetFromSlot(t);
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 {
2018-07-21 12:08:09 +01:00
return CHECKNULL(t, NULL);
2016-07-31 16:09:21 +01:00
}
2018-07-21 12:08:09 +01:00
PyObject *pobj = PyUnicode_FromString(s);
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);
2018-07-21 12:08:09 +01:00
}
2016-07-31 16:09:21 +01:00
#endif
2018-07-21 12:08:09 +01:00
// char *p = malloc(strlen(s)+1);
// strcpy(p, s);
Py_IncRef(pobj);
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-08-21 12:36:48 +01:00
default:
if (PL_is_pair(t)) {
2018-07-21 01:56:48 +01:00
Term t0 = Yap_GetFromHandle(t);
Term *tail;
2018-07-21 12:08:09 +01:00
size_t len, i;
2018-07-27 11:11:04 +01:00
if ((len = Yap_SkipList(&t0, &tail)) > 0 && *tail == TermNil) {
2017-08-21 12:36:48 +01:00
PyObject *out, *a;
2016-07-31 16:09:21 +01:00
2017-08-21 12:36:48 +01:00
out = PyList_New(len);
for (i = 0; i < len; i++) {
2018-07-21 12:08:09 +01:00
Term ai = HeadOfTerm(t0);
a = term_to_python(Yap_InitHandle(ai), eval, o, cvt);
2017-08-21 12:36:48 +01:00
if (a) {
if (PyList_SetItem(out, i, a) < 0) {
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "list->python");
}
}
2018-07-21 12:08:09 +01:00
t0 = TailOfTerm(t0);
2017-08-21 12:36:48 +01:00
}
return out;
} else {
2018-07-27 11:11:04 +01:00
PyObject *no = find_term_obj(o, &t0, false);
return yap_to_python(t0, eval, no, cvt);
2016-07-31 16:09:21 +01:00
}
2017-06-18 11:14:55 +01:00
} else {
2017-08-21 12:36:48 +01:00
{
term_t tail = PL_new_term_ref();
functor_t fun;
2018-08-21 03:01:03 +01:00
2017-08-21 12:36:48 +01:00
if (!PL_get_functor(t, &fun)) {
PL_reset_term_refs(tail);
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "list->python");
}
if (fun == FUNCTOR_pointer1) {
void *ptr;
AOK(PL_get_arg(1, t, t), NULL);
AOK(PL_get_pointer(t, &ptr), NULL)
/* return __main__,s */
return (PyObject *)ptr;
}
if (fun == FUNCTOR_sqbrackets2) {
term_t targ = PL_new_term_ref(), trhs = PL_new_term_ref();
PyObject *v;
Py_ssize_t min, max;
AOK(PL_get_arg(2, t, targ), NULL);
v = term_to_python(targ, eval, o, false);
AOK(PL_get_arg(1, t, targ), NULL);
AOK(PL_get_list(targ, trhs, targ), NULL);
if (PL_is_functor(trhs, FUNCTOR_colon2)) {
if (!PySequence_Check(v))
return NULL;
min = get_p_int(term_to_python(targ, true, NULL, false), 0);
AOK(PL_get_arg(1, trhs, targ), NULL);
if (PL_is_functor(targ, FUNCTOR_colon2)) {
return NULL;
}
max = get_p_int(term_to_python(targ, true, o, false),
PyObject_Size(v));
return PySequence_GetSlice(v, min, max);
} else {
PyObject *ip = term_to_python(trhs, eval, o, cvt);
if (PySequence_Check(v)) {
#if PY_MAJOR_VERSION < 3
2018-07-21 12:08:09 +01:00
if (PyLong_Check(ip)) {
2017-08-21 12:36:48 +01:00
min = PyLong_AsLong(ip);
2018-07-21 12:08:09 +01:00
} else if (PyInt_Check(ip)) {
2017-08-21 12:36:48 +01:00
min = PyInt_asInt(ip);
2018-07-21 12:08:09 +01:00
}
2017-08-21 12:36:48 +01:00
#else
if (PyLong_Check(ip)) {
PyObject *o = PySequence_GetItem(v, PyLong_AsLong(ip));
if (o == NULL)
o = Py_None;
if (CHECKNULL(t, o) == NULL)
return NULL;
Py_INCREF(o);
return o;
}
#endif
} else {
o = PyObject_GetItem(v, ip);
if (o == NULL)
o = Py_None;
Py_INCREF(o);
return o;
}
}
}
if (fun == FUNCTOR_dollar1) {
char *s = NULL;
term_t targ = PL_new_term_ref();
AOK(PL_get_arg(1, t, targ), NULL);
AOK(PL_get_atom_chars(targ, &s), NULL);
/* return __main__,s */
PyObject *o = PyObject_GetAttrString(py_Main, s);
return o;
}
if (fun == FUNCTOR_brackets1) {
AOK(PL_get_arg(1, t, t), NULL);
2018-07-31 15:18:56 +01:00
PyObject *ys = term_to_python(t, true, o, true), *rc;
CHECK_CALL(ys, PyTuple_New(0), NULL);
return rc;
2017-08-21 12:36:48 +01:00
}
if (fun == FUNCTOR_complex2) {
term_t targ = PL_new_term_ref();
PyObject *lhs, *rhs;
double d1, d2;
AOK(PL_get_arg(1, t, targ), NULL);
lhs = term_to_python(targ, eval, NULL, cvt);
AOK(PyNumber_Check(lhs), NULL);
if (PyFloat_Check(lhs)) {
d1 = PyFloat_AsDouble(lhs);
} else if (PyLong_Check(lhs)) {
d1 = PyLong_AsLong(lhs);
#if PY_MAJOR_VERSION < 3
} else if (PyInt_Check(lhs)) {
d1 = PyInt_AsLong(lhs);
#endif
} else {
return NULL;
}
AOK(PL_get_arg(2, t, targ), NULL);
rhs = term_to_python(targ, eval, NULL, cvt);
AOK(PyNumber_Check(rhs), NULL);
if (PyFloat_Check(rhs)) {
d2 = PyFloat_AsDouble(rhs);
} else if (PyLong_Check(rhs)) {
d2 = PyLong_AsLong(rhs);
#if PY_MAJOR_VERSION < 3
} else if (PyInt_Check(rhs)) {
d2 = PyInt_AsLong(rhs);
#endif
} else {
return NULL;
}
return PyComplex_FromDoubles(d1, d2);
}
if (fun == FUNCTOR_curly1) {
2018-08-21 03:01:03 +01:00
PyObject *dict;
Term yt = ArgOfTerm(1,Yap_GetFromHandle(t));
2017-08-21 12:36:48 +01:00
if (!(dict = PyDict_New()))
return NULL;
2018-07-21 12:08:09 +01:00
Py_INCREF(dict);
2018-08-21 03:01:03 +01:00
while (IsApplTerm(yt) && FunctorOfTerm(yt) == FunctorComma) {
if (!entry_to_dictionary(dict, ArgOfTerm(1,yt), eval, cvt))
return NULL;
yt = ArgOfTerm(2,yt);
}
if (entry_to_dictionary(dict, yt, eval, cvt))
return dict;
else
2018-09-18 19:27:10 +01:00
return Py_None;
2017-08-21 12:36:48 +01:00
}
2018-12-21 20:57:53 +00:00
if (fun == FUNCTOR_var1) {
return Py_None;
}
2018-08-21 03:01:03 +01:00
atom_t name;
int arity;
2017-08-21 12:36:48 +01:00
AOK(PL_get_name_arity(t, &name, &arity), NULL);
2018-07-21 01:56:48 +01:00
2017-08-21 12:36:48 +01:00
if (name == ATOM_t) {
int i;
2018-08-21 03:01:03 +01:00
PyObject *rc = PyTuple_New(arity);
2017-08-21 12:36:48 +01:00
for (i = 0; i < arity; 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");
}
2018-08-19 14:39:12 +01:00
PyObject *a = term_to_python(arg, eval, NULL, cvt);
2017-08-21 12:36:48 +01:00
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);
}
}
if (eval)
2018-08-21 03:01:03 +01:00
return compound_to_pyeval(t, o, cvt);
2017-08-21 12:36:48 +01:00
else
2018-08-21 03:01:03 +01:00
return compound_to_pytree(t, o, cvt);
2017-02-20 14:37:26 +00:00
}
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-08-21 12:36:48 +01:00
PyObject *yap_to_python(YAP_Term t, bool eval, PyObject *o, bool cvt) {
2018-09-18 19:27:10 +01:00
if (t == 0 || t == TermNone)
return Py_None;
2017-02-20 14:37:26 +00:00
term_t yt = YAP_InitSlot(t);
2017-08-21 12:36:48 +01:00
o = term_to_python(yt, eval, o, cvt);
2017-02-20 14:37:26 +00:00
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)) {
2018-07-06 16:51:19 +01:00
char b[1024];
char *o = YAP_WriteBuffer(yt, b, 1023, 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-08-21 12:36:48 +01:00
return term_to_python(t, true, NULL, false);
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,
2017-08-21 12:36:48 +01:00
yap_error_number type, term_t where, ...);