This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/real/yap4r/src/yap4r.cpp

131 lines
2.9 KiB
C++
Raw Normal View History

2019-03-31 23:23:04 +01:00
#include <Rcpp.h>
#undef Realloc
#undef Malloc
#undef Free
#include <yapi.hh>
2019-04-02 10:27:37 +01:00
#include <string>
2019-04-06 10:07:54 +01:00
#include <vector>
2019-03-31 23:23:04 +01:00
#include "real.h"
using namespace Rcpp;
2019-04-02 10:27:37 +01:00
class yap4r {
2019-03-31 23:23:04 +01:00
YAPEngine *yap;
YAPQuery *q;
std::vector<YAPTerm> args;
bool failed;
2019-04-03 15:04:24 +01:00
2019-03-31 23:23:04 +01:00
public:
2019-04-03 15:04:24 +01:00
SEXP qsexp;
2019-04-02 10:27:37 +01:00
yap4r();
2019-04-06 10:07:54 +01:00
bool query(std::string p_name, GenericVector sexps = R_NilValue,
std::string p_module = "user");
2019-04-02 10:27:37 +01:00
bool more();
bool done();
SEXP peek(int i);
2019-04-03 15:04:24 +01:00
bool compile(std::string s);
bool library(std::string s);
2019-04-02 10:27:37 +01:00
};
2019-04-06 10:07:54 +01:00
yap4r::yap4r() {
2019-03-31 23:23:04 +01:00
YAPEngineArgs *yargs = new YAPEngineArgs();
yap = new YAPEngine(yargs);
};
2019-04-06 10:07:54 +01:00
bool yap4r::query(std::string p_name, GenericVector sexps,
std::string p_module) {
2019-03-31 23:23:04 +01:00
if (q) {
q->close();
2019-04-03 10:39:22 +01:00
q = nullptr;
2019-03-31 23:23:04 +01:00
}
2019-04-03 15:04:24 +01:00
yhandle_t t;
arity_t arity;
if (sexps.isNULL()) {
YAPTerm qt = YAPAtomTerm(p_name.c_str());
2019-04-06 10:07:54 +01:00
q = new YAPQuery(qt);
t = qt.handle();
2019-04-03 15:04:24 +01:00
} else {
2019-04-06 10:07:54 +01:00
arity = sexps.length();
2019-04-03 15:04:24 +01:00
std::vector<YAPTerm> args = std::vector<YAPTerm>();
2019-04-06 10:07:54 +01:00
yhandle_t sls = Yap_NewHandles(sexps.length());
for (int i = 0; i < sexps.length(); i++) {
if (!sexp_to_pl(sls + i, sexps[i]))
return false;
args.push_back(YAPTerm(Yap_GetFromSlot(sls + i)));
}
YAPFunctor f = YAPFunctor(p_name.c_str(), arity);
YAPAtomTerm mod = YAPAtomTerm(p_module.c_str());
t = YAPApplTerm(p_name.c_str(), args.data()).handle();
q = new YAPQuery(f, mod, args.data());
2019-04-03 10:39:22 +01:00
}
if (q == nullptr)
return false;
bool rc = q->next();
if (!rc) {
failed = true;
q = nullptr;
}
2019-04-06 10:07:54 +01:00
if (rc)
qsexp = term_to_sexp(t, false);
2019-04-03 15:04:24 +01:00
return rc;
2019-04-02 10:27:37 +01:00
}
2019-03-31 23:23:04 +01:00
2019-04-03 15:04:24 +01:00
bool yap4r::compile(std::string s) {
YAPTerm fs[1];
fs[0] = YAPAtomTerm(s.c_str());
2019-04-06 10:07:54 +01:00
return yap->mgoal(YAPApplTerm("compile", fs).term(), USER_MODULE);
2019-04-03 15:04:24 +01:00
}
bool yap4r::library(std::string s) {
YAPTerm fs[1], l[1];
l[0] = YAPAtomTerm(s.c_str());
fs[0] = YAPApplTerm("library", l);
2019-04-06 10:07:54 +01:00
return yap->mgoal(YAPApplTerm("compile", fs).term(), USER_MODULE);
2019-04-03 15:04:24 +01:00
}
2019-04-02 10:27:37 +01:00
2019-04-06 10:07:54 +01:00
bool yap4r::more() {
bool rc = true;
if (failed)
return false;
if (q)
rc = q->next();
if (!rc) {
failed = true;
2019-04-02 10:27:37 +01:00
}
2019-04-06 10:07:54 +01:00
return rc;
}
2019-04-02 10:27:37 +01:00
2019-04-06 10:07:54 +01:00
bool yap4r::done() {
2019-03-31 23:23:04 +01:00
2019-04-06 10:07:54 +01:00
if (failed)
return false;
if (q)
q->cut();
q = NULL;
return true;
}
2019-03-31 23:23:04 +01:00
2019-04-06 10:07:54 +01:00
SEXP yap4r::peek(int i) {
if (failed || q == nullptr)
return R_MissingArg;
if (i == 0)
return qsexp;
return term_to_sexp(Yap_InitSlot(Yap_XREGS[i]), false);
}
2019-03-31 23:23:04 +01:00
2019-04-06 10:07:54 +01:00
RCPP_MODULE(mod_yap4r) {
class_<yap4r>("yap4r")
2019-04-02 10:27:37 +01:00
.constructor("create an object encapsulating a Prolog engine")
2019-04-06 10:07:54 +01:00
.method("query", &yap4r::query,
"create an active query within the engine")
.method("more", &yap4r::more, "ask for an extra solution")
.method("done", &yap4r::done, "terminate the query")
.method("compile", &yap4r::compile, "compile the file")
.method("library", &yap4r::library, "compile the library")
.method("peek", &yap4r::peek, "load arg[i] into R");
2019-03-31 23:23:04 +01:00
}