new version

This commit is contained in:
Vitor Santos Costa 2016-08-18 02:13:55 -05:00
parent 670eacc823
commit b27ab0811b
8 changed files with 58 additions and 46 deletions

View File

@ -68,4 +68,6 @@ add_custom_target ( YAPex ALL
install(CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} install -f
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})")
add_subdirectory(yap_kernel)
endif (PYTHONLIBS_FOUND)

View File

@ -501,7 +501,8 @@ static long get_len_of_range(long lo, long hi, long step) {
return n;
}
static PyStructSequence_Field pnull[] = {
#if PY_MAJOR_VERSION >= 3
static PyStructSequence_Field pnull[] = {
{"A1", NULL}, {"A2", NULL}, {"A3", NULL}, {"A4", NULL},
{"A5", NULL}, {"A6", NULL}, {"A7", NULL}, {"A8", NULL},
{"A9", NULL}, {"A9", NULL}, {"A10", NULL}, {"A11", NULL},
@ -688,6 +689,8 @@ term_t tleft = PL_new_term_ref();
return o;
}
#endif
static PyObject *bip_range(term_t t) {
long ilow = 0, ihigh = 0, istep = 1;
long bign;
@ -948,12 +951,12 @@ PyObject *compound_to_pytree(term_t t, functor_t fun) {
// this should never happen
return term_to_python( t, false);
} else {
const char *s;
#if PY_MAJOR_VERSION >= 3
const char *s;
if (!(s = PL_atom_chars(name)))
return NULL;
return term_to_nametuple(s, arity, t);
}
#if 0
#else
PyObject *c, *o1;
o = PyTuple_New(arity);
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);
return o1;
#endif
}
}
PyObject *compound_to_pyeval(term_t t, functor_t fun) {

View File

@ -594,7 +594,6 @@ static foreign_t python_run_script(term_t cmd, term_t fun) {
Py_DECREF(pModule);
} else {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", s);
return false;
}
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) {
char *s;
size_t len;
PyObject *pName, *pModule;
term_t arg = PL_new_term_ref();
char s0[MAXPATHLEN], *s = s0;
if (PL_is_list(mname)) {
char *s2;
char str[256];
term_t arg = PL_new_term_ref();
if (!PL_get_arg(1, mname, arg) || !PL_get_atom_chars(arg, &s) ||
!PL_get_arg(2, mname, mname) || !PL_get_arg(1, mname, arg) ||
!PL_get_atom_chars(arg, &s2))
return FALSE;
strcpy(str, s);
strcat(str, ".");
strcat(str, s2);
s = str;
} else if (!PL_get_nchars(mname, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
return FALSE;
while (true) {
size_t len;
len = (MAXPATHLEN - 1) - (s - s0);
if (PL_is_pair(mname)) {
char *sa;
if (!PL_get_arg(1, mname, arg) || !PL_get_atom_chars(arg, &sa) ||
!PL_get_arg(2, mname, mname))
return false;
s = stpcpy(s, sa);
*s++ = '.';
} else if (!PL_get_nchars(mname, &len, &s,
CVT_ALL | CVT_EXCEPTION | ENC_ISO_UTF8)) {
return false;
} else {
break;
}
}
#if PY_MAJOR_VERSION < 3
pName = PyString_FromString(s);
pName = PyString_FromString(s0);
#else
printf("Module=%s\n", s);
pName = PyUnicode_FromString(s);
pName = PyUnicode_FromString(s0);
#endif
if (pName == NULL) {
return FALSE;
return false;
}
pModule = PyImport_Import(pName);
Py_DECREF(pName);

View File

@ -7,6 +7,7 @@
#undef HAVE_STATa
#endif
#include <assert.h>
#define EXTRA_MESSSAGES 1
//@{

View File

@ -29,8 +29,8 @@
python/2,
(:=)/2,
(:=)/1,
(<-)/2,
(<-)/1,
% (<-)/2,
% (<-)/1,
op(100,fy,$),
op(950,fy,:=),
op(950,yfx,:=),
@ -110,6 +110,7 @@ Data types are
:- use_module(library(charsio)).
:- dynamic python_mref_cache/2, python_obj_cache/2.
:= import( F ) :- python_import(F).
:= F :- python(F,_).
V := F :- var(V), !, python(F,V0),
@ -131,12 +132,12 @@ A := F :-
python_exports(V0,V).
python_exports(V0, V0) :-
var(V0), !.
var(V0), !.
python_exports(V0, V0) :-
atomic(V0), !.
atomic(V0), !.
python_exports('__obj__'(T0), T) :-
!,
python_export('__obj__'(T0), T).
python_export('__obj__'(T0), T).
python_exports(V0, VF) :-
V0 =.. [F|L],
maplist(python_exports, L, LF),
@ -423,7 +424,6 @@ python_assign_field(C1.E, Obj) :-
atom(E),
!,
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 ).

View File

@ -41,7 +41,7 @@ if svem_flag in sys.argv:
setup(name='yap_kernel',
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',
long_description="A simple YAP kernel for Jupyter/IPython, based on MetaKernel",
url="https://github.com/vscosta/yap-6.3",

View File

@ -50,6 +50,7 @@ class MetaKernelyap(MetaKernel):
self.engine = yap.YAPEngine()
self.q = None
self.engine.query("load_files(library(python), [])").command()
self.engine.query("load_files(library(jupyter), [])").command()
banner = "YAP {0} Kernel".format(self.engine.version())
self.olines = banner
finally:
@ -66,13 +67,13 @@ class MetaKernelyap(MetaKernel):
if not self.q:
self.q = self.engine.query(s)
if self.q.next():
vs = self.q.namedVarsCopy()
myvs = self.q.namedVarsCopy()
wrote = False
if vs:
if myvs:
i = 0
for eq in vs:
name = eq[0]
bind = eq[1]
for peq in myvs:
name = peq[0]
bind = peq[1]
if bind.isVar():
var = yap.YAPAtom('$VAR')
f = yap.YAPFunctor(var, 1)
@ -119,7 +120,7 @@ class MetaKernelyap(MetaKernel):
self.ask = False
self.doReset = False
break
else:
else:
line = line.rstrip()
if line:
nlines += line + "\n"
@ -138,7 +139,7 @@ class MetaKernelyap(MetaKernel):
self.doReset = False
else:
self.ask = False
self.doReset = False
self.doReset = False
self.query_prolog( nlines )
while not self.ask and self.q:
self.query_prolog( nlines )
@ -158,7 +159,9 @@ class MetaKernelyap(MetaKernel):
raise
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]
# default = {'matches': [], 'cursor_start': 0,
# 'cursor_end': cursor_pos, 'metadata': dict(),
@ -177,7 +180,8 @@ class MetaKernelyap(MetaKernel):
# if token[0] == '$':
# # 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()
# completions = set(output.split())
# # append matches including leading $
@ -187,7 +191,7 @@ class MetaKernelyap(MetaKernel):
# cmd = 'compgen -cdfa %s' % token
# output = self.bashwrapper.run_command(cmd).rstrip()
# matches.extend(output.split())
# if not matches:
# return default
# matches = [m for m in matches if m.startswith(token)]
@ -196,9 +200,6 @@ class MetaKernelyap(MetaKernel):
# 'cursor_end': cursor_pos, 'metadata': dict(),
# 'status': 'ok'}
def repr(self, data):
return repr(data)

View File

@ -1,7 +1,10 @@
import yap
import sys
# this class is not being used
# we rely on namedtuples instead.
class T(tuple):
"""Represents a non-interned Prolog atom"""