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; TokEntry *tok = LOCAL_tokptr;
Int start_line = tok->TokLine; Int start_line = tok->TokLine;
Int err_line = errtok->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 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->errorNo = SYNTAX_ERROR;
Yap_local.ActiveError->parserFirstLine = start_line; Yap_local.ActiveError->parserFirstLine = start_line;

View File

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

View File

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

View File

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

View File

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