This commit is contained in:
Vitor Santos Costa 2018-07-21 12:08:09 +01:00
parent 562e9e5af3
commit 0c950ce5b3
8 changed files with 514 additions and 512 deletions

View File

@ -29,9 +29,7 @@ class YAPError;
extern "C" { extern "C" {
X_API extern Term YAP_MkCharPTerm( char *n); X_API extern Term YAP_MkCharPTerm(char *n);
} }
/** /**
@ -46,7 +44,7 @@ class X_API YAPTerm {
friend class YAPApplTerm; friend class YAPApplTerm;
friend class YAPListTerm; friend class YAPListTerm;
protected: protected:
yhandle_t t; /// handle to term, equivalent to term_t yhandle_t t; /// handle to term, equivalent to term_t
public: public:
@ -93,7 +91,7 @@ public:
// fprintf(stderr,"-%d,%lx,%p ",t,LOCAL_HandleBase[t] ,HR); // fprintf(stderr,"-%d,%lx,%p ",t,LOCAL_HandleBase[t] ,HR);
/* if (!t) /* if (!t)
return; return;
Yap_DebugPlWriteln(LOCAL_HandleBase[t]); // Yap_DebugPlWriteln(LOCAL_HandleBase[t]);
LOCAL_HandleBase[t] = TermFreeTerm; LOCAL_HandleBase[t] = TermFreeTerm;
while (LOCAL_HandleBase[LOCAL_CurSlot - 1] == TermFreeTerm) { while (LOCAL_HandleBase[LOCAL_CurSlot - 1] == TermFreeTerm) {
LOCAL_CurSlot--; LOCAL_CurSlot--;
@ -125,7 +123,7 @@ public:
YAPTerm tf; YAPTerm tf;
if (!IsApplTerm(t0) && !IsPairTerm(t)) if (!IsApplTerm(t0) && !IsPairTerm(t))
return (Term)0; return (Term)0;
tf = YAPTerm(ArgOfTerm(i, t0) ); tf = YAPTerm(ArgOfTerm(i, t0));
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
return tf; return tf;
}; };
@ -201,7 +199,8 @@ public:
virtual bool isGround() { return Yap_IsGroundTerm(gt()); } /// term is ground virtual bool isGround() { return Yap_IsGroundTerm(gt()); } /// term is ground
virtual bool isList() { return Yap_IsListTerm(gt()); } /// term is a list virtual bool isList() { return Yap_IsListTerm(gt()); } /// term is a list
/// extract the argument i of the term, where i in 1...arityvoid *Yap_RepStreamFromId(int sno) /// extract the argument i of the term, where i in 1...arityvoid
/// *Yap_RepStreamFromId(int sno)
virtual Term getArg(arity_t i); virtual Term getArg(arity_t i);
/// extract the arity of the term /// extract the arity of the term
@ -292,7 +291,7 @@ public:
/** /**
* @brief Compound Term * @brief Compound Term
*/ */
class X_API YAPApplTerm : public YAPTerm { class X_API YAPApplTerm : public YAPTerm {
friend class YAPTerm; friend class YAPTerm;
public: public:
@ -304,7 +303,10 @@ public:
RECOVER_MACHINE_REGS(); RECOVER_MACHINE_REGS();
}; };
YAPApplTerm(YAPFunctor f, YAPTerm ts[]); YAPApplTerm(YAPFunctor f, YAPTerm ts[]);
YAPApplTerm(const std::string s, unsigned int arity) { mk(Yap_MkNewApplTerm(Yap_MkFunctor(Yap_LookupAtom(s.c_str()), arity), arity)); }; YAPApplTerm(const std::string s, unsigned int arity) {
mk(Yap_MkNewApplTerm(Yap_MkFunctor(Yap_LookupAtom(s.c_str()), arity),
arity));
};
YAPApplTerm(const std::string s, std::vector<YAPTerm> ts); YAPApplTerm(const std::string s, std::vector<YAPTerm> ts);
YAPApplTerm(YAPFunctor f); YAPApplTerm(YAPFunctor f);
inline Functor functor() { return FunctorOfTerm(gt()); } inline Functor functor() { return FunctorOfTerm(gt()); }
@ -319,16 +321,16 @@ public:
return tf; return tf;
}; };
void putArg(int i, Term targ) { void putArg(int i, Term targ) {
//BACKUP_MACHINE_REGS(); // BACKUP_MACHINE_REGS();
Term t0 = gt(); Term t0 = gt();
RepAppl(t0)[i] = Deref(targ); RepAppl(t0)[i] = Deref(targ);
//RECOVER_MACHINE_REGS(); // RECOVER_MACHINE_REGS();
}; };
void putArg(int i, YAPTerm t) { void putArg(int i, YAPTerm t) {
//BACKUP_MACHINE_REGS(); // BACKUP_MACHINE_REGS();
Term t0 = gt(); Term t0 = gt();
RepAppl(t0)[i] = t.term(); RepAppl(t0)[i] = t.term();
//RECOVER_MACHINE_REGS(); // RECOVER_MACHINE_REGS();
}; };
virtual bool isVar() { return false; } /// type check for unbound virtual bool isVar() { return false; } /// type check for unbound
virtual bool isAtom() { return false; } /// type check for atom virtual bool isAtom() { return false; } /// type check for atom
@ -481,8 +483,9 @@ public:
YAPAtomTerm(wchar_t *s); YAPAtomTerm(wchar_t *s);
// Constructor: receives a sequence of n wchar_ts, whatever they may be; // Constructor: receives a sequence of n wchar_ts, whatever they may be;
YAPAtomTerm(wchar_t *s, size_t len); YAPAtomTerm(wchar_t *s, size_t len);
// Constructor: receives a std::string; // Constructor: receives a std::string;
// YAPAtomTerm(std::string s) { mk(MkAtomTerm(Yap_LookupAtom(s.c_str()))); }; // YAPAtomTerm(std::string s) { mk(MkAtomTerm(Yap_LookupAtom(s.c_str())));
// };
bool isVar() { return false; } /// type check for unbound bool isVar() { return false; } /// type check for unbound
bool isAtom() { return true; } /// type check for atom bool isAtom() { return true; } /// type check for atom
bool isInteger() { return false; } /// type check for integer bool isInteger() { return false; } /// type check for integer

