new version
This commit is contained in:
parent
670eacc823
commit
b27ab0811b
@ -68,4 +68,6 @@ add_custom_target ( YAPex ALL
|
|||||||
install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} install -f
|
install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} install -f
|
||||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})")
|
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})")
|
||||||
|
|
||||||
|
add_subdirectory(yap_kernel)
|
||||||
|
|
||||||
endif (PYTHONLIBS_FOUND)
|
endif (PYTHONLIBS_FOUND)
|
||||||
|
@ -501,7 +501,8 @@ static long get_len_of_range(long lo, long hi, long step) {
|
|||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyStructSequence_Field pnull[] = {
|
#if PY_MAJOR_VERSION >= 3
|
||||||
|
static PyStructSequence_Field pnull[] = {
|
||||||
{"A1", NULL}, {"A2", NULL}, {"A3", NULL}, {"A4", NULL},
|
{"A1", NULL}, {"A2", NULL}, {"A3", NULL}, {"A4", NULL},
|
||||||
{"A5", NULL}, {"A6", NULL}, {"A7", NULL}, {"A8", NULL},
|
{"A5", NULL}, {"A6", NULL}, {"A7", NULL}, {"A8", NULL},
|
||||||
{"A9", NULL}, {"A9", NULL}, {"A10", NULL}, {"A11", NULL},
|
{"A9", NULL}, {"A9", NULL}, {"A10", NULL}, {"A11", NULL},
|
||||||
@ -688,6 +689,8 @@ term_t tleft = PL_new_term_ref();
|
|||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#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;
|
||||||
@ -948,12 +951,12 @@ PyObject *compound_to_pytree(term_t t, functor_t fun) {
|
|||||||
// this should never happen
|
// this should never happen
|
||||||
return term_to_python( t, false);
|
return term_to_python( t, false);
|
||||||
} else {
|
} else {
|
||||||
const char *s;
|
#if PY_MAJOR_VERSION >= 3
|
||||||
|
const char *s;
|
||||||
if (!(s = PL_atom_chars(name)))
|
if (!(s = PL_atom_chars(name)))
|
||||||
return NULL;
|
return NULL;
|
||||||
return term_to_nametuple(s, arity, t);
|
return term_to_nametuple(s, arity, t);
|
||||||
}
|
#else
|
||||||
#if 0
|
|
||||||
PyObject *c, *o1;
|
PyObject *c, *o1;
|
||||||
o = PyTuple_New(arity);
|
o = PyTuple_New(arity);
|
||||||
tleft = PL_new_term_ref();
|
tleft = PL_new_term_ref();
|
||||||
@ -974,6 +977,7 @@ PyObject *compound_to_pytree(term_t t, functor_t fun) {
|
|||||||
PyTuple_SET_ITEM(o1, 1, o);
|
PyTuple_SET_ITEM(o1, 1, o);
|
||||||
return o1;
|
return o1;
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *compound_to_pyeval(term_t t, functor_t fun) {
|
PyObject *compound_to_pyeval(term_t t, functor_t fun) {
|
||||||
|
@ -594,7 +594,6 @@ static foreign_t python_run_script(term_t cmd, term_t fun) {
|
|||||||
Py_DECREF(pModule);
|
Py_DECREF(pModule);
|
||||||
} else {
|
} else {
|
||||||
PyErr_Print();
|
PyErr_Print();
|
||||||
fprintf(stderr, "Failed to load \"%s\"\n", s);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -620,33 +619,35 @@ static foreign_t python_export(term_t t, term_t pl) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int python_import(term_t mname, term_t mod) {
|
static int python_import(term_t mname, term_t mod) {
|
||||||
char *s;
|
|
||||||
size_t len;
|
|
||||||
PyObject *pName, *pModule;
|
PyObject *pName, *pModule;
|
||||||
|
term_t arg = PL_new_term_ref();
|
||||||
|
char s0[MAXPATHLEN], *s = s0;
|
||||||
|
|
||||||
if (PL_is_list(mname)) {
|
while (true) {
|
||||||
char *s2;
|
size_t len;
|
||||||
char str[256];
|
|
||||||
term_t arg = PL_new_term_ref();
|
len = (MAXPATHLEN - 1) - (s - s0);
|
||||||
if (!PL_get_arg(1, mname, arg) || !PL_get_atom_chars(arg, &s) ||
|
if (PL_is_pair(mname)) {
|
||||||
!PL_get_arg(2, mname, mname) || !PL_get_arg(1, mname, arg) ||
|
char *sa;
|
||||||
!PL_get_atom_chars(arg, &s2))
|
if (!PL_get_arg(1, mname, arg) || !PL_get_atom_chars(arg, &sa) ||
|
||||||
return FALSE;
|
!PL_get_arg(2, mname, mname))
|
||||||
strcpy(str, s);
|
return false;
|
||||||
strcat(str, ".");
|
s = stpcpy(s, sa);
|
||||||
strcat(str, s2);
|
*s++ = '.';
|
||||||
s = str;
|
} else if (!PL_get_nchars(mname, &len, &s,
|
||||||
} else if (!PL_get_nchars(mname, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
|
CVT_ALL | CVT_EXCEPTION | ENC_ISO_UTF8)) {
|
||||||
return FALSE;
|
return false;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_MAJOR_VERSION < 3
|
||||||
pName = PyString_FromString(s);
|
pName = PyString_FromString(s0);
|
||||||
#else
|
#else
|
||||||
printf("Module=%s\n", s);
|
pName = PyUnicode_FromString(s0);
|
||||||
pName = PyUnicode_FromString(s);
|
|
||||||
#endif
|
#endif
|
||||||
if (pName == NULL) {
|
if (pName == NULL) {
|
||||||
return FALSE;
|
return false;
|
||||||
}
|
}
|
||||||
pModule = PyImport_Import(pName);
|
pModule = PyImport_Import(pName);
|
||||||
Py_DECREF(pName);
|
Py_DECREF(pName);
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#undef HAVE_STATa
|
#undef HAVE_STATa
|
||||||
#endif
|
#endif
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#define EXTRA_MESSSAGES 1
|
||||||
|
|
||||||
//@{
|
//@{
|
||||||
|
|
||||||
|
@ -29,8 +29,8 @@
|
|||||||
python/2,
|
python/2,
|
||||||
(:=)/2,
|
(:=)/2,
|
||||||
(:=)/1,
|
(:=)/1,
|
||||||
(<-)/2,
|
% (<-)/2,
|
||||||
(<-)/1,
|
% (<-)/1,
|
||||||
op(100,fy,$),
|
op(100,fy,$),
|
||||||
op(950,fy,:=),
|
op(950,fy,:=),
|
||||||
op(950,yfx,:=),
|
op(950,yfx,:=),
|
||||||
@ -110,6 +110,7 @@ Data types are
|
|||||||
:- use_module(library(charsio)).
|
:- use_module(library(charsio)).
|
||||||
:- dynamic python_mref_cache/2, python_obj_cache/2.
|
:- dynamic python_mref_cache/2, python_obj_cache/2.
|
||||||
|
|
||||||
|
:= import( F ) :- python_import(F).
|
||||||
:= F :- python(F,_).
|
:= F :- python(F,_).
|
||||||
|
|
||||||
V := F :- var(V), !, python(F,V0),
|
V := F :- var(V), !, python(F,V0),
|
||||||
@ -131,12 +132,12 @@ A := F :-
|
|||||||
python_exports(V0,V).
|
python_exports(V0,V).
|
||||||
|
|
||||||
python_exports(V0, V0) :-
|
python_exports(V0, V0) :-
|
||||||
var(V0), !.
|
var(V0), !.
|
||||||
python_exports(V0, V0) :-
|
python_exports(V0, V0) :-
|
||||||
atomic(V0), !.
|
atomic(V0), !.
|
||||||
python_exports('__obj__'(T0), T) :-
|
python_exports('__obj__'(T0), T) :-
|
||||||
!,
|
!,
|
||||||
python_export('__obj__'(T0), T).
|
python_export('__obj__'(T0), T).
|
||||||
python_exports(V0, VF) :-
|
python_exports(V0, VF) :-
|
||||||
V0 =.. [F|L],
|
V0 =.. [F|L],
|
||||||
maplist(python_exports, L, LF),
|
maplist(python_exports, L, LF),
|
||||||
@ -423,7 +424,6 @@ python_assign_field(C1.E, Obj) :-
|
|||||||
atom(E),
|
atom(E),
|
||||||
!,
|
!,
|
||||||
python_eval_term(C1, O1),
|
python_eval_term(C1, O1),
|
||||||
python_assign_field(O1, E, Obj ).
|
python_assign_field(O1, E, Obj ).
|
||||||
|
|
||||||
:- initialization( use_foreign_library(foreign(libpython), init_python), now ).
|
:- initialization( use_foreign_library(foreign(libpython), init_python), now ).
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ if svem_flag in sys.argv:
|
|||||||
|
|
||||||
setup(name='yap_kernel',
|
setup(name='yap_kernel',
|
||||||
version='0.0.1',
|
version='0.0.1',
|
||||||
package_dir = {'': '${CMAKE_SOURCE_DIR}/packages/python' },
|
package_dir = {'': '${CMAKE_SOURCE_DIR}/packages/python/yap_kernel' },
|
||||||
description='A simple YAP kernel for Jupyter/IPython',
|
description='A simple YAP kernel for Jupyter/IPython',
|
||||||
long_description="A simple YAP kernel for Jupyter/IPython, based on MetaKernel",
|
long_description="A simple YAP kernel for Jupyter/IPython, based on MetaKernel",
|
||||||
url="https://github.com/vscosta/yap-6.3",
|
url="https://github.com/vscosta/yap-6.3",
|
||||||
|
@ -50,6 +50,7 @@ class MetaKernelyap(MetaKernel):
|
|||||||
self.engine = yap.YAPEngine()
|
self.engine = yap.YAPEngine()
|
||||||
self.q = None
|
self.q = None
|
||||||
self.engine.query("load_files(library(python), [])").command()
|
self.engine.query("load_files(library(python), [])").command()
|
||||||
|
self.engine.query("load_files(library(jupyter), [])").command()
|
||||||
banner = "YAP {0} Kernel".format(self.engine.version())
|
banner = "YAP {0} Kernel".format(self.engine.version())
|
||||||
self.olines = banner
|
self.olines = banner
|
||||||
finally:
|
finally:
|
||||||
@ -66,13 +67,13 @@ class MetaKernelyap(MetaKernel):
|
|||||||
if not self.q:
|
if not self.q:
|
||||||
self.q = self.engine.query(s)
|
self.q = self.engine.query(s)
|
||||||
if self.q.next():
|
if self.q.next():
|
||||||
vs = self.q.namedVarsCopy()
|
myvs = self.q.namedVarsCopy()
|
||||||
wrote = False
|
wrote = False
|
||||||
if vs:
|
if myvs:
|
||||||
i = 0
|
i = 0
|
||||||
for eq in vs:
|
for peq in myvs:
|
||||||
name = eq[0]
|
name = peq[0]
|
||||||
bind = eq[1]
|
bind = peq[1]
|
||||||
if bind.isVar():
|
if bind.isVar():
|
||||||
var = yap.YAPAtom('$VAR')
|
var = yap.YAPAtom('$VAR')
|
||||||
f = yap.YAPFunctor(var, 1)
|
f = yap.YAPFunctor(var, 1)
|
||||||
@ -119,7 +120,7 @@ class MetaKernelyap(MetaKernel):
|
|||||||
self.ask = False
|
self.ask = False
|
||||||
self.doReset = False
|
self.doReset = False
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
line = line.rstrip()
|
line = line.rstrip()
|
||||||
if line:
|
if line:
|
||||||
nlines += line + "\n"
|
nlines += line + "\n"
|
||||||
@ -138,7 +139,7 @@ class MetaKernelyap(MetaKernel):
|
|||||||
self.doReset = False
|
self.doReset = False
|
||||||
else:
|
else:
|
||||||
self.ask = False
|
self.ask = False
|
||||||
self.doReset = False
|
self.doReset = False
|
||||||
self.query_prolog( nlines )
|
self.query_prolog( nlines )
|
||||||
while not self.ask and self.q:
|
while not self.ask and self.q:
|
||||||
self.query_prolog( nlines )
|
self.query_prolog( nlines )
|
||||||
@ -158,7 +159,9 @@ class MetaKernelyap(MetaKernel):
|
|||||||
raise
|
raise
|
||||||
|
|
||||||
def do_complete(self, code, cursor_pos):
|
def do_complete(self, code, cursor_pos):
|
||||||
eprint( code, " -- ", str(cursor_pos) )
|
print(code)
|
||||||
|
print(cursor_pos)
|
||||||
|
eprint( code, " -- ", str(cursor_pos ) )
|
||||||
# code = code[:cursor_pos]
|
# code = code[:cursor_pos]
|
||||||
# default = {'matches': [], 'cursor_start': 0,
|
# default = {'matches': [], 'cursor_start': 0,
|
||||||
# 'cursor_end': cursor_pos, 'metadata': dict(),
|
# 'cursor_end': cursor_pos, 'metadata': dict(),
|
||||||
@ -177,7 +180,8 @@ class MetaKernelyap(MetaKernel):
|
|||||||
|
|
||||||
# if token[0] == '$':
|
# if token[0] == '$':
|
||||||
# # complete variables
|
# # complete variables
|
||||||
# cmd = 'compgen -A arrayvar -A export -A variable %s' % token[1:] # strip leading $
|
# cmd = 'compgen -A arrayvar -A export \
|
||||||
|
# -A variable %s' % token[1:] # strip leading $
|
||||||
# output = self.bashwrapper.run_command(cmd).rstrip()
|
# output = self.bashwrapper.run_command(cmd).rstrip()
|
||||||
# completions = set(output.split())
|
# completions = set(output.split())
|
||||||
# # append matches including leading $
|
# # append matches including leading $
|
||||||
@ -187,7 +191,7 @@ class MetaKernelyap(MetaKernel):
|
|||||||
# cmd = 'compgen -cdfa %s' % token
|
# cmd = 'compgen -cdfa %s' % token
|
||||||
# output = self.bashwrapper.run_command(cmd).rstrip()
|
# output = self.bashwrapper.run_command(cmd).rstrip()
|
||||||
# matches.extend(output.split())
|
# matches.extend(output.split())
|
||||||
|
|
||||||
# if not matches:
|
# if not matches:
|
||||||
# return default
|
# return default
|
||||||
# matches = [m for m in matches if m.startswith(token)]
|
# matches = [m for m in matches if m.startswith(token)]
|
||||||
@ -196,9 +200,6 @@ class MetaKernelyap(MetaKernel):
|
|||||||
# 'cursor_end': cursor_pos, 'metadata': dict(),
|
# 'cursor_end': cursor_pos, 'metadata': dict(),
|
||||||
# 'status': 'ok'}
|
# 'status': 'ok'}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def repr(self, data):
|
def repr(self, data):
|
||||||
return repr(data)
|
return repr(data)
|
||||||
|
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
import yap
|
import yap
|
||||||
|
import sys
|
||||||
|
|
||||||
# this class is not being used
|
# this class is not being used
|
||||||
# we rely on namedtuples instead.
|
# we rely on namedtuples instead.
|
||||||
|
|
||||||
|
|
||||||
class T(tuple):
|
class T(tuple):
|
||||||
|
|
||||||
"""Represents a non-interned Prolog atom"""
|
"""Represents a non-interned Prolog atom"""
|
||||||
|
Reference in New Issue
Block a user