From d8ecd6a202d73f703b8decae5aa0c74ee562ac2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Thu, 10 Jul 2014 09:07:55 -0500 Subject: [PATCH] start work on exception handling --- CXX/yapi.cpp | 28 ++++++++++++++++------------ CXX/yapi.hh | 21 ++++++++++++--------- CXX/yapie.hh | 22 +++++++++++++++++++++- 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/CXX/yapi.cpp b/CXX/yapi.cpp index 142c171c6..381003bfa 100644 --- a/CXX/yapi.cpp +++ b/CXX/yapi.cpp @@ -379,7 +379,7 @@ YAPPredicate::YAPPredicate(const char *s, Term **outp, term_t &vnames) throw (in vnames = Yap_NewSlots(1 PASS_REGS); Term t = Yap_StringToTerm(s, strlen(s)+1, vnames); if (t == 0L) - throw YAPError::SYNTAX_ERROR; + throw SYNTAX_ERROR; ap = getPred( t, outp ); //{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "OUT vnames=%d ap=%p LCL0=%p", vnames, ap, LCL0) ; } } @@ -446,7 +446,7 @@ YAPQuery::initQuery( Term *ts ) LOCAL_execution = (struct open_query_struct *)this; this->q_open=1; this->q_state=0; - this->q_flags = 0; + this->q_flags = PL_Q_PASS_EXCEPTION; this->q_g = ts; } @@ -493,7 +493,7 @@ bool YAPQuery::next() return false; // don't forget, on success these guys must create slots if (this->q_state == 0) { - extern void toggle_low_level_trace(void); + // extern void toggle_low_level_trace(void); //toggle_low_level_trace(); result = (bool)YAP_EnterGoal((YAP_PredEntryPtr)this->ap, this->q_g, &this->q_h); } else { @@ -537,7 +537,9 @@ void YAPQuery::close() int YAPPredicate::call(YAPTerm t[]) { YAPQuery q = YAPQuery(*this, t); - int ret = q.next(); + int ret; + + ret = q.next(); q.cut(); q.close(); return ret; @@ -628,12 +630,14 @@ YAPQuery *YAPEngine::query( char *s ) { } YAPQuery *YAPEngine::safeQuery( char *s ) { - try - { - YAPQuery *n = new YAPQuery( s ); - return n; - } - catch (...) { - return 0; - } + try { + YAPQuery *n = new YAPQuery( s ); + n->setFlag( PL_Q_CATCH_EXCEPTION ); + n->resetFlag( PL_Q_PASS_EXCEPTION ); + return n; + } + catch (yap_error_number errno) { + error = errno; + return 0; + } } diff --git a/CXX/yapi.hh b/CXX/yapi.hh index 98f398db9..972be7313 100644 --- a/CXX/yapi.hh +++ b/CXX/yapi.hh @@ -94,10 +94,6 @@ class YAPApplTerm; class YAPPairTerm; class YAPQuery; -#include "yapie.hh" - -class TypeError {}; - /** * @brief Generic Prolog Term */ @@ -425,7 +421,7 @@ public: Term t, tp; t = YAP_ReadBuffer(s,&tp); if (t == 0L) - throw YAPError::SYNTAX_ERROR; + throw SYNTAX_ERROR; ap = getPred( t, (Term **)NULL ); } @@ -437,7 +433,7 @@ public: Term t, tp; t = YAP_ReadBuffer(s,&tp); if (t == 0L) - throw YAPError::SYNTAX_ERROR; + throw SYNTAX_ERROR; ap = getPred( t, (Term **)NULL ); } @@ -515,6 +511,10 @@ public: initQuery( ts ); } + /// set flags for query execution, currently only for exception handling + void setFlag(int flag) {q_flags |= flag; } + /// reset flags for query execution, currently only for exception handling + void resetFlag(int flag) {q_flags &= ~flag; } /// first query /// /// actually implemented by calling the next(); @@ -551,6 +551,7 @@ class YAPEngine { private: YAPCallback *_callback; YAP_init_args init_args; + yap_error_number error; public: YAPEngine(char *savedState = (char *)NULL, size_t stackSize = 0, @@ -569,15 +570,17 @@ public: /// remove current callback void delYAPCallback() { _callback = 0; } /// set a new callback - void setYAPCallback(YAPCallback *cb) { delYAPCallback(); _callback = cb; __android_log_print(ANDROID_LOG_INFO, __FILE__, "after loading startup %p",cb); } + void setYAPCallback(YAPCallback *cb) { delYAPCallback(); _callback = cb; } /// execute the callback. void run() { if (_callback) _callback->run(); } /// execute the callback with a text argument. - void run( char *s) { __android_log_print(ANDROID_LOG_INFO, __FUNCTION__, "bef calling disp %s %p",s, _callback); if (_callback) _callback->run(s); } + void run( char *s) { if (_callback) _callback->run(s); } /// build a query on the engine YAPQuery *query( char *s ); - /// build a query on the engine + /// build a query on the engine handling exceptions YAPQuery *safeQuery( char *s ); + + yap_error_number hasError( ); //> report whether the engine has found an error }; /* diff --git a/CXX/yapie.hh b/CXX/yapie.hh index a219275b9..4cacd1ec6 100644 --- a/CXX/yapie.hh +++ b/CXX/yapie.hh @@ -10,7 +10,27 @@ class YAPPredicate; class YAPError { public: - static const int SYNTAX_ERROR = 0x10000; + static const int SYNTAX_ERROR = 0x10000; //> syntax error + static const int DOMAIN_ERROR = 0x20000; //> usually illegal parameter, like asin( 2 ) + static const int TYPE_ERROR = 0x40000; //> usually illegal parameter in the language, like ( 2 mod 3.0 ) + static const int PERMISSION_ERROR = 0x80000; //> wrong para,eter + static const int EVALUATION_ERROR = 0x100000; //> bad arithmetic expressions + static const int RESOURCE_ERROR = 0x200000; //> no resource available, like MEM + static const int REPRESENTATION_ERROR = 0x400000; //> bad UTF-8 strings, etc + static const int EXISTËNCE_ERROR = 0x400000; //> object not found + static const int PROFILER = 0x400000; //> improve profiling support. + static const int OTHER_ERROR = 0x800000; //> anything rldr.,,,,,,,,,,,,,,,,,,,, }; +class YAPErrorClass { +public: + static const int SYNTAX_ERROR = 0x10000; + static const int DOMAIN_ERROR = 0x20000; + static const int TYPE_ERROR = 0x40000; + static const int PERMISSION_ERROR = 0x80000; + static const int EVALUATION_ERROR = 0x100000; + static const int RESOURCE_ERROR = 0x200000; + static const int REPRESENTATION_ERROR = 0x400000; + static const int OTHER_ERROR = 0x800000; +};