2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: write.c *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: Writing a Prolog Term *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "%W% %G%";
|
|
|
|
#endif
|
|
|
|
|
2009-05-22 18:24:30 +01:00
|
|
|
#include <stdlib.h>
|
2010-05-11 14:44:55 +01:00
|
|
|
#include <math.h>
|
2001-04-09 20:54:03 +01:00
|
|
|
#include "Yap.h"
|
|
|
|
#include "Yatom.h"
|
2009-10-23 14:22:17 +01:00
|
|
|
#include "YapHeap.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
#include "yapio.h"
|
2010-11-01 21:28:18 +00:00
|
|
|
#include "clause.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
#if COROUTINING
|
|
|
|
#include "attvar.h"
|
|
|
|
#endif
|
2013-11-13 12:57:52 +00:00
|
|
|
#include "pl-shared.h"
|
2013-12-02 14:49:41 +00:00
|
|
|
#include "pl-utf8.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
#if HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_CTYPE_H
|
|
|
|
#include <ctype.h>
|
|
|
|
#endif
|
2013-11-23 12:34:49 +00:00
|
|
|
#if HAVE_LOCALE_H
|
|
|
|
#include <locale.h>
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
/* describe the type of the previous term to have been written */
|
|
|
|
typedef enum {
|
|
|
|
separator, /* the previous term was a separator like ',', ')', ... */
|
|
|
|
alphanum, /* the previous term was an atom or number */
|
|
|
|
symbol /* the previous term was a symbol like +, -, *, .... */
|
|
|
|
} wtype;
|
|
|
|
|
2012-02-13 23:07:31 +00:00
|
|
|
typedef void *wrf;
|
2002-11-20 20:00:56 +00:00
|
|
|
|
2009-05-24 21:14:23 +01:00
|
|
|
typedef struct union_slots {
|
2010-05-10 10:21:56 +01:00
|
|
|
Int old;
|
|
|
|
Int ptr;
|
2009-05-24 21:14:23 +01:00
|
|
|
} uslots;
|
|
|
|
|
|
|
|
typedef struct union_direct {
|
2009-05-22 18:24:30 +01:00
|
|
|
Term old;
|
|
|
|
CELL *ptr;
|
2009-05-24 21:14:23 +01:00
|
|
|
} udirect;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct rewind_term {
|
|
|
|
struct rewind_term *parent;
|
|
|
|
union {
|
|
|
|
struct union_slots s;
|
|
|
|
struct union_direct d;
|
2014-02-18 09:44:01 +00:00
|
|
|
} u_sd;
|
2009-05-22 18:24:30 +01:00
|
|
|
} rwts;
|
|
|
|
|
2002-11-20 20:00:56 +00:00
|
|
|
typedef struct write_globs {
|
2013-12-02 14:49:41 +00:00
|
|
|
IOSTREAM*stream;
|
2012-03-22 21:59:04 +00:00
|
|
|
int Quote_illegal, Ignore_ops, Handle_vars, Use_portray, Portray_delays;
|
2012-02-14 09:10:07 +00:00
|
|
|
int Keep_terms;
|
2009-05-22 18:24:30 +01:00
|
|
|
int Write_Loops;
|
2012-02-13 23:07:31 +00:00
|
|
|
int Write_strings;
|
2012-03-19 08:58:26 +00:00
|
|
|
int last_atom_minus;
|
2009-05-25 01:37:07 +01:00
|
|
|
UInt MaxDepth, MaxArgs;
|
2012-02-17 13:41:05 +00:00
|
|
|
wtype lw;
|
2002-11-20 20:00:56 +00:00
|
|
|
} wglbs;
|
|
|
|
|
2012-02-17 13:41:05 +00:00
|
|
|
#define lastw wglb->lw
|
2012-03-19 08:58:26 +00:00
|
|
|
#define last_minus wglb->last_atom_minus
|
2012-02-17 13:41:05 +00:00
|
|
|
|
2013-04-25 23:15:04 +01:00
|
|
|
static void wrputn(Int, struct write_globs *);
|
|
|
|
static void wrputf(Float, struct write_globs *);
|
|
|
|
static void wrputref(CODEADDR, int, struct write_globs *);
|
|
|
|
static int legalAtom(unsigned char *);
|
|
|
|
/*static int LeftOpToProtect(Atom, int);
|
|
|
|
static int RightOpToProtect(Atom, int);*/
|
|
|
|
static wtype AtomIsSymbols(unsigned char *);
|
|
|
|
static void putAtom(Atom, int, struct write_globs *);
|
|
|
|
static void writeTerm(Term, int, int, int, struct write_globs *, struct rewind_term *);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2012-02-13 23:07:31 +00:00
|
|
|
#define wrputc(X,WF) Sputcode(X,WF) /* writes a character */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2012-03-27 16:44:11 +01:00
|
|
|
/*
|
|
|
|
protect bracket from merging with previoous character.
|
|
|
|
avoid stuff like not (2,3) -> not(2,3) or
|
|
|
|
*/
|
2012-03-19 08:58:26 +00:00
|
|
|
static void
|
2012-03-27 16:44:11 +01:00
|
|
|
wropen_bracket(struct write_globs *wglb, int protect)
|
2012-03-19 08:58:26 +00:00
|
|
|
{
|
|
|
|
wrf stream = wglb->stream;
|
|
|
|
|
2012-03-27 16:44:11 +01:00
|
|
|
if (lastw != separator && protect)
|
|
|
|
wrputc(' ', stream);
|
|
|
|
wrputc('(', stream);
|
|
|
|
lastw = separator;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
wrclose_bracket(struct write_globs *wglb, int protect)
|
|
|
|
{
|
|
|
|
wrf stream = wglb->stream;
|
|
|
|
|
|
|
|
wrputc(')', stream);
|
|
|
|
lastw = separator;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
protect_open_number(struct write_globs *wglb, int lm, int minus_required)
|
|
|
|
{
|
|
|
|
wrf stream = wglb->stream;
|
|
|
|
|
|
|
|
if (lastw == symbol && lm && !minus_required) {
|
|
|
|
wropen_bracket(wglb, TRUE);
|
|
|
|
return TRUE;
|
2012-03-27 15:19:07 +01:00
|
|
|
} else if (lastw == alphanum ||
|
|
|
|
(lastw == symbol && minus_required)) {
|
2012-03-19 08:58:26 +00:00
|
|
|
wrputc(' ', stream);
|
|
|
|
}
|
2012-03-27 16:44:11 +01:00
|
|
|
return FALSE;
|
2012-03-19 08:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-27 16:44:11 +01:00
|
|
|
protect_close_number(struct write_globs *wglb, int used_bracket)
|
2012-03-19 08:58:26 +00:00
|
|
|
{
|
2012-03-27 16:44:11 +01:00
|
|
|
if (used_bracket) {
|
|
|
|
wrclose_bracket(wglb, TRUE);
|
2012-03-19 08:58:26 +00:00
|
|
|
} else {
|
|
|
|
lastw = alphanum;
|
|
|
|
}
|
|
|
|
last_minus = FALSE;
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static void
|
2012-02-17 13:41:05 +00:00
|
|
|
wrputn(Int n, struct write_globs *wglb) /* writes an integer */
|
2012-02-13 23:07:31 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2012-02-17 13:41:05 +00:00
|
|
|
wrf stream = wglb->stream;
|
2001-04-09 20:54:03 +01:00
|
|
|
char s[256], *s1=s; /* that should be enough for most integers */
|
2012-03-19 08:58:26 +00:00
|
|
|
int has_minus = (n < 0);
|
2012-03-27 16:44:11 +01:00
|
|
|
int ob;
|
2012-03-19 08:58:26 +00:00
|
|
|
|
2012-03-27 16:44:11 +01:00
|
|
|
ob = protect_open_number(wglb, last_minus, has_minus);
|
2001-04-09 20:54:03 +01:00
|
|
|
#if HAVE_SNPRINTF
|
2010-05-06 15:00:44 +01:00
|
|
|
snprintf(s, 256, Int_FORMAT, n);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2010-05-06 15:00:44 +01:00
|
|
|
sprintf(s, Int_FORMAT, n);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
while (*s1)
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(*s1++, stream);
|
2012-03-27 16:44:11 +01:00
|
|
|
protect_close_number(wglb, ob);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2012-02-13 23:07:31 +00:00
|
|
|
#define wrputs(s, stream) Sfputs(s, stream)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static void
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputws(wchar_t *s, wrf stream) /* writes a string */
|
2006-11-27 17:42:03 +00:00
|
|
|
{
|
|
|
|
while (*s)
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(*s++, stream);
|
2006-11-27 17:42:03 +00:00
|
|
|
}
|
|
|
|
|
2010-05-27 12:24:15 +01:00
|
|
|
#ifdef USE_GMP
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ensure_space(size_t sz) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2010-05-27 12:24:15 +01:00
|
|
|
char *s;
|
|
|
|
|
|
|
|
s = (char *) Yap_PreAllocCodeSpace();
|
|
|
|
while (s+sz >= (char *)AuxSp) {
|
|
|
|
#if USE_SYSTEM_MALLOC
|
|
|
|
/* may require stack expansion */
|
|
|
|
if (!Yap_ExpandPreAllocCodeSpace(sz, NULL, TRUE)) {
|
|
|
|
s = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s = (char *) Yap_PreAllocCodeSpace();
|
|
|
|
#else
|
|
|
|
s = NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (!s) {
|
|
|
|
s = (char *)TR;
|
2011-05-23 16:19:47 +01:00
|
|
|
while (s+sz >= LOCAL_TrailTop) {
|
2010-05-27 12:24:15 +01:00
|
|
|
if (!Yap_growtrail(sz/sizeof(CELL), FALSE)) {
|
|
|
|
s = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s = (char *)TR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!s) {
|
2014-01-19 21:15:05 +00:00
|
|
|
s = (char *)HR;
|
2010-05-27 12:24:15 +01:00
|
|
|
if (s+sz >= (char *)ASP) {
|
|
|
|
Yap_Error(OUT_OF_STACK_ERROR,TermNil,"not enough space to write bignum: it requires %d bytes", sz);
|
|
|
|
s = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-02-17 13:41:05 +00:00
|
|
|
write_mpint(MP_INT *big, struct write_globs *wglb) {
|
2010-05-27 12:24:15 +01:00
|
|
|
char *s;
|
2012-03-19 08:58:26 +00:00
|
|
|
int has_minus = mpz_sgn(big);
|
2012-03-27 16:44:11 +01:00
|
|
|
int ob;
|
2010-05-27 12:24:15 +01:00
|
|
|
|
|
|
|
s = ensure_space(3+mpz_sizeinbase(big, 10));
|
2012-03-27 16:44:11 +01:00
|
|
|
ob = protect_open_number(wglb, last_minus, has_minus);
|
2010-05-27 12:24:15 +01:00
|
|
|
if (!s) {
|
|
|
|
s = mpz_get_str(NULL, 10, big);
|
|
|
|
if (!s)
|
|
|
|
return;
|
2012-02-17 13:41:05 +00:00
|
|
|
wrputs(s,wglb->stream);
|
2010-05-27 12:24:15 +01:00
|
|
|
free(s);
|
|
|
|
} else {
|
|
|
|
mpz_get_str(s, 10, big);
|
2012-02-17 13:41:05 +00:00
|
|
|
wrputs(s,wglb->stream);
|
2010-05-27 12:24:15 +01:00
|
|
|
}
|
2012-03-27 16:44:11 +01:00
|
|
|
protect_close_number(wglb, ob);
|
2010-05-27 12:24:15 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-05-28 09:53:56 +01:00
|
|
|
/* writes a bignum */
|
2010-05-27 12:24:15 +01:00
|
|
|
static void
|
2010-05-28 09:53:56 +01:00
|
|
|
writebig(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, struct rewind_term *rwt)
|
2010-05-27 12:24:15 +01:00
|
|
|
{
|
|
|
|
CELL *pt = RepAppl(t)+1;
|
2012-02-14 07:46:37 +00:00
|
|
|
CELL big_tag = pt[0];
|
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
if (big_tag == ARRAY_INT || big_tag == ARRAY_FLOAT) {
|
|
|
|
wrputc('{', wglb->stream);
|
|
|
|
wrputs("...", wglb->stream);
|
|
|
|
wrputc('}', wglb->stream);
|
|
|
|
lastw = separator;
|
|
|
|
return;
|
2011-06-21 16:53:17 +01:00
|
|
|
#ifdef USE_GMP
|
2013-12-02 14:49:41 +00:00
|
|
|
} else if (big_tag == BIG_INT)
|
2010-05-27 12:24:15 +01:00
|
|
|
{
|
|
|
|
MP_INT *big = Yap_BigIntOfTerm(t);
|
2012-02-17 13:41:05 +00:00
|
|
|
write_mpint(big, wglb);
|
2010-05-27 12:24:15 +01:00
|
|
|
return;
|
2012-02-14 07:46:37 +00:00
|
|
|
} else if (big_tag == BIG_RATIONAL) {
|
2010-05-28 09:53:56 +01:00
|
|
|
Term trat = Yap_RatTermToApplTerm(t);
|
|
|
|
writeTerm(trat, p, depth, rinfixarg, wglb, rwt);
|
2010-05-27 12:24:15 +01:00
|
|
|
return;
|
|
|
|
#endif
|
2012-02-14 07:46:37 +00:00
|
|
|
} else if (big_tag >= USER_BLOB_START && big_tag < USER_BLOB_END) {
|
|
|
|
Opaque_CallOnWrite f;
|
|
|
|
CELL blob_info;
|
|
|
|
|
|
|
|
blob_info = big_tag - USER_BLOB_START;
|
|
|
|
if (GLOBAL_OpaqueHandlers &&
|
|
|
|
(f= GLOBAL_OpaqueHandlers[blob_info].write_handler)) {
|
2012-12-10 12:48:02 +00:00
|
|
|
(f)(wglb->stream, big_tag, ExternalBlobFromTerm(t), 0);
|
2013-09-28 18:20:04 +01:00
|
|
|
return;
|
2012-02-14 07:46:37 +00:00
|
|
|
}
|
2011-06-21 15:11:07 +01:00
|
|
|
}
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputs("0",wglb->stream);
|
2010-05-27 12:24:15 +01:00
|
|
|
}
|
|
|
|
|
2006-11-27 17:42:03 +00:00
|
|
|
static void
|
2012-02-17 13:41:05 +00:00
|
|
|
wrputf(Float f, struct write_globs *wglb) /* writes a float */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
{
|
2012-02-17 13:41:05 +00:00
|
|
|
char s[256];
|
|
|
|
wrf stream = wglb->stream;
|
2012-03-19 08:58:26 +00:00
|
|
|
int sgn;
|
2012-03-27 16:44:11 +01:00
|
|
|
int ob;
|
|
|
|
|
2012-02-17 13:41:05 +00:00
|
|
|
|
|
|
|
#if HAVE_ISNAN || defined(__WIN32)
|
|
|
|
if (isnan(f)) {
|
|
|
|
wrputs("(nan)", stream);
|
|
|
|
lastw = separator;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2012-03-19 08:58:26 +00:00
|
|
|
sgn = (f < 0.0);
|
2012-02-17 13:41:05 +00:00
|
|
|
#if HAVE_ISINF || defined(_WIN32)
|
2012-03-19 08:58:26 +00:00
|
|
|
if (isinf(f)) {
|
|
|
|
if (sgn) {
|
|
|
|
wrputs("(-inf)", stream);
|
|
|
|
} else {
|
|
|
|
wrputs("(+inf)", stream);
|
2012-02-17 13:41:05 +00:00
|
|
|
}
|
2012-03-19 08:58:26 +00:00
|
|
|
lastw = separator;
|
|
|
|
return;
|
|
|
|
}
|
2012-02-17 13:41:05 +00:00
|
|
|
#endif
|
2012-03-27 16:44:11 +01:00
|
|
|
ob = protect_open_number(wglb, last_minus, sgn);
|
2012-02-17 13:41:05 +00:00
|
|
|
#if THREADS
|
|
|
|
/* old style writing */
|
2013-07-07 22:15:25 +01:00
|
|
|
int found_dot = FALSE;
|
2012-02-17 13:41:05 +00:00
|
|
|
char *pt = s;
|
|
|
|
int ch;
|
2013-11-23 12:34:49 +00:00
|
|
|
/* always use C locale for writing numbers */
|
|
|
|
#if O_LOCALE
|
|
|
|
const unsigned char *decimalpoint = (unsigned char*)
|
|
|
|
localeconv()->decimal_point;
|
2013-11-23 14:51:33 +00:00
|
|
|
size_t l1 = strlen((const char *)decimalpoint+1);
|
2013-11-23 12:34:49 +00:00
|
|
|
#else
|
|
|
|
const unsigned char *decimalpoint = ".";
|
|
|
|
l1 = 0;
|
|
|
|
#endif
|
2012-02-17 13:41:05 +00:00
|
|
|
|
2012-03-18 00:08:10 +00:00
|
|
|
if (lastw == symbol || lastw == alphanum) {
|
2012-02-17 13:41:05 +00:00
|
|
|
wrputc(' ', stream);
|
|
|
|
}
|
|
|
|
lastw = alphanum;
|
|
|
|
// sprintf(s, "%.15g", f);
|
|
|
|
sprintf(s, RepAtom(AtomFloatFormat)->StrOfAE, f);
|
|
|
|
while (*pt == ' ')
|
|
|
|
pt++;
|
|
|
|
if (*pt == '-') {
|
|
|
|
wrputc('-', stream);
|
|
|
|
pt++;
|
|
|
|
}
|
|
|
|
while ((ch = *pt) != '\0') {
|
2013-11-23 12:34:49 +00:00
|
|
|
// skip locale
|
|
|
|
if (ch == decimalpoint[0] && !strncmp(pt+1, (char *)decimalpoint+1, l1)) {
|
2012-02-17 13:41:05 +00:00
|
|
|
found_dot = TRUE;
|
2013-11-23 12:34:49 +00:00
|
|
|
pt += l1;
|
|
|
|
ch = '.';
|
|
|
|
}
|
|
|
|
if (ch == 'e' || ch == 'E') {
|
2012-02-17 13:41:05 +00:00
|
|
|
if (!found_dot) {
|
|
|
|
found_dot = TRUE;
|
2013-11-23 14:51:33 +00:00
|
|
|
wrputs(".0" , stream);
|
2012-02-17 13:41:05 +00:00
|
|
|
}
|
2013-11-23 12:34:49 +00:00
|
|
|
found_dot = TRUE;
|
2012-02-17 13:41:05 +00:00
|
|
|
}
|
2013-11-23 12:34:49 +00:00
|
|
|
wrputc(ch, stream);
|
2012-02-17 13:41:05 +00:00
|
|
|
pt++;
|
|
|
|
}
|
|
|
|
if (!found_dot) {
|
2013-11-23 14:51:33 +00:00
|
|
|
wrputs(".0", stream);
|
2012-02-17 13:41:05 +00:00
|
|
|
}
|
|
|
|
#else
|
2012-02-13 23:07:31 +00:00
|
|
|
char *format_float(double f, char *buf);
|
|
|
|
char *buf;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2012-03-18 00:08:10 +00:00
|
|
|
if (lastw == symbol || lastw == alphanum) {
|
|
|
|
wrputc(' ', stream);
|
|
|
|
}
|
2012-02-13 23:07:31 +00:00
|
|
|
/* use SWI's format_float */
|
|
|
|
buf = format_float(f, s);
|
|
|
|
if (!buf) return;
|
|
|
|
wrputs(buf, stream);
|
2012-02-17 13:41:05 +00:00
|
|
|
#endif
|
2012-03-27 16:44:11 +01:00
|
|
|
protect_close_number(wglb, ob);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2013-12-02 14:49:41 +00:00
|
|
|
int
|
|
|
|
Yap_FormatFloat( Float f, const char *s, size_t sz )
|
|
|
|
{
|
|
|
|
struct write_globs wglb;
|
|
|
|
char *ws = (char *)s;
|
|
|
|
IOSTREAM *smem = Sopenmem(&ws, &sz, "w");
|
|
|
|
wglb.stream = smem;
|
|
|
|
wglb.lw = separator;
|
|
|
|
wglb.last_atom_minus = FALSE;
|
|
|
|
wrputf(f, &wglb);
|
|
|
|
Sclose(smem);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-17 13:41:05 +00:00
|
|
|
/* writes a data base reference */
|
2001-04-09 20:54:03 +01:00
|
|
|
static void
|
2012-02-17 13:41:05 +00:00
|
|
|
wrputref(CODEADDR ref, int Quote_illegal, struct write_globs *wglb)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
char s[256];
|
2012-02-17 13:41:05 +00:00
|
|
|
wrf stream = wglb->stream;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2012-02-17 13:41:05 +00:00
|
|
|
putAtom(AtomDBref, Quote_illegal, wglb);
|
2010-01-21 15:05:01 +00:00
|
|
|
#if defined(__linux__) || defined(__APPLE__)
|
2010-11-01 21:28:18 +00:00
|
|
|
sprintf(s, "(%p," UInt_FORMAT ")", ref, ((LogUpdClause*)ref)->ClRefCount);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2010-11-01 21:28:18 +00:00
|
|
|
sprintf(s, "(0x%p," UInt_FORMAT ")", ref, ((LogUpdClause*)ref)->ClRefCount);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputs(s, stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = alphanum;
|
|
|
|
}
|
|
|
|
|
2012-02-27 08:53:18 +00:00
|
|
|
/* writes a blob (default) */
|
2013-11-23 15:19:54 +00:00
|
|
|
static int
|
|
|
|
wrputblob(AtomEntry * ref, int Quote_illegal, struct write_globs *wglb)
|
2012-02-27 08:53:18 +00:00
|
|
|
{
|
|
|
|
char s[256];
|
|
|
|
wrf stream = wglb->stream;
|
2013-11-23 15:19:54 +00:00
|
|
|
PL_blob_t *type = RepBlobProp(ref->PropsOfAE)->blob_t;
|
2012-02-27 08:53:18 +00:00
|
|
|
|
2013-11-23 15:19:54 +00:00
|
|
|
if (type->write) {
|
|
|
|
atom_t at = YAP_SWIAtomFromAtom(AbsAtom(ref));
|
|
|
|
return type->write(stream, at, 0);
|
|
|
|
} else {
|
|
|
|
putAtom(AtomSWIStream, Quote_illegal, wglb);
|
2012-02-27 08:53:18 +00:00
|
|
|
#if defined(__linux__) || defined(__APPLE__)
|
2013-11-23 15:19:54 +00:00
|
|
|
sprintf(s, "(%p)", ref);
|
2012-02-27 08:53:18 +00:00
|
|
|
#else
|
2013-11-23 15:19:54 +00:00
|
|
|
sprintf(s, "(0x%p)", ref);
|
2012-02-27 08:53:18 +00:00
|
|
|
#endif
|
2013-11-23 15:19:54 +00:00
|
|
|
wrputs(s, stream);
|
|
|
|
}
|
2012-02-27 08:53:18 +00:00
|
|
|
lastw = alphanum;
|
2013-12-02 14:49:41 +00:00
|
|
|
return 1;
|
2012-02-27 08:53:18 +00:00
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static int
|
2007-10-02 13:32:46 +01:00
|
|
|
legalAtom(unsigned char *s) /* Is this a legal atom ? */
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2007-10-02 13:32:46 +01:00
|
|
|
wchar_t ch = *s;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
if (ch == '\0')
|
2010-11-26 23:36:50 +00:00
|
|
|
return FALSE;
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_chtype[ch] != LC) {
|
2010-11-26 23:36:50 +00:00
|
|
|
if (ch == '[') {
|
|
|
|
return (s[1] == ']' && !s[2]);
|
|
|
|
} else if (ch == '{') {
|
|
|
|
return (s[1] == '}' && !s[2]);
|
|
|
|
} else if (Yap_chtype[ch] == SL) {
|
|
|
|
return (!s[1]);
|
2013-12-20 00:34:15 +00:00
|
|
|
} else if ((ch == ',' || ch == '.') && !s[1]) {
|
2008-10-23 22:17:45 +01:00
|
|
|
return FALSE;
|
2010-11-26 23:36:50 +00:00
|
|
|
} else {
|
2011-04-22 15:29:41 +01:00
|
|
|
if (ch == '/') {
|
|
|
|
if (s[1] == '*') return FALSE;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
while (ch) {
|
2010-11-26 23:36:50 +00:00
|
|
|
if (Yap_chtype[ch] != SY) {
|
2008-10-23 22:17:45 +01:00
|
|
|
return FALSE;
|
2010-11-26 23:36:50 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
ch = *++s;
|
|
|
|
}
|
2010-11-26 23:36:50 +00:00
|
|
|
}
|
2008-10-23 22:17:45 +01:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else
|
|
|
|
while ((ch = *++s) != 0)
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_chtype[ch] > NU)
|
2008-10-23 22:17:45 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
return (TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static wtype
|
2007-10-02 13:32:46 +01:00
|
|
|
AtomIsSymbols(unsigned char *s) /* Is this atom just formed by symbols ? */
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
int ch;
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_chtype[(int)s[0]] == SL && s[1] == '\0')
|
2001-04-09 20:54:03 +01:00
|
|
|
return(separator);
|
|
|
|
while ((ch = *s++) != '\0') {
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_chtype[ch] != SY)
|
2012-03-27 15:19:07 +01:00
|
|
|
return alphanum;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2012-03-19 08:58:26 +00:00
|
|
|
return symbol;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2008-10-23 22:17:45 +01:00
|
|
|
static void
|
2013-12-05 21:26:05 +00:00
|
|
|
write_quoted(wchar_t ch, wchar_t quote, wrf stream)
|
2008-10-23 22:17:45 +01:00
|
|
|
{
|
2013-11-13 10:38:20 +00:00
|
|
|
CACHE_REGS
|
|
|
|
if (!(Yap_GetModuleEntry(CurrentModule)->flags & M_CHARESCAPE)) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(ch, stream);
|
2008-10-23 22:17:45 +01:00
|
|
|
if (ch == '\'')
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('\'', stream); /* be careful about quotes */
|
2008-10-23 22:17:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(ch < 0xff && chtype(ch) == BS) && ch != '\'' && ch != '\\') {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(ch, stream);
|
2008-10-23 22:17:45 +01:00
|
|
|
} else {
|
|
|
|
switch (ch) {
|
|
|
|
case '\\':
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('\\', stream);
|
2013-12-05 21:26:05 +00:00
|
|
|
wrputc('\\', stream);
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
if (ch == quote)
|
|
|
|
wrputc('\\', stream);
|
|
|
|
wrputc(ch, stream);
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
if (ch == quote)
|
|
|
|
wrputc('\\', stream);
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(ch, stream);
|
2008-10-23 22:17:45 +01:00
|
|
|
break;
|
|
|
|
case 7:
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('\\', stream);
|
|
|
|
wrputc('a', stream);
|
2008-10-23 22:17:45 +01:00
|
|
|
break;
|
|
|
|
case '\b':
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('\\', stream);
|
|
|
|
wrputc('b', stream);
|
2008-10-23 22:17:45 +01:00
|
|
|
break;
|
|
|
|
case '\t':
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('\\', stream);
|
|
|
|
wrputc('t', stream);
|
2008-10-23 22:17:45 +01:00
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
case 160:
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(' ', stream);
|
2008-10-23 22:17:45 +01:00
|
|
|
break;
|
|
|
|
case '\n':
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('\\', stream);
|
|
|
|
wrputc('n', stream);
|
2008-10-23 22:17:45 +01:00
|
|
|
break;
|
|
|
|
case 11:
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('\\', stream);
|
|
|
|
wrputc('v', stream);
|
2008-10-23 22:17:45 +01:00
|
|
|
break;
|
|
|
|
case '\r':
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('\\', stream);
|
|
|
|
wrputc('r', stream);
|
2008-10-23 22:17:45 +01:00
|
|
|
break;
|
|
|
|
case '\f':
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('\\', stream);
|
|
|
|
wrputc('f', stream);
|
2008-10-23 22:17:45 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ( ch <= 0xff ) {
|
|
|
|
char esc[8];
|
|
|
|
|
2013-11-13 10:38:20 +00:00
|
|
|
/* last backslash in ISO mode */
|
|
|
|
sprintf(esc, "\\%03o\\", ch);
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputs(esc, stream);
|
2008-10-23 22:17:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-05 21:26:05 +00:00
|
|
|
static void
|
|
|
|
write_string(const char *s, struct write_globs *wglb) /* writes an integer */
|
|
|
|
{
|
|
|
|
IOSTREAM *stream = wglb->stream;
|
|
|
|
int chr;
|
|
|
|
char *ptr = (char *)s;
|
|
|
|
|
|
|
|
if (wglb->Write_strings)
|
|
|
|
wrputc('`', stream);
|
|
|
|
else
|
|
|
|
wrputc('"', stream);
|
|
|
|
do {
|
|
|
|
ptr = utf8_get_char(ptr, &chr);
|
|
|
|
if (chr == '\0') break;
|
|
|
|
write_quoted(chr, '"', stream);
|
|
|
|
} while (TRUE);
|
|
|
|
if (wglb->Write_strings)
|
|
|
|
wrputc('`', stream);
|
|
|
|
else
|
|
|
|
wrputc('"', stream);
|
|
|
|
}
|
|
|
|
|
2008-10-23 22:17:45 +01:00
|
|
|
|
2012-02-17 13:41:05 +00:00
|
|
|
/* writes an atom */
|
2001-04-09 20:54:03 +01:00
|
|
|
static void
|
2012-02-17 13:41:05 +00:00
|
|
|
putAtom(Atom atom, int Quote_illegal, struct write_globs *wglb)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2012-02-27 08:53:18 +00:00
|
|
|
unsigned char *s;
|
|
|
|
wtype atom_or_symbol;
|
2012-02-17 13:41:05 +00:00
|
|
|
wrf stream = wglb->stream;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-02-13 01:29:25 +00:00
|
|
|
if (IsBlob(atom)) {
|
2014-02-20 10:38:18 +00:00
|
|
|
wrputblob(RepAtom(atom),Quote_illegal,wglb);
|
2011-02-13 01:29:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-11-27 17:42:03 +00:00
|
|
|
if (IsWideAtom(atom)) {
|
2012-02-27 08:53:18 +00:00
|
|
|
wchar_t *ws = RepAtom(atom)->WStrOfAE;
|
2006-11-27 17:42:03 +00:00
|
|
|
|
|
|
|
if (Quote_illegal) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('\'', stream);
|
2006-11-27 17:42:03 +00:00
|
|
|
while (*ws) {
|
|
|
|
wchar_t ch = *ws++;
|
2012-02-13 23:07:31 +00:00
|
|
|
write_quoted(ch, '\'', stream);
|
2006-11-27 17:42:03 +00:00
|
|
|
}
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('\'', stream);
|
2006-11-27 17:42:03 +00:00
|
|
|
} else {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputws(ws, stream);
|
2006-11-27 17:42:03 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2012-02-27 08:53:18 +00:00
|
|
|
s = (unsigned char *)RepAtom(atom)->StrOfAE;
|
|
|
|
/* #define CRYPT_FOR_STEVE 1*/
|
|
|
|
#ifdef CRYPT_FOR_STEVE
|
|
|
|
if (Yap_GetValue(AtomCryptAtoms) != TermNil && Yap_GetAProp(atom, OpProperty) == NIL) {
|
|
|
|
char s[16];
|
|
|
|
sprintf(s,"x%x", (CELL)s);
|
|
|
|
wrputs(s, stream);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2012-03-19 08:58:26 +00:00
|
|
|
/* if symbol then last_minus is important */
|
|
|
|
last_minus = FALSE;
|
2012-02-27 08:53:18 +00:00
|
|
|
atom_or_symbol = AtomIsSymbols(s);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (lastw == atom_or_symbol && atom_or_symbol != separator /* solo */)
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(' ', stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = atom_or_symbol;
|
2011-04-22 15:29:41 +01:00
|
|
|
if (Quote_illegal && !legalAtom(s)) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('\'', stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
while (*s) {
|
2007-10-02 13:32:46 +01:00
|
|
|
wchar_t ch = *s++;
|
2012-02-13 23:07:31 +00:00
|
|
|
write_quoted(ch, '\'', stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('\'', stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputs((char *)s, stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:38:18 +00:00
|
|
|
void
|
|
|
|
Yap_WriteAtom(IOSTREAM *s, Atom atom)
|
|
|
|
{
|
|
|
|
struct write_globs wglb;
|
|
|
|
wglb.stream = s;
|
|
|
|
wglb.Quote_illegal = FALSE;
|
|
|
|
putAtom(atom, 0, &wglb);
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static int
|
2013-12-02 14:49:41 +00:00
|
|
|
IsCodesTerm(Term string) /* checks whether this is a string */
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2008-10-23 22:17:45 +01:00
|
|
|
if (IsVarTerm(string))
|
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
do {
|
|
|
|
Term hd;
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
if (!IsPairTerm(string)) return(FALSE);
|
|
|
|
hd = HeadOfTerm(string);
|
|
|
|
if (IsVarTerm(hd)) return(FALSE);
|
|
|
|
if (!IsIntTerm(hd)) return(FALSE);
|
|
|
|
ch = IntOfTerm(HeadOfTerm(string));
|
2006-11-27 17:42:03 +00:00
|
|
|
if ((ch < ' ' || ch > MAX_ISO_LATIN1) && ch != '\n' && ch != '\t')
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
string = TailOfTerm(string);
|
|
|
|
if (IsVarTerm(string)) return(FALSE);
|
|
|
|
} while (string != TermNil);
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
2012-02-17 13:41:05 +00:00
|
|
|
/* writes a string */
|
2001-04-09 20:54:03 +01:00
|
|
|
static void
|
2012-02-17 13:41:05 +00:00
|
|
|
putString(Term string, struct write_globs *wglb)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
{
|
2012-02-17 13:41:05 +00:00
|
|
|
wrf stream = wglb->stream;
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('"', stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
while (string != TermNil) {
|
2013-12-05 21:26:05 +00:00
|
|
|
wchar_t ch = IntOfTerm(HeadOfTerm(string));
|
2012-02-13 23:07:31 +00:00
|
|
|
write_quoted(ch, '"', stream);
|
2008-10-25 09:02:42 +01:00
|
|
|
string = TailOfTerm(string);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('"', stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = alphanum;
|
|
|
|
}
|
|
|
|
|
2012-02-17 13:41:05 +00:00
|
|
|
/* writes a string */
|
2001-04-09 20:54:03 +01:00
|
|
|
static void
|
2012-02-17 13:41:05 +00:00
|
|
|
putUnquotedString(Term string, struct write_globs *wglb)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
{
|
2012-02-17 13:41:05 +00:00
|
|
|
wrf stream = wglb->stream;
|
2001-04-09 20:54:03 +01:00
|
|
|
while (string != TermNil) {
|
|
|
|
int ch = IntOfTerm(HeadOfTerm(string));
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(ch, stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
string = TailOfTerm(string);
|
|
|
|
}
|
|
|
|
lastw = alphanum;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-07 23:52:15 +00:00
|
|
|
static Term
|
2012-03-08 11:28:59 +00:00
|
|
|
from_pointer(CELL *ptr0, struct rewind_term *rwt, struct write_globs *wglb)
|
2012-03-07 23:52:15 +00:00
|
|
|
{
|
|
|
|
CACHE_REGS
|
|
|
|
Term t;
|
2012-03-08 11:28:59 +00:00
|
|
|
CELL *ptr = ptr0;
|
2012-03-07 23:52:15 +00:00
|
|
|
|
|
|
|
while (IsVarTerm(*ptr) && !IsUnboundVar(ptr))
|
|
|
|
ptr = (CELL *)*ptr;
|
|
|
|
t = *ptr;
|
|
|
|
if (wglb->Keep_terms) {
|
|
|
|
struct rewind_term *x = rwt->parent;
|
|
|
|
|
2014-02-18 09:44:01 +00:00
|
|
|
rwt->u_sd.s.old = Yap_InitSlot(t PASS_REGS);
|
|
|
|
rwt->u_sd.s.ptr = Yap_InitSlot((CELL)ptr0 PASS_REGS);
|
2012-03-07 23:52:15 +00:00
|
|
|
if (!IsAtomicTerm(t) && !IsVarTerm(t)) {
|
|
|
|
while (x) {
|
2014-02-18 09:44:01 +00:00
|
|
|
if (Yap_GetDerefedFromSlot(x->u_sd.s.old PASS_REGS) == t)
|
2012-03-07 23:52:15 +00:00
|
|
|
return TermFoundVar;
|
|
|
|
x = x->parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2014-02-18 09:44:01 +00:00
|
|
|
rwt->u_sd.d.old = t;
|
|
|
|
rwt->u_sd.d.ptr = ptr0;
|
2013-10-30 09:41:15 +00:00
|
|
|
if ( !IsVarTerm(t) && !IsAtomicTerm(t)) {
|
2012-03-07 23:52:15 +00:00
|
|
|
struct rewind_term *x = rwt->parent;
|
|
|
|
|
|
|
|
while (x) {
|
2014-02-18 09:44:01 +00:00
|
|
|
if (x->u_sd.d.old == t)
|
2012-03-07 23:52:15 +00:00
|
|
|
return TermFoundVar;
|
|
|
|
x = x->parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CELL *
|
|
|
|
restore_from_write(struct rewind_term *rwt, struct write_globs *wglb)
|
|
|
|
{
|
|
|
|
CACHE_REGS
|
|
|
|
CELL *ptr;
|
|
|
|
|
|
|
|
if (wglb->Keep_terms) {
|
2014-02-18 09:44:01 +00:00
|
|
|
ptr = (CELL*)Yap_GetPtrFromSlot(rwt->u_sd.s.ptr PASS_REGS);
|
2012-03-07 23:52:15 +00:00
|
|
|
Yap_RecoverSlots(2 PASS_REGS);
|
|
|
|
} else {
|
2014-02-18 09:44:01 +00:00
|
|
|
ptr = rwt->u_sd.d.ptr;
|
2012-03-07 23:52:15 +00:00
|
|
|
}
|
2014-02-18 09:44:01 +00:00
|
|
|
rwt->u_sd.s.ptr = 0;
|
2012-03-07 23:52:15 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2012-02-17 13:41:05 +00:00
|
|
|
/* writes an unbound variable */
|
2001-04-09 20:54:03 +01:00
|
|
|
static void
|
2009-05-22 18:24:30 +01:00
|
|
|
write_var(CELL *t, struct write_globs *wglb, struct rewind_term *rwt)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2001-04-09 20:54:03 +01:00
|
|
|
if (lastw == alphanum) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(' ', wglb->stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('_', wglb->stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* make sure we don't get no creepy spaces where they shouldn't be */
|
|
|
|
lastw = separator;
|
2010-03-08 09:23:58 +00:00
|
|
|
if (IsAttVar(t)) {
|
2012-03-18 00:08:10 +00:00
|
|
|
Int vcount = (t-H0);
|
2012-03-22 21:59:04 +00:00
|
|
|
if (wglb->Portray_delays) {
|
2001-04-09 20:54:03 +01:00
|
|
|
exts ext = ExtFromCell(t);
|
2012-03-07 23:52:15 +00:00
|
|
|
struct rewind_term nrwt;
|
|
|
|
nrwt.parent = rwt;
|
2014-02-18 09:44:01 +00:00
|
|
|
nrwt.u_sd.s.ptr = 0;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2012-03-22 21:59:04 +00:00
|
|
|
wglb->Portray_delays = FALSE;
|
2004-06-05 04:37:01 +01:00
|
|
|
if (ext == attvars_ext) {
|
2010-03-08 09:23:58 +00:00
|
|
|
attvar_record *attv = RepAttVar(t);
|
2012-03-07 23:52:15 +00:00
|
|
|
CELL *l = &attv->Value; /* dirty low-level hack, check atts.h */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputs("$AT(",wglb->stream);
|
2009-05-22 18:24:30 +01:00
|
|
|
write_var(t, wglb, rwt);
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(',', wglb->stream);
|
2012-03-07 23:52:15 +00:00
|
|
|
writeTerm(from_pointer(l, &nrwt, wglb), 999, 1, FALSE, wglb, &nrwt);
|
|
|
|
l = restore_from_write(&nrwt, wglb);
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(',', wglb->stream);
|
2012-05-18 13:03:22 +01:00
|
|
|
l ++;
|
2012-03-07 23:52:15 +00:00
|
|
|
writeTerm(from_pointer(l, &nrwt, wglb), 999, 1, FALSE, wglb, &nrwt);
|
|
|
|
restore_from_write(&nrwt, wglb);
|
2012-03-27 16:44:11 +01:00
|
|
|
wrclose_bracket(wglb, TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2012-03-22 21:59:04 +00:00
|
|
|
wglb->Portray_delays = TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('D', wglb->stream);
|
2012-02-17 13:41:05 +00:00
|
|
|
wrputn(vcount,wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2012-02-17 13:41:05 +00:00
|
|
|
wrputn(((Int) (t- H0)),wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-01 20:10:32 +00:00
|
|
|
static Term
|
|
|
|
check_infinite_loop(Term t, struct rewind_term *x, struct write_globs *wglb)
|
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2012-02-14 09:10:07 +00:00
|
|
|
if (wglb->Keep_terms) {
|
2010-11-01 20:10:32 +00:00
|
|
|
while (x) {
|
2014-02-18 09:44:01 +00:00
|
|
|
if (Yap_GetFromSlot(x->u_sd.s.old PASS_REGS) == t)
|
2010-11-01 20:10:32 +00:00
|
|
|
return TermFoundVar;
|
|
|
|
x = x->parent;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (x) {
|
2014-02-18 09:44:01 +00:00
|
|
|
if (x->u_sd.d.old == t)
|
2010-11-01 20:10:32 +00:00
|
|
|
return TermFoundVar;
|
|
|
|
x = x->parent;
|
2009-05-24 21:14:23 +01:00
|
|
|
}
|
2009-05-22 19:24:27 +01:00
|
|
|
}
|
2009-05-22 18:24:30 +01:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2009-05-24 21:14:23 +01:00
|
|
|
static void
|
|
|
|
write_list(Term t, int direction, int depth, struct write_globs *wglb, struct rewind_term *rwt)
|
|
|
|
{
|
|
|
|
Term ti;
|
|
|
|
struct rewind_term nrwt;
|
|
|
|
nrwt.parent = rwt;
|
2014-02-18 09:44:01 +00:00
|
|
|
nrwt.u_sd.s.ptr = 0;
|
2009-05-24 21:14:23 +01:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int ndirection;
|
|
|
|
int do_jump;
|
|
|
|
|
2009-05-25 01:37:07 +01:00
|
|
|
writeTerm(from_pointer(RepPair(t), &nrwt, wglb), 999, depth+1, FALSE, wglb, &nrwt);
|
2012-03-07 23:52:15 +00:00
|
|
|
t = AbsPair(restore_from_write(&nrwt, wglb));
|
2009-05-24 21:14:23 +01:00
|
|
|
ti = TailOfTerm(t);
|
|
|
|
if (IsVarTerm(ti))
|
|
|
|
break;
|
2010-11-01 20:10:32 +00:00
|
|
|
if (!IsPairTerm(ti) ||
|
|
|
|
!IsPairTerm((ti = check_infinite_loop(ti, rwt, wglb))))
|
2009-05-24 21:14:23 +01:00
|
|
|
break;
|
|
|
|
ndirection = RepPair(ti)-RepPair(t);
|
|
|
|
/* make sure we're not trapped in loops */
|
|
|
|
if (ndirection > 0) {
|
2010-11-01 20:10:32 +00:00
|
|
|
do_jump = (direction <= 0);
|
2009-05-24 21:14:23 +01:00
|
|
|
} else if (ndirection == 0) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(',', wglb->stream);
|
2012-02-17 13:41:05 +00:00
|
|
|
putAtom(AtomFoundVar, wglb->Quote_illegal, wglb);
|
2009-05-24 21:14:23 +01:00
|
|
|
lastw = separator;
|
|
|
|
return;
|
|
|
|
} else {
|
2010-11-01 20:10:32 +00:00
|
|
|
do_jump = (direction >= 0);
|
2009-05-24 21:14:23 +01:00
|
|
|
}
|
2009-06-02 04:54:56 +01:00
|
|
|
if (wglb->MaxDepth != 0 && depth > wglb->MaxDepth) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('|', wglb->stream);
|
2012-02-17 13:41:05 +00:00
|
|
|
putAtom(Atom3Dots, wglb->Quote_illegal, wglb);
|
2009-06-02 04:54:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2009-05-24 21:14:23 +01:00
|
|
|
lastw = separator;
|
|
|
|
direction = ndirection;
|
2009-05-25 01:37:07 +01:00
|
|
|
depth++;
|
2009-05-24 21:14:23 +01:00
|
|
|
if (do_jump)
|
|
|
|
break;
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(',', wglb->stream);
|
2009-05-24 21:14:23 +01:00
|
|
|
t = ti;
|
|
|
|
}
|
|
|
|
if (IsPairTerm(ti)) {
|
2010-11-01 20:10:32 +00:00
|
|
|
Term nt = from_pointer(RepPair(t)+1, &nrwt, wglb);
|
|
|
|
/* we found an infinite loop */
|
|
|
|
if (IsAtomTerm(nt)) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('|', wglb->stream);
|
2010-11-01 20:10:32 +00:00
|
|
|
writeTerm(nt, 999, depth, FALSE, wglb, rwt);
|
|
|
|
} else {
|
|
|
|
/* keep going on the list */
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(',', wglb->stream);
|
2010-11-01 20:10:32 +00:00
|
|
|
write_list(nt, direction, depth, wglb, &nrwt);
|
|
|
|
}
|
2009-05-24 21:14:23 +01:00
|
|
|
restore_from_write(&nrwt, wglb);
|
|
|
|
} else if (ti != MkAtomTerm(AtomNil)) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('|', wglb->stream);
|
2009-05-24 21:14:23 +01:00
|
|
|
lastw = separator;
|
2009-05-25 01:37:07 +01:00
|
|
|
writeTerm(from_pointer(RepPair(t)+1, &nrwt, wglb), 999, depth, FALSE, wglb, &nrwt);
|
2009-05-24 21:14:23 +01:00
|
|
|
restore_from_write(&nrwt, wglb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-27 16:44:11 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static void
|
2009-05-22 18:24:30 +01:00
|
|
|
writeTerm(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, struct rewind_term *rwt)
|
2009-05-24 21:14:23 +01:00
|
|
|
/* term to write */
|
|
|
|
/* context priority */
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2009-05-22 18:24:30 +01:00
|
|
|
struct rewind_term nrwt;
|
|
|
|
nrwt.parent = rwt;
|
2014-02-18 09:44:01 +00:00
|
|
|
nrwt.u_sd.s.ptr = 0;
|
2009-05-22 18:24:30 +01:00
|
|
|
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->MaxDepth != 0 && depth > wglb->MaxDepth) {
|
2012-02-17 13:41:05 +00:00
|
|
|
putAtom(Atom3Dots, wglb->Quote_illegal, wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-07-27 23:29:55 +01:00
|
|
|
if (EX)
|
2002-05-14 19:24:34 +01:00
|
|
|
return;
|
2001-04-09 20:54:03 +01:00
|
|
|
t = Deref(t);
|
|
|
|
if (IsVarTerm(t)) {
|
2009-05-22 18:24:30 +01:00
|
|
|
write_var((CELL *)t, wglb, &nrwt);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (IsIntTerm(t)) {
|
2012-02-17 13:41:05 +00:00
|
|
|
wrputn((Int) IntOfTerm(t),wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (IsAtomTerm(t)) {
|
2012-02-17 13:41:05 +00:00
|
|
|
putAtom(AtomOfTerm(t), wglb->Quote_illegal, wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (IsPairTerm(t)) {
|
2011-04-22 15:29:41 +01:00
|
|
|
if (wglb->Ignore_ops) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputs("'.'(",wglb->stream);
|
2011-04-22 15:29:41 +01:00
|
|
|
lastw = separator;
|
2012-03-07 23:52:15 +00:00
|
|
|
writeTerm(from_pointer(RepPair(t), &nrwt, wglb), 999, depth + 1, FALSE, wglb, &nrwt);
|
|
|
|
t = AbsPair(restore_from_write(&nrwt, wglb));
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputs(",",wglb->stream);
|
2012-03-07 23:52:15 +00:00
|
|
|
writeTerm(from_pointer(RepPair(t)+1, &nrwt, wglb), 999, depth + 1, FALSE, wglb, &nrwt);
|
2011-04-22 15:29:41 +01:00
|
|
|
restore_from_write(&nrwt, wglb);
|
2012-03-27 16:44:11 +01:00
|
|
|
wrclose_bracket(wglb, TRUE);
|
2011-04-22 15:29:41 +01:00
|
|
|
return;
|
|
|
|
}
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->Use_portray) {
|
2001-04-09 20:54:03 +01:00
|
|
|
Term targs[1];
|
2010-07-27 23:29:55 +01:00
|
|
|
struct DB_TERM *old_EX = NULL;
|
2010-05-10 10:21:56 +01:00
|
|
|
Int sl = 0;
|
2002-05-14 19:24:34 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
targs[0] = t;
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PutValue(AtomPortray, MkAtomTerm(AtomNil));
|
2010-07-27 23:29:55 +01:00
|
|
|
if (EX) old_EX = EX;
|
2011-03-07 16:02:55 +00:00
|
|
|
sl = Yap_InitSlot(t PASS_REGS);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_execute_goal(Yap_MkApplTerm(FunctorPortray, 1, targs), 0, 1);
|
2011-03-07 16:02:55 +00:00
|
|
|
t = Yap_GetFromSlot(sl PASS_REGS);
|
|
|
|
Yap_RecoverSlots(1 PASS_REGS);
|
2010-07-27 23:29:55 +01:00
|
|
|
if (old_EX != NULL) EX = old_EX;
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_GetValue(AtomPortray) == MkAtomTerm(AtomTrue))
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-12-02 14:49:41 +00:00
|
|
|
if (yap_flags[WRITE_QUOTED_STRING_FLAG] && IsCodesTerm(t)) {
|
2012-02-17 13:41:05 +00:00
|
|
|
putString(t, wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('[', wglb->stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
2010-11-01 20:10:32 +00:00
|
|
|
/* we assume t was already saved in the stack */
|
|
|
|
write_list(t, 0, depth, wglb, rwt);
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(']', wglb->stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
|
|
|
} else { /* compound term */
|
|
|
|
Functor functor = FunctorOfTerm(t);
|
|
|
|
int Arity;
|
|
|
|
Atom atom;
|
|
|
|
int op, lp, rp;
|
|
|
|
|
2002-11-20 20:00:56 +00:00
|
|
|
if (IsExtensionFunctor(functor)) {
|
|
|
|
switch((CELL)functor) {
|
|
|
|
case (CELL)FunctorDouble:
|
2012-02-17 13:41:05 +00:00
|
|
|
wrputf(FloatOfTerm(t),wglb);
|
2002-11-20 20:00:56 +00:00
|
|
|
return;
|
2013-12-02 14:49:41 +00:00
|
|
|
case (CELL)FunctorString:
|
2013-12-05 21:26:05 +00:00
|
|
|
write_string(StringOfTerm(t),wglb);
|
2013-12-02 14:49:41 +00:00
|
|
|
return;
|
2010-03-08 09:23:58 +00:00
|
|
|
case (CELL)FunctorAttVar:
|
|
|
|
write_var(RepAppl(t)+1, wglb, &nrwt);
|
|
|
|
return;
|
2002-11-20 20:00:56 +00:00
|
|
|
case (CELL)FunctorDBRef:
|
2012-02-27 08:53:18 +00:00
|
|
|
wrputref(RefOfTerm(t), wglb->Quote_illegal, wglb);
|
2002-11-20 20:00:56 +00:00
|
|
|
return;
|
|
|
|
case (CELL)FunctorLongInt:
|
2012-02-17 13:41:05 +00:00
|
|
|
wrputn(LongIntOfTerm(t),wglb);
|
2002-11-20 20:00:56 +00:00
|
|
|
return;
|
2010-05-28 09:53:56 +01:00
|
|
|
/* case (CELL)FunctorBigInt: */
|
|
|
|
default:
|
|
|
|
writebig(t, p, depth, rinfixarg, wglb, rwt);
|
2008-04-14 18:30:18 +01:00
|
|
|
return;
|
2002-11-20 20:00:56 +00:00
|
|
|
}
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
Arity = ArityOfFunctor(functor);
|
|
|
|
atom = NameOfFunctor(functor);
|
|
|
|
#ifdef SFUNC
|
|
|
|
if (Arity == SFArity) {
|
|
|
|
int argno = 1;
|
|
|
|
CELL *p = ArgsOfSFTerm(t);
|
2012-02-17 13:41:05 +00:00
|
|
|
putAtom(atom, wglb->Quote_illegal, wglb);
|
2012-03-27 16:44:11 +01:00
|
|
|
wropen_bracket(wglb, FALSE);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
while (*p) {
|
2010-05-10 10:21:56 +01:00
|
|
|
Int sl = 0;
|
2002-10-17 01:05:29 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
while (argno < *p) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('_', wglb->stream), wrputc(',', wglb->stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
++argno;
|
|
|
|
}
|
|
|
|
*p++;
|
|
|
|
lastw = separator;
|
|
|
|
/* cannot use the term directly with the SBA */
|
2012-03-07 23:52:15 +00:00
|
|
|
writeTerm(from_pointer(p, &nrwt, wglb), 999, depth + 1, FALSE, wglb, &nrwt);
|
|
|
|
p = restore_from_write(&nrwt, wglb)+1;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (*p)
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(',', wglb->stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
argno++;
|
|
|
|
}
|
2012-03-27 16:44:11 +01:00
|
|
|
wrclose_bracket(wglb, TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->Use_portray) {
|
2001-04-09 20:54:03 +01:00
|
|
|
Term targs[1];
|
2010-07-27 23:29:55 +01:00
|
|
|
struct DB_TERM *old_EX = NULL;
|
2010-05-10 10:21:56 +01:00
|
|
|
Int sl = 0;
|
2002-10-17 01:05:29 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
targs[0] = t;
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PutValue(AtomPortray, MkAtomTerm(AtomNil));
|
2010-07-27 23:29:55 +01:00
|
|
|
if (EX) old_EX = EX;
|
2011-03-07 16:02:55 +00:00
|
|
|
sl = Yap_InitSlot(t PASS_REGS);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_execute_goal(Yap_MkApplTerm(FunctorPortray, 1, targs),0, 1);
|
2011-03-07 16:02:55 +00:00
|
|
|
t = Yap_GetFromSlot(sl PASS_REGS);
|
|
|
|
Yap_RecoverSlots(1 PASS_REGS);
|
2010-07-27 23:29:55 +01:00
|
|
|
if (old_EX) EX = old_EX;
|
|
|
|
if (Yap_GetValue(AtomPortray) == MkAtomTerm(AtomTrue) || EX)
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
|
|
|
}
|
2002-11-20 20:00:56 +00:00
|
|
|
if (!wglb->Ignore_ops &&
|
2012-03-15 22:55:50 +00:00
|
|
|
Arity == 1 &&
|
2012-03-19 08:58:26 +00:00
|
|
|
Yap_IsPrefixOp(atom, &op, &rp)
|
2001-04-09 20:54:03 +01:00
|
|
|
) {
|
|
|
|
Term tright = ArgOfTerm(1, t);
|
|
|
|
int bracket_right =
|
|
|
|
!IsVarTerm(tright) && IsAtomTerm(tright) &&
|
2010-11-21 21:47:07 +00:00
|
|
|
Yap_IsOp(AtomOfTerm(tright));
|
2001-04-09 20:54:03 +01:00
|
|
|
if (op > p) {
|
2012-03-27 16:44:11 +01:00
|
|
|
wropen_bracket(wglb, TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2012-02-17 13:41:05 +00:00
|
|
|
putAtom(atom, wglb->Quote_illegal, wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (bracket_right) {
|
2012-03-27 16:44:11 +01:00
|
|
|
/* avoid stuff such as \+ (a,b) being written as \+(a,b) */
|
|
|
|
wropen_bracket(wglb, TRUE);
|
2012-03-19 08:58:26 +00:00
|
|
|
} else if (atom == AtomMinus) {
|
|
|
|
last_minus = TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2012-03-22 21:40:42 +00:00
|
|
|
writeTerm(from_pointer(RepAppl(t)+1, &nrwt, wglb), rp, depth + 1, TRUE, wglb, &nrwt);
|
2009-05-24 21:14:23 +01:00
|
|
|
restore_from_write(&nrwt, wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (bracket_right) {
|
2012-03-27 16:44:11 +01:00
|
|
|
wrclose_bracket(wglb, TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (op > p) {
|
2012-03-27 16:44:11 +01:00
|
|
|
wrclose_bracket(wglb, TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2002-11-20 20:00:56 +00:00
|
|
|
} else if (!wglb->Ignore_ops &&
|
2013-09-28 12:12:09 +01:00
|
|
|
( Arity == 1 || ((atom == AtomEmptyBrackets || atom == AtomEmptyCurlyBrackets || atom == AtomEmptySquareBrackets) && !IsVarTerm(ArgOfTerm(1, t)))) &&
|
2009-11-20 00:33:14 +00:00
|
|
|
Yap_IsPosfixOp(atom, &op, &lp)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
Term tleft = ArgOfTerm(1, t);
|
2012-03-07 23:52:15 +00:00
|
|
|
|
2013-09-13 11:44:26 +01:00
|
|
|
int bracket_left, offset;
|
|
|
|
|
|
|
|
if (Arity != 1) {
|
|
|
|
tleft = ArgOfTerm(1, t);
|
|
|
|
offset = 2;
|
|
|
|
} else {
|
2013-10-07 12:20:52 +01:00
|
|
|
tleft = ArgOfTerm(1, t);
|
2013-09-13 11:44:26 +01:00
|
|
|
offset = 1;
|
|
|
|
}
|
|
|
|
bracket_left =
|
2012-03-27 16:44:11 +01:00
|
|
|
!IsVarTerm(tleft) &&
|
|
|
|
IsAtomTerm(tleft) &&
|
2010-11-21 21:47:07 +00:00
|
|
|
Yap_IsOp(AtomOfTerm(tleft));
|
2001-04-09 20:54:03 +01:00
|
|
|
if (op > p) {
|
|
|
|
/* avoid stuff such as \+ (a,b) being written as \+(a,b) */
|
2012-03-27 16:44:11 +01:00
|
|
|
wropen_bracket(wglb, TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (bracket_left) {
|
2012-03-27 16:44:11 +01:00
|
|
|
wropen_bracket(wglb, TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2013-09-13 11:44:26 +01:00
|
|
|
writeTerm(from_pointer(RepAppl(t)+offset, &nrwt, wglb), lp, depth + 1, rinfixarg, wglb, &nrwt);
|
2009-05-24 21:14:23 +01:00
|
|
|
restore_from_write(&nrwt, wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (bracket_left) {
|
2012-03-27 16:44:11 +01:00
|
|
|
wrclose_bracket(wglb, TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2013-07-07 22:15:25 +01:00
|
|
|
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;
|
2013-09-13 11:44:26 +01:00
|
|
|
write_list(ArgOfTerm(1,t), 0, depth, wglb, rwt);
|
2013-07-07 22:15:25 +01:00
|
|
|
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);
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
if (op > p) {
|
2012-03-27 16:44:11 +01:00
|
|
|
wrclose_bracket(wglb, TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2002-11-20 20:00:56 +00:00
|
|
|
} else if (!wglb->Ignore_ops &&
|
2009-11-20 00:33:14 +00:00
|
|
|
Arity == 2 && Yap_IsInfixOp(atom, &op, &lp,
|
2001-04-09 20:54:03 +01:00
|
|
|
&rp) ) {
|
|
|
|
Term tleft = ArgOfTerm(1, t);
|
|
|
|
Term tright = ArgOfTerm(2, t);
|
|
|
|
int bracket_left =
|
|
|
|
!IsVarTerm(tleft) && IsAtomTerm(tleft) &&
|
2010-11-21 21:47:07 +00:00
|
|
|
Yap_IsOp(AtomOfTerm(tleft));
|
2001-04-09 20:54:03 +01:00
|
|
|
int bracket_right =
|
|
|
|
!IsVarTerm(tright) && IsAtomTerm(tright) &&
|
2010-11-21 21:47:07 +00:00
|
|
|
Yap_IsOp(AtomOfTerm(tright));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (op > p) {
|
|
|
|
/* avoid stuff such as \+ (a,b) being written as \+(a,b) */
|
2012-03-27 16:44:11 +01:00
|
|
|
wropen_bracket(wglb, TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
|
|
|
if (bracket_left) {
|
2012-03-27 16:44:11 +01:00
|
|
|
wropen_bracket(wglb, TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2009-05-24 21:14:23 +01:00
|
|
|
writeTerm(from_pointer(RepAppl(t)+1, &nrwt, wglb), lp, depth + 1, rinfixarg, wglb, &nrwt);
|
2012-03-07 23:52:15 +00:00
|
|
|
t = AbsAppl(restore_from_write(&nrwt, wglb)-1);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (bracket_left) {
|
2012-03-27 16:44:11 +01:00
|
|
|
wrclose_bracket(wglb, TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2012-03-22 22:15:07 +00:00
|
|
|
/* avoid quoting commas and bars */
|
|
|
|
if (!strcmp(RepAtom(atom)->StrOfAE,",")) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(',', wglb->stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
2012-03-22 22:15:07 +00:00
|
|
|
} else if (!strcmp(RepAtom(atom)->StrOfAE,"|")) {
|
|
|
|
wrputc('|', wglb->stream);
|
|
|
|
lastw = separator;
|
|
|
|
} else
|
|
|
|
putAtom(atom, wglb->Quote_illegal, wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (bracket_right) {
|
2012-03-27 16:44:11 +01:00
|
|
|
wropen_bracket(wglb, TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2009-05-24 21:14:23 +01:00
|
|
|
writeTerm(from_pointer(RepAppl(t)+2, &nrwt, wglb), rp, depth + 1, TRUE, wglb, &nrwt);
|
|
|
|
restore_from_write(&nrwt, wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (bracket_right) {
|
2012-03-27 16:44:11 +01:00
|
|
|
wrclose_bracket(wglb, TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (op > p) {
|
2012-03-27 16:44:11 +01:00
|
|
|
wrclose_bracket(wglb, TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2012-03-15 22:19:48 +00:00
|
|
|
} else if (wglb->Handle_vars && functor == LOCAL_FunctorVar) {
|
2001-04-09 20:54:03 +01:00
|
|
|
Term ti = ArgOfTerm(1, t);
|
|
|
|
if (lastw == alphanum) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(' ', wglb->stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2013-12-02 14:49:41 +00:00
|
|
|
if (!IsVarTerm(ti) && (IsIntTerm(ti) || IsCodesTerm(ti) || IsAtomTerm(ti))) {
|
2001-04-09 20:54:03 +01:00
|
|
|
if (IsIntTerm(ti)) {
|
|
|
|
Int k = IntOfTerm(ti);
|
|
|
|
if (k == -1) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('_', wglb->stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = alphanum;
|
|
|
|
return;
|
|
|
|
} else {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc((k % 26) + 'A', wglb->stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (k >= 26) {
|
|
|
|
/* make sure we don't get confused about our context */
|
|
|
|
lastw = separator;
|
2012-02-17 13:41:05 +00:00
|
|
|
wrputn( k / 26 ,wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else
|
|
|
|
lastw = alphanum;
|
|
|
|
}
|
2012-02-14 06:54:32 +00:00
|
|
|
} else if (IsAtomTerm(ti)) {
|
2012-02-17 13:41:05 +00:00
|
|
|
putAtom(AtomOfTerm(ti), FALSE, wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2012-02-17 13:41:05 +00:00
|
|
|
putUnquotedString(ti, wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
} else {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputs("'$VAR'(",wglb->stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
2009-05-24 21:14:23 +01:00
|
|
|
writeTerm(from_pointer(RepAppl(t)+1, &nrwt, wglb), 999, depth + 1, FALSE, wglb, &nrwt);
|
|
|
|
restore_from_write(&nrwt, wglb);
|
2012-03-27 16:44:11 +01:00
|
|
|
wrclose_bracket(wglb, TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2009-04-22 00:04:36 +01:00
|
|
|
} else if (!wglb->Ignore_ops && functor == FunctorBraces) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('{', wglb->stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
2009-05-24 21:14:23 +01:00
|
|
|
writeTerm(from_pointer(RepAppl(t)+1, &nrwt, wglb), 1200, depth + 1, FALSE, wglb, &nrwt);
|
|
|
|
restore_from_write(&nrwt, wglb);
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('}', wglb->stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
} else if (atom == AtomArray) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('{', wglb->stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
for (op = 1; op <= Arity; ++op) {
|
2005-12-05 17:16:12 +00:00
|
|
|
if (op == wglb->MaxArgs) {
|
2012-02-14 07:46:37 +00:00
|
|
|
wrputs("...", wglb->stream);
|
2005-12-05 17:16:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-05-24 21:14:23 +01:00
|
|
|
writeTerm(from_pointer(RepAppl(t)+op, &nrwt, wglb), 999, depth + 1, FALSE, wglb, &nrwt);
|
2012-03-07 23:52:15 +00:00
|
|
|
t = AbsAppl(restore_from_write(&nrwt, wglb)-op);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (op != Arity) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(',', wglb->stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
|
|
|
}
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('}', wglb->stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
} else {
|
2012-02-17 13:41:05 +00:00
|
|
|
putAtom(atom, wglb->Quote_illegal, wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
2012-03-27 16:44:11 +01:00
|
|
|
wropen_bracket(wglb, FALSE);
|
2001-04-09 20:54:03 +01:00
|
|
|
for (op = 1; op <= Arity; ++op) {
|
2005-12-05 17:16:12 +00:00
|
|
|
if (op == wglb->MaxArgs) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc('.', wglb->stream);
|
|
|
|
wrputc('.', wglb->stream);
|
|
|
|
wrputc('.', wglb->stream);
|
2005-12-05 17:16:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-05-24 21:14:23 +01:00
|
|
|
writeTerm(from_pointer(RepAppl(t)+op, &nrwt, wglb), 999, depth + 1, FALSE, wglb, &nrwt);
|
|
|
|
restore_from_write(&nrwt, wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (op != Arity) {
|
2012-02-13 23:07:31 +00:00
|
|
|
wrputc(',', wglb->stream);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
|
|
|
}
|
2012-03-27 16:44:11 +01:00
|
|
|
wrclose_bracket(wglb, TRUE);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-14 07:46:37 +00:00
|
|
|
Yap_plwrite(Term t, void *mywrite, int max_depth, int flags, int priority)
|
2001-04-09 20:54:03 +01:00
|
|
|
/* term to be written */
|
|
|
|
/* consumer */
|
|
|
|
/* write options */
|
|
|
|
{
|
2002-11-20 20:00:56 +00:00
|
|
|
struct write_globs wglb;
|
2009-05-22 18:24:30 +01:00
|
|
|
struct rewind_term rwt;
|
2002-11-20 20:00:56 +00:00
|
|
|
|
2012-02-13 23:07:31 +00:00
|
|
|
if (!mywrite)
|
|
|
|
wglb.stream = Serror;
|
|
|
|
else
|
|
|
|
wglb.stream = mywrite;
|
|
|
|
|
2012-02-17 13:41:05 +00:00
|
|
|
wglb.lw = separator;
|
2012-03-19 08:58:26 +00:00
|
|
|
wglb.last_atom_minus = FALSE;
|
2002-11-20 20:00:56 +00:00
|
|
|
wglb.Quote_illegal = flags & Quote_illegal_f;
|
|
|
|
wglb.Handle_vars = flags & Handle_vars_f;
|
|
|
|
wglb.Use_portray = flags & Use_portray_f;
|
2012-03-22 21:59:04 +00:00
|
|
|
wglb.Portray_delays = flags & AttVar_Portray_f;
|
2012-02-14 07:46:37 +00:00
|
|
|
wglb.MaxDepth = max_depth;
|
|
|
|
wglb.MaxArgs = max_depth;
|
2002-10-17 01:05:29 +01:00
|
|
|
/* notice: we must have ASP well set when using portray, otherwise
|
|
|
|
we cannot make recursive Prolog calls */
|
2012-02-14 09:10:07 +00:00
|
|
|
wglb.Keep_terms = (flags & (Use_portray_f|To_heap_f));
|
2010-11-01 20:10:32 +00:00
|
|
|
/* initialise wglb */
|
|
|
|
rwt.parent = NULL;
|
2002-11-20 20:00:56 +00:00
|
|
|
wglb.Ignore_ops = flags & Ignore_ops_f;
|
2012-02-13 23:07:31 +00:00
|
|
|
wglb.Write_strings = flags & BackQuote_String_f;
|
2004-05-01 04:58:46 +01:00
|
|
|
/* protect slots for portray */
|
2009-05-24 21:14:23 +01:00
|
|
|
writeTerm(from_pointer(&t, &rwt, &wglb), priority, 1, FALSE, &wglb, &rwt);
|
|
|
|
restore_from_write(&rwt, &wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|