Merge ssh://ssh.dcc.fc.up.pt:31064/home/vsc/yap
This commit is contained in:
commit
ba00e98ad8
11
C/atomic.c
11
C/atomic.c
@ -592,8 +592,9 @@ restart_aux:
|
||||
|
||||
|
||||
The predicate holds when at least one of the arguments is
|
||||
ground (otherwise, YAP will generate an error event. _A_ must be unifiable with an atom, and the
|
||||
argument _L_ with the list of the character codes for string _A_.
|
||||
ground (otherwise, YAP will generate an error event. _A_ must be unifiable
|
||||
with an atom, and the argument _L_ with the list of the character codes for
|
||||
string _A_.
|
||||
|
||||
|
||||
*/
|
||||
@ -1465,9 +1466,7 @@ error:
|
||||
if (LOCAL_Error_TYPE && Yap_HandleError("atom_concat/3")) {
|
||||
goto restart_aux;
|
||||
}
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
{ return FALSE; }
|
||||
}
|
||||
|
||||
static Int atomics_to_string2(USES_REGS1) {
|
||||
@ -2766,6 +2765,8 @@ void Yap_InitAtomPreds(void) {
|
||||
Yap_InitCPred("downcase_atom", 2, downcase_text_to_atom, 0);
|
||||
Yap_InitCPred("upcase_text_to_atom", 2, upcase_text_to_atom, 0);
|
||||
Yap_InitCPred("upcase_atom", 2, upcase_text_to_atom, 0);
|
||||
Yap_InitCPred("text_to_string", 2, downcase_text_to_string, 0);
|
||||
Yap_InitCPred("text_to_atom", 2, downcase_text_to_string, 0);
|
||||
Yap_InitCPred("downcase_text_to_string", 2, downcase_text_to_string, 0);
|
||||
Yap_InitCPred("upcase_text_to_string", 2, upcase_text_to_string, 0);
|
||||
Yap_InitCPred("downcase_text_to_codes", 2, downcase_text_to_codes, 0);
|
||||
|
3
C/exec.c
3
C/exec.c
@ -2116,7 +2116,8 @@ static Int jump_env(USES_REGS1) {
|
||||
}
|
||||
// Yap_DebugPlWriteln(t);
|
||||
// char *buf = Yap_TermToBuffer(t, ENC_ISO_UTF8,
|
||||
// Quote_illegal_f | Ignore_ops_f | Unfold_cyclics_f);
|
||||
// Quote_illegal_f | Ignore_ops_f |
|
||||
// Unfold_cyclics_f);
|
||||
// __android_log_print(ANDROID_LOG_INFO, "YAPDroid ", " throw(%s)", buf);
|
||||
LOCAL_ActiveError = Yap_UserError(t0, LOCAL_ActiveError);
|
||||
bool out = JumpToEnv(PASS_REGS1);
|
||||
|
@ -70,7 +70,6 @@ Moyle. All rights reserved.
|
||||
|
||||
static atom_t ATOM_nil;
|
||||
|
||||
extern int PL_unify_termv(term_t l, va_list args);
|
||||
|
||||
extern X_API Atom YAP_AtomFromSWIAtom(atom_t at);
|
||||
extern X_API atom_t YAP_SWIAtomFromAtom(Atom at);
|
||||
|
26
os/iopreds.c
26
os/iopreds.c
@ -1129,12 +1129,13 @@ static void check_bom(int sno, StreamDesc *st) {
|
||||
bool Yap_initStream(int sno, FILE *fd, const char *name, const char *io_mode,
|
||||
Term file_name, encoding_t encoding, stream_flags_t flags,
|
||||
void *vfs) {
|
||||
fprintf(stderr,"+ %s --> %d\n", name, sno);
|
||||
// fprintf(stderr,"+ %s --> %d\n", name, sno);
|
||||
StreamDesc *st = &GLOBAL_Stream[sno];
|
||||
__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "init %s %s:%s stream <%d>",
|
||||
io_mode, CurrentModule == 0? "prolog": RepAtom(AtomOfTerm(CurrentModule))->StrOfAE,
|
||||
name,
|
||||
sno);
|
||||
__android_log_print(
|
||||
ANDROID_LOG_INFO, "YAPDroid", "init %s %s:%s stream <%d>", io_mode,
|
||||
CurrentModule == 0 ? "prolog"
|
||||
: RepAtom(AtomOfTerm(CurrentModule))->StrOfAE,
|
||||
name, sno);
|
||||
if (io_mode == NULL)
|
||||
Yap_Error(PERMISSION_ERROR_NEW_ALIAS_FOR_STREAM, MkIntegerTerm(sno),
|
||||
"File opened with NULL Permissions");
|
||||
@ -1232,14 +1233,11 @@ typedef enum open_enum_choices { OPEN_DEFS() } open_choices_t;
|
||||
static const param_t open_defs[] = {OPEN_DEFS()};
|
||||
#undef PAR
|
||||
|
||||
|
||||
static bool fill_stream(int sno, StreamDesc *st, Term tin, const char *io_mode, Term user_name,
|
||||
encoding_t enc)
|
||||
{
|
||||
static bool fill_stream(int sno, StreamDesc *st, Term tin, const char *io_mode,
|
||||
Term user_name, encoding_t enc) {
|
||||
struct vfs *vfsp = NULL;
|
||||
const char *fname;
|
||||
|
||||
|
||||
if (IsAtomTerm(tin))
|
||||
fname = RepAtom(AtomOfTerm(tin))->StrOfAE;
|
||||
else if (IsStringTerm(tin))
|
||||
@ -1292,7 +1290,8 @@ static bool fill_stream(int sno, StreamDesc *st, Term tin, const char *io_mode,
|
||||
buf = pop_output_text_stack(i, buf);
|
||||
Atom nat = Yap_LookupAtom(Yap_StrPrefix(buf, 32));
|
||||
sno = Yap_open_buf_read_stream(buf, strlen(buf) + 1, &LOCAL_encoding,
|
||||
MEM_BUF_MALLOC, nat, MkAtomTerm(NameOfFunctor(f)));
|
||||
MEM_BUF_MALLOC, nat,
|
||||
MkAtomTerm(NameOfFunctor(f)));
|
||||
return Yap_OpenBufWriteStream(PASS_REGS1);
|
||||
}
|
||||
} else if (!strcmp(RepAtom(NameOfFunctor(f))->StrOfAE, "popen")) {
|
||||
@ -1365,8 +1364,8 @@ static Int do_open(Term file_name, Term t2, Term tlist USES_REGS) {
|
||||
open_mode = AtomOfTerm(t2);
|
||||
}
|
||||
/* get options */
|
||||
xarg *args = Yap_ArgListToVector(tlist, open_defs, OPEN_END,
|
||||
DOMAIN_ERROR_OPEN_OPTION);
|
||||
xarg *args =
|
||||
Yap_ArgListToVector(tlist, open_defs, OPEN_END, DOMAIN_ERROR_OPEN_OPTION);
|
||||
if (args == NULL) {
|
||||
if (LOCAL_Error_TYPE != YAP_NO_ERROR) {
|
||||
Yap_Error(LOCAL_Error_TYPE, tlist, "option handling in open/3");
|
||||
@ -1671,7 +1670,6 @@ int Yap_OpenStream(Term tin, const char *io_mode, Term user_name,
|
||||
st = GLOBAL_Stream + sno;
|
||||
// fname = Yap_VF(fname);
|
||||
|
||||
|
||||
if (fill_stream(sno, st, tin, io_mode, user_name, enc))
|
||||
return sno;
|
||||
return -1;
|
||||
|
@ -998,7 +998,7 @@ static void CloseStream(int sno) {
|
||||
// __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "close stream <%d>",
|
||||
// sno);
|
||||
VFS_t *me;
|
||||
fprintf( stderr, "- %d\n",sno);
|
||||
//fprintf( stderr, "- %d\n",sno);
|
||||
if ((me = GLOBAL_Stream[sno].vfs) != NULL &&
|
||||
GLOBAL_Stream[sno].file == NULL) {
|
||||
if (me->close) {
|
||||
|
@ -50,7 +50,7 @@ foreign_t assign_to_symbol(term_t t, PyObject *e) {
|
||||
return PyObject_SetAttrString(dic, s, e) == 0;
|
||||
}
|
||||
|
||||
foreign_t python_to_term(PyObject *pVal, term_t t) {
|
||||
static int python_to_term__(PyObject *pVal, term_t t) {
|
||||
bool rc = true;
|
||||
term_t to = PL_new_term_ref();
|
||||
// fputs(" <<*** ",stderr); PyObject_Print(pVal,stderr,0);
|
||||
@ -92,7 +92,10 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
|
||||
// if (PyDict_GetItemString(py_Atoms, s))
|
||||
// rc = rc && PL_unify_atom_chars(t, s);
|
||||
// else
|
||||
rc = rc && PL_unify_atom_chars(t, s);
|
||||
rc =
|
||||
rc && (PL_is_string(t) ? PL_unify_string_chars(t, s)
|
||||
: PL_is_variable(t) ? PL_unify_atom_chars(t, s)
|
||||
: PL_unify_atom_chars(t, s));
|
||||
} else if (PyByteArray_Check(pVal)) {
|
||||
rc = rc && PL_unify_string_chars(t, PyByteArray_AsString(pVal));
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
@ -137,7 +140,7 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
|
||||
PyErr_Clear();
|
||||
p = Py_None;
|
||||
}
|
||||
rc = rc && python_to_term(p, to);
|
||||
rc = rc && python_to_term__(p, to);
|
||||
}
|
||||
} else {
|
||||
rc = false;
|
||||
@ -154,7 +157,7 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
|
||||
if ((obj = PyList_GetItem(pVal, i)) == NULL) {
|
||||
obj = Py_None;
|
||||
}
|
||||
rc = rc && python_to_term(obj, to);
|
||||
rc = rc && python_to_term__(obj, to);
|
||||
if (!rc)
|
||||
return false;
|
||||
}
|
||||
@ -174,10 +177,10 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
|
||||
term_t tkey = PL_new_term_ref(), tval = PL_new_term_ref(), tint,
|
||||
tnew = PL_new_term_ref();
|
||||
/* do something interesting with the values... */
|
||||
if (!python_to_term(key, tkey)) {
|
||||
if (!python_to_term__(key, tkey)) {
|
||||
continue;
|
||||
}
|
||||
if (!python_to_term(value, tval)) {
|
||||
if (!python_to_term__(value, tval)) {
|
||||
continue;
|
||||
}
|
||||
/* reuse */
|
||||
@ -209,7 +212,7 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
|
||||
X_API YAP_Term pythonToYAP(PyObject *pVal) {
|
||||
|
||||
term_t t = PL_new_term_ref();
|
||||
if (pVal == NULL || !python_to_term(pVal, t)) {
|
||||
if (pVal == NULL || !python_to_term__(pVal, t)) {
|
||||
PL_reset_term_refs(t);
|
||||
return 0;
|
||||
}
|
||||
@ -221,6 +224,14 @@ X_API YAP_Term pythonToYAP(PyObject *pVal) {
|
||||
|
||||
PyObject *py_Local, *py_Global;
|
||||
|
||||
X_API foreign_t python_to_term(PyObject *pVal, term_t t) {
|
||||
yap_error_descriptor_t *ctx = malloc(sizeof(yap_error_descriptor_t));
|
||||
bool newxp = Yap_pushErrorContext(true, ctx);
|
||||
int rc = python_to_term(pVal, t);
|
||||
Yap_popErrorContext(newxp, true);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* assigns the Python RHS to a Prolog term LHS, ie LHS = RHS
|
||||
*
|
||||
|
@ -23,8 +23,12 @@
|
||||
|
||||
:- python_import(sys).
|
||||
|
||||
jupyter_query(Caller, Cell, Line ) :-
|
||||
jupyter_cell(Caller, Cell, Line).
|
||||
jupyter_query(Caller, Prog, Query ) :-
|
||||
catch(
|
||||
jupyter_cell(Caller, Prog, Query),
|
||||
E,
|
||||
'$Error'(E, top)
|
||||
).
|
||||
|
||||
jupyter_cell(_Caller, Cell, _Line) :-
|
||||
jupyter_consult(Cell), %stack_dump,
|
||||
@ -33,8 +37,8 @@ jupyter_cell( _Caller, _, '' ) :- !.
|
||||
jupyter_cell( _Caller, _, Line ) :-
|
||||
blank( Line ),
|
||||
!.
|
||||
jupyter_cell( Caller, _, Line ) :-
|
||||
Self := Caller.query,
|
||||
jupyter_cell( Self, _, Line ) :-
|
||||
%Self := Caller.query,
|
||||
python_query(Self,Line).
|
||||
|
||||
restreams(call) :-
|
||||
@ -52,143 +56,77 @@ jupyter_consult(Text) :-
|
||||
blank( Text ),
|
||||
!.
|
||||
jupyter_consult(Cell) :-
|
||||
% Name = 'Inp',
|
||||
% stream_property(Stream, file_name(Name) ),
|
||||
% setup_call_cleanup(
|
||||
open_mem_read_stream( Cell, Stream),
|
||||
load_files(user:'jupyter cell',[stream(Stream)]).
|
||||
|
||||
blank(Text) :-
|
||||
atom(Text),
|
||||
!,
|
||||
atom_codes(Text, L),
|
||||
maplist( code_type(space), L).
|
||||
blank(Text) :-
|
||||
string(Text),
|
||||
!,
|
||||
string_codes(Text, L),
|
||||
maplist( code_type(space), L).
|
||||
|
||||
:- dynamic cell_stream/1.
|
||||
|
||||
streams(false) :-
|
||||
nb_setval(jupyter_cell, false),
|
||||
retract(cell_stream(S)),
|
||||
close(S),
|
||||
fail.
|
||||
streams(false).
|
||||
close(user_input),
|
||||
close(user_output),
|
||||
close(user_error).
|
||||
streams(true) :-
|
||||
streams( false ),
|
||||
nb_setval(jupyter_cell, true),
|
||||
% \+ current_stream('/python/input',_,_),
|
||||
open('/python/input', read, Input, [alias(user_input),bom(false),script(false)]),
|
||||
assert( cell_stream( Input) ),
|
||||
set_prolog_flag(user_input,Input),
|
||||
fail.
|
||||
streams(true) :-
|
||||
% \+ current_stream('/python/sys.stdout',_,_),
|
||||
open('/python/sys.stdout', append, Output, [alias(user_output)]),
|
||||
set_prolog_flag(user_output, Output),
|
||||
assert( cell_stream( Output) ),
|
||||
fail.
|
||||
streams(true) :-
|
||||
% \+ current_stream('/python/sys.stderr',_,_),
|
||||
open('/python/sys.stderr', append, Error, [alias(user_error)]),
|
||||
assert( cell_stream( Error) ),
|
||||
set_prolog_flag(user_error, Error),
|
||||
fail.
|
||||
streams(true).
|
||||
open('/python/input', read, _Input, [alias(user_input),bom(false),script(false)]),
|
||||
open('/python/sys.stdout', append, _Output, [alias(user_output)]),
|
||||
open('/python/sys.stderr', append, _Error, [alias(user_error)]).
|
||||
|
||||
ready(_Self, Line ) :-
|
||||
blank( Line ),
|
||||
!.
|
||||
ready(Self, Line ) :-
|
||||
errors( Self, Line ),
|
||||
\+ syntax_error(_,_).
|
||||
ready(Self, Cell, P, Q ) :-
|
||||
catch(
|
||||
all_clear(Self, Cell, P, Q)
|
||||
E,
|
||||
system_error(error,E).
|
||||
|
||||
errors( Self, Text ) :-
|
||||
all_clear( Self, _Cell, P, Q) :-
|
||||
no_errors( Self, P ),
|
||||
yap_flag(singleton_variables, Old, false)
|
||||
no_errors( Self, Q ).
|
||||
|
||||
no_errors( _Self, Text ) :-
|
||||
blank(Text),
|
||||
no_errors( Self, Text ) :-
|
||||
setup_call_cleanup(
|
||||
open_events( Self, Text, Stream),
|
||||
goals(Self, Stream),
|
||||
close_events( Self )
|
||||
open_esh( Self, Text, Stream),
|
||||
esh(Self, Stream),
|
||||
close_esh( Self, Stream )
|
||||
).
|
||||
|
||||
clauses(_Self, Stream) :-
|
||||
esh(Self, Stream) :-
|
||||
repeat,
|
||||
catch(
|
||||
read_clause(Stream, Cl, [term_position(_Pos), syntax_errors(fail)] ),
|
||||
% command( Self, Cl ),
|
||||
Error,
|
||||
syntax(Self, Error)
|
||||
),
|
||||
Cl == end_of_file,
|
||||
!.
|
||||
|
||||
goals(_Self, Stream) :-
|
||||
repeat,
|
||||
read_term(Stream, Cl, [term_position(_Pos), syntax_errors(fail)] ),
|
||||
% command( Self, Cl ),
|
||||
Cl == end_of_file,
|
||||
|
||||
syntax(_Self, E) :- writeln(user_error, E), fail.
|
||||
syntax(Self, error(syntax_error(Cause),info(between(_,LN,_), _FileName, CharPos, Details))) :-
|
||||
Self.errors := [t(Cause,LN,CharPos,Details)] + Self.errors,
|
||||
!.
|
||||
syntax(_Self, E) :- throw(E).
|
||||
|
||||
command(_, end_of_file) :- !.
|
||||
|
||||
command( _Self, ( :- op(Prio,Assoc,Name) ) ) :-
|
||||
addop(Prio,Assoc,Name).
|
||||
|
||||
command( _Self, ( :- module(Name, Exports) )) :-
|
||||
retract( active_module( M0 ) ),
|
||||
atom_concat( '__m0_', Name, M ),
|
||||
assert( active_module(M) ),
|
||||
assert( undo( active_module(M0) ) ),
|
||||
maplist( addop2(M), Exports).
|
||||
|
||||
|
||||
addop(Prio,Assoc,Name) :-
|
||||
(
|
||||
current_op(OPrio, SimilarAssoc, Name),
|
||||
op(Prio, Assoc, Name),
|
||||
matched_op(Assoc, SimilarAssoc)
|
||||
->
|
||||
assertz( undo(op( OPrio, Assoc, Name ) ) )
|
||||
;
|
||||
assertz( undo(op( 0, Assoc, Name ) ) )
|
||||
).
|
||||
|
||||
addop2(M, op(Prio, Assoc, Name)) :-
|
||||
addop( Prio, Assoc, M:Name ).
|
||||
|
||||
matched_op(A, B) :-
|
||||
optype( A, T),
|
||||
optype( B, T).
|
||||
|
||||
optype(fx,pre).
|
||||
optype(fy,pre).
|
||||
optype(xfx,in).
|
||||
optype(xfy,in).
|
||||
optype(yfx,in).
|
||||
optype(yfy,in).
|
||||
optype(xf,pos).
|
||||
optype(yf,pos).
|
||||
|
||||
:- dynamic user:portray_message/2.
|
||||
:- multifile user:portray_message/2.
|
||||
|
||||
:- dynamic syntax_error/4, undo/1.
|
||||
|
||||
user:portray_message(_Severity, error(syntax_error(Cause),info(between(_,LN,_), _FileName, CharPos, Details))) :-
|
||||
nb_getval(jupyter_cell, on),
|
||||
assert( syntax_error(Cause,LN,CharPos,Details) ).
|
||||
user:portray_message(_Severity, error(style_check(_),_) ) :-
|
||||
nb_getval(jupyter_cell, on).
|
||||
|
||||
open_events(Self, Text, Stream) :-
|
||||
open_esh(Self, Text, Stream) :-
|
||||
Self.errors := [],
|
||||
nb_setval( jupyter, on),
|
||||
open_mem_read_stream( Text, Stream ).
|
||||
|
||||
:- initialization( nb_setval( jupyter, off ) ).
|
||||
|
||||
close_events( _Self ) :-
|
||||
nb_setval( jupyter, off ),
|
||||
retract( undo(G) ),
|
||||
call(G),
|
||||
fail.
|
||||
close_events( Self ) :-
|
||||
retract( syntax_error( C, L, N, A )),
|
||||
Self.errors := [t(C,L,N,A)] + Self.errors,
|
||||
fail.
|
||||
close_events( _ ).
|
||||
|
||||
close_esh( _Self, Stream ) :-
|
||||
close(Stream).
|
||||
|
||||
:- if( current_prolog_flag(apple, true) ).
|
||||
|
||||
|
@ -23,11 +23,11 @@ library = namedtuple('library', 'list')
|
||||
v = namedtuple('_', 'slot')
|
||||
load_files = namedtuple('load_files', 'file ofile args')
|
||||
python_query = namedtuple('python_query', 'query_mgr string')
|
||||
jupyter_query = namedtuple('jupyter_query', 'self text query')
|
||||
jupyter_query = namedtuple('jupyter_query', 'self text p q')
|
||||
enter_cell = namedtuple('enter_cell', 'self' )
|
||||
exit_cell = namedtuple('exit_cell', 'self' )
|
||||
completions = namedtuple('completions', 'txt self' )
|
||||
errors = namedtuple('errors', 'self text' )
|
||||
errors = namedtuple('errors', 'self text p q' )
|
||||
streams = namedtuple('streams', ' text' )
|
||||
nostreams = namedtuple('nostreams', ' text' )
|
||||
|
||||
@ -114,7 +114,7 @@ class YAPInputSplitter(InputSplitter):
|
||||
if not line:
|
||||
line = text.rstrip()
|
||||
self.errors = []
|
||||
engine.mgoal(errors(self, line),"user",True)
|
||||
engine.mgoal(errors(self, text ,text,''),"user",True)
|
||||
return self.errors != []
|
||||
|
||||
|
||||
@ -520,7 +520,7 @@ class YAPRun:
|
||||
self.shell.yapeng = self.yapeng
|
||||
self._get_exc_info = shell._get_exc_info
|
||||
|
||||
def syntaxErrors(self, text):
|
||||
def syntaxErrors(self, text,program,query):
|
||||
"""Return whether a legal query
|
||||
"""
|
||||
if not text:
|
||||
@ -528,8 +528,7 @@ class YAPRun:
|
||||
if text == self.os:
|
||||
return self.errors
|
||||
self.errors=[]
|
||||
(text,_,_,_) = self.clean_end(text)
|
||||
self.yapeng.mgoal(errors(self,text),"user",True)
|
||||
self.yapeng.mgoal(errors(self,text,program,query),"user",True)
|
||||
return self.errors
|
||||
|
||||
def jupyter_query(self, s):
|
||||
@ -655,7 +654,8 @@ class YAPRun:
|
||||
# except SyntaxError:
|
||||
# preprocessing_exc_tuple = self.shell.syntax_error() # sys.exc_info()
|
||||
cell = raw_cell # cell has to exist so it can be stored/logged
|
||||
for i in self.syntaxErrors(raw_cell):
|
||||
(text,program,query,_) = self.clean_end(raw_cell)
|
||||
for i in self.syntaxErrors(raw_cell,raw_cell,''):
|
||||
try:
|
||||
(what,lin,_,text) = i
|
||||
e = SyntaxError(what, ("<string>", lin, 1, text))
|
||||
|
Reference in New Issue
Block a user