2014-01-17 06:58:31 -05:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
2014-01-16 07:31:46 -05:00
|
|
|
#include "picrin.h"
|
|
|
|
#include "picrin/port.h"
|
|
|
|
#include "picrin/pair.h"
|
|
|
|
#include "picrin/blob.h"
|
|
|
|
#include "picrin/macro.h"
|
|
|
|
|
2014-02-12 09:07:12 -05:00
|
|
|
static bool
|
|
|
|
is_quote(pic_state *pic, pic_value pair)
|
|
|
|
{
|
2014-02-17 13:38:12 -05:00
|
|
|
return pic_pair_p(pic_cdr(pic, pair))
|
|
|
|
&& pic_nil_p(pic_cddr(pic, pair))
|
2014-02-12 09:07:12 -05:00
|
|
|
&& pic_eq_p(pic_car(pic, pair), pic_symbol_value(pic->sQUOTE));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
is_unquote(pic_state *pic, pic_value pair)
|
|
|
|
{
|
2014-02-17 13:38:12 -05:00
|
|
|
return pic_pair_p(pic_cdr(pic, pair))
|
|
|
|
&& pic_nil_p(pic_cddr(pic, pair))
|
2014-02-12 09:07:12 -05:00
|
|
|
&& pic_eq_p(pic_car(pic, pair), pic_symbol_value(pic->sUNQUOTE));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
is_unquote_splicing(pic_state *pic, pic_value pair)
|
|
|
|
{
|
2014-02-17 13:38:12 -05:00
|
|
|
return pic_pair_p(pic_cdr(pic, pair))
|
|
|
|
&& pic_nil_p(pic_cddr(pic, pair))
|
2014-02-12 09:07:12 -05:00
|
|
|
&& pic_eq_p(pic_car(pic, pair), pic_symbol_value(pic->sUNQUOTE_SPLICING));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
is_quasiquote(pic_state *pic, pic_value pair)
|
|
|
|
{
|
2014-02-17 13:38:12 -05:00
|
|
|
return pic_pair_p(pic_cdr(pic, pair))
|
|
|
|
&& pic_nil_p(pic_cddr(pic, pair))
|
2014-02-12 09:07:12 -05:00
|
|
|
&& pic_eq_p(pic_car(pic, pair), pic_symbol_value(pic->sQUASIQUOTE));
|
|
|
|
}
|
|
|
|
|
2014-02-17 13:39:32 -05:00
|
|
|
struct writer_control {
|
|
|
|
pic_state *pic;
|
|
|
|
XFILE *file;
|
|
|
|
xhash *labels;
|
|
|
|
xhash *visited;
|
|
|
|
int cnt;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct writer_control *
|
|
|
|
writer_control_new(pic_state *pic, XFILE *file)
|
|
|
|
{
|
|
|
|
struct writer_control *p;
|
|
|
|
|
|
|
|
p = (struct writer_control *)pic_alloc(pic, sizeof(struct writer_control));
|
|
|
|
p->pic = pic;
|
|
|
|
p->file = file;
|
|
|
|
p->labels = xh_new_ptr();
|
|
|
|
p->visited = xh_new_ptr();
|
|
|
|
p->cnt = 0;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
traverse_shared(struct writer_control *p, pic_value obj)
|
|
|
|
{
|
|
|
|
xh_entry *e;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
switch (pic_type(obj)) {
|
|
|
|
case PIC_TT_PAIR:
|
|
|
|
case PIC_TT_VECTOR:
|
|
|
|
e = xh_get(p->labels, pic_obj_ptr(obj));
|
|
|
|
if (e == NULL) {
|
|
|
|
xh_put(p->labels, pic_obj_ptr(obj), -1);
|
|
|
|
}
|
|
|
|
else if (e->val == -1) {
|
|
|
|
xh_put(p->labels, pic_obj_ptr(obj), p->cnt++);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pic_pair_p(obj)) {
|
|
|
|
traverse_shared(p, pic_car(p->pic, obj));
|
|
|
|
traverse_shared(p, pic_cdr(p->pic, obj));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (i = 0; i < pic_vec_ptr(obj)->len; ++i) {
|
|
|
|
traverse_shared(p, pic_vec_ptr(obj)->data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* pass */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_core(struct writer_control *p, pic_value);
|
|
|
|
|
2014-01-16 07:32:22 -05:00
|
|
|
static void
|
2014-02-17 13:39:32 -05:00
|
|
|
write_pair(struct writer_control *p, struct pic_pair *pair)
|
2014-01-16 07:32:22 -05:00
|
|
|
{
|
2014-02-17 13:39:32 -05:00
|
|
|
xh_entry *e;
|
|
|
|
|
|
|
|
write_core(p, pair->car);
|
2014-01-16 07:32:22 -05:00
|
|
|
|
|
|
|
if (pic_nil_p(pair->cdr)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-17 13:39:32 -05:00
|
|
|
else if (pic_pair_p(pair->cdr)) {
|
|
|
|
|
|
|
|
/* shared objects */
|
|
|
|
if ((e = xh_get(p->labels, pic_obj_ptr(pair->cdr))) && e->val != -1) {
|
|
|
|
xfprintf(p->file, " . ");
|
|
|
|
|
|
|
|
if ((xh_get(p->visited, pic_obj_ptr(pair->cdr)))) {
|
|
|
|
xfprintf(p->file, "#%d#", e->val);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
xfprintf(p->file, "#%d=", e->val);
|
|
|
|
xh_put(p->visited, pic_obj_ptr(pair->cdr), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
xfprintf(p->file, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
write_pair(p, pic_pair_ptr(pair->cdr));
|
2014-01-16 07:32:22 -05:00
|
|
|
return;
|
|
|
|
}
|
2014-02-17 13:39:32 -05:00
|
|
|
else {
|
|
|
|
xfprintf(p->file, " . ");
|
|
|
|
write_core(p, pair->cdr);
|
|
|
|
}
|
2014-01-16 07:32:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-02-08 03:15:34 -05:00
|
|
|
write_str(pic_state *pic, struct pic_string *str, XFILE *file)
|
2014-01-16 07:32:22 -05:00
|
|
|
{
|
2014-01-30 04:14:33 -05:00
|
|
|
size_t i;
|
2014-01-16 07:32:22 -05:00
|
|
|
const char *cstr = str->str;
|
|
|
|
|
2014-01-30 04:15:59 -05:00
|
|
|
UNUSED(pic);
|
|
|
|
|
2014-01-16 07:32:22 -05:00
|
|
|
for (i = 0; i < str->len; ++i) {
|
|
|
|
if (cstr[i] == '"' || cstr[i] == '\\') {
|
2014-02-08 03:15:34 -05:00
|
|
|
xfputc('\\', file);
|
2014-01-16 07:32:22 -05:00
|
|
|
}
|
2014-02-08 03:15:34 -05:00
|
|
|
xfputc(cstr[i], file);
|
2014-01-16 07:32:22 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-16 07:31:46 -05:00
|
|
|
|
|
|
|
static void
|
2014-02-17 13:39:32 -05:00
|
|
|
write_core(struct writer_control *p, pic_value obj)
|
2014-01-16 07:31:46 -05:00
|
|
|
{
|
2014-02-17 13:39:32 -05:00
|
|
|
pic_state *pic = p->pic;
|
|
|
|
XFILE *file = p->file;
|
2014-01-30 04:14:33 -05:00
|
|
|
size_t i;
|
2014-02-17 13:39:32 -05:00
|
|
|
xh_entry *e;
|
|
|
|
|
|
|
|
/* shared objects */
|
|
|
|
if (pic_vtype(obj) == PIC_VTYPE_HEAP
|
|
|
|
&& (e = xh_get(p->labels, pic_obj_ptr(obj)))
|
|
|
|
&& e->val != -1) {
|
|
|
|
if ((xh_get(p->visited, pic_obj_ptr(obj)))) {
|
|
|
|
xfprintf(file, "#%d#", e->val);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
xfprintf(file, "#%d=", e->val);
|
|
|
|
xh_put(p->visited, pic_obj_ptr(obj), 1);
|
|
|
|
}
|
|
|
|
}
|
2014-01-16 07:31:46 -05:00
|
|
|
|
|
|
|
switch (pic_type(obj)) {
|
|
|
|
case PIC_TT_NIL:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "()");
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_BOOL:
|
|
|
|
if (pic_true_p(obj))
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#t");
|
2014-01-16 07:31:46 -05:00
|
|
|
else
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#f");
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_PAIR:
|
2014-02-12 09:07:12 -05:00
|
|
|
if (is_quote(pic, obj)) {
|
|
|
|
xfprintf(file, "'");
|
2014-02-17 13:39:32 -05:00
|
|
|
write_core(p, pic_list_ref(pic, obj, 1));
|
2014-02-12 09:07:12 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (is_unquote(pic, obj)) {
|
|
|
|
xfprintf(file, ",");
|
2014-02-17 13:39:32 -05:00
|
|
|
write_core(p, pic_list_ref(pic, obj, 1));
|
2014-02-12 09:07:12 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (is_unquote_splicing(pic, obj)) {
|
|
|
|
xfprintf(file, ",@");
|
2014-02-17 13:39:32 -05:00
|
|
|
write_core(p, pic_list_ref(pic, obj, 1));
|
2014-02-12 09:07:12 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (is_quasiquote(pic, obj)) {
|
|
|
|
xfprintf(file, "`");
|
2014-02-17 13:39:32 -05:00
|
|
|
write_core(p, pic_list_ref(pic, obj, 1));
|
2014-02-12 09:07:12 -05:00
|
|
|
break;
|
|
|
|
}
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "(");
|
2014-02-17 13:39:32 -05:00
|
|
|
write_pair(p, pic_pair_ptr(obj));
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, ")");
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_SYMBOL:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "%s", pic_symbol_name(pic, pic_sym(obj)));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_CHAR:
|
|
|
|
switch (pic_char(obj)) {
|
2014-02-08 03:15:34 -05:00
|
|
|
default: xfprintf(file, "#\\%c", pic_char(obj)); break;
|
|
|
|
case '\a': xfprintf(file, "#\\alarm"); break;
|
|
|
|
case '\b': xfprintf(file, "#\\backspace"); break;
|
|
|
|
case 0x7f: xfprintf(file, "#\\delete"); break;
|
|
|
|
case 0x1b: xfprintf(file, "#\\escape"); break;
|
|
|
|
case '\n': xfprintf(file, "#\\newline"); break;
|
|
|
|
case '\r': xfprintf(file, "#\\return"); break;
|
|
|
|
case ' ': xfprintf(file, "#\\space"); break;
|
|
|
|
case '\t': xfprintf(file, "#\\tab"); break;
|
2014-01-16 07:31:46 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PIC_TT_FLOAT:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "%f", pic_float(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_INT:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "%d", pic_int(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_EOF:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<eof-object>");
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_UNDEF:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<undef>");
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_PROC:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<proc %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_PORT:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<port %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_STRING:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "\"");
|
|
|
|
write_str(pic, pic_str_ptr(obj), file);
|
|
|
|
xfprintf(file, "\"");
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_VECTOR:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#(");
|
2014-01-16 07:31:46 -05:00
|
|
|
for (i = 0; i < pic_vec_ptr(obj)->len; ++i) {
|
2014-02-17 13:39:32 -05:00
|
|
|
write_core(p, pic_vec_ptr(obj)->data[i]);
|
2014-01-16 07:31:46 -05:00
|
|
|
if (i + 1 < pic_vec_ptr(obj)->len) {
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, " ");
|
2014-01-16 07:31:46 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, ")");
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_BLOB:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#u8(");
|
2014-01-16 07:31:46 -05:00
|
|
|
for (i = 0; i < pic_blob_ptr(obj)->len; ++i) {
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "%d", pic_blob_ptr(obj)->data[i]);
|
2014-01-16 07:31:46 -05:00
|
|
|
if (i + 1 < pic_blob_ptr(obj)->len) {
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, " ");
|
2014-01-16 07:31:46 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, ")");
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_ERROR:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<error %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_ENV:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<env %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_CONT:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<cont %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_SENV:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<senv %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
2014-02-11 21:13:29 -05:00
|
|
|
case PIC_TT_MACRO:
|
|
|
|
xfprintf(file, "#<macro %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_SC:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<sc %p: ", pic_ptr(obj));
|
2014-02-17 13:39:32 -05:00
|
|
|
write_core(p, pic_sc(obj)->expr);
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, ">");
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_LIB:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<lib %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_VAR:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<var %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
2014-01-18 02:51:54 -05:00
|
|
|
case PIC_TT_IREP:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<irep %p>", pic_ptr(obj));
|
2014-01-18 02:51:54 -05:00
|
|
|
break;
|
2014-01-16 07:31:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 13:39:32 -05:00
|
|
|
static void
|
|
|
|
write_simple(pic_state *pic, pic_value obj, XFILE *file)
|
|
|
|
{
|
|
|
|
struct writer_control *p;
|
|
|
|
|
|
|
|
p = writer_control_new(pic, file);
|
|
|
|
|
|
|
|
/* no traverse here! */
|
|
|
|
|
|
|
|
write_core(p, obj);
|
|
|
|
}
|
|
|
|
|
2014-02-17 13:40:03 -05:00
|
|
|
static void
|
|
|
|
write_shared(pic_state *pic, pic_value obj, XFILE *file)
|
|
|
|
{
|
|
|
|
struct writer_control *p;
|
|
|
|
|
|
|
|
p = writer_control_new(pic, file);
|
|
|
|
|
|
|
|
traverse_shared(p, obj);
|
|
|
|
|
|
|
|
write_core(p, obj);
|
|
|
|
}
|
|
|
|
|
2014-02-10 23:38:42 -05:00
|
|
|
pic_value
|
2014-01-16 07:31:46 -05:00
|
|
|
pic_debug(pic_state *pic, pic_value obj)
|
|
|
|
{
|
2014-02-10 23:38:42 -05:00
|
|
|
return pic_fdebug(pic, obj, xstdout);
|
2014-02-10 10:47:57 -05:00
|
|
|
}
|
|
|
|
|
2014-02-10 23:38:42 -05:00
|
|
|
pic_value
|
2014-02-10 10:47:57 -05:00
|
|
|
pic_fdebug(pic_state *pic, pic_value obj, XFILE *file)
|
|
|
|
{
|
2014-02-17 13:40:03 -05:00
|
|
|
write_shared(pic, obj, file);
|
2014-02-10 10:47:57 -05:00
|
|
|
xfflush(file);
|
2014-02-10 23:38:42 -05:00
|
|
|
return obj;
|
2014-01-16 07:31:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_port_write_simple(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
2014-02-08 03:15:34 -05:00
|
|
|
struct pic_port *port = pic_stdout(pic);
|
2014-01-16 07:31:46 -05:00
|
|
|
|
2014-02-08 03:15:34 -05:00
|
|
|
pic_get_args(pic, "o|p", &v, &port);
|
2014-02-17 13:39:32 -05:00
|
|
|
write_simple(pic, v, port->file);
|
2014-01-16 07:31:46 -05:00
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
2014-02-17 13:40:03 -05:00
|
|
|
static pic_value
|
|
|
|
pic_port_write_shared(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
struct pic_port *port = pic_stdout(pic);
|
|
|
|
|
|
|
|
pic_get_args(pic, "o|p", &v, &port);
|
|
|
|
write_shared(pic, v, port->file);
|
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
2014-01-16 07:31:46 -05:00
|
|
|
void
|
|
|
|
pic_init_write(pic_state *pic)
|
|
|
|
{
|
2014-02-01 01:41:30 -05:00
|
|
|
pic_deflibrary ("(scheme write)") {
|
2014-01-16 07:31:46 -05:00
|
|
|
pic_defun(pic, "write-simple", pic_port_write_simple);
|
2014-02-17 13:40:03 -05:00
|
|
|
pic_defun(pic, "write-shared", pic_port_write_shared);
|
2014-01-16 07:31:46 -05:00
|
|
|
}
|
|
|
|
}
|