picrin/extlib/benz/write.c

509 lines
12 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2016-02-18 10:08:39 -05:00
#include "picrin/object.h"
2014-08-25 00:38:09 -04:00
2015-06-24 18:58:58 -04:00
KHASH_DECLARE(l, void *, int)
KHASH_DECLARE(v, void *, int)
KHASH_DEFINE2(l, void *, int, 1, kh_ptr_hash_func, kh_ptr_hash_equal)
KHASH_DEFINE2(v, void *, int, 0, kh_ptr_hash_func, kh_ptr_hash_equal)
2014-08-25 00:38:09 -04:00
struct writer_control {
pic_state *pic;
xFILE *file;
int mode;
int op;
2015-06-24 18:58:58 -04:00
khash_t(l) labels; /* object -> int */
khash_t(v) visited; /* object -> int */
2014-08-25 00:38:09 -04:00
int cnt;
};
#define WRITE_MODE 1
#define DISPLAY_MODE 2
#define OP_WRITE 1
#define OP_WRITE_SHARED 2
#define OP_WRITE_SIMPLE 3
2014-08-25 00:38:09 -04:00
static void
writer_control_init(struct writer_control *p, pic_state *pic, xFILE *file, int mode, int op)
2014-08-25 00:38:09 -04:00
{
p->pic = pic;
p->file = file;
p->mode = mode;
p->op = op;
2014-08-25 00:38:09 -04:00
p->cnt = 0;
2015-06-24 18:58:58 -04:00
kh_init(l, &p->labels);
kh_init(v, &p->visited);
2014-08-25 00:38:09 -04:00
}
static void
writer_control_destroy(struct writer_control *p)
{
2015-06-24 18:58:58 -04:00
pic_state *pic = p->pic;
kh_destroy(l, &p->labels);
kh_destroy(v, &p->visited);
2014-08-25 00:38:09 -04:00
}
2015-06-25 04:50:20 -04:00
static void
2016-02-19 09:22:41 -05:00
write_blob(pic_state *pic, pic_value blob, xFILE *file)
2015-06-25 04:50:20 -04:00
{
2016-02-19 09:22:41 -05:00
const unsigned char *buf;
int len, i;
buf = pic_blob(pic, blob, &len);
2015-06-25 04:50:20 -04:00
xfprintf(pic, file, "#u8(");
2016-02-19 09:22:41 -05:00
for (i = 0; i < len; ++i) {
xfprintf(pic, file, "%d", buf[i]);
if (i + 1 < len) {
2015-06-25 04:50:20 -04:00
xfprintf(pic, file, " ");
}
}
xfprintf(pic, file, ")");
}
static void
write_char(pic_state *pic, char c, xFILE *file, int mode)
{
if (mode == DISPLAY_MODE) {
xfputc(pic, c, file);
return;
}
switch (c) {
default: xfprintf(pic, file, "#\\%c", c); break;
case '\a': xfprintf(pic, file, "#\\alarm"); break;
case '\b': xfprintf(pic, file, "#\\backspace"); break;
case 0x7f: xfprintf(pic, file, "#\\delete"); break;
case 0x1b: xfprintf(pic, file, "#\\escape"); break;
case '\n': xfprintf(pic, file, "#\\newline"); break;
case '\r': xfprintf(pic, file, "#\\return"); break;
case ' ': xfprintf(pic, file, "#\\space"); break;
case '\t': xfprintf(pic, file, "#\\tab"); break;
}
}
static void
2016-02-19 13:26:52 -05:00
write_str(pic_state *pic, pic_value str, xFILE *file, int mode)
2015-06-25 04:50:20 -04:00
{
2015-08-26 06:04:27 -04:00
int i;
const char *cstr = pic_str(pic, str);
2015-06-25 04:50:20 -04:00
if (mode == DISPLAY_MODE) {
xfprintf(pic, file, "%s", pic_str(pic, str));
2015-06-25 04:50:20 -04:00
return;
}
xfprintf(pic, file, "\"");
for (i = 0; i < pic_str_len(pic, str); ++i) {
2015-06-25 04:50:20 -04:00
if (cstr[i] == '"' || cstr[i] == '\\') {
xfputc(pic, '\\', file);
}
xfputc(pic, cstr[i], file);
}
xfprintf(pic, file, "\"");
}
static void
write_float(pic_state *pic, double f, xFILE *file)
{
2015-07-19 13:19:41 -04:00
if (f != f) {
xfprintf(pic, file, "+nan.0");
} else if (f == 1.0 / 0.0) {
xfprintf(pic, file, "+inf.0");
} else if (f == -1.0 / 0.0) {
xfprintf(pic, file, "-inf.0");
2015-06-25 04:50:20 -04:00
} else {
xfprintf(pic, file, "%f", f);
}
}
2014-08-25 00:38:09 -04:00
static void write_core(struct writer_control *p, pic_value);
static void
2016-02-19 08:09:06 -05:00
write_pair_help(struct writer_control *p, pic_value pair)
2014-08-25 00:38:09 -04:00
{
2015-06-18 13:05:56 -04:00
pic_state *pic = p->pic;
2015-06-24 18:58:58 -04:00
khash_t(l) *lh = &p->labels;
khash_t(v) *vh = &p->visited;
khiter_t it;
int ret;
2014-08-25 00:38:09 -04:00
2016-02-19 08:09:06 -05:00
write_core(p, pic_car(pic, pair));
2014-08-25 00:38:09 -04:00
2016-02-19 08:09:06 -05:00
if (pic_nil_p(pic, pic_cdr(pic, pair))) {
2014-08-25 00:38:09 -04:00
return;
}
2016-02-19 08:09:06 -05:00
else if (pic_pair_p(pic, pic_cdr(pic, pair))) {
2014-08-25 00:38:09 -04:00
/* shared objects */
2016-02-19 08:09:06 -05:00
if ((it = kh_get(l, lh, pic_obj_ptr(pic_cdr(pic, pair)))) != kh_end(lh) && kh_val(lh, it) != -1) {
2015-06-18 13:05:56 -04:00
xfprintf(pic, p->file, " . ");
2014-08-25 00:38:09 -04:00
2016-02-19 08:09:06 -05:00
kh_put(v, vh, pic_obj_ptr(pic_cdr(pic, pair)), &ret);
2015-06-24 18:58:58 -04:00
if (ret == 0) { /* if exists */
xfprintf(pic, p->file, "#%d#", kh_val(lh, it));
2014-08-25 00:38:09 -04:00
return;
}
2015-06-24 18:58:58 -04:00
xfprintf(pic, p->file, "#%d=", kh_val(lh, it));
2014-08-25 00:38:09 -04:00
}
else {
2015-06-18 13:05:56 -04:00
xfprintf(pic, p->file, " ");
2014-08-25 00:38:09 -04:00
}
2016-02-19 08:09:06 -05:00
write_pair_help(p, pic_cdr(pic, pair));
if (p->op == OP_WRITE) {
2016-02-19 08:09:06 -05:00
if ((it = kh_get(l, lh, pic_obj_ptr(pic_cdr(pic, pair)))) != kh_end(lh) && kh_val(lh, it) != -1) {
it = kh_get(v, vh, pic_obj_ptr(pic_cdr(pic, pair)));
kh_del(v, vh, it);
}
}
2014-08-25 00:38:09 -04:00
return;
}
else {
2015-06-18 13:05:56 -04:00
xfprintf(pic, p->file, " . ");
2016-02-19 08:09:06 -05:00
write_core(p, pic_cdr(pic, pair));
2014-08-25 00:38:09 -04:00
}
}
static void
2016-02-19 08:09:06 -05:00
write_pair(struct writer_control *p, pic_value pair)
2015-06-25 04:50:20 -04:00
{
pic_state *pic = p->pic;
xFILE *file = p->file;
pic_sym *tag;
2016-02-19 08:09:06 -05:00
if (pic_pair_p(pic, pic_cdr(pic, pair)) && pic_nil_p(pic, pic_cddr(pic, pair)) && pic_sym_p(pic, pic_car(pic, pair))) {
tag = pic_sym_ptr(pic_car(pic, pair));
2015-06-25 04:50:20 -04:00
if (tag == pic->sQUOTE) {
xfprintf(pic, file, "'");
2016-02-19 08:09:06 -05:00
write_core(p, pic_cadr(pic, pair));
2015-06-25 04:50:20 -04:00
return;
}
else if (tag == pic->sUNQUOTE) {
xfprintf(pic, file, ",");
2016-02-19 08:09:06 -05:00
write_core(p, pic_cadr(pic, pair));
2015-06-25 04:50:20 -04:00
return;
}
else if (tag == pic->sUNQUOTE_SPLICING) {
xfprintf(pic, file, ",@");
2016-02-19 08:09:06 -05:00
write_core(p, pic_cadr(pic, pair));
2015-06-25 04:50:20 -04:00
return;
}
else if (tag == pic->sQUASIQUOTE) {
xfprintf(pic, file, "`");
2016-02-19 08:09:06 -05:00
write_core(p, pic_cadr(pic, pair));
2015-06-25 04:50:20 -04:00
return;
}
else if (tag == pic->sSYNTAX_QUOTE) {
xfprintf(pic, file, "#'");
2016-02-19 08:09:06 -05:00
write_core(p, pic_cadr(pic, pair));
2015-06-25 04:50:20 -04:00
return;
}
else if (tag == pic->sSYNTAX_UNQUOTE) {
xfprintf(pic, file, "#,");
2016-02-19 08:09:06 -05:00
write_core(p, pic_cadr(pic, pair));
2015-06-25 04:50:20 -04:00
return;
}
else if (tag == pic->sSYNTAX_UNQUOTE_SPLICING) {
xfprintf(pic, file, "#,@");
2016-02-19 08:09:06 -05:00
write_core(p, pic_cadr(pic, pair));
2015-06-25 04:50:20 -04:00
return;
}
else if (tag == pic->sSYNTAX_QUASIQUOTE) {
xfprintf(pic, file, "#`");
2016-02-19 08:09:06 -05:00
write_core(p, pic_cadr(pic, pair));
2015-06-25 04:50:20 -04:00
return;
}
}
xfprintf(pic, file, "(");
write_pair_help(p, pair);
xfprintf(pic, file, ")");
}
static void
2016-02-19 07:56:45 -05:00
write_vec(struct writer_control *p, pic_value vec)
2014-08-25 00:38:09 -04:00
{
2015-06-25 04:50:20 -04:00
pic_state *pic = p->pic;
xFILE *file = p->file;
2016-02-19 07:56:45 -05:00
int i, len = pic_vec_len(pic, vec);
2014-08-25 00:38:09 -04:00
2015-06-25 04:50:20 -04:00
xfprintf(pic, file, "#(");
2016-02-19 07:56:45 -05:00
for (i = 0; i < len; ++i) {
write_core(p, pic_vec_ref(pic, vec, i));
if (i + 1 < len) {
2015-06-25 04:50:20 -04:00
xfprintf(pic, file, " ");
2014-08-25 00:38:09 -04:00
}
}
2015-06-25 04:50:20 -04:00
xfprintf(pic, file, ")");
}
static void
2016-02-19 05:08:45 -05:00
write_dict(struct writer_control *p, pic_value dict)
2015-06-25 04:50:20 -04:00
{
pic_state *pic = p->pic;
xFILE *file = p->file;
2016-02-18 10:39:13 -05:00
pic_sym *key;
pic_value val;
int it = 0;
2015-06-25 04:50:20 -04:00
xfprintf(pic, file, "#.(dictionary");
2016-02-18 10:39:13 -05:00
while (pic_dict_next(pic, dict, &it, &key, &val)) {
xfprintf(pic, file, " '%s ", pic_str(pic, pic_sym_name(pic, key)));
write_core(p, val);
2015-06-25 04:50:20 -04:00
}
xfprintf(pic, file, ")");
2014-08-25 00:38:09 -04:00
}
static void
write_core(struct writer_control *p, pic_value obj)
{
pic_state *pic = p->pic;
2015-06-24 18:58:58 -04:00
khash_t(l) *lh = &p->labels;
khash_t(v) *vh = &p->visited;
2014-08-25 00:38:09 -04:00
xFILE *file = p->file;
2015-06-24 16:56:15 -04:00
khiter_t it;
2015-06-24 18:58:58 -04:00
int ret;
2014-08-25 00:38:09 -04:00
/* shared objects */
if (pic_obj_p(pic, obj) && ((it = kh_get(l, lh, pic_obj_ptr(obj))) != kh_end(lh)) && kh_val(lh, it) != -1) {
kh_put(v, vh, pic_obj_ptr(obj), &ret);
2015-06-24 18:58:58 -04:00
if (ret == 0) { /* if exists */
xfprintf(pic, file, "#%d#", kh_val(lh, it));
2014-08-25 00:38:09 -04:00
return;
}
2015-06-24 18:58:58 -04:00
xfprintf(pic, file, "#%d=", kh_val(lh, it));
2014-08-25 00:38:09 -04:00
}
switch (pic_type(pic, obj)) {
case PIC_TYPE_UNDEF:
2015-06-18 13:05:56 -04:00
xfprintf(pic, file, "#undefined");
2014-08-25 00:38:09 -04:00
break;
case PIC_TYPE_NIL:
2015-06-18 13:05:56 -04:00
xfprintf(pic, file, "()");
2014-08-25 00:38:09 -04:00
break;
case PIC_TYPE_TRUE:
xfprintf(pic, file, "#t");
2014-08-25 00:38:09 -04:00
break;
case PIC_TYPE_FALSE:
xfprintf(pic, file, "#f");
break;
case PIC_TYPE_ID:
2016-02-18 11:05:28 -05:00
xfprintf(pic, file, "#<identifier %s>", pic_str(pic, pic_id_name(pic, pic_id_ptr(obj))));
2014-08-25 00:38:09 -04:00
break;
case PIC_TYPE_EOF:
2015-06-25 04:50:20 -04:00
xfprintf(pic, file, "#.(eof-object)");
2014-08-25 00:38:09 -04:00
break;
case PIC_TYPE_INT:
xfprintf(pic, file, "%d", pic_int(pic, obj));
2014-08-25 00:38:09 -04:00
break;
case PIC_TYPE_FLOAT:
write_float(pic, pic_float(pic, obj), file);
2014-08-25 00:38:09 -04:00
break;
case PIC_TYPE_SYMBOL:
xfprintf(pic, file, "%s", pic_str(pic, pic_sym_name(pic, pic_sym_ptr(obj))));
2014-08-25 00:38:09 -04:00
break;
case PIC_TYPE_BLOB:
2016-02-19 09:22:41 -05:00
write_blob(pic, obj, file);
2015-06-25 04:50:20 -04:00
break;
case PIC_TYPE_CHAR:
write_char(pic, pic_char(pic, obj), file, p->mode);
2014-08-25 00:38:09 -04:00
break;
case PIC_TYPE_STRING:
2016-02-19 13:26:52 -05:00
write_str(pic, obj, file, p->mode);
2014-08-25 00:38:09 -04:00
break;
case PIC_TYPE_PAIR:
2016-02-19 08:09:06 -05:00
write_pair(p, obj);
2014-08-25 00:38:09 -04:00
break;
case PIC_TYPE_VECTOR:
2016-02-19 07:56:45 -05:00
write_vec(p, obj);
2014-08-25 00:38:09 -04:00
break;
case PIC_TYPE_DICT:
2016-02-19 05:08:45 -05:00
write_dict(p, obj);
break;
2014-08-25 00:38:09 -04:00
default:
xfprintf(pic, file, "#<%s %p>", pic_typename(pic, pic_type(pic, obj)), pic_obj_ptr(obj));
2014-08-25 00:38:09 -04:00
break;
}
if (p->op == OP_WRITE) {
if (pic_obj_p(pic, obj) && ((it = kh_get(l, lh, pic_obj_ptr(obj))) != kh_end(lh)) && kh_val(lh, it) != -1) {
it = kh_get(v, vh, pic_obj_ptr(obj));
kh_del(v, vh, it);
}
}
2014-08-25 00:38:09 -04:00
}
2015-06-25 05:51:21 -04:00
static void
traverse(struct writer_control *p, pic_value obj)
{
pic_state *pic = p->pic;
if (p->op == OP_WRITE_SIMPLE) {
return;
}
switch (pic_type(pic, obj)) {
case PIC_TYPE_PAIR:
case PIC_TYPE_VECTOR:
case PIC_TYPE_DICT: {
2015-06-25 05:51:21 -04:00
khash_t(l) *h = &p->labels;
khiter_t it;
int ret;
it = kh_put(l, h, pic_obj_ptr(obj), &ret);
2015-06-25 05:51:21 -04:00
if (ret != 0) {
/* first time */
kh_val(h, it) = -1;
if (pic_pair_p(pic, obj)) {
2015-06-25 05:51:21 -04:00
/* pair */
traverse(p, pic_car(pic, obj));
traverse(p, pic_cdr(pic, obj));
} else if (pic_vec_p(pic, obj)) {
2015-06-25 05:51:21 -04:00
/* vector */
2016-02-19 07:56:45 -05:00
int i, len = pic_vec_len(pic, obj);
for (i = 0; i < len; ++i) {
traverse(p, pic_vec_ref(pic, obj, i));
2015-06-25 05:51:21 -04:00
}
2015-06-25 09:33:17 -04:00
} else {
/* dictionary */
2016-02-18 10:39:13 -05:00
int it = 0;
pic_value val;
2016-02-19 05:08:45 -05:00
while (pic_dict_next(pic, obj, &it, NULL, &val)) {
2016-02-18 10:39:13 -05:00
traverse(p, val);
2015-06-25 09:33:17 -04:00
}
2015-06-25 05:51:21 -04:00
}
if (p->op == OP_WRITE) {
it = kh_get(l, h, pic_obj_ptr(obj));
2015-06-25 05:51:21 -04:00
if (kh_val(h, it) == -1) {
kh_del(l, h, it);
}
}
} else if (kh_val(h, it) == -1) {
/* second time */
kh_val(h, it) = p->cnt++;
}
break;
}
default:
break;
}
}
2014-08-25 00:38:09 -04:00
static void
write(pic_state *pic, pic_value obj, xFILE *file, int mode, int op)
2014-08-25 00:38:09 -04:00
{
struct writer_control p;
writer_control_init(&p, pic, file, mode, op);
2014-08-25 00:38:09 -04:00
2015-06-25 05:51:21 -04:00
traverse(&p, obj);
2014-08-25 00:38:09 -04:00
write_core(&p, obj);
writer_control_destroy(&p);
}
pic_value
pic_write(pic_state *pic, pic_value obj)
{
2015-05-27 11:14:10 -04:00
return pic_fwrite(pic, obj, pic_stdout(pic)->file);
2014-08-25 00:38:09 -04:00
}
pic_value
pic_fwrite(pic_state *pic, pic_value obj, xFILE *file)
{
write(pic, obj, file, WRITE_MODE, OP_WRITE);
2015-06-18 13:05:56 -04:00
xfflush(pic, file);
2014-08-25 00:38:09 -04:00
return obj;
}
pic_value
pic_display(pic_state *pic, pic_value obj)
{
2015-05-27 11:14:10 -04:00
return pic_fdisplay(pic, obj, pic_stdout(pic)->file);
2014-08-25 00:38:09 -04:00
}
pic_value
pic_fdisplay(pic_state *pic, pic_value obj, xFILE *file)
{
write(pic, obj, file, DISPLAY_MODE, OP_WRITE);
2015-06-18 13:05:56 -04:00
xfflush(pic, file);
2014-08-25 00:38:09 -04:00
return obj;
}
void
pic_printf(pic_state *pic, const char *fmt, ...)
{
2015-05-27 11:14:10 -04:00
xFILE *file = pic_stdout(pic)->file;
2014-08-25 00:38:09 -04:00
va_list ap;
2016-02-19 13:26:52 -05:00
pic_value str;
2014-08-25 00:38:09 -04:00
va_start(ap, fmt);
str = pic_vstrf_value(pic, fmt, ap);
2014-08-25 00:38:09 -04:00
va_end(ap);
xfprintf(pic, file, "%s", pic_str(pic, str));
2015-06-18 13:05:56 -04:00
xfflush(pic, file);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_write_write(pic_state *pic)
{
pic_value v;
struct pic_port *port = pic_stdout(pic);
pic_get_args(pic, "o|p", &v, &port);
write(pic, v, port->file, WRITE_MODE, OP_WRITE);
return pic_undef_value(pic);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_write_write_simple(pic_state *pic)
{
pic_value v;
struct pic_port *port = pic_stdout(pic);
pic_get_args(pic, "o|p", &v, &port);
write(pic, v, port->file, WRITE_MODE, OP_WRITE_SIMPLE);
return pic_undef_value(pic);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_write_write_shared(pic_state *pic)
{
pic_value v;
struct pic_port *port = pic_stdout(pic);
pic_get_args(pic, "o|p", &v, &port);
write(pic, v, port->file, WRITE_MODE, OP_WRITE_SHARED);
return pic_undef_value(pic);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_write_display(pic_state *pic)
{
pic_value v;
struct pic_port *port = pic_stdout(pic);
pic_get_args(pic, "o|p", &v, &port);
write(pic, v, port->file, DISPLAY_MODE, OP_WRITE);
return pic_undef_value(pic);
2014-08-25 00:38:09 -04:00
}
void
pic_init_write(pic_state *pic)
{
2014-08-31 22:37:52 -04:00
pic_defun(pic, "write", pic_write_write);
pic_defun(pic, "write-simple", pic_write_write_simple);
pic_defun(pic, "write-shared", pic_write_write_shared);
pic_defun(pic, "display", pic_write_display);
2014-08-25 00:38:09 -04:00
}