View File

@ -278,15 +278,13 @@ static Term scanToList(TokEntry *tok, TokEntry *errtok) {
} }
tok = tok->TokNext; tok = tok->TokNext;
} }
if (ts[0])
Yap_DebugPlWriteln(ts[0]);
return ts[0]; return ts[0];
} }
/** /**
@pred scan_to_list( +Stream, -Tokens ) @pred scan_to_list( +Stream, -Tokens )
Generate a list of tokens from a scan of the (input) stream, Tokens are of the Generate a list of tokens from a scan of the (input) stream, Tokens are of
form: the form:
+ `atom`(Atom) + `atom`(Atom)
+ `<QQ>`(Text) + `<QQ>`(Text)
@ -838,7 +836,10 @@ static parser_state_t initParser(Term opts, FEnv *fe, REnv *re, int inp_stream,
fe->old_TR = TR; fe->old_TR = TR;
LOCAL_Error_TYPE = YAP_NO_ERROR; LOCAL_Error_TYPE = YAP_NO_ERROR;
__android_log_print(ANDROID_LOG_INFO, "YAPDroid ", " open %s, %d", __android_log_print(ANDROID_LOG_INFO, "YAPDroid ", " open %s, %d",
CurrentModule == 0? "prolog": RepAtom(AtomOfTerm(CurrentModule))->StrOfAE, inp_stream); CurrentModule == 0
? "prolog"
: RepAtom(AtomOfTerm(CurrentModule))->StrOfAE,
inp_stream);
LOCAL_SourceFileName = GLOBAL_Stream[inp_stream].name; LOCAL_SourceFileName = GLOBAL_Stream[inp_stream].name;
LOCAL_eot_before_eof = false; LOCAL_eot_before_eof = false;
fe->tpos = StreamPosition(inp_stream); fe->tpos = StreamPosition(inp_stream);
@ -1062,7 +1063,7 @@ Term Yap_read_term(int sno, Term opts, bool clause) {
} }
static Int static Int
read_term2(USES_REGS1) { /* '$read'(+Flag,?Term,?Module,?Vars,-Pos,-Err) */ read_term2(USES_REGS1) { /* '$read'(+Flag,?Term,?Module,?Vars,-Pos,-Err) */
return Yap_read_term(LOCAL_c_input_stream, add_output(ARG1, ARG2), false) != return Yap_read_term(LOCAL_c_input_stream, add_output(ARG1, ARG2), false) !=
0; 0;
} }
@ -1261,7 +1262,7 @@ static Int start_mega(USES_REGS1) {
] ]
} }
#endif #endif
/** /**
* @pred source_location( - _File_ , _Line_ ) * @pred source_location( - _File_ , _Line_ )
* *
* unify _File_ and _Line_ wuth the position of the last term read, if the * unify _File_ and _Line_ wuth the position of the last term read, if the
@ -1278,12 +1279,12 @@ static Int start_mega(USES_REGS1) {
* *
* @note SWI-Prolog built-in. * @note SWI-Prolog built-in.
*/ */
static Int source_location(USES_REGS1) { static Int source_location(USES_REGS1) {
return Yap_unify(ARG1, MkAtomTerm(LOCAL_SourceFileName)) && return Yap_unify(ARG1, MkAtomTerm(LOCAL_SourceFileName)) &&
Yap_unify(ARG2, MkIntegerTerm(LOCAL_SourceFileLineno)); Yap_unify(ARG2, MkIntegerTerm(LOCAL_SourceFileLineno));
} }
/** /**
* @pred read(+ Stream, -Term ) is iso * @pred read(+ Stream, -Term ) is iso
* *
* Reads term _T_ from the stream _S_ instead of from the current input * Reads term _T_ from the stream _S_ instead of from the current input
@ -1293,7 +1294,7 @@ static Int start_mega(USES_REGS1) {
* @param - _Term_ * @param - _Term_
* *
*/ */
static Int read2( static Int read2(
USES_REGS1) { /* '$read2'(+Flag,?Term,?Module,?Vars,-Pos,-Err,+Stream) */ USES_REGS1) { /* '$read2'(+Flag,?Term,?Module,?Vars,-Pos,-Err,+Stream) */
int sno; int sno;
Int out; Int out;
@ -1306,9 +1307,9 @@ static Int start_mega(USES_REGS1) {
out = Yap_read_term(sno, add_output(ARG2, TermNil), false); out = Yap_read_term(sno, add_output(ARG2, TermNil), false);
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
return out; return out;
} }
/** @pred read(- T) is iso /** @pred read(- T) is iso
Reads the next term from the current input stream, and unifies it with Reads the next term from the current input stream, and unifies it with
_T_. The term must be followed by a dot (`.`) and any blank-character _T_. The term must be followed by a dot (`.`) and any blank-character
@ -1317,24 +1318,24 @@ static Int start_mega(USES_REGS1) {
_T_ is unified with the atom `end_of_file`. Further reads from of _T_ is unified with the atom `end_of_file`. Further reads from of
the same stream may cause an error failure (see open/3). the same stream may cause an error failure (see open/3).
*/ */
static Int read1( static Int read1(
USES_REGS1) { /* '$read2'(+Flag,?Term,?Module,?Vars,-Pos,-Err,+Stream) */ USES_REGS1) { /* '$read2'(+Flag,?Term,?Module,?Vars,-Pos,-Err,+Stream) */
Term out = Yap_read_term(LOCAL_c_input_stream, add_output(ARG1, TermNil), 1); Term out = Yap_read_term(LOCAL_c_input_stream, add_output(ARG1, TermNil), 1);
return out; return out;
} }
/** @pred fileerrors /** @pred fileerrors
Switches on the file_errors flag so that in certain error conditions Switches on the file_errors flag so that in certain error conditions
Input/Output predicates will produce an appropriated message and abort. Input/Output predicates will produce an appropriated message and abort.
*/ */
static Int fileerrors(USES_REGS1) { static Int fileerrors(USES_REGS1) {
return setYapFlag(TermFileErrors, TermTrue); return setYapFlag(TermFileErrors, TermTrue);
} }
/** /**
@pred nofileerrors @pred nofileerrors
Switches off the `file_errors` flag, so that the predicates see/1, Switches off the `file_errors` flag, so that the predicates see/1,
@ -1342,13 +1343,13 @@ static Int start_mega(USES_REGS1) {
an error message and aborting whenever the specified file cannot be an error message and aborting whenever the specified file cannot be
opened or closed. opened or closed.
*/ */
static Int nofileerrors( static Int nofileerrors(
USES_REGS1) { /* '$read2'(+Flag,?Term,?Module,?Vars,-Pos,-Err,+Stream) */ USES_REGS1) { /* '$read2'(+Flag,?Term,?Module,?Vars,-Pos,-Err,+Stream) */
return setYapFlag(TermFileerrors, TermFalse); return setYapFlag(TermFileerrors, TermFalse);
} }
static Int style_checker(USES_REGS1) { static Int style_checker(USES_REGS1) {
Term t = Deref(ARG1); Term t = Deref(ARG1);
if (IsVarTerm(t)) { if (IsVarTerm(t)) {
@ -1390,34 +1391,36 @@ static Int start_mega(USES_REGS1) {
} }
} }
return TRUE; return TRUE;
} }
Term Yap_BufferToTerm(const char *s, Term opts) { Term Yap_BufferToTerm(const char *s, Term opts) {
Term rval; Term rval;
int sno; int sno;
encoding_t l = ENC_ISO_UTF8; encoding_t l = ENC_ISO_UTF8;
sno = Yap_open_buf_read_stream((char *)s, strlen(s)+1, &l, sno =
MEM_BUF_USER, Yap_LookupAtom(Yap_StrPrefix(s,16)), TermNone ); Yap_open_buf_read_stream((char *)s, strlen(s) + 1, &l, MEM_BUF_USER,
Yap_LookupAtom(Yap_StrPrefix(s, 16)), TermNone);
GLOBAL_Stream[sno].status |= CloseOnException_Stream_f; GLOBAL_Stream[sno].status |= CloseOnException_Stream_f;
rval = Yap_read_term(sno, opts, false); rval = Yap_read_term(sno, opts, false);
Yap_CloseStream(sno); Yap_CloseStream(sno);
return rval; return rval;
} }
Term Yap_UBufferToTerm(const unsigned char *s, Term opts) { Term Yap_UBufferToTerm(const unsigned char *s, Term opts) {
Term rval; Term rval;
int sno; int sno;
encoding_t l = ENC_ISO_UTF8; encoding_t l = ENC_ISO_UTF8;
sno = Yap_open_buf_read_stream((char *)s, strlen((const char *)s), &l, sno = Yap_open_buf_read_stream(
MEM_BUF_USER, Yap_LookupAtom(Yap_StrPrefix((char *)s,16)), TermNone); (char *)s, strlen((const char *)s), &l, MEM_BUF_USER,
Yap_LookupAtom(Yap_StrPrefix((char *)s, 16)), TermNone);
GLOBAL_Stream[sno].status |= CloseOnException_Stream_f; GLOBAL_Stream[sno].status |= CloseOnException_Stream_f;
rval = Yap_read_term(sno, opts, false); rval = Yap_read_term(sno, opts, false);
Yap_CloseStream(sno); Yap_CloseStream(sno);
return rval; return rval;
} }
X_API Term Yap_BufferToTermWithPrioBindings(const char *s, Term opts, X_API Term Yap_BufferToTermWithPrioBindings(const char *s, Term opts,
Term bindings, size_t len, Term bindings, size_t len,
int prio) { int prio) {
CACHE_REGS CACHE_REGS
@ -1431,9 +1434,9 @@ static Int start_mega(USES_REGS1) {
ctl = add_priority(bindings, ctl); ctl = add_priority(bindings, ctl);
} }
return Yap_BufferToTerm(s, ctl); return Yap_BufferToTerm(s, ctl);
} }
/** /**
* @pred read_term_from_atom( +Atom , -T , +Options ) * @pred read_term_from_atom( +Atom , -T , +Options )
* *
* read a term _T_ stored in constant _Atom_ according to _Options_ * read a term _T_ stored in constant _Atom_ according to _Options_
@ -1449,7 +1452,7 @@ static Int start_mega(USES_REGS1) {
*supposed to *supposed to
* use YAP's internal encoding, so please avoid the encoding/1 option. * use YAP's internal encoding, so please avoid the encoding/1 option.
*/ */
static Int read_term_from_atom(USES_REGS1) { static Int read_term_from_atom(USES_REGS1) {
Term t1 = Deref(ARG1); Term t1 = Deref(ARG1);
Atom at; Atom at;
const unsigned char *s; const unsigned char *s;
@ -1467,9 +1470,9 @@ static Int start_mega(USES_REGS1) {
Term ctl = add_output(ARG2, ARG3); Term ctl = add_output(ARG2, ARG3);
return Yap_UBufferToTerm(s, ctl); return Yap_UBufferToTerm(s, ctl);
} }
/** /**
* @pred read_term_from_atomic( +Atomic , - T , +Options ) * @pred read_term_from_atomic( +Atomic , - T , +Options )
* *
* read a term _T_ stored in text _Atomic_ according to _Options_ * read a term _T_ stored in text _Atomic_ according to _Options_
@ -1484,7 +1487,7 @@ static Int start_mega(USES_REGS1) {
*atom. *atom.
* Encoding is fixed in atoms and strings. * Encoding is fixed in atoms and strings.
*/ */
static Int read_term_from_atomic(USES_REGS1) { static Int read_term_from_atomic(USES_REGS1) {
Term t1 = Deref(ARG1); Term t1 = Deref(ARG1);
const unsigned char *s; const unsigned char *s;
@ -1501,9 +1504,9 @@ static Int start_mega(USES_REGS1) {
Term ctl = add_output(ARG2, ARG3); Term ctl = add_output(ARG2, ARG3);
return Yap_UBufferToTerm(s, ctl); return Yap_UBufferToTerm(s, ctl);
} }
/** /**
* @pred read_term_from_string( +String , - T , + Options ) * @pred read_term_from_string( +String , - T , + Options )
* *
* read a term _T_ stored in constant _String_ according to _Options_ * read a term _T_ stored in constant _String_ according to _Options_
@ -1515,7 +1518,7 @@ static Int start_mega(USES_REGS1) {
* Idea from SWI-Prolog, in YAP only works with strings * Idea from SWI-Prolog, in YAP only works with strings
* Check read_term_from_atomic/3 for the general version. * Check read_term_from_atomic/3 for the general version.
*/ */
static Int read_term_from_string(USES_REGS1) { static Int read_term_from_string(USES_REGS1) {
Term t1 = Deref(ARG1), rc; Term t1 = Deref(ARG1), rc;
const unsigned char *s; const unsigned char *s;
size_t len; size_t len;
@ -1532,7 +1535,9 @@ static Int start_mega(USES_REGS1) {
} }
char *ss = (char *)s; char *ss = (char *)s;
encoding_t enc = ENC_ISO_UTF8; encoding_t enc = ENC_ISO_UTF8;
int sno = Yap_open_buf_read_stream(ss, len, &enc, MEM_BUF_USER, Yap_LookupAtom(Yap_StrPrefix(ss,16)), TermString); int sno = Yap_open_buf_read_stream(ss, len, &enc, MEM_BUF_USER,
Yap_LookupAtom(Yap_StrPrefix(ss, 16)),
TermString);
GLOBAL_Stream[sno].status |= CloseOnException_Stream_f; GLOBAL_Stream[sno].status |= CloseOnException_Stream_f;
rc = Yap_read_term(sno, Deref(ARG3), 3); rc = Yap_read_term(sno, Deref(ARG3), 3);
Yap_CloseStream(sno); Yap_CloseStream(sno);
@ -1540,18 +1545,18 @@ static Int start_mega(USES_REGS1) {
if (!rc) if (!rc)
return false; return false;
return Yap_unify(rc, ARG2); return Yap_unify(rc, ARG2);
} }
static Int atomic_to_term(USES_REGS1) { static Int atomic_to_term(USES_REGS1) {
Term t1 = Deref(ARG1); Term t1 = Deref(ARG1);
int l = push_text_stack(); int l = push_text_stack();
const unsigned char *s = Yap_TextToUTF8Buffer(t1 PASS_REGS); const unsigned char *s = Yap_TextToUTF8Buffer(t1 PASS_REGS);
Int rc = Yap_UBufferToTerm(s, add_output(ARG2, add_names(ARG3, TermNil))); Int rc = Yap_UBufferToTerm(s, add_output(ARG2, add_names(ARG3, TermNil)));
pop_text_stack(l); pop_text_stack(l);
return rc; return rc;
} }
static Int atom_to_term(USES_REGS1) { static Int atom_to_term(USES_REGS1) {
Term t1 = Deref(ARG1); Term t1 = Deref(ARG1);
if (IsVarTerm(t1)) { if (IsVarTerm(t1)) {
Yap_Error(INSTANTIATION_ERROR, t1, "read_term_from_string/3"); Yap_Error(INSTANTIATION_ERROR, t1, "read_term_from_string/3");
@ -1564,9 +1569,9 @@ static Int start_mega(USES_REGS1) {
const unsigned char *us = UStringOfTerm(t); const unsigned char *us = UStringOfTerm(t);
return Yap_UBufferToTerm(us, add_output(ARG2, add_names(ARG3, TermNil))); return Yap_UBufferToTerm(us, add_output(ARG2, add_names(ARG3, TermNil)));
} }
} }
static Int string_to_term(USES_REGS1) { static Int string_to_term(USES_REGS1) {
Term t1 = Deref(ARG1); Term t1 = Deref(ARG1);
if (IsVarTerm(t1)) { if (IsVarTerm(t1)) {
@ -1579,9 +1584,9 @@ static Int start_mega(USES_REGS1) {
const unsigned char *us = UStringOfTerm(t1); const unsigned char *us = UStringOfTerm(t1);
return Yap_UBufferToTerm(us, add_output(ARG2, add_names(ARG3, TermNil))); return Yap_UBufferToTerm(us, add_output(ARG2, add_names(ARG3, TermNil)));
} }
} }
void Yap_InitReadTPreds(void) { void Yap_InitReadTPreds(void) {
Yap_InitCPred("read_term", 2, read_term2, SyncPredFlag); Yap_InitCPred("read_term", 2, read_term2, SyncPredFlag);
Yap_InitCPred("read_term", 3, read_term, SyncPredFlag); Yap_InitCPred("read_term", 3, read_term, SyncPredFlag);
@ -1602,4 +1607,4 @@ static Int start_mega(USES_REGS1) {
Yap_InitCPred("source_location", 2, source_location, SyncPredFlag); Yap_InitCPred("source_location", 2, source_location, SyncPredFlag);
Yap_InitCPred("$style_checker", 1, style_checker, Yap_InitCPred("$style_checker", 1, style_checker,
SyncPredFlag | HiddenPredFlag); SyncPredFlag | HiddenPredFlag);
} }

View File

@ -92,8 +92,9 @@ static Term readFromBuffer(const char *s, Term opts) {
Term rval; Term rval;
int sno; int sno;
encoding_t enc = ENC_ISO_UTF8; encoding_t enc = ENC_ISO_UTF8;
sno = Yap_open_buf_read_stream((char *)s, strlen_utf8((unsigned char *)s), sno = Yap_open_buf_read_stream(
&enc, MEM_BUF_USER, Yap_LookupAtom(Yap_StrPrefix((char *)s,16)), TermNone); (char *)s, strlen_utf8((unsigned char *)s), &enc, MEM_BUF_USER,
Yap_LookupAtom(Yap_StrPrefix((char *)s, 16)), TermNone);
rval = Yap_read_term(sno, opts, 3); rval = Yap_read_term(sno, opts, 3);
Yap_CloseStream(sno); Yap_CloseStream(sno);
@ -593,7 +594,6 @@ static Int writeln(USES_REGS1) {
return false; return false;
} }
int output_stream = Yap_CheckTextStream(ARG1, Output_Stream_f, "writeln/2"); int output_stream = Yap_CheckTextStream(ARG1, Output_Stream_f, "writeln/2");
fprintf(stderr, "writeln %d\n", output_stream);
if (output_stream < 0) { if (output_stream < 0) {
free(args); free(args);
return false; return false;
@ -692,8 +692,8 @@ static Int term_to_atom(USES_REGS1) {
Term t2 = Deref(ARG2), ctl, rc = false; Term t2 = Deref(ARG2), ctl, rc = false;
Atom at; Atom at;
if (IsVarTerm(t2)) { if (IsVarTerm(t2)) {
const char *s = Yap_TermToBuffer(Deref(ARG1), const char *s =
Quote_illegal_f | Handle_vars_f); Yap_TermToBuffer(Deref(ARG1), Quote_illegal_f | Handle_vars_f);
if (!s || !(at = Yap_UTF8ToAtom((const unsigned char *)s))) { if (!s || !(at = Yap_UTF8ToAtom((const unsigned char *)s))) {
Yap_Error(RESOURCE_ERROR_HEAP, t2, Yap_Error(RESOURCE_ERROR_HEAP, t2,
"Could not get memory from the operating system"); "Could not get memory from the operating system");
@ -713,7 +713,7 @@ static Int term_to_atom(USES_REGS1) {
char *Yap_TermToBuffer(Term t, int flags) { char *Yap_TermToBuffer(Term t, int flags) {
CACHE_REGS CACHE_REGS
int sno = Yap_open_buf_write_stream(LOCAL_encoding,flags); int sno = Yap_open_buf_write_stream(LOCAL_encoding, flags);
if (sno < 0) if (sno < 0)
return NULL; return NULL;

View File

@ -51,7 +51,6 @@ static PyObject *s_to_python(const char *s, bool eval, PyObject *p0) {
*/ */
X_API PyObject *string_to_python(const char *s, bool eval, PyObject *p0) { X_API PyObject *string_to_python(const char *s, bool eval, PyObject *p0) {
char *buf = malloc(strlen(s) + 1), *child; char *buf = malloc(strlen(s) + 1), *child;
while ((child = strchr(s, '.')) != NULL) { while ((child = strchr(s, '.')) != NULL) {
size_t len = child - s; size_t len = child - s;
@ -108,9 +107,8 @@ static bool copy_to_dictionary(PyObject *dict, term_t targ, term_t taux,
* @return a Python object descriptor or NULL if failed * @return a Python object descriptor or NULL if failed
*/ */
PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) { PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
// o≈ //
YAP_Term yt = YAP_GetFromSlot(t); YAP_Term yt = YAP_GetFromSlot(t);
Yap_DebugPlWriteln(yt);
switch (PL_term_type(t)) { switch (PL_term_type(t)) {
case PL_VARIABLE: { case PL_VARIABLE: {
if (yt == 0) { if (yt == 0) {
@ -137,7 +135,7 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
o = PyUnicode_FromString(s); o = PyUnicode_FromString(s);
} }
if (o) { if (o) {
//PyDict_SetItemString(py_Atoms, s, Py_None); // PyDict_SetItemString(py_Atoms, s, Py_None);
Py_INCREF(o); Py_INCREF(o);
return o; return o;
} }
@ -189,8 +187,8 @@ PyObject *term_to_python(term_t t, bool eval, PyObject *o, bool cvt) {
if (PL_is_pair(t)) { if (PL_is_pair(t)) {
Term t0 = Yap_GetFromHandle(t); Term t0 = Yap_GetFromHandle(t);
Term *tail; Term *tail;
size_t len,i; size_t len, i;
if ((len = Yap_SkipList(&t0, &tail))>=0 && *tail == TermNil) { if ((len = Yap_SkipList(&t0, &tail)) >= 0 && *tail == TermNil) {
PyObject *out, *a; PyObject *out, *a;
out = PyList_New(len); out = PyList_New(len);

View File

@ -957,7 +957,6 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
lhs = term_to_python(targ, true, NULL, true); lhs = term_to_python(targ, true, NULL, true);
AOK(PL_get_arg(2, t, targ), NULL); AOK(PL_get_arg(2, t, targ), NULL);
rhs = term_to_python(targ, true, NULL, true); rhs = term_to_python(targ, true, NULL, true);
Yap_DebugPlWriteln(YAP_GetFromSlot(targ));
if (PySequence_Check(lhs) && PySequence_Check(rhs)) { if (PySequence_Check(lhs) && PySequence_Check(rhs)) {
return PySequence_Concat(lhs, rhs); return PySequence_Concat(lhs, rhs);
} }
@ -1039,7 +1038,8 @@ PyObject *compound_to_pyeval(term_t t, PyObject *context, bool cvt) {
pArgs = PyTuple_New(i); pArgs = PyTuple_New(i);
} }
} }
fprintf(stderr,"Tuple %p: %s\n", pyDict, PyUnicode_AsUTF8(PyObject_Str(pyDict))); fprintf(stderr, "Tuple %p: %s\n", pyDict,
PyUnicode_AsUTF8(PyObject_Str(pyDict)));
if (!indict) { if (!indict) {
if (PL_is_variable(tleft)) { if (PL_is_variable(tleft)) {
pArg = Py_None; pArg = Py_None;

View File

@ -8,7 +8,6 @@
YAP_Term TermErrStream, TermOutStream; YAP_Term TermErrStream, TermOutStream;
static void pyflush(StreamDesc *st) { static void pyflush(StreamDesc *st) {
#if 0 #if 0
st->u.w_irl.ptr[0] = '\0'; st->u.w_irl.ptr[0] = '\0';
@ -75,8 +74,7 @@ static void *py_open(VFS_t *me, const char *name, const char *io_mode,
} }
StreamDesc *st = YAP_RepStreamFromId(sno); StreamDesc *st = YAP_RepStreamFromId(sno);
st->name = YAP_LookupAtom(name); st->name = YAP_LookupAtom(name);
if (strcmp(name, "sys.stdout") == 0 || if (strcmp(name, "sys.stdout") == 0 || strcmp(name, "sys.stderr") == 0 ||
strcmp(name, "sys.stderr") == 0 ||
strcmp(name, "input") == 0) { strcmp(name, "input") == 0) {
st->status |= Tty_Stream_f; st->status |= Tty_Stream_f;
} }
@ -131,20 +129,23 @@ static bool py_close(int sno) {
static bool getLine(StreamDesc *rl_iostream, int sno) { static bool getLine(StreamDesc *rl_iostream, int sno) {
char *myrl_line = NULL; char *myrl_line = NULL;
term_t ctk = python_acquire_GIL(); // term_t ctk = python_acquire_GIL();
PyObject_Print(rl_iostream->u.private_data, stderr, 0);
/* window of vulnerability opened */ /* window of vulnerability opened */
myrl_line = PyUnicode_AsUTF8(PyObject_CallFunctionObjArgs(rl_iostream->u.private_data, NULL)); PyObject *readl =
python_release_GIL(ctk); PyObject_GetAttrString(rl_iostream->u.private_data, "readline");
PyObject_Print(readl, stderr, 0);
myrl_line = PyUnicode_AsUTF8(
PyObject_CallFunctionObjArgs(readl, rl_iostream->u.private_data, NULL));
// python_release_GIL(ctk);
PyObject *err; PyObject *err;
if ((err = PyErr_Occurred())) { if ((err = PyErr_Occurred())) {
PyErr_SetString( PyErr_SetString(err, "Error in getLine\n");
err,
"Error in getLine\n");
Yap_ThrowError(SYSTEM_ERROR_GET_FAILED, YAP_MkIntTerm(sno), err); Yap_ThrowError(SYSTEM_ERROR_GET_FAILED, YAP_MkIntTerm(sno), err);
} }
size_t size = strlen (myrl_line)+1; size_t size = strlen(myrl_line) + 1;
rl_iostream->u.irl.ptr = rl_iostream->u.irl.buf = rl_iostream->u.irl.ptr = rl_iostream->u.irl.buf =
(const unsigned char *)malloc(size); (const unsigned char *)malloc(size);
memmove((void *)rl_iostream->u.irl.buf, myrl_line, size); memmove((void *)rl_iostream->u.irl.buf, myrl_line, size);
return true; return true;

View File

@ -79,11 +79,7 @@ close_esh( _Engine , Stream ) :-
p3_message( _Severity, Engine, error(syntax_error(Cause),info(between(_,LN,_), _FileName, CharPos, Details))) :- p3_message( _Severity, Engine, error(syntax_error(Cause),info(between(_,LN,_), _FileName, CharPos, Details))) :-
python_clear_errors, python_clear_errors,
!, !,
writeln(E), Engine.errors := [t(Cause,LN,CharPos,Details)]+Engine.errors.
NE := [t(Cause,LN,CharPos,Details)]+Engine.errors,
writeln(E),
writeln(NE),
Engine.errors := NE.
p3_message(error, Engine, E) :- p3_message(error, Engine, E) :-
python_clear_errors, python_clear_errors,
!. !.
@ -181,4 +177,3 @@ p3_message(error, Engine, E) :-
%% Self.errors := [t(C,L,N,A)] + Self.errors, %% Self.errors := [t(C,L,N,A)] + Self.errors,
%% fail. %% fail.
%% close_events( _ ). %% close_events( _ ).

View File

@ -132,7 +132,7 @@ ypp_file(File,PPFile):-
% Cmdline % Cmdline
defines2string(Defs),ypp_extcmd(Cmd), defines2string(Defs),ypp_extcmd(Cmd),
atom_concat([Cmd,' ',PPFile,' ',Defs,' ',File],Cmdline), atom_concat([Cmd,' ',PPFile,' ',Defs,' ',File],Cmdline),
write(Cmdline),nl, % write(Cmdline),nl,
% current_module(M1,M2), % current_module(M1,M2),
% write(M1:M2),nl, % write(M1:M2),nl,
system(Cmdline), system(Cmdline),