more jupytr fixes

This commit is contained in:
Vitor Santos Costa 2018-06-02 10:36:04 +01:00
parent 94a826efcc
commit 08ece03f4a
6 changed files with 2399 additions and 2296 deletions

View File

@ -525,11 +525,10 @@ bool YAPEngine::mgoal(Term t, Term tmod, bool release) {
q.CurSlot = Yap_StartSlots();
q.p = P;
q.cp = CP;
try {
PredEntry *ap;
if (IsStringTerm(tmod))
tmod = MkAtomTerm(Yap_LookupAtom(StringOfTerm(tmod)));
YAPPredicate *p = new YAPPredicate(t, tmod, ts, "C++");
PredEntry *ap = nullptr;
if (p == nullptr || (ap = p->ap) == nullptr ||
ap->OpcodeOfPred == UNDEF_OPCODE) {
ap = rewriteUndefEngineQuery(ap, t, tmod);
@ -555,11 +554,6 @@ bool YAPEngine::mgoal(Term t, Term tmod, bool release) {
// PyEval_RestoreThread(_save);
RECOVER_MACHINE_REGS();
return result;
} catch (...) {
YAPCatchError();
return false;
}
}
/**
* called when a query must be terminated and its state fully recovered,
@ -722,17 +716,14 @@ PredEntry *YAPQuery::rewriteUndefQuery() {
Term ts[2];
ts[0] = CurrentModule;
ts[1] = goal;
ARG1 = goal = Yap_SaveTerm(Yap_MkApplTerm(FunctorModule, 2, ts));
ARG1 = goal = Yap_SaveTerm(Yap_MkApplTerm(FunctorCall
, 1, &goal));
return ap = PredCall;
}
PredEntry *YAPEngine::rewriteUndefEngineQuery(PredEntry *a, Term &tgoal,
Term mod) {
Term ts[2];
ts[0] = mod;
ts[1] = tgoal;
ARG1 = tgoal = Yap_SaveTerm(Yap_MkApplTerm(FunctorModule, 2, ts));
// goal = YAPTerm(Yap_MkApplTerm(FunctorMetaCall, 1, &ARG1));
tgoal = Yap_MkApplTerm(FunctorCall, 1, &tgoal);
return PredCall;
// return YAPApplTerm(FunctorUndefinedQuery, ts);

View File

@ -997,7 +997,7 @@ static Int doformat(volatile Term otail, volatile Term oargs,
fstr = NULL;
targs = NULL;
format_clean_up(sno, sno0, finfo);
return (TRUE);
return true;
}
static Term memStreamToTerm(int output_stream, Functor f, Term inp) {

File diff suppressed because it is too large Load Diff

View File

@ -82,6 +82,11 @@ static void *py_open(VFS_t *me, const char *name, const char *io_mode,
if (!outbuf)
outbuf = ( unsigned char *)malloc(1024);
st->u.w_irl.ptr = st->u.w_irl.buf = outbuf;
]\]
st->user_name = TermOutStream;
} else if (strcmp(name, "sys.stderr") == 0) {
st->user_name = TermErrStream;

View File

@ -65,32 +65,34 @@ blankc(' ').
blankc('\n').
blankc('\t').
:- dynamic cell_stream/1.
streams(false) :-
nb_setval(jupyter_cell, false),
flush_output,
retract(cell_stream(S)),
close(S),
fail.
streams(false).
streams(true) :-
streams( false ),
nb_setval(jupyter_cell, true),
\+ current_stream('/python/input',_,_),
% \+ 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)]),
assert( cell_stream( Output) ),
fail.
% \+ 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',_,_),
% \+ 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.
fail.
streams(true).
ready(_Self, Line ) :-

View File

@ -1,26 +1,17 @@
import os
import sys
import abc
import math
import itertools
import trace
from typing import Iterator, List, Tuple, Iterable, Union
from traitlets import Bool, Enum, observe, Int
from typing import List
from traitlets import Bool
from yap4py.yapi import *
from yap_ipython.core.completer import Completer, Completion
from yap_ipython.utils.strdispatch import StrDispatch
from yap_ipython.core.completer import Completer
# import yap_ipython.core
from traitlets import Instance
from yap_ipython.core.inputsplitter import *
from yap_ipython.core.inputtransformer import *
from yap_ipython.core.interactiveshell import *
from pygments import highlight
from pygments.lexers.prolog import PrologLexer
from pygments.formatters import HtmlFormatter
from yap_ipython.core import interactiveshell
@ -38,6 +29,7 @@ exit_cell = namedtuple('exit_cell', 'self' )
completions = namedtuple('completions', 'txt self' )
errors = namedtuple('errors', 'self text' )
streams = namedtuple('streams', ' text' )
nostreams = namedtuple('nostreams', ' text' )
global engine
@ -50,7 +42,6 @@ def tracefunc(frame, event, arg, indent=[0]):
indent[0] -= 2
return tracefunc
import sys
class YAPInputSplitter(InputSplitter):
"""An input splitter that recognizes all of iyap's special syntax."""
@ -560,8 +551,6 @@ class YAPRun:
pg = jupyter_query( self, program, squery)
self.query = self.yapeng.query(pg)
self.query.answer = {}
self.yapeng.mgoal(streams(False),"user", True)
self.yapeng.mgoal(streams(True),"user", True)
while self.query.next():
answer = self.query.answer
found = True
@ -570,7 +559,6 @@ class YAPRun:
if self.query.port == "exit":
self.os = None
sys.stderr.writeln('Done, with', self.bindings)
self.yapeng.mgoal(streams(False),"user", True)
return True,self.bindings
if stop or howmany == self.iterations:
return True, self.bindings
@ -579,9 +567,8 @@ class YAPRun:
else:
self.os = None
self.query.close()
`` self.query = None
self.query = None
sys.stderr.write('Fail\n')
self.yapeng.mgoal(streams(False),"user", True)
return True,self.bindings
except Exception as e:
sys.stderr.write('Exception after', self.bindings, '\n')
@ -738,9 +725,10 @@ class YAPRun:
# run the new command using the given tracer
#
# tracer.runfunc(f,self,cell,state)
self.yapeng.mgoal(streams(True),"user", True)
self.jupyter_query( cell )
# state = tracer.runfunc(jupyter_query( self, cell ) )
self.yapeng.mgoal(streams(False),"user", True)
# state = tracer.runfunc(jupyter_query( self, cell ) )
self.shell.last_execution_succeeded = True
self.result.result = (True, dicts)
@ -786,7 +774,6 @@ class YAPRun:
s = s[:i-1]
if n:
its = 0
j = 0
for ch in n:
if not ch.isdigit():
raise SyntaxError()