137 lines
2.8 KiB
C
137 lines
2.8 KiB
C
|
|
#include "pl-incl.h"
|
|
|
|
typedef struct
|
|
{
|
|
term_t varnames; /* Report variables+names */
|
|
IOSTREAM *stream;
|
|
int has_exception; /* exception is raised */
|
|
|
|
term_t exception; /* raised exception */
|
|
} read_data, *ReadData;
|
|
|
|
static void
|
|
init_read_data(ReadData _PL_rd, IOSTREAM *in ARG_LD)
|
|
{
|
|
_PL_rd->varnames = 0;
|
|
_PL_rd->stream = in;
|
|
_PL_rd->has_exception = 0;
|
|
_PL_rd->exception = 0;
|
|
}
|
|
|
|
static void
|
|
free_read_data(ReadData _PL_rd)
|
|
{
|
|
}
|
|
|
|
static int
|
|
read_term(term_t t, ReadData rd ARG_LD)
|
|
{
|
|
return Yap_read_term(t, rd->stream, rd->varnames);
|
|
}
|
|
|
|
|
|
/*******************************
|
|
* TERM <->ATOM *
|
|
*******************************/
|
|
|
|
static int
|
|
atom_to_term(term_t atom, term_t term, term_t bindings)
|
|
{ GET_LD
|
|
PL_chars_t txt;
|
|
|
|
if ( !bindings && PL_is_variable(atom) ) /* term_to_atom(+, -) */
|
|
{ char buf[1024];
|
|
size_t bufsize = sizeof(buf);
|
|
int rval;
|
|
char *s = buf;
|
|
IOSTREAM *stream;
|
|
PL_chars_t txt;
|
|
|
|
stream = Sopenmem(&s, &bufsize, "w");
|
|
stream->encoding = ENC_UTF8;
|
|
PL_write_term(stream, term, 1200, PL_WRT_QUOTED);
|
|
Sflush(stream);
|
|
|
|
txt.text.t = s;
|
|
txt.length = bufsize;
|
|
txt.storage = PL_CHARS_HEAP;
|
|
txt.encoding = ENC_UTF8;
|
|
txt.canonical = FALSE;
|
|
rval = PL_unify_text(atom, 0, &txt, PL_ATOM);
|
|
|
|
Sclose(stream);
|
|
if ( s != buf )
|
|
Sfree(s);
|
|
|
|
return rval;
|
|
}
|
|
|
|
if ( PL_get_text(atom, &txt, CVT_ALL|CVT_EXCEPTION) )
|
|
{ GET_LD
|
|
read_data rd;
|
|
int rval;
|
|
IOSTREAM *stream;
|
|
source_location oldsrc = LD->read_source;
|
|
|
|
stream = Sopen_text(&txt, "r");
|
|
|
|
init_read_data(&rd, stream PASS_LD);
|
|
if ( bindings && (PL_is_variable(bindings) || PL_is_list(bindings)) )
|
|
rd.varnames = bindings;
|
|
else if ( bindings )
|
|
return PL_error(NULL, 0, NULL, ERR_TYPE, ATOM_list, bindings);
|
|
|
|
if ( !(rval = read_term(term, &rd PASS_LD)) && rd.has_exception )
|
|
rval = PL_raise_exception(rd.exception);
|
|
free_read_data(&rd);
|
|
Sclose(stream);
|
|
LD->read_source = oldsrc;
|
|
|
|
return rval;
|
|
}
|
|
|
|
fail;
|
|
}
|
|
|
|
|
|
static
|
|
PRED_IMPL("atom_to_term", 3, atom_to_term, 0)
|
|
{ return atom_to_term(A1, A2, A3);
|
|
}
|
|
|
|
|
|
static
|
|
PRED_IMPL("term_to_atom", 2, term_to_atom, 0)
|
|
{ return atom_to_term(A2, A1, 0);
|
|
}
|
|
|
|
|
|
int
|
|
PL_chars_to_term(const char *s, term_t t)
|
|
{ GET_LD
|
|
read_data rd;
|
|
int rval;
|
|
IOSTREAM *stream = Sopen_string(NULL, (char *)s, -1, "r");
|
|
source_location oldsrc = LD->read_source;
|
|
|
|
init_read_data(&rd, stream PASS_LD);
|
|
PL_put_variable(t);
|
|
if ( !(rval = read_term(t, &rd PASS_LD)) && rd.has_exception )
|
|
PL_put_term(t, rd.exception);
|
|
free_read_data(&rd);
|
|
Sclose(stream);
|
|
LD->read_source = oldsrc;
|
|
|
|
return rval;
|
|
}
|
|
|
|
/*******************************
|
|
* PUBLISH PREDICATES *
|
|
*******************************/
|
|
|
|
BeginPredDefs(read)
|
|
PRED_DEF("atom_to_term", 3, atom_to_term, 0)
|
|
PRED_DEF("term_to_atom", 2, term_to_atom, 0)
|
|
EndPredDefs
|