aboutsummaryrefslogtreecommitdiff
path: root/c/pl.cpp
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-07-15 13:13:50 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-07-15 13:13:50 +0200
commit8f1ead168558dac9cf4c10e269f568e15b5f8faf (patch)
treeb749f5abfa5720f491a56740f61f6de00b9e0bfc /c/pl.cpp
parent9d5ee7f8bd62f11cb0d9019cc4a715f265452bb7 (diff)
downloadEpisodeBrowser-8f1ead168558dac9cf4c10e269f568e15b5f8faf.tar.gz
Add support for Prolog exceptions.
Diffstat (limited to 'c/pl.cpp')
-rw-r--r--c/pl.cpp39
1 files changed, 35 insertions, 4 deletions
diff --git a/c/pl.cpp b/c/pl.cpp
index 3172090..1fbc4e7 100644
--- a/c/pl.cpp
+++ b/c/pl.cpp
@@ -1,9 +1,40 @@
#include <SWI-Prolog.h>
+#include "defs.h"
-int Pl(const char *szMod, const char *szPred)
+Query::Query(module_t ctx, predicate_t p, term_t t0)
+{
+ m_q = PL_open_query(ctx, PL_Q_CATCH_EXCEPTION, p, t0);
+}
+
+Query::~Query()
+{
+ PL_cut_query(m_q);
+}
+
+int Query::Cut()
+{
+ if (PL_cut_query(m_q)) return 1;
+ if (term_t t = PL_exception(m_q)) throw t;
+ return 0;
+}
+
+int Query::Close()
+{
+ if (PL_close_query(m_q)) return 1;
+ if (term_t t = PL_exception(m_q)) throw t;
+ return 0;
+}
+
+int Query::NextSolution()
+{
+ if (PL_next_solution(m_q)) return 1;
+ if (term_t t = PL_exception(m_q)) throw t;
+ return 0;
+}
+
+int Plx(const char *szMod, const char *szPred)
{
term_t t = PL_new_term_refs(0);
- if (!PL_call_predicate(NULL, PL_Q_CATCH_EXCEPTION, PL_predicate(szPred, 0, szMod), t))
- return 0;
- return 1;
+ Query q(NULL, PL_predicate(szPred, 0, szMod), t);
+ return q.NextSolution();
}