fix - and (.

This commit is contained in:
Vítor Santos Costa 2012-03-19 08:58:26 +00:00
parent 3cb8b99852
commit 0b8c998ae1

123
C/write.c
View File

@ -70,11 +70,13 @@ typedef struct write_globs {
int Keep_terms; int Keep_terms;
int Write_Loops; int Write_Loops;
int Write_strings; int Write_strings;
int last_atom_minus;
UInt MaxDepth, MaxArgs; UInt MaxDepth, MaxArgs;
wtype lw; wtype lw;
} wglbs; } wglbs;
#define lastw wglb->lw #define lastw wglb->lw
#define last_minus wglb->last_atom_minus
STATIC_PROTO(void wrputn, (Int, struct write_globs *)); STATIC_PROTO(void wrputn, (Int, struct write_globs *));
STATIC_PROTO(void wrputf, (Float, struct write_globs *)); STATIC_PROTO(void wrputf, (Float, struct write_globs *));
@ -88,19 +90,39 @@ STATIC_PROTO(void writeTerm, (Term, int, int, int, struct write_globs *, struct
#define wrputc(X,WF) Sputcode(X,WF) /* writes a character */ #define wrputc(X,WF) Sputcode(X,WF) /* writes a character */
static void
protect_open_number(struct write_globs *wglb, int minus_required)
{
wrf stream = wglb->stream;
if (lastw == symbol && last_minus && !minus_required) {
wrputc('(', wglb->stream);
} else if (lastw == symbol) {
wrputc(' ', stream);
}
}
static void
protect_close_number(struct write_globs *wglb, int minus_required)
{
if (lastw == symbol && last_minus && !minus_required) {
wrputc(')', wglb->stream);
lastw = separator;
} else {
lastw = alphanum;
}
last_minus = FALSE;
}
static void static void
wrputn(Int n, struct write_globs *wglb) /* writes an integer */ wrputn(Int n, struct write_globs *wglb) /* writes an integer */
{ {
wrf stream = wglb->stream; wrf stream = wglb->stream;
char s[256], *s1=s; /* that should be enough for most integers */ char s[256], *s1=s; /* that should be enough for most integers */
if (n < 0) { int has_minus = (n < 0);
if (lastw == symbol)
wrputc(' ', stream); protect_open_number(wglb, has_minus);
} else {
if (lastw == alphanum)
wrputc(' ', stream);
}
#if HAVE_SNPRINTF #if HAVE_SNPRINTF
snprintf(s, 256, Int_FORMAT, n); snprintf(s, 256, Int_FORMAT, n);
#else #else
@ -108,7 +130,7 @@ wrputn(Int n, struct write_globs *wglb) /* writes an integer */
#endif #endif
while (*s1) while (*s1)
wrputc(*s1++, stream); wrputc(*s1++, stream);
lastw = alphanum; protect_close_number(wglb, has_minus);
} }
#define wrputs(s, stream) Sfputs(s, stream) #define wrputs(s, stream) Sfputs(s, stream)
@ -163,15 +185,10 @@ ensure_space(size_t sz) {
static void static void
write_mpint(MP_INT *big, struct write_globs *wglb) { write_mpint(MP_INT *big, struct write_globs *wglb) {
char *s; char *s;
int has_minus = mpz_sgn(big);
s = ensure_space(3+mpz_sizeinbase(big, 10)); s = ensure_space(3+mpz_sizeinbase(big, 10));
if (mpz_sgn(big) < 0) { protect_open_number(wglb, has_minus);
if (lastw == symbol)
wrputc(' ', wglb->stream);
} else {
if (lastw == alphanum)
wrputc(' ', wglb->stream);
}
if (!s) { if (!s) {
s = mpz_get_str(NULL, 10, big); s = mpz_get_str(NULL, 10, big);
if (!s) if (!s)
@ -182,6 +199,7 @@ write_mpint(MP_INT *big, struct write_globs *wglb) {
mpz_get_str(s, 10, big); mpz_get_str(s, 10, big);
wrputs(s,wglb->stream); wrputs(s,wglb->stream);
} }
protect_close_number(wglb, has_minus);
} }
#endif #endif
@ -248,6 +266,7 @@ wrputf(Float f, struct write_globs *wglb) /* writes a float */
{ {
char s[256]; char s[256];
wrf stream = wglb->stream; wrf stream = wglb->stream;
int sgn;
#if HAVE_ISNAN || defined(__WIN32) #if HAVE_ISNAN || defined(__WIN32)
if (isnan(f)) { if (isnan(f)) {
@ -256,17 +275,19 @@ wrputf(Float f, struct write_globs *wglb) /* writes a float */
return; return;
} }
#endif #endif
sgn = (f < 0.0);
#if HAVE_ISINF || defined(_WIN32) #if HAVE_ISINF || defined(_WIN32)
if (isinf(f)) { if (isinf(f)) {
if (f < 0) { if (sgn) {
wrputs("(-inf)", stream); wrputs("(-inf)", stream);
} else { } else {
wrputs("(+inf)", stream); wrputs("(+inf)", stream);
}
lastw = separator;
return;
} }
lastw = separator;
return;
}
#endif #endif
protect_open_number(wglb, sgn);
#if THREADS #if THREADS
/* old style writing */ /* old style writing */
int found_dot = FALSE, found_exp = FALSE; int found_dot = FALSE, found_exp = FALSE;
@ -318,6 +339,7 @@ wrputf(Float f, struct write_globs *wglb) /* writes a float */
if (!buf) return; if (!buf) return;
wrputs(buf, stream); wrputs(buf, stream);
#endif #endif
protect_close_number(wglb, sgn);
} }
/* writes a data base reference */ /* writes a data base reference */
@ -399,7 +421,7 @@ AtomIsSymbols(unsigned char *s) /* Is this atom just formed by symbols ? */
if (Yap_chtype[ch] != SY) if (Yap_chtype[ch] != SY)
return(alphanum); return(alphanum);
} }
return(symbol); return symbol;
} }
static void static void
@ -506,6 +528,8 @@ putAtom(Atom atom, int Quote_illegal, struct write_globs *wglb)
return; return;
} }
#endif #endif
/* if symbol then last_minus is important */
last_minus = FALSE;
atom_or_symbol = AtomIsSymbols(s); atom_or_symbol = AtomIsSymbols(s);
if (lastw == atom_or_symbol && atom_or_symbol != separator /* solo */) if (lastw == atom_or_symbol && atom_or_symbol != separator /* solo */)
wrputc(' ', stream); wrputc(' ', stream);
@ -762,51 +786,6 @@ write_list(Term t, int direction, int depth, struct write_globs *wglb, struct re
} }
} }
/* never write '+' and '-' as infix
operators */
static int op_can_be_read_as_number(char *s, Term t)
{
if (s[0] != '+' && s[0] != '-')
return FALSE;
if (s[1])
return FALSE;
t = ArgOfTerm(1,t);
if (IsIntTerm(t)) return IntOfTerm(t) >= 0;
if (IsApplTerm(t)) {
Functor f = FunctorOfTerm(t);
if (IsExtensionFunctor(f)) {
switch((CELL)f) {
case (CELL)FunctorDouble:
return FloatOfTerm(t) >= 0.0;
case (CELL)FunctorAttVar:
case (CELL)FunctorDBRef:
return FALSE;
case (CELL)FunctorLongInt:
return LongIntOfTerm(t) >= 0;
/* case (CELL)FunctorBigInt: */
default:
{
CELL *pt = RepAppl(t)+1;
CELL big_tag = pt[0];
#ifdef USE_GMP
if (big_tag == BIG_INT || big_tag == BIG_RATIONAL) {
extern int Yap_gmp_cmp_big_int(Term t, Int i);
return Yap_gmp_cmp_big_int(t, 0) >= 0;
} else
#endif
if (big_tag == BLOB_STRING || BLOB_WIDE_STRING)
return FALSE;
else
return TRUE;
}
}
}
}
/* standard Prolog term */
return FALSE;
}
static void static void
writeTerm(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, struct rewind_term *rwt) writeTerm(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, struct rewind_term *rwt)
/* term to write */ /* term to write */
@ -944,8 +923,7 @@ writeTerm(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, str
} }
if (!wglb->Ignore_ops && if (!wglb->Ignore_ops &&
Arity == 1 && Arity == 1 &&
Yap_IsPrefixOp(atom, &op, &rp) && Yap_IsPrefixOp(atom, &op, &rp)
!op_can_be_read_as_number(RepAtom(atom)->StrOfAE, t)
) { ) {
Term tright = ArgOfTerm(1, t); Term tright = ArgOfTerm(1, t);
int bracket_right = int bracket_right =
@ -962,6 +940,8 @@ writeTerm(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, str
if (bracket_right) { if (bracket_right) {
wrputc('(', wglb->stream); wrputc('(', wglb->stream);
lastw = separator; lastw = separator;
} else if (atom == AtomMinus) {
last_minus = TRUE;
} }
writeTerm(from_pointer(RepAppl(t)+1, &nrwt, wglb), rp, depth + 1, FALSE, wglb, &nrwt); writeTerm(from_pointer(RepAppl(t)+1, &nrwt, wglb), rp, depth + 1, FALSE, wglb, &nrwt);
restore_from_write(&nrwt, wglb); restore_from_write(&nrwt, wglb);
@ -1150,6 +1130,7 @@ Yap_plwrite(Term t, void *mywrite, int max_depth, int flags, int priority)
wglb.stream = mywrite; wglb.stream = mywrite;
wglb.lw = separator; wglb.lw = separator;
wglb.last_atom_minus = FALSE;
wglb.Quote_illegal = flags & Quote_illegal_f; wglb.Quote_illegal = flags & Quote_illegal_f;
wglb.Handle_vars = flags & Handle_vars_f; wglb.Handle_vars = flags & Handle_vars_f;
wglb.Use_portray = flags & Use_portray_f; wglb.Use_portray = flags & Use_portray_f;