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>
|
2001-04-09 20:54:03 +01:00
|
|
|
#include "Yap.h"
|
|
|
|
#include "Yatom.h"
|
|
|
|
#include "Heap.h"
|
|
|
|
#include "yapio.h"
|
|
|
|
#if COROUTINING
|
|
|
|
#include "attvar.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_CTYPE_H
|
|
|
|
#include <ctype.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
static wtype lastw;
|
|
|
|
|
2007-01-28 14:26:37 +00:00
|
|
|
typedef int (*wrf) (int, wchar_t);
|
2002-11-20 20:00:56 +00:00
|
|
|
|
2009-05-24 21:14:23 +01:00
|
|
|
typedef struct union_slots {
|
|
|
|
long old;
|
|
|
|
long ptr;
|
|
|
|
} 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;
|
|
|
|
} u;
|
2009-05-22 18:24:30 +01:00
|
|
|
} rwts;
|
|
|
|
|
2002-11-20 20:00:56 +00:00
|
|
|
typedef struct write_globs {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrf writewch;
|
2002-11-20 20:00:56 +00:00
|
|
|
int Quote_illegal, Ignore_ops, Handle_vars, Use_portray;
|
|
|
|
int keep_terms;
|
2009-05-22 18:24:30 +01:00
|
|
|
int Write_Loops;
|
2005-12-05 17:16:12 +00:00
|
|
|
UInt MaxDepth, MaxList, MaxArgs;
|
2002-11-20 20:00:56 +00:00
|
|
|
} wglbs;
|
|
|
|
|
|
|
|
STATIC_PROTO(void wrputn, (Int, wrf));
|
|
|
|
STATIC_PROTO(void wrputs, (char *, wrf));
|
|
|
|
STATIC_PROTO(void wrputf, (Float, wrf));
|
2004-02-13 18:39:29 +00:00
|
|
|
STATIC_PROTO(void wrputref, (CODEADDR, int, wrf));
|
2007-10-02 13:32:46 +01:00
|
|
|
STATIC_PROTO(int legalAtom, (unsigned char *));
|
2001-04-09 20:54:03 +01:00
|
|
|
STATIC_PROTO(int LeftOpToProtect, (Atom, int));
|
|
|
|
STATIC_PROTO(int RightOpToProtect, (Atom, int));
|
2007-10-02 13:32:46 +01:00
|
|
|
STATIC_PROTO(wtype AtomIsSymbols, (unsigned char *));
|
2002-11-20 20:00:56 +00:00
|
|
|
STATIC_PROTO(void putAtom, (Atom, int, wrf));
|
2009-05-22 18:24:30 +01:00
|
|
|
STATIC_PROTO(void writeTerm, (Term, int, int, int, struct write_globs *, struct rewind_term *));
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2002-11-20 20:00:56 +00:00
|
|
|
#define wrputc(X,WF) ((*WF)(Yap_c_output_stream,X)) /* writes a character */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
static void
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputn(Int n, wrf writewch) /* writes an integer */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
char s[256], *s1=s; /* that should be enough for most integers */
|
|
|
|
if (n < 0) {
|
|
|
|
if (lastw == symbol)
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(' ', writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
|
|
|
if (lastw == alphanum)
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(' ', writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#if HAVE_SNPRINTF
|
|
|
|
#if SHORT_INTS
|
|
|
|
snprintf(s, 256, "%ld", n);
|
|
|
|
#else
|
|
|
|
snprintf(s, 256, "%d", n);
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#if SHORT_INTS
|
|
|
|
sprintf(s, "%ld", n);
|
|
|
|
#else
|
|
|
|
sprintf(s, "%d", n);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
while (*s1)
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(*s1++, writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = alphanum;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputs(char *s, wrf writewch) /* writes a string */
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2007-10-02 13:32:46 +01:00
|
|
|
while (*s) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(*s++, writewch);
|
2007-10-02 13:32:46 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputws(wchar_t *s, wrf writewch) /* writes a string */
|
|
|
|
{
|
|
|
|
while (*s)
|
|
|
|
wrputc(*s++, writewch);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
wrputf(Float f, wrf writewch) /* writes a float */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
{
|
2006-11-27 17:42:03 +00:00
|
|
|
char s[256], *pt = s, ch;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (f < 0) {
|
|
|
|
if (lastw == symbol)
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(' ', writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
|
|
|
if (lastw == alphanum)
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(' ', writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
lastw = alphanum;
|
2006-01-02 02:16:19 +00:00
|
|
|
// sprintf(s, "%.15g", f);
|
2008-12-23 01:53:52 +00:00
|
|
|
sprintf(s, RepAtom(AtomFloatFormat)->StrOfAE, f);
|
2001-04-09 20:54:03 +01:00
|
|
|
while (*pt == ' ')
|
|
|
|
pt++;
|
2006-01-02 02:16:19 +00:00
|
|
|
if (*pt == 'i' || *pt == 'n') /* inf or nan */ {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('(', writewch);
|
|
|
|
wrputc('+', writewch);
|
|
|
|
wrputs(pt, writewch);
|
|
|
|
wrputc(')', writewch);
|
2006-01-02 02:16:19 +00:00
|
|
|
} else {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputs(pt, writewch);
|
2006-01-02 02:16:19 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
if (*pt == '-') pt++;
|
|
|
|
while ((ch = *pt) != '\0') {
|
|
|
|
if (ch < '0' || ch > '9')
|
|
|
|
return;
|
|
|
|
pt++;
|
|
|
|
}
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputs(".0", writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputref(CODEADDR ref, int Quote_illegal, wrf writewch) /* writes a data base reference */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
char s[256];
|
|
|
|
|
2008-12-23 01:53:52 +00:00
|
|
|
putAtom(AtomDBref, Quote_illegal, writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
#if SHORT_INTS
|
2004-02-13 18:39:29 +00:00
|
|
|
sprintf(s, "(0x%p,0)", ref);
|
2006-05-02 17:39:06 +01:00
|
|
|
#elif __linux__
|
2004-02-13 18:39:29 +00:00
|
|
|
sprintf(s, "(%p,0)", ref);
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
2004-02-13 18:39:29 +00:00
|
|
|
sprintf(s, "(0x%p,0)", ref);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputs(s, writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = alphanum;
|
|
|
|
}
|
|
|
|
|
|
|
|
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')
|
|
|
|
return(FALSE);
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_chtype[ch] != LC) {
|
2001-04-09 20:54:03 +01:00
|
|
|
if (ch == '[')
|
|
|
|
return (*++s == ']' && !(*++s));
|
|
|
|
else if (ch == '{')
|
|
|
|
return (*++s == '}' && !(*++s));
|
2002-11-18 18:18:05 +00:00
|
|
|
else if (Yap_chtype[ch] == SL)
|
2001-04-09 20:54:03 +01:00
|
|
|
return (!*++s);
|
|
|
|
else if ((ch == ',' || ch == '.') && !s[1])
|
2008-10-23 22:17:45 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
else
|
|
|
|
while (ch) {
|
2009-05-10 04:49:10 +01:00
|
|
|
if (Yap_chtype[ch] != SY)
|
2008-10-23 22:17:45 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
ch = *++s;
|
|
|
|
}
|
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 int LeftOpToProtect(Atom at, int p)
|
|
|
|
{
|
|
|
|
int op, rp;
|
2005-10-21 17:09:03 +01:00
|
|
|
OpEntry *opinfo = Yap_GetOpProp(at);
|
2002-11-18 18:18:05 +00:00
|
|
|
return(opinfo && Yap_IsPrefixOp(opinfo, &op, &rp) );
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int RightOpToProtect(Atom at, int p)
|
|
|
|
{
|
|
|
|
int op, lp;
|
2005-10-21 17:09:03 +01:00
|
|
|
OpEntry *opinfo = Yap_GetOpProp(at);
|
2002-11-18 18:18:05 +00:00
|
|
|
return(opinfo && Yap_IsPosfixOp(opinfo, &op, &lp) );
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2001-04-09 20:54:03 +01:00
|
|
|
return(alphanum);
|
|
|
|
}
|
|
|
|
return(symbol);
|
|
|
|
}
|
|
|
|
|
2008-10-23 22:17:45 +01:00
|
|
|
static void
|
|
|
|
write_quoted(int ch, int quote, wrf writewch)
|
|
|
|
{
|
|
|
|
if (yap_flags[CHARACTER_ESCAPE_FLAG] == CPROLOG_CHARACTER_ESCAPES) {
|
|
|
|
wrputc(ch, writewch);
|
|
|
|
if (ch == '\'')
|
|
|
|
wrputc('\'', writewch); /* be careful about quotes */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(ch < 0xff && chtype(ch) == BS) && ch != '\'' && ch != '\\') {
|
|
|
|
wrputc(ch, writewch);
|
|
|
|
} else {
|
|
|
|
switch (ch) {
|
|
|
|
case '\\':
|
|
|
|
case '\'':
|
|
|
|
wrputc('\\', writewch);
|
|
|
|
wrputc(ch, writewch);
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
wrputc('\\', writewch);
|
|
|
|
wrputc('a', writewch);
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
wrputc('\\', writewch);
|
|
|
|
wrputc('b', writewch);
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
wrputc('\\', writewch);
|
|
|
|
wrputc('t', writewch);
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
case 160:
|
|
|
|
wrputc(' ', writewch);
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
wrputc('\\', writewch);
|
|
|
|
wrputc('n', writewch);
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
wrputc('\\', writewch);
|
|
|
|
wrputc('v', writewch);
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
wrputc('\\', writewch);
|
|
|
|
wrputc('r', writewch);
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
wrputc('\\', writewch);
|
|
|
|
wrputc('f', writewch);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ( ch <= 0xff ) {
|
|
|
|
char esc[8];
|
|
|
|
|
|
|
|
if (yap_flags[CHARACTER_ESCAPE_FLAG] == SICSTUS_CHARACTER_ESCAPES) {
|
|
|
|
sprintf(esc, "\\%03o", ch);
|
|
|
|
} else {
|
|
|
|
/* last backslash in ISO mode */
|
|
|
|
sprintf(esc, "\\%03o\\", ch);
|
|
|
|
}
|
|
|
|
wrputs(esc, writewch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static void
|
2006-11-27 17:42:03 +00:00
|
|
|
putAtom(Atom atom, int Quote_illegal, wrf writewch) /* writes an atom */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
{
|
2007-10-02 13:32:46 +01:00
|
|
|
unsigned char *s = (unsigned char *)RepAtom(atom)->StrOfAE;
|
2001-04-09 20:54:03 +01:00
|
|
|
wtype atom_or_symbol = AtomIsSymbols(s);
|
|
|
|
|
2002-04-03 20:33:38 +01:00
|
|
|
/* #define CRYPT_FOR_STEVE 1*/
|
2002-03-28 18:48:55 +00:00
|
|
|
#ifdef CRYPT_FOR_STEVE
|
2008-12-23 01:53:52 +00:00
|
|
|
if (Yap_GetValue(AtomCryptAtoms) != TermNil && Yap_GetAProp(atom, OpProperty) == NIL) {
|
2002-03-28 18:48:55 +00:00
|
|
|
char s[16];
|
|
|
|
sprintf(s,"x%x", (CELL)s);
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputs(s, writewch);
|
2002-03-28 18:48:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2006-11-27 17:42:03 +00:00
|
|
|
if (IsWideAtom(atom)) {
|
|
|
|
wchar_t *ws = (wchar_t *)s;
|
|
|
|
|
|
|
|
if (Quote_illegal) {
|
|
|
|
wrputc('\'', writewch);
|
|
|
|
while (*ws) {
|
|
|
|
wchar_t ch = *ws++;
|
2008-10-23 22:17:45 +01:00
|
|
|
write_quoted(ch, '\'', writewch);
|
2006-11-27 17:42:03 +00:00
|
|
|
}
|
|
|
|
wrputc('\'', writewch);
|
|
|
|
} else {
|
|
|
|
wrputws(ws, writewch);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
if (lastw == atom_or_symbol && atom_or_symbol != separator /* solo */)
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(' ', writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = atom_or_symbol;
|
|
|
|
if (!legalAtom(s) && Quote_illegal) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('\'', writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
while (*s) {
|
2007-10-02 13:32:46 +01:00
|
|
|
wchar_t ch = *s++;
|
2008-10-23 22:17:45 +01:00
|
|
|
write_quoted(ch, '\'', writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('\'', writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2007-10-02 13:32:46 +01:00
|
|
|
wrputs((char *)s, writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
IsStringTerm(Term string) /* checks whether this is a string */
|
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-11-27 17:42:03 +00:00
|
|
|
putString(Term string, wrf writewch) /* writes a string */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
{
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('"', writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
while (string != TermNil) {
|
|
|
|
int ch = IntOfTerm(HeadOfTerm(string));
|
2008-10-23 22:17:45 +01:00
|
|
|
write_quoted(ch, '"', writewch);
|
2008-10-25 09:02:42 +01:00
|
|
|
string = TailOfTerm(string);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('"', writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = alphanum;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-11-27 17:42:03 +00:00
|
|
|
putUnquotedString(Term string, wrf writewch) /* writes a string */
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
while (string != TermNil) {
|
|
|
|
int ch = IntOfTerm(HeadOfTerm(string));
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(ch, writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
string = TailOfTerm(string);
|
|
|
|
}
|
|
|
|
lastw = alphanum;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (lastw == alphanum) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(' ', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('_', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
/* make sure we don't get no creepy spaces where they shouldn't be */
|
|
|
|
lastw = separator;
|
|
|
|
if (CellPtr(t) < H0) {
|
2007-08-06 15:55:43 +01:00
|
|
|
Int vcount = (H0-t)/(sizeof(attvar_record)/sizeof(CELL));
|
2001-04-09 20:54:03 +01:00
|
|
|
#if COROUTINING
|
|
|
|
#if DEBUG
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_Portray_delays) {
|
2001-04-09 20:54:03 +01:00
|
|
|
exts ext = ExtFromCell(t);
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Portray_delays = FALSE;
|
2004-06-05 04:37:01 +01:00
|
|
|
if (ext == attvars_ext) {
|
2001-04-09 20:54:03 +01:00
|
|
|
attvar_record *attv = (attvar_record *)t;
|
2002-10-17 01:05:29 +01:00
|
|
|
long sl = 0;
|
2005-09-09 18:24:39 +01:00
|
|
|
Term l = attv->Atts;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputs("$AT(",wglb->writewch);
|
2009-05-22 18:24:30 +01:00
|
|
|
write_var(t, wglb, rwt);
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(',', wglb->writewch);
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->keep_terms) {
|
2002-10-17 01:05:29 +01:00
|
|
|
/* garbage collection may be called */
|
2002-11-18 18:18:05 +00:00
|
|
|
sl = Yap_InitSlot((CELL)attv);
|
2002-10-17 01:05:29 +01:00
|
|
|
}
|
2009-05-22 18:24:30 +01:00
|
|
|
writeTerm((Term)&(attv->Value), 999, 1, FALSE, wglb, rwt);
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(',', wglb->writewch);
|
2009-05-22 18:24:30 +01:00
|
|
|
writeTerm(l, 999, 1, FALSE, wglb, rwt);
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->keep_terms) {
|
2002-11-18 18:18:05 +00:00
|
|
|
attv = (attvar_record *)Yap_GetFromSlot(sl);
|
|
|
|
Yap_RecoverSlots(1);
|
2002-10-17 01:05:29 +01:00
|
|
|
}
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(')', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Portray_delays = TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('D', wglb->writewch);
|
2007-08-06 15:55:43 +01:00
|
|
|
wrputn(vcount,wglb->writewch);
|
2006-11-14 11:42:26 +00:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputn(((Int) (t- H0)),wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 18:24:30 +01:00
|
|
|
static Term
|
2009-05-24 21:14:23 +01:00
|
|
|
from_pointer(CELL *ptr, struct rewind_term *rwt, struct write_globs *wglb)
|
2009-05-22 18:24:30 +01:00
|
|
|
{
|
|
|
|
Term t;
|
2009-05-24 21:14:23 +01:00
|
|
|
|
2009-05-22 19:24:27 +01:00
|
|
|
while (IsVarTerm(*ptr) && !IsUnboundVar(ptr))
|
2009-05-22 18:24:30 +01:00
|
|
|
ptr = (CELL *)*ptr;
|
|
|
|
t = *ptr;
|
2009-05-22 19:24:27 +01:00
|
|
|
if (!IsVarTerm(t)) {
|
2009-05-24 21:14:23 +01:00
|
|
|
if (wglb->keep_terms) {
|
|
|
|
rwt->u.s.old = Yap_InitSlot(t);
|
|
|
|
rwt->u.s.ptr = Yap_InitSlot((CELL)ptr);
|
|
|
|
} else {
|
|
|
|
rwt->u.d.old = t;
|
|
|
|
rwt->u.d.ptr = ptr;
|
|
|
|
}
|
2009-05-22 19:24:27 +01:00
|
|
|
*ptr = TermFoundVar;
|
|
|
|
}
|
2009-05-22 18:24:30 +01:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2009-05-24 21:14:23 +01:00
|
|
|
static void
|
|
|
|
restore_from_write(struct rewind_term *rwt, struct write_globs *wglb)
|
|
|
|
{
|
|
|
|
Term t;
|
|
|
|
if (rwt->u.s.ptr) {
|
|
|
|
CELL *ptr;
|
|
|
|
if (wglb->keep_terms) {
|
|
|
|
ptr = (CELL *)Yap_GetPtrFromSlot(rwt->u.s.ptr);
|
|
|
|
t = Yap_GetPtrFromSlot(rwt->u.s.old);
|
|
|
|
Yap_RecoverSlots(2);
|
|
|
|
} else {
|
|
|
|
ptr = rwt->u.d.ptr;
|
|
|
|
t = rwt->u.d.old;
|
|
|
|
}
|
|
|
|
*ptr = t;
|
|
|
|
}
|
|
|
|
rwt->u.s.ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
nrwt.u.s.ptr = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int new_depth = depth + 1;
|
|
|
|
long sl= 0;
|
|
|
|
int ndirection;
|
|
|
|
int do_jump;
|
|
|
|
|
|
|
|
if (wglb->keep_terms) {
|
|
|
|
/* garbage collection may be called */
|
|
|
|
sl = Yap_InitSlot(t);
|
|
|
|
}
|
|
|
|
writeTerm(from_pointer(RepPair(t), &nrwt, wglb), 999, new_depth, FALSE, wglb, &nrwt);
|
|
|
|
restore_from_write(&nrwt, wglb);
|
|
|
|
if (wglb->keep_terms) {
|
|
|
|
t = Yap_GetFromSlot(sl);
|
|
|
|
Yap_RecoverSlots(1);
|
|
|
|
}
|
|
|
|
ti = TailOfTerm(t);
|
|
|
|
if (IsVarTerm(ti))
|
|
|
|
break;
|
|
|
|
if (!IsPairTerm(ti))
|
|
|
|
break;
|
|
|
|
ndirection = RepPair(ti)-RepPair(t);
|
|
|
|
/* make sure we're not trapped in loops */
|
|
|
|
if (ndirection > 0) {
|
|
|
|
do_jump = (direction < 0);
|
|
|
|
} else if (ndirection == 0) {
|
|
|
|
wrputc(',', wglb->writewch);
|
|
|
|
putAtom(AtomFoundVar, wglb->Quote_illegal, wglb->writewch);
|
|
|
|
lastw = separator;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
do_jump = (direction > 0);
|
|
|
|
}
|
|
|
|
wrputc(',', wglb->writewch);
|
|
|
|
lastw = separator;
|
|
|
|
direction = ndirection;
|
|
|
|
if (do_jump)
|
|
|
|
break;
|
|
|
|
t = ti;
|
|
|
|
}
|
|
|
|
if (IsPairTerm(ti)) {
|
|
|
|
write_list(from_pointer(RepPair(t)+1, &nrwt, wglb), direction, depth+1, wglb, &nrwt);
|
|
|
|
restore_from_write(&nrwt, wglb);
|
|
|
|
} else if (ti != MkAtomTerm(AtomNil)) {
|
|
|
|
wrputc('|', wglb->writewch);
|
|
|
|
lastw = separator;
|
|
|
|
writeTerm(from_pointer(RepPair(t)+1, &nrwt, wglb), 999, depth + 1, FALSE, wglb, &nrwt);
|
|
|
|
restore_from_write(&nrwt, wglb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2009-05-22 18:24:30 +01:00
|
|
|
struct rewind_term nrwt;
|
|
|
|
nrwt.parent = rwt;
|
2009-05-24 21:14:23 +01:00
|
|
|
nrwt.u.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) {
|
2008-12-23 01:53:52 +00:00
|
|
|
putAtom(Atom3Dots, wglb->Quote_illegal, wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
|
|
|
}
|
2002-05-14 19:24:34 +01:00
|
|
|
if (EX != 0)
|
|
|
|
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)) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputn((Int) IntOfTerm(t),wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (IsAtomTerm(t)) {
|
2006-11-27 17:42:03 +00:00
|
|
|
putAtom(AtomOfTerm(t), wglb->Quote_illegal, wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (IsPairTerm(t)) {
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->Use_portray) {
|
2001-04-09 20:54:03 +01:00
|
|
|
Term targs[1];
|
2002-05-14 19:24:34 +01:00
|
|
|
Term old_EX = 0L;
|
2002-10-17 01:05:29 +01:00
|
|
|
long 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));
|
2002-05-14 19:24:34 +01:00
|
|
|
if (EX != 0L) old_EX = EX;
|
2002-11-18 18:18:05 +00:00
|
|
|
sl = Yap_InitSlot(t);
|
|
|
|
Yap_execute_goal(Yap_MkApplTerm(FunctorPortray, 1, targs), 0, 1);
|
|
|
|
t = Yap_GetFromSlot(sl);
|
|
|
|
Yap_RecoverSlots(1);
|
2002-05-14 19:24:34 +01:00
|
|
|
if (old_EX != 0L) 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;
|
|
|
|
}
|
|
|
|
if (yap_flags[WRITE_QUOTED_STRING_FLAG] && IsStringTerm(t)) {
|
2006-11-27 17:42:03 +00:00
|
|
|
putString(t, wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2009-05-24 21:14:23 +01:00
|
|
|
Term ls = t;
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('[', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
2009-05-24 21:14:23 +01:00
|
|
|
write_list(from_pointer(&ls, &nrwt, wglb), 0, depth+1, wglb, &nrwt);
|
|
|
|
restore_from_write(&nrwt, wglb);
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(']', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
|
|
|
} else { /* compound term */
|
|
|
|
Functor functor = FunctorOfTerm(t);
|
|
|
|
int Arity;
|
|
|
|
Atom atom;
|
2005-10-21 17:09:03 +01:00
|
|
|
OpEntry *opinfo;
|
2001-04-09 20:54:03 +01:00
|
|
|
int op, lp, rp;
|
|
|
|
|
2002-11-20 20:00:56 +00:00
|
|
|
if (IsExtensionFunctor(functor)) {
|
|
|
|
switch((CELL)functor) {
|
|
|
|
case (CELL)FunctorDouble:
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputf(FloatOfTerm(t),wglb->writewch);
|
2002-11-20 20:00:56 +00:00
|
|
|
return;
|
|
|
|
case (CELL)FunctorDBRef:
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputref(RefOfTerm(t), wglb->Quote_illegal, wglb->writewch);
|
2002-11-20 20:00:56 +00:00
|
|
|
return;
|
|
|
|
case (CELL)FunctorLongInt:
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputn(LongIntOfTerm(t),wglb->writewch);
|
2002-11-20 20:00:56 +00:00
|
|
|
return;
|
|
|
|
case (CELL)FunctorBigInt:
|
2008-04-14 18:30:18 +01:00
|
|
|
#ifdef USE_GMP
|
2002-11-20 20:00:56 +00:00
|
|
|
{
|
2006-01-02 02:16:19 +00:00
|
|
|
MP_INT *big = Yap_BigIntOfTerm(t);
|
2009-05-20 16:12:18 +01:00
|
|
|
char *s;
|
|
|
|
s = (char *) Yap_PreAllocCodeSpace();
|
|
|
|
while (s+3+mpz_sizeinbase(big, 10) >= (char *)AuxSp) {
|
2009-05-21 07:04:13 +01:00
|
|
|
#if USE_SYSTEM_MALLOC
|
|
|
|
/* may require stack expansion */
|
2009-05-23 00:35:24 +01:00
|
|
|
if (!Yap_ExpandPreAllocCodeSpace(3+mpz_sizeinbase(big, 10), NULL, TRUE)) {
|
2009-05-20 16:12:18 +01:00
|
|
|
s = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s = (char *) Yap_PreAllocCodeSpace();
|
2009-05-21 07:04:13 +01:00
|
|
|
#else
|
|
|
|
s = NULL;
|
|
|
|
#endif
|
2009-05-20 16:12:18 +01:00
|
|
|
}
|
|
|
|
if (!s) {
|
|
|
|
s = (char *)TR;
|
|
|
|
while (s+3+mpz_sizeinbase(big, 10) >= Yap_TrailTop) {
|
2009-05-21 07:04:13 +01:00
|
|
|
if (!Yap_growtrail((3+mpz_sizeinbase(big, 10))/sizeof(CELL), FALSE)) {
|
2009-05-20 16:12:18 +01:00
|
|
|
s = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s = (char *)TR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!s) {
|
2006-05-02 17:39:06 +01:00
|
|
|
s = (char *)H;
|
2008-11-28 15:54:46 +00:00
|
|
|
if (s+3+mpz_sizeinbase(big, 10) >= (char *)ASP) {
|
2009-05-20 16:12:18 +01:00
|
|
|
Yap_Error(OUT_OF_STACK_ERROR,TermNil,"not enough space to write bignum: it requires %d bytes", 3+mpz_sizeinbase(big, 10));
|
|
|
|
s = NULL;
|
2006-04-28 17:14:05 +01:00
|
|
|
}
|
2006-01-02 02:16:19 +00:00
|
|
|
}
|
|
|
|
if (mpz_sgn(big) < 0) {
|
|
|
|
if (lastw == symbol)
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(' ', wglb->writewch);
|
2006-01-02 02:16:19 +00:00
|
|
|
} else {
|
|
|
|
if (lastw == alphanum)
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(' ', wglb->writewch);
|
2006-01-02 02:16:19 +00:00
|
|
|
}
|
2009-05-21 07:04:13 +01:00
|
|
|
if (!s) {
|
|
|
|
s = mpz_get_str(NULL, 10, big);
|
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
wrputs(s,wglb->writewch);
|
|
|
|
free(s);
|
|
|
|
} else {
|
|
|
|
mpz_get_str(s, 10, big);
|
|
|
|
wrputs(s,wglb->writewch);
|
|
|
|
}
|
2002-11-20 20:00:56 +00:00
|
|
|
}
|
2008-04-14 18:30:18 +01:00
|
|
|
#else
|
|
|
|
{
|
|
|
|
wrputs("0",wglb->writewch);
|
|
|
|
}
|
2002-11-20 20:00:56 +00:00
|
|
|
#endif
|
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);
|
2005-10-21 17:09:03 +01:00
|
|
|
opinfo = Yap_GetOpProp(atom);
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef SFUNC
|
|
|
|
if (Arity == SFArity) {
|
|
|
|
int argno = 1;
|
|
|
|
CELL *p = ArgsOfSFTerm(t);
|
2006-11-27 17:42:03 +00:00
|
|
|
putAtom(atom, wglb->Quote_illegal, wglb->writewch);
|
|
|
|
wrputc('(', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
while (*p) {
|
2002-10-17 01:05:29 +01:00
|
|
|
long sl = 0;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
while (argno < *p) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('_', wglb->writewch), wrputc(',', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
++argno;
|
|
|
|
}
|
|
|
|
*p++;
|
|
|
|
lastw = separator;
|
|
|
|
/* cannot use the term directly with the SBA */
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->keep_terms) {
|
2002-10-17 01:05:29 +01:00
|
|
|
/* garbage collection may be called */
|
2002-11-18 18:18:05 +00:00
|
|
|
sl = Yap_InitSlot((CELL)p);
|
2002-10-17 01:05:29 +01:00
|
|
|
}
|
2009-05-24 21:14:23 +01:00
|
|
|
writeTerm(from_pointer(p++, &nrwt, wglb), 999, depth + 1, FALSE, wglb, &nrwt);
|
|
|
|
restore_from_write(&nrwt, wglb);
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->keep_terms) {
|
2002-10-17 01:05:29 +01:00
|
|
|
/* garbage collection may be called */
|
2002-11-18 18:18:05 +00:00
|
|
|
p = (CELL *)Yap_GetFromSlot(sl);
|
|
|
|
Yap_RecoverSlots(1);
|
2002-10-17 01:05:29 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
if (*p)
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(',', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
argno++;
|
|
|
|
}
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(')', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
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];
|
2002-05-14 19:24:34 +01:00
|
|
|
Term old_EX = 0L;
|
2002-10-17 01:05:29 +01:00
|
|
|
long sl = 0;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
targs[0] = t;
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_PutValue(AtomPortray, MkAtomTerm(AtomNil));
|
2002-05-14 19:24:34 +01:00
|
|
|
if (EX != 0L) old_EX = EX;
|
2002-11-18 18:18:05 +00:00
|
|
|
sl = Yap_InitSlot(t);
|
|
|
|
Yap_execute_goal(Yap_MkApplTerm(FunctorPortray, 1, targs),0, 1);
|
|
|
|
t = Yap_GetFromSlot(sl);
|
|
|
|
Yap_RecoverSlots(1);
|
2002-05-14 19:24:34 +01:00
|
|
|
if (old_EX != 0L) EX = old_EX;
|
2002-11-18 18:18:05 +00:00
|
|
|
if (Yap_GetValue(AtomPortray) == MkAtomTerm(AtomTrue) || EX != 0L)
|
2001-04-09 20:54:03 +01:00
|
|
|
return;
|
|
|
|
}
|
2002-11-20 20:00:56 +00:00
|
|
|
if (!wglb->Ignore_ops &&
|
2002-11-18 18:18:05 +00:00
|
|
|
Arity == 1 && opinfo && Yap_IsPrefixOp(opinfo, &op,
|
2001-04-09 20:54:03 +01:00
|
|
|
&rp)
|
|
|
|
#ifdef DO_NOT_WRITE_PLUS_AND_MINUS_AS_PREFIX
|
|
|
|
&&
|
|
|
|
/* never write '+' and '-' as infix
|
|
|
|
operators */
|
|
|
|
( (RepAtom(atom)->StrOfAE[0] != '+' &&
|
|
|
|
RepAtom(atom)->StrOfAE[0] != '-') ||
|
|
|
|
RepAtom(atom)->StrOfAE[1] )
|
|
|
|
#endif /* DO_NOT_WRITE_PLUS_AND_MINUS_AS_PREFIX */
|
|
|
|
) {
|
|
|
|
Term tright = ArgOfTerm(1, t);
|
|
|
|
int bracket_right =
|
|
|
|
!IsVarTerm(tright) && IsAtomTerm(tright) &&
|
|
|
|
RightOpToProtect(AtomOfTerm(tright), rp);
|
|
|
|
if (op > p) {
|
|
|
|
/* avoid stuff such as \+ (a,b) being written as \+(a,b) */
|
|
|
|
if (lastw != separator && !rinfixarg)
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(' ', wglb->writewch);
|
|
|
|
wrputc('(', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
2006-11-27 17:42:03 +00:00
|
|
|
putAtom(atom, wglb->Quote_illegal, wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (bracket_right) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('(', wglb->writewch);
|
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), rp, depth + 1, FALSE, wglb, &nrwt);
|
|
|
|
restore_from_write(&nrwt, wglb);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (bracket_right) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(')', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
|
|
|
if (op > p) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(')', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
2002-11-20 20:00:56 +00:00
|
|
|
} else if (!wglb->Ignore_ops &&
|
2002-11-18 18:18:05 +00:00
|
|
|
Arity == 1 && opinfo && Yap_IsPosfixOp(opinfo, &op, &lp)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
Term tleft = ArgOfTerm(1, t);
|
2002-10-17 01:05:29 +01:00
|
|
|
long sl = 0;
|
2001-04-09 20:54:03 +01:00
|
|
|
int bracket_left =
|
|
|
|
!IsVarTerm(tleft) && IsAtomTerm(tleft) &&
|
|
|
|
LeftOpToProtect(AtomOfTerm(tleft), lp);
|
|
|
|
if (op > p) {
|
|
|
|
/* avoid stuff such as \+ (a,b) being written as \+(a,b) */
|
|
|
|
if (lastw != separator && !rinfixarg)
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(' ', wglb->writewch);
|
|
|
|
wrputc('(', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
|
|
|
if (bracket_left) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('(', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->keep_terms) {
|
2002-10-17 01:05:29 +01:00
|
|
|
/* garbage collection may be called */
|
2002-11-18 18:18:05 +00:00
|
|
|
sl = Yap_InitSlot(t);
|
2002-10-17 01:05:29 +01:00
|
|
|
}
|
2009-05-24 21:14:23 +01:00
|
|
|
writeTerm(from_pointer(RepAppl(t)+1, &nrwt, wglb), lp, depth + 1, rinfixarg, wglb, &nrwt);
|
|
|
|
restore_from_write(&nrwt, wglb);
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->keep_terms) {
|
2002-10-17 01:05:29 +01:00
|
|
|
/* garbage collection may be called */
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_GetFromSlot(sl);
|
|
|
|
Yap_RecoverSlots(1);
|
2002-10-17 01:05:29 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
if (bracket_left) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(')', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
2006-11-27 17:42:03 +00:00
|
|
|
putAtom(atom, wglb->Quote_illegal, wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (op > p) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(')', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
2002-11-20 20:00:56 +00:00
|
|
|
} else if (!wglb->Ignore_ops &&
|
2002-11-18 18:18:05 +00:00
|
|
|
Arity == 2 && opinfo && Yap_IsInfixOp(opinfo, &op, &lp,
|
2001-04-09 20:54:03 +01:00
|
|
|
&rp) ) {
|
|
|
|
Term tleft = ArgOfTerm(1, t);
|
|
|
|
Term tright = ArgOfTerm(2, t);
|
2002-10-17 01:05:29 +01:00
|
|
|
long sl = 0;
|
2001-04-09 20:54:03 +01:00
|
|
|
int bracket_left =
|
|
|
|
!IsVarTerm(tleft) && IsAtomTerm(tleft) &&
|
|
|
|
LeftOpToProtect(AtomOfTerm(tleft), lp);
|
|
|
|
int bracket_right =
|
|
|
|
!IsVarTerm(tright) && IsAtomTerm(tright) &&
|
|
|
|
RightOpToProtect(AtomOfTerm(tright), rp);
|
|
|
|
|
|
|
|
if (op > p) {
|
|
|
|
/* avoid stuff such as \+ (a,b) being written as \+(a,b) */
|
|
|
|
if (lastw != separator && !rinfixarg)
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(' ', wglb->writewch);
|
|
|
|
wrputc('(', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
|
|
|
if (bracket_left) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('(', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->keep_terms) {
|
2002-10-17 01:05:29 +01:00
|
|
|
/* garbage collection may be called */
|
2002-11-18 18:18:05 +00:00
|
|
|
sl = Yap_InitSlot(t);
|
2002-10-17 01:05:29 +01:00
|
|
|
}
|
2009-05-24 21:14:23 +01:00
|
|
|
writeTerm(from_pointer(RepAppl(t)+1, &nrwt, wglb), lp, depth + 1, rinfixarg, wglb, &nrwt);
|
|
|
|
restore_from_write(&nrwt, wglb);
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->keep_terms) {
|
2002-10-17 01:05:29 +01:00
|
|
|
/* garbage collection may be called */
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_GetFromSlot(sl);
|
|
|
|
Yap_RecoverSlots(1);
|
2002-10-17 01:05:29 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
if (bracket_left) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(')', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
|
|
|
/* avoid quoting commas */
|
|
|
|
if (strcmp(RepAtom(atom)->StrOfAE,","))
|
2006-11-27 17:42:03 +00:00
|
|
|
putAtom(atom, wglb->Quote_illegal, wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
else {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(',', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
|
|
|
if (bracket_right) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('(', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
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) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(')', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
|
|
|
if (op > p) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(')', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
2002-11-20 20:00:56 +00:00
|
|
|
} else if (wglb->Handle_vars && functor == FunctorVar) {
|
2001-04-09 20:54:03 +01:00
|
|
|
Term ti = ArgOfTerm(1, t);
|
|
|
|
if (lastw == alphanum) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(' ', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (!IsVarTerm(ti) && (IsIntTerm(ti) || IsStringTerm(ti))) {
|
|
|
|
if (IsIntTerm(ti)) {
|
|
|
|
Int k = IntOfTerm(ti);
|
|
|
|
if (k == -1) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('_', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = alphanum;
|
|
|
|
return;
|
|
|
|
} else {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc((k % 26) + 'A', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (k >= 26) {
|
|
|
|
/* make sure we don't get confused about our context */
|
|
|
|
lastw = separator;
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputn( k / 26 ,wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else
|
|
|
|
lastw = alphanum;
|
|
|
|
}
|
|
|
|
} else {
|
2006-11-27 17:42:03 +00:00
|
|
|
putUnquotedString(ti, wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
} else {
|
2002-10-17 01:05:29 +01:00
|
|
|
long sl = 0;
|
|
|
|
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputs("'$VAR'(",wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->keep_terms) {
|
2002-10-17 01:05:29 +01:00
|
|
|
/* garbage collection may be called */
|
2002-11-18 18:18:05 +00:00
|
|
|
sl = Yap_InitSlot(t);
|
2002-10-17 01:05:29 +01:00
|
|
|
}
|
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);
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->keep_terms) {
|
2002-10-17 01:05:29 +01:00
|
|
|
/* garbage collection may be called */
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_GetFromSlot(sl);
|
|
|
|
Yap_RecoverSlots(1);
|
2002-10-17 01:05:29 +01:00
|
|
|
}
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(')', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
2009-04-22 00:04:36 +01:00
|
|
|
} else if (!wglb->Ignore_ops && functor == FunctorBraces) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('{', wglb->writewch);
|
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);
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('}', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
} else if (atom == AtomArray) {
|
2002-10-17 01:05:29 +01:00
|
|
|
long sl = 0;
|
|
|
|
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('{', wglb->writewch);
|
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) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('.', wglb->writewch);
|
|
|
|
wrputc('.', wglb->writewch);
|
|
|
|
wrputc('.', wglb->writewch);
|
2005-12-05 17:16:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->keep_terms) {
|
2002-10-17 01:05:29 +01:00
|
|
|
/* garbage collection may be called */
|
2002-11-18 18:18:05 +00:00
|
|
|
sl = Yap_InitSlot(t);
|
2002-10-17 01:05:29 +01:00
|
|
|
}
|
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);
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->keep_terms) {
|
2002-10-17 01:05:29 +01:00
|
|
|
/* garbage collection may be called */
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_GetFromSlot(sl);
|
|
|
|
Yap_RecoverSlots(1);
|
2002-10-17 01:05:29 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
if (op != Arity) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(',', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
|
|
|
}
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('}', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
} else {
|
2006-11-27 17:42:03 +00:00
|
|
|
putAtom(atom, wglb->Quote_illegal, wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('(', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
for (op = 1; op <= Arity; ++op) {
|
2002-10-17 01:05:29 +01:00
|
|
|
long sl = 0;
|
|
|
|
|
2005-12-05 17:16:12 +00:00
|
|
|
if (op == wglb->MaxArgs) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc('.', wglb->writewch);
|
|
|
|
wrputc('.', wglb->writewch);
|
|
|
|
wrputc('.', wglb->writewch);
|
2005-12-05 17:16:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->keep_terms) {
|
2002-10-17 01:05:29 +01:00
|
|
|
/* garbage collection may be called */
|
2002-11-18 18:18:05 +00:00
|
|
|
sl = Yap_InitSlot(t);
|
2002-10-17 01:05:29 +01:00
|
|
|
}
|
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);
|
2002-11-20 20:00:56 +00:00
|
|
|
if (wglb->keep_terms) {
|
2002-10-17 01:05:29 +01:00
|
|
|
/* garbage collection may be called */
|
2002-11-18 18:18:05 +00:00
|
|
|
t = Yap_GetFromSlot(sl);
|
|
|
|
Yap_RecoverSlots(1);
|
2002-10-17 01:05:29 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
if (op != Arity) {
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(',', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
|
|
|
}
|
2006-11-27 17:42:03 +00:00
|
|
|
wrputc(')', wglb->writewch);
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-05-22 19:24:27 +01:00
|
|
|
Yap_plwrite(Term t, int (*mywrite) (int, wchar_t), 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;
|
|
|
|
rwt.parent = NULL;
|
2009-05-24 21:14:23 +01:00
|
|
|
rwt.u.s.ptr = 0;
|
2002-11-20 20:00:56 +00:00
|
|
|
|
2006-11-27 17:42:03 +00:00
|
|
|
wglb.writewch = mywrite;
|
2001-04-09 20:54:03 +01:00
|
|
|
lastw = separator;
|
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;
|
|
|
|
wglb.MaxDepth = max_depth;
|
|
|
|
wglb.MaxList = max_list;
|
2005-12-05 17:16:12 +00:00
|
|
|
wglb.MaxArgs = max_write_args;
|
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 */
|
2004-01-29 13:37:10 +00:00
|
|
|
wglb.keep_terms = (flags & (Use_portray_f|To_heap_f));
|
2002-11-20 20:00:56 +00:00
|
|
|
wglb.Ignore_ops = flags & Ignore_ops_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
|
|
|
}
|
|
|
|
|