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/CXX/yapt.hh

449 lines
13 KiB
C++
Raw Normal View History

2016-04-12 16:22:53 +01:00
2015-02-09 01:52:10 +00:00
#ifndef YAPT_HH
#define YAPT_HH 1
2016-12-10 07:01:10 +00:00
#include "config.h"
extern "C" {
Term YAP_ReadBuffer(const char *s, Term *tp);
#if defined(SWIGPYTHON) && 0
#include <Python.h>
extern Term pythonToYAP(PyObject *inp);
#define YAPTerm _YAPTERM
#elifndef HAVE_PYTHON_H
typdef struct { int no_python; } PyObject;
#else
#include <Python.h>
#endif
};
2015-06-18 00:52:31 +01:00
2016-09-21 20:41:23 +01:00
class YAPError;
2016-12-10 07:01:10 +00:00
2015-02-09 01:52:10 +00:00
/**
* @brief Generic Prolog Term
*/
class YAPTerm {
friend class YAPPredicate;
friend class YAPPrologPredicate;
friend class YAPQuery;
friend class YAPModule;
2015-04-13 13:28:17 +01:00
friend class YAPModuleProp;
friend class YAPApplTerm;
2016-07-31 16:22:24 +01:00
friend class YAPListTerm;
2015-02-09 01:52:10 +00:00
protected:
2016-12-10 07:01:10 +00:00
yhandle_t t; /// handle to term, equivalent to term_t
2015-02-09 01:52:10 +00:00
public:
2016-12-10 07:01:10 +00:00
virtual ~YAPTerm() {
// fprintf(stderr,"-%d,%lx,%p ",t,LOCAL_HandleBase[t] ,HR);
// Yap_DebugPlWriteln(LOCAL_HandleBase[t]); }
// LOCAL_HandleBase[t] = TermFreeTerm;
// while ( LOCAL_HandleBase[LOCAL_CurSlot-1] == TermFreeTerm)
LOCAL_CurSlot--;
};
Term gt() {
CACHE_REGS
// fprintf(stderr,"?%d,%lx,%p\n",t,LOCAL_HandleBase[t], HR);
// Yap_DebugPlWriteln(LOCAL_HandleBase[t]);
return Yap_GetFromSlot(t);
};
void mk(Term t0) {
CACHE_REGS t = Yap_InitSlot(t0);
// fprintf(stderr,"+%d,%lx,%p,%p",t,t0,HR,ASP); Yap_DebugPlWriteln(t0);
};
YAPTerm(Term tn) { mk(tn); };
YAPTerm(PyObject *inp) {
#ifdef SWIGPYTHON
Term tinp = pythonToYAP(inp);
t = Yap_InitSlot(tinp);
#else
t = 0;
#endif
}
/// private method to convert from Term (internal YAP representation) to
2016-07-31 16:22:24 +01:00
/// YAPTerm
2015-02-09 01:52:10 +00:00
// do nothing constructor
2016-12-10 07:01:10 +00:00
YAPTerm() { mk(MkVarTerm()); };
2016-09-27 18:28:54 +01:00
// YAPTerm(yhandle_t i) { t = i; };
2015-02-09 01:52:10 +00:00
/// pointer to term
YAPTerm(void *ptr);
/// parse string s and construct a term.
2016-07-31 16:22:24 +01:00
YAPTerm(char *s) {
Term tp;
mk(YAP_ReadBuffer(s, &tp));
}
2016-09-27 18:28:54 +01:00
/// construct a term out of an integer (if you know object type use
/// YAPIntegerTerm)
2016-11-08 07:37:36 +00:00
/// YAPTerm(long int num) { mk(MkIntegerTerm(num)); }
2016-09-27 18:28:54 +01:00
/// construct a term out of an integer (if you know object type use
/// YAPIntegerTerm)
2016-11-08 07:37:36 +00:00
/// YAPTerm(double num) { mk(MkFloatTerm(num)); }
2016-09-21 20:41:23 +01:00
/// parse string s and construct a term.
2016-11-08 07:37:36 +00:00
/// YAPTerm(YAPFunctor f, YAPTerm ts[]);
2015-02-09 01:52:10 +00:00
/// extract the tag of a term, after dereferencing.
2016-07-31 16:22:24 +01:00
YAP_tag_t tag();
2015-02-09 01:52:10 +00:00
/// copy the term ( term copy )
2016-12-10 07:01:10 +00:00
Term deepCopy();
2016-07-31 16:22:24 +01:00
/// numbervars ( int start, bool process=false )
intptr_t numberVars(intptr_t start, bool skip_singletons = false);
inline Term term() {
return gt();
} /// from YAPTerm to Term (internal YAP representation)
2016-12-10 07:01:10 +00:00
inline void bind(Term b) { LOCAL_HandleBase[t] = b; }
inline void bind(YAPTerm *b) { LOCAL_HandleBase[t] = b->term(); }
/// from YAPTerm to Term (internal YAP representation)
2016-07-31 16:22:24 +01:00
/// fetch a sub-term
2016-12-10 07:01:10 +00:00
Term &operator[](size_t n);
2016-07-31 16:22:24 +01:00
// const YAPTerm *vars();
2015-02-09 01:52:10 +00:00
/// this term is == to t1
2016-12-10 07:01:10 +00:00
virtual bool exactlyEqual(YAPTerm t1) {
bool out;
BACKUP_MACHINE_REGS();
out = Yap_eq(gt(), t1.term());
RECOVER_MACHINE_REGS();
return out;
};
/// t = t1
virtual bool unify(YAPTerm t1) {
intptr_t out;
BACKUP_MACHINE_REGS();
out = Yap_unify(gt(), t1.term());
RECOVER_MACHINE_REGS();
return out;
};
/// we can unify t and t1
virtual bool unifiable(YAPTerm t1) {
bool out;
BACKUP_MACHINE_REGS();
out = Yap_eq(gt(), t1.term());
RECOVER_MACHINE_REGS();
return out;
};
/// t =@= t1, the two terms are equal up to variable renamingvirtual bool
/// variant(
inline virtual YAP_Term variant(YAPTerm t1) {
intptr_t out;
BACKUP_MACHINE_REGS();
out = Yap_Variant(gt(), t1.term());
RECOVER_MACHINE_REGS();
return out;
};
virtual intptr_t hashTerm(size_t sz, size_t depth, bool variant) {
intptr_t out;
BACKUP_MACHINE_REGS();
out = Yap_TermHash(gt(), sz, depth, variant);
RECOVER_MACHINE_REGS();
return out;
};
/// term hash,
2016-07-31 16:22:24 +01:00
virtual bool isVar() { return IsVarTerm(gt()); } /// type check for unound
virtual bool isAtom() { return IsAtomTerm(gt()); } /// type check for atom
virtual bool isInteger() {
return IsIntegerTerm(gt());
} /// type check for integer
virtual bool isFloat() {
return IsFloatTerm(gt());
} /// type check for floating-point
virtual bool isString() {
return IsStringTerm(gt());
} /// type check for a string " ... "
virtual bool isCompound() {
return !(IsVarTerm(gt()) || IsNumTerm(gt()));
} /// is a primitive term
virtual bool isAppl() { return IsApplTerm(gt()); } /// is a structured term
virtual bool isPair() { return IsPairTerm(gt()); } /// is a pair term
virtual bool isGround() { return Yap_IsGroundTerm(gt()); } /// term is ground
virtual bool isList() { return Yap_IsListTerm(gt()); } /// term is a list
2015-02-09 01:52:10 +00:00
/// extract the argument i of the term, where i in 1...arity
2016-12-10 07:01:10 +00:00
virtual Term getArg(arity_t i) {
2015-03-11 22:18:00 +00:00
BACKUP_MACHINE_REGS();
2016-12-10 07:01:10 +00:00
Term tf = 0;
2015-02-09 01:52:10 +00:00
Term t0 = gt();
if (IsApplTerm(t0))
2016-12-10 07:01:10 +00:00
tf = (ArgOfTerm(i, t0));
2015-02-09 01:52:10 +00:00
else if (IsPairTerm(t0)) {
2016-07-31 16:22:24 +01:00
if (i == 1)
2016-12-10 07:01:10 +00:00
tf = (HeadOfTerm(t0));
2016-07-31 16:22:24 +01:00
else if (i == 2)
2016-12-10 07:01:10 +00:00
tf = (TailOfTerm(t0));
2015-03-11 22:18:00 +00:00
} else {
2016-12-10 07:01:10 +00:00
tf = ((Term)0);
2015-02-09 01:52:10 +00:00
}
2015-03-11 22:18:00 +00:00
RECOVER_MACHINE_REGS();
return tf;
2015-02-09 01:52:10 +00:00
}
2016-07-31 16:22:24 +01:00
/// extract the arity of the term
/// variables have arity 0
virtual inline arity_t arity() {
Term t0 = gt();
if (IsApplTerm(t0)) {
Functor f = FunctorOfTerm(t0);
if (IsExtensionFunctor(f))
return 0;
return ArityOfFunctor(f);
} else if (IsPairTerm(t0)) {
return 2;
}
return 0;
}
2015-02-09 01:52:10 +00:00
/// return a string with a textual representation of the term
2016-12-10 07:01:10 +00:00
virtual const char *text(){
CACHE_REGS
size_t length = 0;
encoding_t enc = LOCAL_encoding;
char *os;
BACKUP_MACHINE_REGS();
if (!(os = Yap_TermToString(Yap_GetFromSlot(t), &length, enc,
Handle_vars_f))) {
RECOVER_MACHINE_REGS();
return 0;
}
RECOVER_MACHINE_REGS();
length = strlen(os) + 1;
char *sm = (char *)malloc(length + 1);
strcpy(sm, os);
return sm;
};
2016-07-31 16:22:24 +01:00
/// return a handle to the term
inline yhandle_t handle() { return t; };
2016-09-27 18:28:54 +01:00
/// whether the term actually refers to a live object
inline bool initialized() { return t != 0; };
2015-02-09 01:52:10 +00:00
};
/**
* @brief Variable Term
*/
2016-07-31 16:22:24 +01:00
class YAPVarTerm : public YAPTerm {
YAPVarTerm(Term t) {
2016-09-27 18:28:54 +01:00
if (IsVarTerm(t)) {
2016-07-31 16:22:24 +01:00
mk(t);
2016-09-27 18:28:54 +01:00
}
2016-07-31 16:22:24 +01:00
}
2015-02-09 01:52:10 +00:00
public:
/// constructor
YAPVarTerm();
/// get the internal representation
2016-07-31 16:22:24 +01:00
CELL *getVar() { return VarOfTerm(gt()); }
2015-02-09 01:52:10 +00:00
/// is the variable bound to another one
2016-07-31 16:22:24 +01:00
bool unbound() { return IsUnboundVar(VarOfTerm(gt())); }
virtual bool isVar() { return true; } /// type check for unbound
virtual bool isAtom() { return false; } /// type check for atom
2015-03-16 17:25:09 +00:00
virtual bool isInteger() { return false; } /// type check for integer
2016-07-31 16:22:24 +01:00
virtual bool isFloat() { return false; } /// type check for floating-point
virtual bool isString() { return false; } /// type check for a string " ... "
2015-03-16 17:25:09 +00:00
virtual bool isCompound() { return false; } /// is a primitive term
2016-07-31 16:22:24 +01:00
virtual bool isAppl() { return false; } /// is a structured term
virtual bool isPair() { return false; } /// is a pair term
virtual bool isGround() { return false; } /// term is ground
virtual bool isList() { return false; } /// term is a list
2015-02-09 01:52:10 +00:00
};
/**
* @brief Compound Term
*/
2016-07-31 16:22:24 +01:00
class YAPApplTerm : public YAPTerm {
2015-02-09 01:52:10 +00:00
friend class YAPTerm;
YAPApplTerm(Term t0) { mk(t0); }
2016-07-31 16:22:24 +01:00
2015-02-09 01:52:10 +00:00
public:
2016-07-31 16:22:24 +01:00
~YAPApplTerm() {}
2016-12-10 07:01:10 +00:00
YAPApplTerm(YAPFunctor f, YAPTerm ts[]);
//YAPApplTerm(const char *s, std::vector<YAPTerm> ts);
//YAPApplTerm(YAPFunctor f);
2015-02-09 01:52:10 +00:00
YAPFunctor getFunctor();
2016-12-10 07:01:10 +00:00
Term getArg(arity_t i) {
2016-07-31 16:22:24 +01:00
BACKUP_MACHINE_REGS();
Term t0 = gt();
2016-12-10 07:01:10 +00:00
Term tf;
tf = ArgOfTerm(i, t0);
2016-07-31 16:22:24 +01:00
RECOVER_MACHINE_REGS();
return tf;
};
virtual bool isVar() { return false; } /// type check for unbound
virtual bool isAtom() { return false; } /// type check for atom
2015-03-16 17:25:09 +00:00
virtual bool isInteger() { return false; } /// type check for integer
2016-07-31 16:22:24 +01:00
virtual bool isFloat() { return false; } /// type check for floating-point
virtual bool isString() { return false; } /// type check for a string " ... "
2015-03-16 17:25:09 +00:00
virtual bool isCompound() { return true; } /// is a primitive term
2016-07-31 16:22:24 +01:00
virtual bool isAppl() { return true; } /// is a structured term
virtual bool isPair() { return false; } /// is a pair term
virtual bool isGround() { return true; } /// term is ground
virtual bool isList() { return false; } /// [] is a list
2015-02-09 01:52:10 +00:00
};
/**
* @brief List Constructor Term
*/
2016-07-31 16:22:24 +01:00
class YAPPairTerm : public YAPTerm {
2015-02-09 01:52:10 +00:00
friend class YAPTerm;
2016-07-31 16:22:24 +01:00
YAPPairTerm(Term t0) {
if (IsPairTerm(t0))
mk(t0);
else
mk(0);
}
2015-02-09 01:52:10 +00:00
public:
YAPPairTerm(YAPTerm hd, YAPTerm tl);
YAPPairTerm();
2016-12-10 07:01:10 +00:00
Term getHead() { return (HeadOfTerm(gt())); }
Term getTail() { return (TailOfTerm(gt())); }
2015-02-09 01:52:10 +00:00
};
2015-04-13 13:28:17 +01:00
/**
* @brief Number Term
*/
2016-07-31 16:22:24 +01:00
class YAPNumberTerm : public YAPTerm {
2015-04-13 13:28:17 +01:00
public:
2016-07-31 16:22:24 +01:00
YAPNumberTerm(){};
bool isTagged() { return IsIntTerm(gt()); }
2015-04-13 13:28:17 +01:00
};
2015-02-09 01:52:10 +00:00
/**
* @brief Integer Term
*/
2016-07-31 16:22:24 +01:00
class YAPIntegerTerm : public YAPNumberTerm {
2015-02-09 01:52:10 +00:00
public:
YAPIntegerTerm(intptr_t i);
2016-09-27 18:28:54 +01:00
intptr_t getInteger() { return IntegerOfTerm(gt()); };
};
/**
* @brief Floating Point Term
*/
class YAPFloatTerm : public YAPNumberTerm {
public:
YAPFloatTerm(double dbl) { mk(MkFloatTerm(dbl)); };
double getFl() { return FloatOfTerm(gt()); };
2015-02-09 01:52:10 +00:00
};
2016-07-31 16:22:24 +01:00
class YAPListTerm : public YAPTerm {
2015-02-09 01:52:10 +00:00
public:
/// Create a list term out of a standard term. Check if a valid operation.
///
/// @param[in] the term
2015-03-11 22:18:00 +00:00
YAPListTerm() { mk(TermNil); /* else type_error */ }
/// Create an empty list term.
///
/// @param[in] the term
2015-02-09 01:52:10 +00:00
YAPListTerm(Term t0) { mk(t0); /* else type_error */ }
2016-07-31 16:22:24 +01:00
/// Create a list term out of an array of terms.
///
/// @param[in] the array of terms
/// @param[in] the length of the array
YAPListTerm(YAPTerm ts[], size_t n);
2015-02-09 01:52:10 +00:00
// YAPListTerm( vector<YAPTerm> v );
/// Return the number of elements in a list term.
2016-07-31 16:22:24 +01:00
size_t length() {
Term *tailp;
Term t1 = gt();
return Yap_SkipList(&t1, &tailp);
}
/// Extract the nth element.
2016-12-10 07:01:10 +00:00
Term &operator[](size_t n);
2015-02-09 01:52:10 +00:00
/// Extract the first element of a list.
///
/// @param[in] the list
2016-12-10 07:01:10 +00:00
Term car();
2015-02-09 01:52:10 +00:00
/// Extract the tail elements of a list.
///
/// @param[in] the list
2016-12-10 07:01:10 +00:00
Term cdr() {
2015-02-09 01:52:10 +00:00
Term to = gt();
2016-07-31 16:22:24 +01:00
if (IsPairTerm(to))
2016-12-10 07:01:10 +00:00
return (TailOfTerm(to));
2016-07-31 16:22:24 +01:00
else if (to == TermNil)
2016-12-10 07:01:10 +00:00
return TermNil;
2015-04-13 13:28:17 +01:00
/* error */
2016-09-21 20:41:23 +01:00
Yap_Error(TYPE_ERROR_LIST, t, 0);
throw YAPError();
2015-02-09 01:52:10 +00:00
}
2016-07-31 16:22:24 +01:00
/// copy a list.
///
/// @param[in] the list
2016-12-10 07:01:10 +00:00
Term dup();
2015-02-09 01:52:10 +00:00
/// Check if the list is empty.
///
/// @param[in] the list
2015-03-16 17:25:09 +00:00
inline bool nil() {
return gt() == TermNil;
}
2016-07-31 16:22:24 +01:00
;
2015-02-09 01:52:10 +00:00
};
/**
* @brief String Term
*/
2016-07-31 16:22:24 +01:00
class YAPStringTerm : public YAPTerm {
2015-02-09 01:52:10 +00:00
public:
/// your standard constructor
2016-07-31 16:22:24 +01:00
YAPStringTerm(char *s);
2015-02-09 01:52:10 +00:00
/// use this one to construct length limited strings
YAPStringTerm(char *s, size_t len);
/// construct using wide chars
2016-07-31 16:22:24 +01:00
YAPStringTerm(wchar_t *s);
2015-02-09 01:52:10 +00:00
/// construct using length-limited wide chars
YAPStringTerm(wchar_t *s, size_t len);
2016-07-31 16:22:24 +01:00
const char *getString() { return StringOfTerm(gt()); }
};
/**
* @brief Atom Term
* Term Representation of an Atom
*/
class YAPAtomTerm : public YAPTerm {
friend class YAPModule;
// Constructor: receives a C-atom;
YAPAtomTerm(Atom a) { mk(MkAtomTerm(a)); }
YAPAtomTerm(Term t) : YAPTerm(t) { IsAtomTerm(t); }
public:
// Constructor: receives an atom;
YAPAtomTerm(YAPAtom a) : YAPTerm() { mk(MkAtomTerm(a.a)); }
// Constructor: receives a sequence of ISO-LATIN1 codes;
2016-09-21 20:41:23 +01:00
YAPAtomTerm(char s[]);
2016-07-31 16:22:24 +01:00
// Constructor: receives a sequence of up to n ISO-LATIN1 codes;
YAPAtomTerm(char *s, size_t len);
// Constructor: receives a sequence of wchar_ts, whatever they may be;
YAPAtomTerm(wchar_t *s);
// Constructor: receives a sequence of n wchar_ts, whatever they may be;
YAPAtomTerm(wchar_t *s, size_t len);
virtual bool isVar() { return false; } /// type check for unbound
virtual bool isAtom() { return true; } /// type check for atom
virtual bool isInteger() { return false; } /// type check for integer
virtual bool isFloat() { return false; } /// type check for floating-point
virtual bool isString() { return false; } /// type check for a string " ... "
virtual bool isCompound() { return false; } /// is a primitive term
virtual bool isAppl() { return false; } /// is a structured term
virtual bool isPair() { return false; } /// is a pair term
virtual bool isGround() { return true; } /// term is ground
virtual bool isList() { return gt() == TermNil; } /// [] is a list
// Getter: outputs the atom;
YAPAtom getAtom() { return YAPAtom(AtomOfTerm(gt())); }
// Getter: outputs the name as a sequence of ISO-LATIN1 codes;
const char *text() { return (const char *)AtomOfTerm(gt())->StrOfAE; }
2015-02-09 01:52:10 +00:00
};
#endif /* YAPT_HH */