From 3cb343479e820c9b3b06d8117ef49d5ec1401659 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Tue, 2 May 2017 03:34:56 +0100 Subject: [PATCH] small compiler change --- C/c_interface.c | 19 ++++---- C/exec.c | 2 +- C/load_dl.c | 9 +++- C/modules.c | 2 + C/signals.c | 2 +- packages/python/yapi.py | 101 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 123 insertions(+), 12 deletions(-) create mode 100644 packages/python/yapi.py diff --git a/C/c_interface.c b/C/c_interface.c index 53e99c6ca..a535cb95f 100755 --- a/C/c_interface.c +++ b/C/c_interface.c @@ -1685,7 +1685,6 @@ X_API bool YAP_EnterGoal(PredEntry *pe, CELL *ptr, YAP_dogoalinfo *dgi) { // LOCAL_CurSlot); dgi->b = LCL0 - (CELL *)B; out = Yap_exec_absmi(true, false); - RECOVER_MACHINE_REGS(); if (out) { dgi->EndSlot = LOCAL_CurSlot; Yap_StartSlots(); @@ -1693,6 +1692,7 @@ X_API bool YAP_EnterGoal(PredEntry *pe, CELL *ptr, YAP_dogoalinfo *dgi) { LOCAL_CurSlot = dgi->CurSlot; // ignore any slots created within the called goal } + RECOVER_MACHINE_REGS(); return out; } @@ -1702,24 +1702,26 @@ X_API bool YAP_RetryGoal(YAP_dogoalinfo *dgi) { bool out; BACKUP_MACHINE_REGS(); - myB = (choiceptr)(LCL0 - dgi->b); + printf("before RETRY H=%p, ASP=%p, B=%p, ENV=%p, TR=%p %ld\n", HR, ASP, B, ENV, TR, LOCAL_CurSlot); + myB = (choiceptr)(LCL0 - dgi->b); CP = myB->cp_cp; /* sanity check */ if (B >= myB) { - return FALSE; + return false; } P = FAILCODE; /* make sure we didn't leave live slots when we backtrack */ ASP = (CELL *)B; LOCAL_CurSlot = dgi->EndSlot; out = run_emulator(PASS_REGS1); - RECOVER_MACHINE_REGS(); - if (out) { + printf("out=%d RETRY H=%p, ASP=%p, B=%p, ENV=%p, TR=%p %ld\n", out, HR, ASP, B, ENV, TR, LOCAL_CurSlot); + if (out) { dgi->EndSlot = LOCAL_CurSlot; } else { LOCAL_CurSlot = dgi->CurSlot; // ignore any slots created within the called goal } + RECOVER_MACHINE_REGS(); return out; } @@ -1729,7 +1731,7 @@ X_API bool YAP_LeaveGoal(bool backtrack, YAP_dogoalinfo *dgi) { BACKUP_MACHINE_REGS(); myB = (choiceptr)(LCL0 - dgi->b); - if (B > myB) { + if (B < myB) { /* someone cut us */ return FALSE; } @@ -2342,7 +2344,7 @@ YAP_file_type_t YAP_Init(YAP_init_args *yap_init) { #endif /* YAPOR_COPY || YAPOR_COW || YAPOR_SBA */ // GLOBAL_PrologShouldHandleInterrupts = // yap_init->PrologShouldHandleInterrupts && - if (!yap_init->Embedded) + if (!yap_init->Embedded) { Yap_InitSysbits(0); /* init signal handling and time, required by later functions */ GLOBAL_argv = yap_init->Argv; @@ -2353,6 +2355,7 @@ YAP_file_type_t YAP_Init(YAP_init_args *yap_init) { } else { yroot = BootFilePath; } + } if (yap_init->SavedState == NULL) { yap_init->SavedState = YAP_STARTUP; } @@ -2361,7 +2364,7 @@ YAP_file_type_t YAP_Init(YAP_init_args *yap_init) { if (yap_init->SavedState == NULL) yap_init->SavedState = YAP_STARTUP; #else - yap_init->SavedState = Yap_findFile(yap_init->SavedState, YAP_STARTUP, yroot, + yap_init->SavedState = Yap_findFile(yap_init->SavedState, YAP_STARTUP, yap_init->YapLibDir, boot_file, true, YAP_QLY, true, true); #endif if (yap_init->SavedState == NULL) { diff --git a/C/exec.c b/C/exec.c index 7feb9e6da..ee150dc90 100755 --- a/C/exec.c +++ b/C/exec.c @@ -1467,7 +1467,7 @@ static bool exec_absmi(bool top, yap_reset_t reset_mode USES_REGS) { while (B) { Yap_JumpToEnv(TermDAbort); } - LOCAL_PrologMode &~ AbortMode; + LOCAL_PrologMode &= ~AbortMode; P = (yamop *)FAILCODE; if (LOCAL_CBorder) LOCAL_CBorder = OldBorder; diff --git a/C/load_dl.c b/C/load_dl.c index 0a437d589..af5ed6f00 100755 --- a/C/load_dl.c +++ b/C/load_dl.c @@ -215,12 +215,17 @@ static Int LoadForeign(StringList ofiles, StringList libs, char *proc_name, if (proc_name && !*init_proc) *init_proc = (YapInitProc)dlsym(handle, proc_name); - ofiles = ofiles->next; } if (!*init_proc) { - LOCAL_ErrorMessage = "Could not locate initialization routine"; + LOCAL_ErrorMessage = malloc(MAX_ERROR_MSG_SIZE); + snprintf(LOCAL_ErrorMessage, + "Could not locate routine %s in %s: %s\n", + proc_name, LOCAL_FileNameBuf, dlerror()); + fprintf(stderr, + "Could not locate routine %s in %s: %s\n", + proc_name, LOCAL_FileNameBuf, dlerror()); return LOAD_FAILLED; } diff --git a/C/modules.c b/C/modules.c index a35cd917b..3aa974b07 100644 --- a/C/modules.c +++ b/C/modules.c @@ -213,6 +213,8 @@ struct pred_entry *Yap_ModulePred(Term mod) { void Yap_NewModulePred(Term mod, struct pred_entry *ap) { ModEntry *me; + if (mod == 0) + mod = TermProlog; if (!(me = LookupModule(mod))) return; WRITE_LOCK(me->ModRWLock); diff --git a/C/signals.c b/C/signals.c index 5d87ce6dc..b2684b519 100755 --- a/C/signals.c +++ b/C/signals.c @@ -67,7 +67,7 @@ static yap_signals InteractSIGINT(int ch) { #if PUSH_REGS // restore_absmi_regs(&Yap_standard_regs); #endif - siglongjmp(&LOCAL_RestartEnv, 4); + siglongjmp(LOCAL_RestartEnv, 4); return YAP_ABORT_SIGNAL; case 'b': /* continue */ diff --git a/packages/python/yapi.py b/packages/python/yapi.py new file mode 100644 index 000000000..5329de897 --- /dev/null +++ b/packages/python/yapi.py @@ -0,0 +1,101 @@ + +import yap +import sys +# debugging support. +import pdb + +def query_prolog(engine, s): + + def answer(q): + try: + return q.next() + except Exception as e: + print(e.args[1]) + return False + + # + #construct a query from a one-line string + # q is opaque to Python + q = engine.query(s) + # 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 + vs = q.namedVars() + # atom match either symbols, or if no symbol exists, sttrings, In this case + # variable names should match strings + for eq in vs: + if not isinstance(eq[0],str): + print( "Error: Variable Name matches a Python Symbol") + return + ask = True + # launch the query + while answer(q): + # this new vs should contain bindings to vars + vs= q.namedVars() + #numbervars + i=0 + # iteratw + for eq in vs: + name = eq[0] + # this is tricky, we're going to bind the variables in the term so thay we can + # output X=Y. The Python way is to use dictionares. + #Instead, we use the T function to tranform the Python term back to Prolog + binding = yap.T(eq[1]) + if binding.isVar(): + binding.unify(name) + else: + i = binding.numberVars(i, True) + print(name + " = " + binding.text()) + #ok, that was Prolog code + print("yes") + # deterministic = one solution + if q.deterministic(): + # done + q.close() + return + if ask: + s = input("more(;), all(*), no(\\n), python(#) ?").lstrip() + if s.startswith(';') or s.startswith('y'): + continue + elif s.startswith('#'): + try: + exec(s.lstrip('#')) + except: + raise + elif s.startswith('*') or s.startswith('a'): + ask = False + continue + else: + break + print("No (more) answers") + q.close() + return + + +def live(): + engine = yap.YAPEngine() + loop = True + pdb.set_trace() + while loop: + try: + s = input("?- ") + if not s: + loop = False + query_prolog(engine, s) + except SyntaxError as err: + print("Syntax Error error: {0}".format(err)) + except EOFError: + return + except RuntimeError as err: + print("YAP Execution Error: {0}".format(err)) + except ValueError: + print("Could not convert data to an integer.") + except: + print("Unexpected error:", sys.exc_info()[0]) + raise + engine.close() +# +# initialize engine +# engine = yap.YAPEngine(); +# engine = yap.YAPEngine(yap.YAPParams()); +#live()