fix bad handle recovery properly.
This commit is contained in:
parent
7c8e2bbd16
commit
0aa91d7c00
336
C/write.c
336
C/write.c
@ -1,19 +1,19 @@
|
|||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
* *
|
* *
|
||||||
* YAP Prolog *
|
* YAP Prolog *
|
||||||
* *
|
* *
|
||||||
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
||||||
* *
|
* *
|
||||||
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
||||||
* *
|
* *
|
||||||
**************************************************************************
|
**************************************************************************
|
||||||
* *
|
* *
|
||||||
* File: write.c *
|
* File: write.c *
|
||||||
* Last rev: *
|
* Last rev: *
|
||||||
* mods: *
|
* mods: *
|
||||||
* comments: Writing a Prolog Term *
|
* comments: Writing a Prolog Term *
|
||||||
* *
|
* *
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
#ifdef SCCS
|
#ifdef SCCS
|
||||||
static char SccsId[] = "%W% %G%";
|
static char SccsId[] = "%W% %G%";
|
||||||
#endif
|
#endif
|
||||||
@ -98,7 +98,7 @@ static void writeTerm(Term, int, int, int, struct write_globs *, struct rewind_t
|
|||||||
/*
|
/*
|
||||||
protect bracket from merging with previoous character.
|
protect bracket from merging with previoous character.
|
||||||
avoid stuff like not (2,3) -> not(2,3) or
|
avoid stuff like not (2,3) -> not(2,3) or
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
wropen_bracket(struct write_globs *wglb, int protect)
|
wropen_bracket(struct write_globs *wglb, int protect)
|
||||||
{
|
{
|
||||||
@ -237,7 +237,7 @@ write_mpint(MP_INT *big, struct write_globs *wglb) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* writes a bignum */
|
/* writes a bignum */
|
||||||
static void
|
static void
|
||||||
writebig(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, struct rewind_term *rwt)
|
writebig(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, struct rewind_term *rwt)
|
||||||
{
|
{
|
||||||
@ -865,7 +865,7 @@ write_list(Term t, int direction, int depth, struct write_globs *wglb, struct re
|
|||||||
writeTerm(from_pointer(RepPair(t)+1, &nrwt, wglb), 999, depth, FALSE, wglb, &nrwt);
|
writeTerm(from_pointer(RepPair(t)+1, &nrwt, wglb), 999, depth, FALSE, wglb, &nrwt);
|
||||||
restore_from_write(&nrwt, wglb);
|
restore_from_write(&nrwt, wglb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -874,7 +874,6 @@ writeTerm(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, str
|
|||||||
/* context priority */
|
/* context priority */
|
||||||
|
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
CACHE_REGS
|
CACHE_REGS
|
||||||
struct rewind_term nrwt;
|
struct rewind_term nrwt;
|
||||||
nrwt.parent = rwt;
|
nrwt.parent = rwt;
|
||||||
@ -937,108 +936,6 @@ writeTerm(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, str
|
|||||||
Atom atom;
|
Atom atom;
|
||||||
int op, lp, rp;
|
int op, lp, rp;
|
||||||
|
|
||||||
if (IsExtensionFunctor(functor)) {
|
|
||||||
switch((CELL)functor) {
|
|
||||||
case (CELL)FunctorDouble:
|
|
||||||
wrputf(FloatOfTerm(t),wglb);
|
|
||||||
return;
|
|
||||||
case (CELL)FunctorString:
|
|
||||||
write_string(StringOfTerm(t),wglb);
|
|
||||||
return;
|
|
||||||
case (CELL)FunctorAttVar:
|
|
||||||
write_var(RepAppl(t)+1, wglb, &nrwt);
|
|
||||||
return;
|
|
||||||
case (CELL)FunctorDBRef:
|
|
||||||
wrputref(RefOfTerm(t), wglb->Quote_illegal, wglb);
|
|
||||||
return;
|
|
||||||
case (CELL)FunctorLongInt:
|
|
||||||
wrputn(LongIntOfTerm(t),wglb);
|
|
||||||
return;
|
|
||||||
/* case (CELL)FunctorBigInt: */
|
|
||||||
default:
|
|
||||||
writebig(t, p, depth, rinfixarg, wglb, rwt);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Arity = ArityOfFunctor(functor);
|
|
||||||
atom = NameOfFunctor(functor);
|
|
||||||
#ifdef SFUNC
|
|
||||||
if (Arity == SFArity) {
|
|
||||||
int argno = 1;
|
|
||||||
CELL *p = ArgsOfSFTerm(t);
|
|
||||||
putAtom(atom, wglb->Quote_illegal, wglb);
|
|
||||||
wropen_bracket(wglb, FALSE);
|
|
||||||
lastw = separator;
|
|
||||||
while (*p) {
|
|
||||||
Int sl = 0;
|
|
||||||
|
|
||||||
while (argno < *p) {
|
|
||||||
wrputc('_', wglb->stream), wrputc(',', wglb->stream);
|
|
||||||
++argno;
|
|
||||||
=======
|
|
||||||
CACHE_REGS
|
|
||||||
struct rewind_term nrwt;
|
|
||||||
nrwt.parent = rwt;
|
|
||||||
nrwt.u_sd.s.ptr = 0;
|
|
||||||
|
|
||||||
if (wglb->MaxDepth != 0 && depth > wglb->MaxDepth) {
|
|
||||||
putAtom(Atom3Dots, wglb->Quote_illegal, wglb);
|
|
||||||
return;
|
|
||||||
>>>>>>> 596768a56b69ad4170d5ca0f18dcc7a0f55316e9
|
|
||||||
}
|
|
||||||
if (EX)
|
|
||||||
return;
|
|
||||||
t = Deref(t);
|
|
||||||
if (IsVarTerm(t)) {
|
|
||||||
write_var((CELL *)t, wglb, &nrwt);
|
|
||||||
} else if (IsIntTerm(t)) {
|
|
||||||
wrputn((Int) IntOfTerm(t),wglb);
|
|
||||||
} else if (IsAtomTerm(t)) {
|
|
||||||
putAtom(AtomOfTerm(t), wglb->Quote_illegal, wglb);
|
|
||||||
} else if (IsPairTerm(t)) {
|
|
||||||
if (wglb->Ignore_ops) {
|
|
||||||
wrputs("'.'(",wglb->stream);
|
|
||||||
lastw = separator;
|
|
||||||
writeTerm(from_pointer(RepPair(t), &nrwt, wglb), 999, depth + 1, FALSE, wglb, &nrwt);
|
|
||||||
t = AbsPair(restore_from_write(&nrwt, wglb));
|
|
||||||
wrputs(",",wglb->stream);
|
|
||||||
writeTerm(from_pointer(RepPair(t)+1, &nrwt, wglb), 999, depth + 1, FALSE, wglb, &nrwt);
|
|
||||||
restore_from_write(&nrwt, wglb);
|
|
||||||
wrclose_bracket(wglb, TRUE);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (wglb->Use_portray) {
|
|
||||||
Term targs[1];
|
|
||||||
struct DB_TERM *old_EX = NULL;
|
|
||||||
Int sl = 0;
|
|
||||||
|
|
||||||
targs[0] = t;
|
|
||||||
Yap_PutValue(AtomPortray, MkAtomTerm(AtomNil));
|
|
||||||
if (EX) old_EX = EX;
|
|
||||||
sl = Yap_InitSlot(t PASS_REGS);
|
|
||||||
Yap_execute_goal(Yap_MkApplTerm(FunctorPortray, 1, targs), 0, 1);
|
|
||||||
t = Yap_GetFromSlot(sl PASS_REGS);
|
|
||||||
Yap_RecoverSlots(1 PASS_REGS);
|
|
||||||
if (old_EX != NULL) EX = old_EX;
|
|
||||||
if (Yap_GetValue(AtomPortray) == MkAtomTerm(AtomTrue))
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (yap_flags[WRITE_QUOTED_STRING_FLAG] && IsCodesTerm(t)) {
|
|
||||||
putString(t, wglb);
|
|
||||||
} else {
|
|
||||||
wrputc('[', wglb->stream);
|
|
||||||
lastw = separator;
|
|
||||||
/* we assume t was already saved in the stack */
|
|
||||||
write_list(t, 0, depth, wglb, rwt);
|
|
||||||
wrputc(']', wglb->stream);
|
|
||||||
lastw = separator;
|
|
||||||
}
|
|
||||||
} else { /* compound term */
|
|
||||||
Functor functor = FunctorOfTerm(t);
|
|
||||||
int Arity;
|
|
||||||
Atom atom;
|
|
||||||
int op, lp, rp;
|
|
||||||
|
|
||||||
if (IsExtensionFunctor(functor)) {
|
if (IsExtensionFunctor(functor)) {
|
||||||
switch((CELL)functor) {
|
switch((CELL)functor) {
|
||||||
case (CELL)FunctorDouble:
|
case (CELL)FunctorDouble:
|
||||||
@ -1091,7 +988,6 @@ writeTerm(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, str
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
<<<<<<< HEAD
|
|
||||||
if (wglb->Use_portray) {
|
if (wglb->Use_portray) {
|
||||||
Term targs[1];
|
Term targs[1];
|
||||||
struct DB_TERM *old_EX = NULL;
|
struct DB_TERM *old_EX = NULL;
|
||||||
@ -1200,195 +1096,6 @@ writeTerm(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, str
|
|||||||
!IsVarTerm(tright) && IsAtomTerm(tright) &&
|
!IsVarTerm(tright) && IsAtomTerm(tright) &&
|
||||||
Yap_IsOp(AtomOfTerm(tright));
|
Yap_IsOp(AtomOfTerm(tright));
|
||||||
|
|
||||||
if (op > p) {
|
|
||||||
/* avoid stuff such as \+ (a,b) being written as \+(a,b) */
|
|
||||||
wropen_bracket(wglb, TRUE);
|
|
||||||
lastw = separator;
|
|
||||||
}
|
|
||||||
if (bracket_left) {
|
|
||||||
wropen_bracket(wglb, TRUE);
|
|
||||||
}
|
|
||||||
writeTerm(from_pointer(RepAppl(t)+1, &nrwt, wglb), lp, depth + 1, rinfixarg, wglb, &nrwt);
|
|
||||||
t = AbsAppl(restore_from_write(&nrwt, wglb)-1);
|
|
||||||
if (bracket_left) {
|
|
||||||
wrclose_bracket(wglb, TRUE);
|
|
||||||
}
|
|
||||||
/* avoid quoting commas and bars */
|
|
||||||
if (!strcmp(RepAtom(atom)->StrOfAE,",")) {
|
|
||||||
wrputc(',', wglb->stream);
|
|
||||||
lastw = separator;
|
|
||||||
} else if (!strcmp(RepAtom(atom)->StrOfAE,"|")) {
|
|
||||||
wrputc('|', wglb->stream);
|
|
||||||
lastw = separator;
|
|
||||||
} else
|
|
||||||
putAtom(atom, wglb->Quote_illegal, wglb);
|
|
||||||
if (bracket_right) {
|
|
||||||
wropen_bracket(wglb, TRUE);
|
|
||||||
}
|
|
||||||
writeTerm(from_pointer(RepAppl(t)+2, &nrwt, wglb), rp, depth + 1, TRUE, wglb, &nrwt);
|
|
||||||
restore_from_write(&nrwt, wglb);
|
|
||||||
if (bracket_right) {
|
|
||||||
wrclose_bracket(wglb, TRUE);
|
|
||||||
}
|
|
||||||
if (op > p) {
|
|
||||||
wrclose_bracket(wglb, TRUE);
|
|
||||||
}
|
|
||||||
} else if (wglb->Handle_vars && functor == LOCAL_FunctorVar) {
|
|
||||||
Term ti = ArgOfTerm(1, t);
|
|
||||||
if (lastw == alphanum) {
|
|
||||||
wrputc(' ', wglb->stream);
|
|
||||||
}
|
|
||||||
if (!IsVarTerm(ti) && (IsIntTerm(ti) || IsCodesTerm(ti) || IsAtomTerm(ti))) {
|
|
||||||
if (IsIntTerm(ti)) {
|
|
||||||
Int k = IntOfTerm(ti);
|
|
||||||
if (k == -1) {
|
|
||||||
wrputc('_', wglb->stream);
|
|
||||||
lastw = alphanum;
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
wrputc((k % 26) + 'A', wglb->stream);
|
|
||||||
if (k >= 26) {
|
|
||||||
/* make sure we don't get confused about our context */
|
|
||||||
lastw = separator;
|
|
||||||
wrputn( k / 26 ,wglb);
|
|
||||||
} else
|
|
||||||
lastw = alphanum;
|
|
||||||
}
|
|
||||||
} else if (IsAtomTerm(ti)) {
|
|
||||||
putAtom(AtomOfTerm(ti), FALSE, wglb);
|
|
||||||
} else {
|
|
||||||
putUnquotedString(ti, wglb);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
wrputs("'$VAR'(",wglb->stream);
|
|
||||||
lastw = separator;
|
|
||||||
writeTerm(from_pointer(RepAppl(t)+1, &nrwt, wglb), 999, depth + 1, FALSE, wglb, &nrwt);
|
|
||||||
restore_from_write(&nrwt, wglb);
|
|
||||||
wrclose_bracket(wglb, TRUE);
|
|
||||||
}
|
|
||||||
} else if (!wglb->Ignore_ops && functor == FunctorBraces) {
|
|
||||||
wrputc('{', wglb->stream);
|
|
||||||
lastw = separator;
|
|
||||||
writeTerm(from_pointer(RepAppl(t)+1, &nrwt, wglb), 1200, depth + 1, FALSE, wglb, &nrwt);
|
|
||||||
restore_from_write(&nrwt, wglb);
|
|
||||||
wrputc('}', wglb->stream);
|
|
||||||
lastw = separator;
|
|
||||||
} else if (atom == AtomArray) {
|
|
||||||
wrputc('{', wglb->stream);
|
|
||||||
lastw = separator;
|
|
||||||
for (op = 1; op <= Arity; ++op) {
|
|
||||||
if (op == wglb->MaxArgs) {
|
|
||||||
wrputs("...", wglb->stream);
|
|
||||||
break;
|
|
||||||
=======
|
|
||||||
if (wglb->Use_portray) {
|
|
||||||
Term targs[1];
|
|
||||||
struct DB_TERM *old_EX = NULL;
|
|
||||||
Int sl = 0;
|
|
||||||
|
|
||||||
targs[0] = t;
|
|
||||||
Yap_PutValue(AtomPortray, MkAtomTerm(AtomNil));
|
|
||||||
if (EX) old_EX = EX;
|
|
||||||
sl = Yap_InitSlot(t PASS_REGS);
|
|
||||||
Yap_execute_goal(Yap_MkApplTerm(FunctorPortray, 1, targs),0, 1);
|
|
||||||
t = Yap_GetFromSlot(sl PASS_REGS);
|
|
||||||
Yap_RecoverSlots(1 PASS_REGS);
|
|
||||||
if (old_EX) EX = old_EX;
|
|
||||||
if (Yap_GetValue(AtomPortray) == MkAtomTerm(AtomTrue) || EX)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!wglb->Ignore_ops &&
|
|
||||||
Arity == 1 &&
|
|
||||||
Yap_IsPrefixOp(atom, &op, &rp)
|
|
||||||
) {
|
|
||||||
Term tright = ArgOfTerm(1, t);
|
|
||||||
int bracket_right =
|
|
||||||
!IsVarTerm(tright) && IsAtomTerm(tright) &&
|
|
||||||
Yap_IsOp(AtomOfTerm(tright));
|
|
||||||
if (op > p) {
|
|
||||||
wropen_bracket(wglb, TRUE);
|
|
||||||
}
|
|
||||||
putAtom(atom, wglb->Quote_illegal, wglb);
|
|
||||||
if (bracket_right) {
|
|
||||||
/* avoid stuff such as \+ (a,b) being written as \+(a,b) */
|
|
||||||
wropen_bracket(wglb, TRUE);
|
|
||||||
} else if (atom == AtomMinus) {
|
|
||||||
last_minus = TRUE;
|
|
||||||
}
|
|
||||||
writeTerm(from_pointer(RepAppl(t)+1, &nrwt, wglb), rp, depth + 1, TRUE, wglb, &nrwt);
|
|
||||||
restore_from_write(&nrwt, wglb);
|
|
||||||
if (bracket_right) {
|
|
||||||
wrclose_bracket(wglb, TRUE);
|
|
||||||
}
|
|
||||||
if (op > p) {
|
|
||||||
wrclose_bracket(wglb, TRUE);
|
|
||||||
}
|
|
||||||
} else if (!wglb->Ignore_ops &&
|
|
||||||
( Arity == 1 || ((atom == AtomEmptyBrackets || atom == AtomEmptyCurlyBrackets || atom == AtomEmptySquareBrackets) && !IsVarTerm(ArgOfTerm(1, t)))) &&
|
|
||||||
Yap_IsPosfixOp(atom, &op, &lp)) {
|
|
||||||
Term tleft = ArgOfTerm(1, t);
|
|
||||||
|
|
||||||
int bracket_left, offset;
|
|
||||||
|
|
||||||
if (Arity != 1) {
|
|
||||||
tleft = ArgOfTerm(1, t);
|
|
||||||
offset = 2;
|
|
||||||
} else {
|
|
||||||
tleft = ArgOfTerm(1, t);
|
|
||||||
offset = 1;
|
|
||||||
}
|
|
||||||
bracket_left =
|
|
||||||
!IsVarTerm(tleft) &&
|
|
||||||
IsAtomTerm(tleft) &&
|
|
||||||
Yap_IsOp(AtomOfTerm(tleft));
|
|
||||||
if (op > p) {
|
|
||||||
/* avoid stuff such as \+ (a,b) being written as \+(a,b) */
|
|
||||||
wropen_bracket(wglb, TRUE);
|
|
||||||
}
|
|
||||||
if (bracket_left) {
|
|
||||||
wropen_bracket(wglb, TRUE);
|
|
||||||
}
|
|
||||||
writeTerm(from_pointer(RepAppl(t)+offset, &nrwt, wglb), lp, depth + 1, rinfixarg, wglb, &nrwt);
|
|
||||||
restore_from_write(&nrwt, wglb);
|
|
||||||
if (bracket_left) {
|
|
||||||
wrclose_bracket(wglb, TRUE);
|
|
||||||
}
|
|
||||||
if (Arity > 1) {
|
|
||||||
if (atom == AtomEmptyBrackets) {
|
|
||||||
wrputc('(', wglb->stream);
|
|
||||||
} else if (atom == AtomEmptySquareBrackets) {
|
|
||||||
wrputc('[', wglb->stream);
|
|
||||||
} else if (atom == AtomEmptyCurlyBrackets) {
|
|
||||||
wrputc('{', wglb->stream);
|
|
||||||
}
|
|
||||||
lastw = separator;
|
|
||||||
write_list(ArgOfTerm(1,t), 0, depth, wglb, rwt);
|
|
||||||
if (atom == AtomEmptyBrackets) {
|
|
||||||
wrputc(')', wglb->stream);
|
|
||||||
} else if (atom == AtomEmptySquareBrackets) {
|
|
||||||
wrputc(']', wglb->stream);
|
|
||||||
} else if (atom == AtomEmptyCurlyBrackets) {
|
|
||||||
wrputc('}', wglb->stream);
|
|
||||||
}
|
|
||||||
lastw = separator;
|
|
||||||
} else {
|
|
||||||
putAtom(atom, wglb->Quote_illegal, wglb);
|
|
||||||
}
|
|
||||||
if (op > p) {
|
|
||||||
wrclose_bracket(wglb, TRUE);
|
|
||||||
}
|
|
||||||
} else if (!wglb->Ignore_ops &&
|
|
||||||
Arity == 2 && Yap_IsInfixOp(atom, &op, &lp,
|
|
||||||
&rp) ) {
|
|
||||||
Term tleft = ArgOfTerm(1, t);
|
|
||||||
Term tright = ArgOfTerm(2, t);
|
|
||||||
int bracket_left =
|
|
||||||
!IsVarTerm(tleft) && IsAtomTerm(tleft) &&
|
|
||||||
Yap_IsOp(AtomOfTerm(tleft));
|
|
||||||
int bracket_right =
|
|
||||||
!IsVarTerm(tright) && IsAtomTerm(tright) &&
|
|
||||||
Yap_IsOp(AtomOfTerm(tright));
|
|
||||||
|
|
||||||
if (op > p) {
|
if (op > p) {
|
||||||
/* avoid stuff such as \+ (a,b) being written as \+(a,b) */
|
/* avoid stuff such as \+ (a,b) being written as \+(a,b) */
|
||||||
wropen_bracket(wglb, TRUE);
|
wropen_bracket(wglb, TRUE);
|
||||||
@ -1499,15 +1206,14 @@ writeTerm(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, str
|
|||||||
}
|
}
|
||||||
wrclose_bracket(wglb, TRUE);
|
wrclose_bracket(wglb, TRUE);
|
||||||
}
|
}
|
||||||
>>>>>>> 596768a56b69ad4170d5ca0f18dcc7a0f55316e9
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Yap_plwrite(Term t, void *mywrite, int max_depth, int flags, int priority)
|
Yap_plwrite(Term t, void *mywrite, int max_depth, int flags, int priority)
|
||||||
/* term to be written */
|
/* term to be written */
|
||||||
/* consumer */
|
/* consumer */
|
||||||
/* write options */
|
/* write options */
|
||||||
{
|
{
|
||||||
struct write_globs wglb;
|
struct write_globs wglb;
|
||||||
struct rewind_term rwt;
|
struct rewind_term rwt;
|
||||||
|
@ -146,7 +146,7 @@ Yap_RecoverSlots(int n, Int topSlot USES_REGS)
|
|||||||
if (ASP+1 != LCL0+topSlot)
|
if (ASP+1 != LCL0+topSlot)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
ASP += n;
|
ASP += n;
|
||||||
ASP[old_slots+(n-old_slots+1)] = ASP[0] = MkIntTerm(old_slots-n);
|
ASP[old_slots+(n-1)] = ASP[0] = MkIntTerm(old_slots-n);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user