This commit is contained in:
Vitor Santos Costa 2018-12-16 02:21:54 +00:00
parent 3fa4bfbcb2
commit 37d5dcedc1
7 changed files with 69 additions and 46 deletions

19
library/android.yap Normal file
View File

@ -0,0 +1,19 @@
%:- start_low_level_trace.
:- module(user).
:- yap_flag(verbose,normal).
query( String ) :-
yap_flag(typein_module, Mod),
atomic_to_term( String, Goal, VarNames ),
query_to_answer( Mod:Goal, VarNames, Status, Bindings),
output( Bindings, Status) .
output( Bindings, Status) :-
(Status == answer -> true ;
Status == exit ->true
),
write_query_answer( Bindings ),
nl(user_error).
%:- [sqlitest].

View File

@ -376,10 +376,10 @@ static Term syntax_error(TokEntry *errtok, int sno, Term cmod, Int newpos, bool
TokEntry *tok = LOCAL_tokptr;
Int start_line = tok->TokLine;
Int err_line = errtok->TokLine;
Int end_line = GetCurInpLine(GLOBAL_Stream + sno);
Int startpos = tok->TokPos;
Int startpos = tok->TokPos;
Int errpos = errtok->TokPos;
Int endpos = GetCurInpPos(GLOBAL_Stream + sno);
Int end_line = GetCurInpLine(GLOBAL_Stream + sno);
Int endpos = GetCurInpPos(GLOBAL_Stream + sno);
Yap_local.ActiveError->errorNo = SYNTAX_ERROR;
Yap_local.ActiveError->parserFirstLine = start_line;

View File

@ -73,7 +73,7 @@ python_query( Caller, String ) :-
query_to_answer( Goal, VarNames, Status, Bindings),
Caller.port := Status,
write_query_answer( Bindings ),
nl(user_error),
nl(user_error),
maplist(in_dict(Caller.answer, Bindings), Bindings).
/**

View File

@ -78,16 +78,20 @@ class Query (YAPQuery):
super().__init__(g)
self.engine = engine
self.port = "call"
self.bindings = None
self.answer = {}
def __iter__(self):
return self
def done(self):
return self.port == "fail" or self.port == "exit"
def __next__(self):
if self.port == "fail":
raise IndexError()
return self.next()
self.answer = {}
if self.port == "fail" or self.port == "exit":
raise StopIteration()
if self.next():
return self.answer
raise StopIteration()
def name( name, arity):
try:
@ -141,7 +145,7 @@ class YAPShell:
# # vs is the list of variables
# you can print it out, the left-side is the variable name,
# the right side wraps a handle to a variable
import pdb; pdb.set_trace()
# import pdb; pdb.set_trace()
# #pdb.set_trace()
# atom match either symbols, or if no symbol exists, sttrings, In this case
# variable names should match strings
@ -151,14 +155,13 @@ class YAPShell:
# return
try:
engine = self.engine
bindings = []
bindings = []
loop = False
g = python_query(self, query)
self.q = Query( engine, g )
while self.q.next():
bindings += [self.q.answer]
if self.q.port == "exit":
break
q = Query( engine, python_query( engine, query) )
for answer in q:
bindings += [answer]
if g.done():
return bindings
if loop:
continue
s = input("more(;), all(*), no(\\n), python(#)? ").lstrip()
@ -177,10 +180,8 @@ class YAPShell:
if self.q:
self.q.close()
self.q = None
if bindings:
return True,bindings
print("No (more) answers")
return False, None
return bindings
except Exception as e:
if not self.q:
return False, None

View File

@ -550,7 +550,7 @@ class YAPRun(InteractiveShell):
self.yapeng.mgoal(errors(self,text),"user",True)
return self.errors
def jupyter_query(self, s):
def prolog(self, s):
#
# construct a self.queryuery from a one-line string
# self.query is opaque to Python
@ -590,7 +590,7 @@ class YAPRun(InteractiveShell):
if found:
sys.stderr.write('Completed, with '+str(self.answers)+'\n')
result.result = self.answers
return result
return result.results
except Exception as e:
sys.stderr.write('Exception '+str(e)+'in query '+ str(self.query)+
@ -736,7 +736,7 @@ class YAPRun(InteractiveShell):
# run the new command using the given tracer
#
# tracer.runfunc(f,self,cell,state)
self.jupyter_query( cell )
self.prolog( cell )
# state = tracer.runfunc(jupyter_query( self, cell ) )
self.shell.last_execution_succeeded = True
result.result = []
@ -814,12 +814,15 @@ class YAPRun(InteractiveShell):
If the line terminates on a `*/` or starts on a `%` we assume the line
is a comment.
"""
s0 = s.rstrip(' \n\t\i')
[program,x,query] = s0.rpartition('\n')
if query[-1] == '.':
return s,'',False,0
(query, _,loop, sols) = self.clean_end(query)
return (program, query, loop, sols)
try:
s0 = s.rstrip(' \n\t\i')
[program,x,query] = s0.rpartition('\n')
if query[-1] == '.':
return s,'',False,0
(query, _,loop, sols) = self.clean_end(query)
return (program, query, loop, sols)
except:
return (s,'',true,1)
# global
#globals = {}

View File

@ -476,9 +476,7 @@ If this hook preodicate succeeds it must instantiate the _Action_ argument to t
:- ensure_loaded('../pl/pathconf.yap').
:- current_prolog_fkag(android,true), ensure_loaded('../pl/android.yap').
:- current_prolog_flag(android,true), ensure_loaded('../android.yap').
:- set_prolog_flag(unknown,error).

View File

@ -102,7 +102,7 @@ undefined_query(G0, M0, Cut) :-
'$undefp_search'(M0:G0, MG) :-
'$pred_exists'(unknown_predicate_handler(_,_,_,_), user),
'$yap_strip_module'(M0:G0, EM0, GM0),
user:unknown_predicate_handler(GM0,EM0,M1:G1),
user:unknown_predicate_handler(GM0,EM0,MG),
!.
'$undefp_search'(M0:G0, M:G) :-
'$get_undefined_predicates'(G, M0, G0, M), !.
@ -113,29 +113,30 @@ undefined_query(G0, M0, Cut) :-
% undef handler
'$undefp'([M0|G0],_) :-
start_low_level_trace,
% make sure we do not loop on undefined predicates
setup_and_call_cleanup(
´$undef_set'(MG,Action,Debug,Current),
´$search_def'(M0,G0,MG),
setup_call_catcher_cleanup(
'$undef_set'(Action,Debug,Current),
'$search_def'(M0,G0,MG),
Port,
´$undef_reset'(Port,Mo:GO,MG,Action,Debug,Current)
'$undef_reset'(Port,M0:G0,MG,Action,Debug,Current)
).
'$undef_set'(MG,Action,Debug,Current) :-
'$undef_set'(Action,Debug,Current) :-
yap_flag( unknown, Action, fail),
yap_flag( debug, Debug, false),
'$stop_creeping'(Current).
'$search_def'(M0,G0,NG:NM) :-
'$undefp_search'(M0:G0, NM:NG),
!,
'$pred_exists'(NG,NM).
'$undefp_search'(M0:G0, NM:NG),
!,
'$pred_exists'(NG,NM).
´$undef_reset'(exit,_G0,NG:NM,Action,Debug,Current) :-
'$undef_reset'(exit,_G0,NG:NM,Action,Debug,Current) :-
yap_flag( unknown, _, Action),
yap_flag( debug, _, Debug),
nonvar(NG)
nonvar(NG),
nonvar(NM),
(
Current == true
->
@ -144,12 +145,13 @@ undefined_query(G0, M0, Cut) :-
;
'$execute0'(NG, NM)
).
´$undef_reset'(_,M0:G0,_NG,Action,Debug,_Current) :-
'$undef_reset'(_,M0:G0,_NG,Action,Debug,_Current) :-
yap_flag( unknown, _, Action),
yap_flag( debug, _, Debug),
'$start_creep'([prolog|true], creep),
'$handle_error'(Action,G0,M0).
:- '$undefp_handler'('$undefp'(_), prolog).
:- '$undefp_handler'('$undefp'(_,_), prolog).
/** @pred unknown(- _O_,+ _N_)