Merge branch 'master' of https://github.com/vscosta/yap-6.3
This commit is contained in:
commit
efaf86c675
@ -149,43 +149,65 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o) {
|
|||||||
arg = tail;
|
arg = tail;
|
||||||
out = PyList_New(len);
|
out = PyList_New(len);
|
||||||
if (!out) {
|
if (!out) {
|
||||||
PL_reset_term_refs(tail);
|
PL_reset_term_refs(tail);
|
||||||
return CHECKNULL(t, Py_None);
|
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "list->python");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
if (!PL_get_list(t, arg, t)) {
|
if (!PL_get_list(t, arg, t)) {
|
||||||
PL_reset_term_refs(tail);
|
PL_reset_term_refs(tail);
|
||||||
return Py_None;
|
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "list->python");
|
||||||
}
|
}
|
||||||
a = term_to_python(arg, eval, o);
|
a = term_to_python(arg, eval, o);
|
||||||
if (a) {
|
if (a) {
|
||||||
if (PyList_SetItem(out, i, a) < 0) {
|
if (PyList_SetItem(out, i, a) < 0) {
|
||||||
PL_reset_term_refs(tail);
|
PL_reset_term_refs(tail);
|
||||||
return Py_None;
|
YAPPy_ThrowError(SYSTEM_ERROR_INTERNAL, t, "list->python");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PL_reset_term_refs(tail);
|
PL_reset_term_refs(tail);
|
||||||
return CHECKNULL(t, out);
|
return CHECKNULL(t, out);
|
||||||
} else {
|
} else {
|
||||||
functor_t fun;
|
functor_t fun;
|
||||||
|
atom_t name;
|
||||||
|
int arity;
|
||||||
PyObject *rc;
|
PyObject *rc;
|
||||||
|
|
||||||
if (!PL_get_functor(t, &fun)) {
|
if (!PL_get_functor(t, &fun)) {
|
||||||
PL_reset_term_refs(tail);
|
PL_reset_term_refs(tail);
|
||||||
return CHECKNULL(t, Py_None);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (eval)
|
if (eval)
|
||||||
rc = compound_to_pyeval(t, o);
|
rc = compound_to_pyeval(t, o);
|
||||||
else
|
else
|
||||||
rc = compound_to_pytree(t, o);
|
rc = compound_to_pytree(t, o);
|
||||||
PL_reset_term_refs(tail);
|
PL_reset_term_refs(tail);
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return CHECKNULL(t, Py_None);
|
return Py_None;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *yap_to_python(YAP_Term t, bool eval, PyObject *o) {
|
PyObject *yap_to_python(YAP_Term t, bool eval, PyObject *o) {
|
||||||
@ -209,3 +231,7 @@ PyObject *deref_term_to_python(term_t t) {
|
|||||||
}
|
}
|
||||||
return term_to_python(t, false, NULL);
|
return term_to_python(t, false, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void YAPPy_ThrowError__(const char *file, const char *function, int lineno,
|
||||||
|
yap_error_number type, term_t where, ...);
|
||||||
|
@ -1,5 +1,55 @@
|
|||||||
|
|
||||||
#include "py4yap.h"
|
#include "py4yap.h"
|
||||||
|
#include <frameobject.h>
|
||||||
|
|
||||||
|
void YAPPy_ThrowError__(const char *file, const char *function, int lineno,
|
||||||
|
yap_error_number type, term_t where, ...) {
|
||||||
|
va_list ap;
|
||||||
|
char tmpbuf[MAXPATHLEN];
|
||||||
|
YAP_Term wheret = YAP_GetFromSlot(where);
|
||||||
|
|
||||||
|
if (PyErr_Occurred()) {
|
||||||
|
PyErr_Print();
|
||||||
|
} else {
|
||||||
|
PyObject *ptype, *pvalue, *ptraceback;
|
||||||
|
PyObject *pystr, *module_name, *pyth_module, *pyth_func;
|
||||||
|
|
||||||
|
/* See if we can get a full traceback */
|
||||||
|
module_name = PyUnicode_FromString("traceback");
|
||||||
|
pyth_module = PyImport_Import(module_name);
|
||||||
|
Py_DECREF(module_name);
|
||||||
|
|
||||||
|
if (pyth_module != NULL) {
|
||||||
|
pyth_func = PyObject_GetAttrString(pyth_module, "format_exception");
|
||||||
|
if (pyth_func && PyCallable_Check(pyth_func)) {
|
||||||
|
PyObject *pyth_val;
|
||||||
|
|
||||||
|
pyth_val = PyObject_CallFunctionObjArgs(pyth_func, ptype, pvalue, ptraceback, NULL);
|
||||||
|
|
||||||
|
pystr = PyObject_Str(pyth_val);
|
||||||
|
fprintf(stderr, "%s", PyUnicode_AsUTF8(pystr));
|
||||||
|
Py_DECREF(pyth_val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PyFrameObject *fr;
|
||||||
|
if ((fr = PyEval_GetFrame())) {
|
||||||
|
fprintf(stderr, "at frame %p, line %d\n", fr, PyFrame_GetLineNumber(fr));
|
||||||
|
}
|
||||||
|
va_start(ap, where);
|
||||||
|
char *format = va_arg(ap, char *);
|
||||||
|
if (format != NULL) {
|
||||||
|
#if HAVE_VSNPRINTF
|
||||||
|
(void)vsnprintf(tmpbuf, MAXPATHLEN - 1, format, ap);
|
||||||
|
#else
|
||||||
|
(void)vsprintf(tnpbuf, format, ap);
|
||||||
|
#endif
|
||||||
|
// fprintf(stderr, "warning: ");
|
||||||
|
Yap_ThrowError__(file, function, lineno, type, wheret, tmpbuf);
|
||||||
|
} else {
|
||||||
|
Yap_ThrowError__(file, function, lineno, type, wheret);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static foreign_t repr_term(PyObject *pVal, term_t t) {
|
static foreign_t repr_term(PyObject *pVal, term_t t) {
|
||||||
term_t to = PL_new_term_ref(), t1 = PL_new_term_ref();
|
term_t to = PL_new_term_ref(), t1 = PL_new_term_ref();
|
||||||
|
@ -197,4 +197,9 @@ extern PyObject *PythonLookup(const char *s, PyObject *o);
|
|||||||
|
|
||||||
extern PyObject *PythonLookupSpecial(const char *s);
|
extern PyObject *PythonLookupSpecial(const char *s);
|
||||||
|
|
||||||
|
#define YAPPy_ThrowError(id, inp, ...) \
|
||||||
|
YAPPy_ThrowError__(__FILE__, __FUNCTION__, __LINE__, id, inp, __VA_ARGS__)
|
||||||
|
|
||||||
|
extern void YAPPy_ThrowError__(const char *file, const char *function, int lineno,
|
||||||
|
yap_error_number type, term_t where, ...);
|
||||||
#endif
|
#endif
|
||||||
|
@ -716,17 +716,19 @@ PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
|
|||||||
desc->n_in_sequence = arity;
|
desc->n_in_sequence = arity;
|
||||||
if (PyStructSequence_InitType2(typp, desc) < 0)
|
if (PyStructSequence_InitType2(typp, desc) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
typp->tp_flags &= ~Py_TPFLAGS_HEAPTYPE;
|
// typp->tp_flags &= ~Py_TPFLAGS_HEAPTYPE;
|
||||||
|
// typp->tp_flags &= ~Py_TPFLAGS_HAVE_GC;
|
||||||
// typp->tp_str = structseq_str;
|
// typp->tp_str = structseq_str;
|
||||||
typp->tp_repr = structseq_repr;
|
typp->tp_repr = structseq_repr;
|
||||||
// typp = PyStructSequence_NewType(desc);
|
// typp = PyStructSequence_NewType(desc);
|
||||||
// don't do this: we cannot add a type as an atribute.
|
// don't do this: we cannot add a type as an atribute.
|
||||||
// PyModule_AddObject(py_Main, s, (PyObject *)typp);
|
// PyModule_AddGObject(py_Main, s, (PyObject *)typp);
|
||||||
if (py_F2P)
|
if (py_F2P)
|
||||||
PyDict_SetItem(py_F2P, key, (PyObject *)typp);
|
PyDict_SetItem(py_F2P, key, (PyObject *)typp);
|
||||||
Py_INCREF(typp);
|
Py_INCREF(typp);
|
||||||
}
|
}
|
||||||
o = PyStructSequence_New(typp);
|
o = PyStructSequence_New(typp);
|
||||||
|
Py_INCREF(typp);
|
||||||
arity_t i;
|
arity_t i;
|
||||||
for (i = 0; i < arity; i++) {
|
for (i = 0; i < arity; i++) {
|
||||||
PyObject *pArg = PyTuple_GET_ITEM(tuple, i);
|
PyObject *pArg = PyTuple_GET_ITEM(tuple, i);
|
||||||
@ -747,493 +749,493 @@ PyObject *term_to_nametuple(const char *s, arity_t arity, PyObject *tuple) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *bip_range(term_t t) {
|
static PyObject *bip_range(term_t t) {
|
||||||
long ilow = 0, ihigh = 0, istep = 1;
|
long ilow = 0, ihigh = 0, istep = 1;
|
||||||
long bign;
|
long bign;
|
||||||
Py_ssize_t i, n;
|
Py_ssize_t i, n;
|
||||||
int arity;
|
int arity;
|
||||||
atom_t name;
|
atom_t name;
|
||||||
term_t arg = PL_new_term_ref();
|
term_t arg = PL_new_term_ref();
|
||||||
|
|
||||||
PyObject *v;
|
PyObject *v;
|
||||||
|
|
||||||
if (!PL_get_name_arity(t, &name, &arity))
|
if (!PL_get_name_arity(t, &name, &arity))
|
||||||
return NULL;
|
return NULL;
|
||||||
AOK(PL_get_arg(1, t, arg), NULL);
|
AOK(PL_get_arg(1, t, arg), NULL);
|
||||||
ilow = get_int(arg, true);
|
ilow = get_int(arg, true);
|
||||||
if (arity == 1) {
|
if (arity == 1) {
|
||||||
ihigh = ilow;
|
ihigh = ilow;
|
||||||
ilow = 0;
|
ilow = 0;
|
||||||
} else {
|
} else {
|
||||||
AOK(PL_get_arg(2, t, arg), NULL);
|
AOK(PL_get_arg(2, t, arg), NULL);
|
||||||
ihigh = get_int(arg, true);
|
ihigh = get_int(arg, true);
|
||||||
if (arity == 3) {
|
if (arity == 3) {
|
||||||
AOK(PL_get_arg(3, t, arg), NULL);
|
AOK(PL_get_arg(3, t, arg), NULL);
|
||||||
istep = get_int(arg, true);
|
istep = get_int(arg, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (istep == 0) {
|
if (istep == 0) {
|
||||||
PyErr_SetString(PyExc_ValueError, "range() step argument must not be zero");
|
PyErr_SetString(PyExc_ValueError, "range() step argument must not be zero");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (istep > 0)
|
if (istep > 0)
|
||||||
bign = get_len_of_range(ilow, ihigh, istep);
|
bign = get_len_of_range(ilow, ihigh, istep);
|
||||||
else
|
else
|
||||||
bign = get_len_of_range(ihigh, ilow, -istep);
|
bign = get_len_of_range(ihigh, ilow, -istep);
|
||||||
n = (Py_ssize_t)bign;
|
n = (Py_ssize_t)bign;
|
||||||
AOK(((bign >= 0 && (long)n == bign) || "range() result has too many items"),
|
AOK(((bign >= 0 && (long)n == bign) || "range() result has too many items"),
|
||||||
NULL);
|
NULL);
|
||||||
v = PyList_New(n);
|
v = PyList_New(n);
|
||||||
|
|
||||||
if (v == NULL)
|
if (v == NULL)
|
||||||
return Py_None;
|
return Py_None;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_MAJOR_VERSION < 3
|
||||||
PyObject *w = PyInt_FromLong(ilow);
|
PyObject *w = PyInt_FromLong(ilow);
|
||||||
#else
|
#else
|
||||||
PyObject *w = PyLong_FromLong(ilow);
|
PyObject *w = PyLong_FromLong(ilow);
|
||||||
#endif
|
#endif
|
||||||
if (w == NULL) {
|
if (w == NULL) {
|
||||||
Py_DECREF(v);
|
Py_DECREF(v);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
PyList_SET_ITEM(v, i, w);
|
PyList_SET_ITEM(v, i, w);
|
||||||
Py_INCREF(w);
|
Py_INCREF(w);
|
||||||
ilow += istep;
|
ilow += istep;
|
||||||
}
|
}
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool copy_to_dictionary(PyObject *dict, term_t targ, term_t taux,
|
static bool copy_to_dictionary(PyObject *dict, term_t targ, term_t taux,
|
||||||
bool eval) {
|
bool eval) {
|
||||||
PyObject *lhs, *rhs;
|
PyObject *lhs, *rhs;
|
||||||
term_t tleft = PL_new_term_ref(), tright = PL_new_term_ref();
|
term_t tleft = PL_new_term_ref(), tright = PL_new_term_ref();
|
||||||
|
|
||||||
functor_t fun;
|
functor_t fun;
|
||||||
|
|
||||||
AOK(PL_get_functor(targ, &fun), false);
|
AOK(PL_get_functor(targ, &fun), false);
|
||||||
while (fun == FUNCTOR_comma2) {
|
while (fun == FUNCTOR_comma2) {
|
||||||
AOK(PL_get_arg(1, targ, tleft), false);
|
AOK(PL_get_arg(1, targ, tleft), false);
|
||||||
if (!copy_to_dictionary(dict, tleft, taux, eval))
|
if (!copy_to_dictionary(dict, tleft, taux, eval))
|
||||||
return false;
|
return false;
|
||||||
AOK(PL_get_arg(2, targ, targ), false);
|
AOK(PL_get_arg(2, targ, targ), false);
|
||||||
return copy_to_dictionary(dict, tright, taux, eval);
|
return copy_to_dictionary(dict, tright, taux, eval);
|
||||||
}
|
}
|
||||||
// PyObject_Print(dict, stderr, 0); fprintf(stderr,"\n");
|
// PyObject_Print(dict, stderr, 0); fprintf(stderr,"\n");
|
||||||
// Py_DECREF(lhs);
|
// Py_DECREF(lhs);
|
||||||
// Py_DECREF(rhs);
|
// Py_DECREF(rhs);
|
||||||
|
|
||||||
AOK(PL_get_arg(1, targ, tleft), false);
|
AOK(PL_get_arg(1, targ, tleft), false);
|
||||||
lhs = atom_to_python_string(tleft);
|
lhs = atom_to_python_string(tleft);
|
||||||
if (lhs == NULL) {
|
if (lhs == NULL) {
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
AOK(PL_get_arg(2, targ, tright), false);
|
AOK(PL_get_arg(2, targ, tright), false);
|
||||||
rhs = term_to_python(tright, eval, NULL);
|
rhs = term_to_python(tright, eval, NULL);
|
||||||
if (rhs == NULL) {
|
if (rhs == NULL) {
|
||||||
PyErr_Print();
|
PyErr_Print();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return PyDict_SetItem(dict, lhs, rhs) >= 0;
|
return PyDict_SetItem(dict, lhs, rhs) >= 0;
|
||||||
// PyObject_Print(dict, stderr, 0); fprintf(stderr,"\n");
|
// PyObject_Print(dict, stderr, 0); fprintf(stderr,"\n");
|
||||||
// Py_DECREF(lhs);
|
// Py_DECREF(lhs);
|
||||||
// Py_DECREF(rhs);
|
// Py_DECREF(rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) {
|
PyObject *compound_to_data(term_t t, PyObject *o, functor_t fun, bool exec) {
|
||||||
atom_t name;
|
atom_t name;
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
AOK(PL_get_name_arity(t, &name, &len), o);
|
AOK(PL_get_name_arity(t, &name, &len), o);
|
||||||
|
|
||||||
if (fun == FUNCTOR_pointer1) {
|
if (fun == FUNCTOR_pointer1) {
|
||||||
void *ptr;
|
void *ptr;
|
||||||
|
|
||||||
AOK(PL_get_arg(1, t, t), NULL);
|
AOK(PL_get_arg(1, t, t), NULL);
|
||||||
AOK(PL_get_pointer(t, &ptr), NULL)
|
AOK(PL_get_pointer(t, &ptr), NULL)
|
||||||
/* return __main__,s */
|
/* return __main__,s */
|
||||||
return (PyObject *)ptr;
|
return (PyObject *)ptr;
|
||||||
}
|
}
|
||||||
if (name == ATOM_t) {
|
if (name == ATOM_t) {
|
||||||
term_t targ = PL_new_term_ref();
|
term_t targ = PL_new_term_ref();
|
||||||
PyObject *out;
|
PyObject *out;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
out = PyTuple_New(len);
|
out = PyTuple_New(len);
|
||||||
DebugPrintf("Tuple %p\n", out);
|
DebugPrintf("Tuple %p\n", out);
|
||||||
|
|
||||||
if (!out)
|
if (!out)
|
||||||
return NULL;
|
return NULL;
|
||||||
for (i = 0; i < len; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
AOK(PL_get_arg(i + 1, t, targ), NULL);
|
AOK(PL_get_arg(i + 1, t, targ), NULL);
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
PyObject *oa = term_to_python(targ, true, o);
|
PyObject *oa = term_to_python(targ, true, o);
|
||||||
bool rc = PyTuple_SET_ITEM(out, i, oa) == 0;
|
bool rc = PyTuple_SET_ITEM(out, i, oa) == 0;
|
||||||
if (rc) {
|
if (rc) {
|
||||||
PyErr_Print();
|
PyErr_Print();
|
||||||
}
|
}
|
||||||
Py_INCREF(oa);
|
Py_INCREF(oa);
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
} else if (fun == FUNCTOR_div2) {
|
} else if (fun == FUNCTOR_div2) {
|
||||||
term_t targ = PL_new_term_ref();
|
term_t targ = PL_new_term_ref();
|
||||||
PyObject *lhs, *rhs;
|
PyObject *lhs, *rhs;
|
||||||
|
|
||||||
AOK(PL_get_arg(1, t, targ), NULL);
|
AOK(PL_get_arg(1, t, targ), NULL);
|
||||||
lhs = term_to_python(targ, true, NULL);
|
lhs = term_to_python(targ, true, NULL);
|
||||||
if (!PyNumber_Check(lhs))
|
if (!PyNumber_Check(lhs))
|
||||||
return NULL;
|
return NULL;
|
||||||
AOK(PL_get_arg(2, t, targ), NULL);
|
AOK(PL_get_arg(2, t, targ), NULL);
|
||||||
rhs = term_to_python(targ, true, NULL);
|
rhs = term_to_python(targ, true, NULL);
|
||||||
if (!PyNumber_Check(rhs))
|
if (!PyNumber_Check(rhs))
|
||||||
return NULL;
|
return NULL;
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_MAJOR_VERSION < 3
|
||||||
return PyNumber_Divide(lhs, rhs);
|
return PyNumber_Divide(lhs, rhs);
|
||||||
#else
|
#else
|
||||||
return PyNumber_TrueDivide(lhs, rhs);
|
return PyNumber_TrueDivide(lhs, rhs);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
if (fun == FUNCTOR_sqbrackets2) {
|
if (fun == FUNCTOR_sqbrackets2) {
|
||||||
term_t targ = PL_new_term_ref(), trhs = PL_new_term_ref();
|
term_t targ = PL_new_term_ref(), trhs = PL_new_term_ref();
|
||||||
PyObject *v;
|
PyObject *v;
|
||||||
Py_ssize_t min, max;
|
Py_ssize_t min, max;
|
||||||
AOK(PL_get_arg(2, t, targ), NULL);
|
AOK(PL_get_arg(2, t, targ), NULL);
|
||||||
v = term_to_python(targ, true, o);
|
v = term_to_python(targ, true, o);
|
||||||
|
|
||||||
AOK(PL_get_arg(1, t, targ), NULL);
|
AOK(PL_get_arg(1, t, targ), NULL);
|
||||||
AOK(PL_get_list(targ, trhs, targ), NULL);
|
AOK(PL_get_list(targ, trhs, targ), NULL);
|
||||||
if (PL_is_functor(trhs, FUNCTOR_colon2)) {
|
if (PL_is_functor(trhs, FUNCTOR_colon2)) {
|
||||||
if (!PySequence_Check(v))
|
if (!PySequence_Check(v))
|
||||||
return NULL;
|
return NULL;
|
||||||
min = get_p_int(term_to_python(targ, true, NULL), 0);
|
min = get_p_int(term_to_python(targ, true, NULL), 0);
|
||||||
AOK(PL_get_arg(1, trhs, targ), NULL);
|
AOK(PL_get_arg(1, trhs, targ), NULL);
|
||||||
if (PL_is_functor(targ, FUNCTOR_colon2)) {
|
if (PL_is_functor(targ, FUNCTOR_colon2)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
max = get_p_int(term_to_python(targ, true, o), PyObject_Size(v));
|
max = get_p_int(term_to_python(targ, true, o), PyObject_Size(v));
|
||||||
return PySequence_GetSlice(v, min, max);
|
return PySequence_GetSlice(v, min, max);
|
||||||
} else {
|
} else {
|
||||||
PyObject *ip = term_to_python(trhs, true, o);
|
PyObject *ip = term_to_python(trhs, true, o);
|
||||||
if (PySequence_Check(v)) {
|
if (PySequence_Check(v)) {
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_MAJOR_VERSION < 3
|
||||||
if (PyLong_Check(ip) {
|
if (PyLong_Check(ip) {
|
||||||
min = PyLong_AsLong(ip);
|
min = PyLong_AsLong(ip);
|
||||||
} else if (PyInt_Check(ip) {
|
} else if (PyInt_Check(ip) {
|
||||||
min = PyInt_asInt(ip);
|
min = PyInt_asInt(ip);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
if (PyLong_Check(ip)) {
|
if (PyLong_Check(ip)) {
|
||||||
PyObject *o = PySequence_GetItem(v, PyLong_AsLong(ip));
|
PyObject *o = PySequence_GetItem(v, PyLong_AsLong(ip));
|
||||||
if (o == NULL)
|
if (o == NULL)
|
||||||
o = Py_None;
|
o = Py_None;
|
||||||
if (CHECKNULL(t, o) == NULL)
|
if (CHECKNULL(t, o) == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
Py_INCREF(o);
|
Py_INCREF(o);
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
o = PyObject_GetItem(v, ip);
|
o = PyObject_GetItem(v, ip);
|
||||||
if (o == NULL)
|
if (o == NULL)
|
||||||
o = Py_None;
|
o = Py_None;
|
||||||
Py_INCREF(o);
|
Py_INCREF(o);
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (fun == FUNCTOR_dollar1) {
|
if (fun == FUNCTOR_dollar1) {
|
||||||
char *s = NULL;
|
char *s = NULL;
|
||||||
term_t targ = PL_new_term_ref();
|
term_t targ = PL_new_term_ref();
|
||||||
AOK(PL_get_arg(1, t, targ), NULL);
|
AOK(PL_get_arg(1, t, targ), NULL);
|
||||||
AOK(PL_get_atom_chars(targ, &s), NULL);
|
AOK(PL_get_atom_chars(targ, &s), NULL);
|
||||||
/* return __main__,s */
|
/* return __main__,s */
|
||||||
PyObject *o = PyObject_GetAttrString(py_Main, s);
|
PyObject *o = PyObject_GetAttrString(py_Main, s);
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
if (fun == FUNCTOR_brackets1) {
|
if (fun == FUNCTOR_brackets1) {
|
||||||
AOK(PL_get_arg(1, t, t), NULL);
|
AOK(PL_get_arg(1, t, t), NULL);
|
||||||
return term_to_python(t, true, NULL);
|
return term_to_python(t, true, NULL);
|
||||||
}
|
}
|
||||||
if (fun == FUNCTOR_complex2) {
|
if (fun == FUNCTOR_complex2) {
|
||||||
term_t targ = PL_new_term_ref();
|
term_t targ = PL_new_term_ref();
|
||||||
PyObject *lhs, *rhs;
|
PyObject *lhs, *rhs;
|
||||||
double d1, d2;
|
double d1, d2;
|
||||||
|
|
||||||
AOK(PL_get_arg(1, t, targ), NULL);
|
AOK(PL_get_arg(1, t, targ), NULL);
|
||||||
lhs = term_to_python(targ, true, NULL);
|
lhs = term_to_python(targ, true, NULL);
|
||||||
AOK(PyNumber_Check(lhs), NULL);
|
AOK(PyNumber_Check(lhs), NULL);
|
||||||
if (PyFloat_Check(lhs)) {
|
if (PyFloat_Check(lhs)) {
|
||||||
d1 = PyFloat_AsDouble(lhs);
|
d1 = PyFloat_AsDouble(lhs);
|
||||||
} else if (PyLong_Check(lhs)) {
|
} else if (PyLong_Check(lhs)) {
|
||||||
d1 = PyLong_AsLong(lhs);
|
d1 = PyLong_AsLong(lhs);
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_MAJOR_VERSION < 3
|
||||||
} else if (PyInt_Check(lhs)) {
|
} else if (PyInt_Check(lhs)) {
|
||||||
d1 = PyInt_AsLong(lhs);
|
d1 = PyInt_AsLong(lhs);
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
AOK(PL_get_arg(2, t, targ), NULL);
|
AOK(PL_get_arg(2, t, targ), NULL);
|
||||||
rhs = term_to_python(targ, true, NULL);
|
rhs = term_to_python(targ, true, NULL);
|
||||||
AOK(PyNumber_Check(rhs), NULL);
|
AOK(PyNumber_Check(rhs), NULL);
|
||||||
if (PyFloat_Check(rhs)) {
|
if (PyFloat_Check(rhs)) {
|
||||||
d2 = PyFloat_AsDouble(rhs);
|
d2 = PyFloat_AsDouble(rhs);
|
||||||
} else if (PyLong_Check(rhs)) {
|
} else if (PyLong_Check(rhs)) {
|
||||||
d2 = PyLong_AsLong(rhs);
|
d2 = PyLong_AsLong(rhs);
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_MAJOR_VERSION < 3
|
||||||
} else if (PyInt_Check(rhs)) {
|
} else if (PyInt_Check(rhs)) {
|
||||||
d2 = PyInt_AsLong(rhs);
|
d2 = PyInt_AsLong(rhs);
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return PyComplex_FromDoubles(d1, d2);
|
return PyComplex_FromDoubles(d1, d2);
|
||||||
}
|
}
|
||||||
if (fun == FUNCTOR_curly1) {
|
if (fun == FUNCTOR_curly1) {
|
||||||
term_t targ = PL_new_term_ref(), taux = PL_new_term_ref();
|
term_t targ = PL_new_term_ref(), taux = PL_new_term_ref();
|
||||||
PyObject *dict;
|
PyObject *dict;
|
||||||
|
|
||||||
AOK(PL_get_arg(1, t, t), NULL);
|
AOK(PL_get_arg(1, t, t), NULL);
|
||||||
if (!(dict = PyDict_New()))
|
if (!(dict = PyDict_New()))
|
||||||
return NULL;
|
return NULL;
|
||||||
DebugPrintf("Dict %p\n", dict);
|
DebugPrintf("Dict %p\n", dict);
|
||||||
|
|
||||||
while (PL_is_functor(t, FUNCTOR_comma2)) {
|
while (PL_is_functor(t, FUNCTOR_comma2)) {
|
||||||
AOK(PL_get_arg(1, t, targ), NULL);
|
AOK(PL_get_arg(1, t, targ), NULL);
|
||||||
AOK(PL_is_functor(targ, FUNCTOR_colon2), NULL);
|
AOK(PL_is_functor(targ, FUNCTOR_colon2), NULL);
|
||||||
|
|
||||||
AOK(copy_to_dictionary(dict, targ, taux, true), NULL);
|
AOK(copy_to_dictionary(dict, targ, taux, true), NULL);
|
||||||
AOK(PL_get_arg(2, t, t), NULL);
|
AOK(PL_get_arg(2, t, t), NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PL_is_functor(t, FUNCTOR_colon2)) {
|
if (PL_is_functor(t, FUNCTOR_colon2)) {
|
||||||
AOK(copy_to_dictionary(dict, t, taux, true), NULL);
|
AOK(copy_to_dictionary(dict, t, taux, true), NULL);
|
||||||
}
|
}
|
||||||
return dict;
|
return dict;
|
||||||
}
|
}
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *compound_to_pytree(term_t t, PyObject *context) {
|
PyObject *compound_to_pytree(term_t t, PyObject *context) {
|
||||||
PyObject *o = py_Main, *no;
|
PyObject *o = py_Main, *no;
|
||||||
functor_t fun;
|
functor_t fun;
|
||||||
atom_t name;
|
atom_t name;
|
||||||
int arity;
|
int arity;
|
||||||
|
|
||||||
o = find_obj(context, t, false);
|
o = find_obj(context, t, false);
|
||||||
AOK(PL_get_name_arity(t, &name, &arity), NULL);
|
AOK(PL_get_name_arity(t, &name, &arity), NULL);
|
||||||
if (arity == 0)
|
if (arity == 0)
|
||||||
return term_to_python(t, false, o);
|
return term_to_python(t, false, o);
|
||||||
AOK(PL_get_functor(t, &fun), NULL);
|
AOK(PL_get_functor(t, &fun), NULL);
|
||||||
if ((no = compound_to_data(t, o, fun, false)) != o && no) {
|
if ((no = compound_to_data(t, o, fun, false)) != o && no) {
|
||||||
return no;
|
return no;
|
||||||
}
|
}
|
||||||
if (!arity) {
|
if (!arity) {
|
||||||
char *s = NULL;
|
char *s = NULL;
|
||||||
|
|
||||||
AOK(!PL_get_atom_chars(t, &s), NULL);
|
AOK(!PL_get_atom_chars(t, &s), NULL);
|
||||||
// this should never happen
|
// this should never happen
|
||||||
return term_to_python(t, false, o);
|
return term_to_python(t, false, o);
|
||||||
} else {
|
} else {
|
||||||
const char *s;
|
const char *s;
|
||||||
if (!(s = PL_atom_chars(name))) {
|
if (!(s = PL_atom_chars(name))) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
term_t tleft;
|
term_t tleft;
|
||||||
int i;
|
int i;
|
||||||
PyObject *out = PyTuple_New(arity);
|
PyObject *out = PyTuple_New(arity);
|
||||||
DebugPrintf("Tuple %p\n", o);
|
DebugPrintf("Tuple %p\n", o);
|
||||||
tleft = PL_new_term_ref();
|
tleft = PL_new_term_ref();
|
||||||
for (i = 0; i < arity; i++) {
|
for (i = 0; i < arity; i++) {
|
||||||
PyObject *pArg;
|
PyObject *pArg;
|
||||||
AOK(PL_get_arg(i + 1, t, tleft), NULL);
|
AOK(PL_get_arg(i + 1, t, tleft), NULL);
|
||||||
pArg = term_to_python(tleft, false, NULL);
|
pArg = term_to_python(tleft, false, NULL);
|
||||||
if (pArg) {
|
if (pArg) {
|
||||||
/* pArg reference stolen here: */
|
/* pArg reference stolen here: */
|
||||||
PyTuple_SET_ITEM(out, i, pArg);
|
PyTuple_SET_ITEM(out, i, pArg);
|
||||||
Py_INCREF(pArg);
|
Py_INCREF(pArg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (CHECKNULL(t, out) == NULL) {
|
if (CHECKNULL(t, out) == NULL) {
|
||||||
PyErr_Print();
|
PyErr_Print();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
PyObject *c = lookupPySymbol(s, o, NULL);
|
PyObject *c = lookupPySymbol(s, o, NULL);
|
||||||
|
|
||||||
if (c && PyCallable_Check(c)) {
|
if (c && PyCallable_Check(c)) {
|
||||||
PyObject *n = PyTuple_New(arity);
|
PyObject *n = PyTuple_New(arity);
|
||||||
PyTuple_SET_ITEM(n, 0, c);
|
PyTuple_SET_ITEM(n, 0, c);
|
||||||
PyTuple_SET_ITEM(n, 1, out);
|
PyTuple_SET_ITEM(n, 1, out);
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
return term_to_nametuple(s, arity, out);
|
return term_to_nametuple(s, arity, out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *compound_to_pyeval(term_t t, PyObject *context) {
|
PyObject *compound_to_pyeval(term_t t, PyObject *context) {
|
||||||
PyObject *o = NULL, *no;
|
PyObject *o = NULL, *no;
|
||||||
atom_t name;
|
atom_t name;
|
||||||
int arity;
|
int arity;
|
||||||
functor_t fun;
|
functor_t fun;
|
||||||
|
|
||||||
o = find_obj(context, t, true);
|
o = find_obj(context, t, true);
|
||||||
AOK(PL_get_name_arity(t, &name, &arity), NULL);
|
AOK(PL_get_name_arity(t, &name, &arity), NULL);
|
||||||
if (arity == 0)
|
if (arity == 0)
|
||||||
return term_to_python(t, true, o);
|
return term_to_python(t, true, o);
|
||||||
if (!PL_get_functor(t, &fun))
|
if (!PL_get_functor(t, &fun))
|
||||||
return NULL;
|
return NULL;
|
||||||
if ((no = compound_to_data(t, o, fun, true)) != o && no) {
|
if ((no = compound_to_data(t, o, fun, true)) != o && no) {
|
||||||
return no;
|
return no;
|
||||||
}
|
}
|
||||||
if (fun == FUNCTOR_abs1) {
|
if (fun == FUNCTOR_abs1) {
|
||||||
return bip_abs(t);
|
return bip_abs(t);
|
||||||
} else if (fun == FUNCTOR_all1) {
|
} else if (fun == FUNCTOR_all1) {
|
||||||
return bip_all(t);
|
return bip_all(t);
|
||||||
} else if (fun == FUNCTOR_any1) {
|
} else if (fun == FUNCTOR_any1) {
|
||||||
return bip_any(t);
|
return bip_any(t);
|
||||||
} else if (fun == FUNCTOR_bin1) {
|
} else if (fun == FUNCTOR_bin1) {
|
||||||
return bip_bin(t);
|
return bip_bin(t);
|
||||||
} else if (fun == FUNCTOR_ord1) {
|
} else if (fun == FUNCTOR_ord1) {
|
||||||
return bip_ord(t);
|
return bip_ord(t);
|
||||||
} else if (fun == FUNCTOR_int1) {
|
} else if (fun == FUNCTOR_int1) {
|
||||||
return bip_int(t);
|
return bip_int(t);
|
||||||
} else if (fun == FUNCTOR_long1) {
|
} else if (fun == FUNCTOR_long1) {
|
||||||
return bip_long(t);
|
return bip_long(t);
|
||||||
} else if (fun == FUNCTOR_float1) {
|
} else if (fun == FUNCTOR_float1) {
|
||||||
return bip_float(t, true);
|
return bip_float(t, true);
|
||||||
} else if (fun == FUNCTOR_iter1) {
|
} else if (fun == FUNCTOR_iter1) {
|
||||||
return bip_iter(t);
|
return bip_iter(t);
|
||||||
} else if (fun == FUNCTOR_range1 || fun == FUNCTOR_range2 ||
|
} else if (fun == FUNCTOR_range1 || fun == FUNCTOR_range2 ||
|
||||||
fun == FUNCTOR_range3) {
|
fun == FUNCTOR_range3) {
|
||||||
return bip_range(t);
|
return bip_range(t);
|
||||||
} else if (fun == FUNCTOR_sum1) {
|
} else if (fun == FUNCTOR_sum1) {
|
||||||
return bip_sum(t);
|
return bip_sum(t);
|
||||||
}
|
}
|
||||||
if (fun == FUNCTOR_len1) {
|
if (fun == FUNCTOR_len1) {
|
||||||
term_t targ = PL_new_term_ref();
|
term_t targ = PL_new_term_ref();
|
||||||
PyObject *ptr;
|
PyObject *ptr;
|
||||||
|
|
||||||
AOK(PL_get_arg(1, t, targ), NULL);
|
AOK(PL_get_arg(1, t, targ), NULL);
|
||||||
ptr = term_to_python(targ, true, NULL);
|
ptr = term_to_python(targ, true, NULL);
|
||||||
return PyLong_FromLong(PyObject_Length(ptr));
|
return PyLong_FromLong(PyObject_Length(ptr));
|
||||||
}
|
}
|
||||||
if (fun == FUNCTOR_dir1) {
|
if (fun == FUNCTOR_dir1) {
|
||||||
term_t targ = PL_new_term_ref();
|
term_t targ = PL_new_term_ref();
|
||||||
PyObject *ptr;
|
PyObject *ptr;
|
||||||
|
|
||||||
AOK(PL_get_arg(1, t, targ), NULL);
|
AOK(PL_get_arg(1, t, targ), NULL);
|
||||||
ptr = term_to_python(targ, true, NULL);
|
ptr = term_to_python(targ, true, NULL);
|
||||||
return PyObject_Dir(ptr);
|
return PyObject_Dir(ptr);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (fun == FUNCTOR_plus2) {
|
else if (fun == FUNCTOR_plus2) {
|
||||||
term_t targ = PL_new_term_ref();
|
term_t targ = PL_new_term_ref();
|
||||||
PyObject *lhs, *rhs;
|
PyObject *lhs, *rhs;
|
||||||
|
|
||||||
if (!PL_get_arg(1, t, targ))
|
if (!PL_get_arg(1, t, targ))
|
||||||
return NULL;
|
return NULL;
|
||||||
lhs = term_to_python(targ, true, NULL);
|
lhs = term_to_python(targ, true, NULL);
|
||||||
AOK(PL_get_arg(2, t, targ), NULL);
|
AOK(PL_get_arg(2, t, targ), NULL);
|
||||||
rhs = term_to_python(targ, true, NULL);
|
rhs = term_to_python(targ, true, NULL);
|
||||||
if (PySequence_Check(lhs) && PySequence_Check(rhs)) {
|
if (PySequence_Check(lhs) && PySequence_Check(rhs)) {
|
||||||
return PySequence_Concat(lhs, rhs);
|
return PySequence_Concat(lhs, rhs);
|
||||||
}
|
}
|
||||||
if (!PyNumber_Check(lhs))
|
if (!PyNumber_Check(lhs))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (!PyNumber_Check(rhs))
|
if (!PyNumber_Check(rhs))
|
||||||
return NULL;
|
return NULL;
|
||||||
return PyNumber_Add(lhs, rhs);
|
return PyNumber_Add(lhs, rhs);
|
||||||
} else if (fun == FUNCTOR_sub2) {
|
} else if (fun == FUNCTOR_sub2) {
|
||||||
term_t targ = PL_new_term_ref();
|
term_t targ = PL_new_term_ref();
|
||||||
PyObject *lhs, *rhs;
|
PyObject *lhs, *rhs;
|
||||||
|
|
||||||
if (!PL_get_arg(1, t, targ))
|
if (!PL_get_arg(1, t, targ))
|
||||||
return NULL;
|
return NULL;
|
||||||
lhs = term_to_python(targ, true, NULL);
|
lhs = term_to_python(targ, true, NULL);
|
||||||
if (!PyNumber_Check(lhs))
|
if (!PyNumber_Check(lhs))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (!PL_get_arg(2, t, targ))
|
if (!PL_get_arg(2, t, targ))
|
||||||
return NULL;
|
return NULL;
|
||||||
rhs = term_to_python(targ, true, NULL);
|
rhs = term_to_python(targ, true, NULL);
|
||||||
if (!PyNumber_Check(rhs))
|
if (!PyNumber_Check(rhs))
|
||||||
return NULL;
|
return NULL;
|
||||||
return PyNumber_Subtract(lhs, rhs);
|
return PyNumber_Subtract(lhs, rhs);
|
||||||
} else if (fun == FUNCTOR_mul2) {
|
} else if (fun == FUNCTOR_mul2) {
|
||||||
term_t targ = PL_new_term_ref();
|
term_t targ = PL_new_term_ref();
|
||||||
PyObject *lhs, *rhs;
|
PyObject *lhs, *rhs;
|
||||||
|
|
||||||
AOK(PL_get_arg(1, t, targ), NULL);
|
AOK(PL_get_arg(1, t, targ), NULL);
|
||||||
(lhs = term_to_python(targ, true, NULL));
|
(lhs = term_to_python(targ, true, NULL));
|
||||||
CHECKNULL(targ, lhs);
|
CHECKNULL(targ, lhs);
|
||||||
AOK(PL_get_arg(2, t, targ), NULL);
|
AOK(PL_get_arg(2, t, targ), NULL);
|
||||||
(rhs = term_to_python(targ, true, NULL));
|
(rhs = term_to_python(targ, true, NULL));
|
||||||
CHECKNULL(targ, rhs);
|
CHECKNULL(targ, rhs);
|
||||||
if (PySequence_Check(lhs) && (
|
if (PySequence_Check(lhs) && (
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_MAJOR_VERSION < 3
|
||||||
PyInt_Check(rhs) ||
|
PyInt_Check(rhs) ||
|
||||||
#endif
|
#endif
|
||||||
PyLong_Check(rhs))) {
|
PyLong_Check(rhs))) {
|
||||||
return PySequence_Repeat(lhs, get_p_int(rhs, 0));
|
return PySequence_Repeat(lhs, get_p_int(rhs, 0));
|
||||||
}
|
}
|
||||||
if (!PyNumber_Check(lhs) + !PyNumber_Check(rhs))
|
if (!PyNumber_Check(lhs) + !PyNumber_Check(rhs))
|
||||||
return NULL;
|
return NULL;
|
||||||
return PyNumber_Multiply(lhs, rhs);
|
return PyNumber_Multiply(lhs, rhs);
|
||||||
}
|
}
|
||||||
if (!arity) {
|
if (!arity) {
|
||||||
char *s = NULL;
|
char *s = NULL;
|
||||||
PyObject *pValue;
|
PyObject *pValue;
|
||||||
|
|
||||||
AOK(PL_get_atom_chars(t, &s), NULL);
|
AOK(PL_get_atom_chars(t, &s), NULL);
|
||||||
pValue = PyObject_GetAttrString(o, s);
|
pValue = PyObject_GetAttrString(o, s);
|
||||||
if (CHECKNULL(t, pValue) == NULL) {
|
if (CHECKNULL(t, pValue) == NULL) {
|
||||||
PyErr_Print();
|
PyErr_Print();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return pValue;
|
return pValue;
|
||||||
} else {
|
} else {
|
||||||
char *s = PL_atom_chars(name);
|
char *s = PL_atom_chars(name);
|
||||||
o = lookupPySymbol(s, o, NULL);
|
o = lookupPySymbol(s, o, NULL);
|
||||||
if (CHECKNULL(t, o) == NULL) {
|
if (CHECKNULL(t, o) == NULL) {
|
||||||
PyErr_Print();
|
PyErr_Print();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
PyObject *pArgs = PyTuple_New(arity);
|
PyObject *pArgs = PyTuple_New(arity);
|
||||||
DebugPrintf("Tuple %p\n", pArgs);
|
DebugPrintf("Tuple %p\n", pArgs);
|
||||||
int i;
|
int i;
|
||||||
term_t tleft = PL_new_term_ref();
|
term_t tleft = PL_new_term_ref();
|
||||||
for (i = 0; i < arity; i++) {
|
for (i = 0; i < arity; i++) {
|
||||||
PyObject *pArg;
|
PyObject *pArg;
|
||||||
AOK(PL_get_arg(i + 1, t, tleft), NULL);
|
AOK(PL_get_arg(i + 1, t, tleft), NULL);
|
||||||
/* ignore (_) */
|
/* ignore (_) */
|
||||||
if (i == 0 && PL_is_variable(tleft)) {
|
if (i == 0 && PL_is_variable(tleft)) {
|
||||||
pArg = Py_None;
|
pArg = Py_None;
|
||||||
} else {
|
} else {
|
||||||
pArg = term_to_python(tleft, true, NULL);
|
pArg = term_to_python(tleft, true, NULL);
|
||||||
// PyObject_Print(pArg,fdopen(2,"w"),0);
|
// PyObject_Print(pArg,fdopen(2,"w"),0);
|
||||||
if (pArg == NULL) {
|
if (pArg == NULL) {
|
||||||
pArg = Py_None;
|
pArg = Py_None;
|
||||||
}
|
}
|
||||||
/* pArg reference stolen here: */
|
/* pArg reference stolen here: */
|
||||||
Py_INCREF(pArg);
|
Py_INCREF(pArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyTuple_SetItem(pArgs, i, pArg);
|
PyTuple_SetItem(pArgs, i, pArg);
|
||||||
}
|
}
|
||||||
if (!PyCallable_Check(o)) {
|
if (!PyCallable_Check(o)) {
|
||||||
return term_to_nametuple(s, arity, pArgs);
|
return term_to_nametuple(s, arity, pArgs);
|
||||||
}
|
}
|
||||||
PyObject *rc;
|
PyObject *rc;
|
||||||
|
|
||||||
// PyObject_Print(pArgs, stderr, 0);
|
// PyObject_Print(pArgs, stderr, 0);
|
||||||
// PyObject_Print(o, stderr, 0);
|
// PyObject_Print(o, stderr, 0);
|
||||||
CHECK_CALL(rc, t, PyObject_CallObject(o, pArgs));
|
CHECK_CALL(rc, t, PyObject_CallObject(o, pArgs));
|
||||||
Py_DECREF(pArgs);
|
Py_DECREF(pArgs);
|
||||||
Py_DECREF(o);
|
Py_DECREF(o);
|
||||||
DebugPrintf("CallObject %p\n", rc);
|
DebugPrintf("CallObject %p\n", rc);
|
||||||
|
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,8 +125,7 @@ static void add_modules(void) {
|
|||||||
py_Yapex = PyImport_AddModule("yap4py.yapi");
|
py_Yapex = PyImport_AddModule("yap4py.yapi");
|
||||||
if (py_Yapex)
|
if (py_Yapex)
|
||||||
Py_INCREF(py_Yapex);
|
Py_INCREF(py_Yapex);
|
||||||
//py_F2P = PyObject_GetAttrString(py_Yap, "globals");
|
py_F2P = PyDict_New();
|
||||||
py_F2P = NULL;
|
|
||||||
init_python_stream();
|
init_python_stream();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +75,8 @@ add_custom_target( YAP4PY_SETUP_DIRS
|
|||||||
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog/os
|
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog/os
|
||||||
)
|
)
|
||||||
|
|
||||||
add_custom_target( YAP4PY ALL
|
add_custom_target( YAP4PY ALL
|
||||||
|
COMMAND ${PYTHON_EXECUTABLE} -m pip uninstall -y YAP4PY
|
||||||
COMMAND ${SWIG_EXECUTABLE} -python -modern -c++ -py3 -DX_API -I${CMAKE_SOURCE_DIR}/CXX -I${CMAKE_SOURCE_DIR}/include -I${CMAKE_SOURCE_DIR}/H -I${CMAKE_SOURCE_DIR}/H/generated -I${CMAKE_SOURCE_DIR}/OPTYap -I../../.. -o yap_wrap.cpp yap.i
|
COMMAND ${SWIG_EXECUTABLE} -python -modern -c++ -py3 -DX_API -I${CMAKE_SOURCE_DIR}/CXX -I${CMAKE_SOURCE_DIR}/include -I${CMAKE_SOURCE_DIR}/H -I${CMAKE_SOURCE_DIR}/H/generated -I${CMAKE_SOURCE_DIR}/OPTYap -I../../.. -o yap_wrap.cpp yap.i
|
||||||
COMMAND ${PYTHON_EXECUTABLE} setup.py sdist bdist_wheel
|
COMMAND ${PYTHON_EXECUTABLE} setup.py sdist bdist_wheel
|
||||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||||
@ -83,8 +84,9 @@ DEPENDS YAP4PY_SETUP)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pip install --force --no-index -f dist yap4py
|
install(CODE "execute_process(
|
||||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})"
|
COMMAND ${PYTHON_EXECUTABLE} -m pip install --force --no-index -f packages/python/swig/dist YAP4PY
|
||||||
|
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})"
|
||||||
DEPENDS Py4YAP ${CMAKE_BINARY_DIR}/${YAP_STARTUP} ${dlls} )
|
DEPENDS Py4YAP ${CMAKE_BINARY_DIR}/${YAP_STARTUP} ${dlls} )
|
||||||
|
|
||||||
install(FILES ${PROLOG_SOURCES} DESTINATION ${libpl})
|
install(FILES ${PROLOG_SOURCES} DESTINATION ${libpl})
|
||||||
|
@ -7,11 +7,12 @@ from collections import namedtuple
|
|||||||
|
|
||||||
from yap import *
|
from yap import *
|
||||||
|
|
||||||
|
|
||||||
class Engine( YAPEngine ):
|
class Engine( YAPEngine ):
|
||||||
def __init__(self, args=None):
|
def __init__(self, args=None,**kwargs):
|
||||||
# type: (object) -> object
|
# type: (object) -> object
|
||||||
if not args:
|
if not args:
|
||||||
args = YAPEngineArgs()
|
args = EngineArgs(**kwargs)
|
||||||
yap_lib_path = os.path.dirname(__file__)
|
yap_lib_path = os.path.dirname(__file__)
|
||||||
args.setYapShareDir(os.path.join(yap_lib_path,"prolog"))
|
args.setYapShareDir(os.path.join(yap_lib_path,"prolog"))
|
||||||
args.setYapLibDir(yap_lib_path)
|
args.setYapLibDir(yap_lib_path)
|
||||||
@ -32,11 +33,55 @@ class Engine( YAPEngine ):
|
|||||||
|
|
||||||
class EngineArgs( YAPEngineArgs ):
|
class EngineArgs( YAPEngineArgs ):
|
||||||
""" Interface to Engine Options class"""
|
""" Interface to Engine Options class"""
|
||||||
|
def __init__(self, args=None,**kwargs):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
|
||||||
class Predicate( YAPPredicate ):
|
class Predicate( YAPPredicate ):
|
||||||
""" Interface to Generic Predicate"""
|
""" Interface to Generic Predicate"""
|
||||||
|
|
||||||
|
class Predicate:
|
||||||
|
"""Goal is a predicate instantiated under a specific environment """
|
||||||
|
def __init__( self, name, args, module=None, engine = None):
|
||||||
|
self = namedtuple( name, args )
|
||||||
|
if module:
|
||||||
|
self.p = YAPPredicate( name, len(self), module )
|
||||||
|
else:
|
||||||
|
self.p = YAPPredicate( name, len(self) )
|
||||||
|
self.e = engine
|
||||||
|
|
||||||
|
def goals( self, engine):
|
||||||
|
self.e = engine
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return PrologTableIter(self.e, self.p)
|
||||||
|
|
||||||
|
def holds(self):
|
||||||
|
return self.e.run(self._make_())
|
||||||
|
|
||||||
|
class PrologTableIter:
|
||||||
|
|
||||||
|
def __init__(self, e, goal):
|
||||||
|
try:
|
||||||
|
self.e = e
|
||||||
|
self.q = e.YAPQuery(goal)
|
||||||
|
except:
|
||||||
|
print('Error')
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
# Iterators are iterables too.
|
||||||
|
# Adding this functions to make them so.
|
||||||
|
return self
|
||||||
|
|
||||||
|
def next(self):
|
||||||
|
if self.q.next():
|
||||||
|
return goal
|
||||||
|
else:
|
||||||
|
self.q.close()
|
||||||
|
self.q = None
|
||||||
|
raise StopIteration()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class PrologPredicate( YAPPrologPredicate ):
|
class PrologPredicate( YAPPrologPredicate ):
|
||||||
""" Interface to Prolog Predicate"""
|
""" Interface to Prolog Predicate"""
|
||||||
@ -163,6 +208,17 @@ def live(**kwargs):
|
|||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
|
||||||
|
def boot_yap(**kwargs):
|
||||||
|
args = EngineArgs(**kwarg)
|
||||||
|
yap_lib_path = os.path.dirname(__file__)
|
||||||
|
args.setYapShareDir(os.path.join(yap_lib_path,"prolog"))
|
||||||
|
args.setYapLibDir(yap_lib_path)
|
||||||
|
args.setSavedState(os.path.join(yap_lib_path,"startup.yss"))
|
||||||
|
engine = YAPEngine(args)
|
||||||
|
engine.goal( set_prolog_flag('verbose', 'silent' ) )
|
||||||
|
engine.goal( use_module(library('yapi') ) )
|
||||||
|
return engine
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
engine = boot_yap()
|
engine = boot_yap()
|
||||||
handler = numbervars
|
handler = numbervars
|
||||||
|
Reference in New Issue
Block a user