picrin/extlib/benz/write.c

478 lines
11 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-20 10:58:58 -05:00
#include "picrin/extra.h"
2016-02-20 11:13:16 -05:00
#include "picrin/private/object.h"
2014-08-25 00:38:09 -04:00
struct writer_control {
int mode;
int op;
2014-08-25 00:38:09 -04:00
int cnt;
2016-02-21 04:01:41 -05:00
pic_value shared; /* is object shared? (yes if >0) */
pic_value labels; /* object -> int */
2014-08-25 00:38:09 -04:00
};
#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
2016-02-21 04:01:41 -05:00
writer_control_init(pic_state *pic, struct writer_control *p, int mode, int op)
2014-08-25 00:38:09 -04:00
{
p->mode = mode;
p->op = op;
2014-08-25 00:38:09 -04:00
p->cnt = 0;
2016-02-21 04:01:41 -05:00
p->shared = pic_make_weak(pic);
p->labels = pic_make_weak(pic);
2014-08-25 00:38:09 -04:00
}
static void
traverse(pic_state *pic, pic_value obj, struct writer_control *p)
{
2016-02-21 04:01:41 -05:00
pic_value shared = p->shared;
if (p->op == OP_WRITE_SIMPLE) {
return;
}
switch (pic_type(pic, obj)) {
case PIC_TYPE_PAIR:
case PIC_TYPE_VECTOR:
case PIC_TYPE_DICT: {
2016-02-21 04:01:41 -05:00
if (! pic_weak_has(pic, shared, obj)) {
/* first time */
2016-02-21 04:01:41 -05:00
pic_weak_set(pic, shared, obj, pic_int_value(pic, 0));
if (pic_pair_p(pic, obj)) {
/* pair */
traverse(pic, pic_car(pic, obj), p);
traverse(pic, pic_cdr(pic, obj), p);
} else if (pic_vec_p(pic, obj)) {
/* vector */
int i, len = pic_vec_len(pic, obj);
for (i = 0; i < len; ++i) {
traverse(pic, pic_vec_ref(pic, obj, i), p);
}
} else {
/* dictionary */
int it = 0;
pic_value val;
while (pic_dict_next(pic, obj, &it, NULL, &val)) {
traverse(pic, val, p);
}
}
if (p->op == OP_WRITE) {
2016-02-21 04:01:41 -05:00
if (pic_int(pic, pic_weak_ref(pic, shared, obj)) == 0) {
pic_weak_del(pic, shared, obj);
}
}
} else {
/* second time */
2016-02-21 04:01:41 -05:00
pic_weak_set(pic, shared, obj, pic_int_value(pic, 1));
}
break;
}
default:
break;
}
}
static bool
is_shared_object(pic_state *pic, pic_value obj, struct writer_control *p) {
2016-02-21 04:01:41 -05:00
pic_value shared = p->shared;
if (! pic_obj_p(pic, obj)) {
return false;
}
2016-02-21 04:01:41 -05:00
if (! pic_weak_has(pic, shared, obj)) {
return false;
}
2016-02-21 04:01:41 -05:00
return pic_int(pic, pic_weak_ref(pic, shared, obj)) > 0;
}
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
2016-02-21 02:04:34 -05:00
write_char(pic_state *pic, pic_value ch, xFILE *file, struct writer_control *p)
2015-06-25 04:50:20 -04:00
{
2016-02-21 02:04:34 -05:00
char c = pic_char(pic, ch);
if (p->mode == DISPLAY_MODE) {
2015-06-25 04:50:20 -04:00
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-21 02:04:34 -05:00
write_str(pic_state *pic, pic_value str, xFILE *file, struct writer_control *p)
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
2016-02-21 02:04:34 -05:00
if (p->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
2016-02-21 04:28:59 -05:00
write_float(pic_state *pic, pic_value flo, xFILE *file)
2015-06-25 04:50:20 -04:00
{
2016-02-21 04:28:59 -05:00
double f = pic_float(pic, flo);
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);
}
}
2016-02-21 02:04:34 -05:00
static void write_core(pic_state *, pic_value, xFILE *, struct writer_control *);
2014-08-25 00:38:09 -04:00
static void
2016-02-21 02:04:34 -05:00
write_pair_help(pic_state *pic, pic_value pair, xFILE *file, struct writer_control *p)
2014-08-25 00:38:09 -04:00
{
pic_value cdr = pic_cdr(pic, pair);
2014-08-25 00:38:09 -04:00
2016-02-21 02:04:34 -05:00
write_core(pic, pic_car(pic, pair), file, p);
2014-08-25 00:38:09 -04:00
if (pic_nil_p(pic, cdr)) {
2014-08-25 00:38:09 -04:00
return;
}
else if (pic_pair_p(pic, cdr) && ! is_shared_object(pic, cdr, p)) {
xfprintf(pic, file, " ");
write_pair_help(pic, cdr, file, p);
2014-08-25 00:38:09 -04:00
}
else {
2016-02-21 02:04:34 -05:00
xfprintf(pic, file, " . ");
write_core(pic, cdr, file, p);
2014-08-25 00:38:09 -04:00
}
}
2016-02-22 12:37:33 -05:00
#define EQ(sym, lit) (strcmp(pic_sym(pic, sym), lit) == 0)
2016-02-20 14:34:26 -05:00
2014-08-25 00:38:09 -04:00
static void
2016-02-21 02:04:34 -05:00
write_pair(pic_state *pic, pic_value pair, xFILE *file, struct writer_control *p)
2015-06-25 04:50:20 -04:00
{
2016-02-20 01:31:14 -05:00
pic_value tag;
2015-06-25 04:50:20 -04:00
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))) {
2016-02-20 01:31:14 -05:00
tag = pic_car(pic, pair);
2016-02-20 14:34:26 -05:00
if (EQ(tag, "quote")) {
2015-06-25 04:50:20 -04:00
xfprintf(pic, file, "'");
2016-02-21 02:04:34 -05:00
write_core(pic, pic_cadr(pic, pair), file, p);
2015-06-25 04:50:20 -04:00
return;
}
2016-02-20 14:34:26 -05:00
else if (EQ(tag, "unquote")) {
2015-06-25 04:50:20 -04:00
xfprintf(pic, file, ",");
2016-02-21 02:04:34 -05:00
write_core(pic, pic_cadr(pic, pair), file, p);
2015-06-25 04:50:20 -04:00
return;
}
2016-02-20 14:34:26 -05:00
else if (EQ(tag, "unquote-splicing")) {
2015-06-25 04:50:20 -04:00
xfprintf(pic, file, ",@");
2016-02-21 02:04:34 -05:00
write_core(pic, pic_cadr(pic, pair), file, p);
2015-06-25 04:50:20 -04:00
return;
}
2016-02-20 14:34:26 -05:00
else if (EQ(tag, "quasiquote")) {
2015-06-25 04:50:20 -04:00
xfprintf(pic, file, "`");
2016-02-21 02:04:34 -05:00
write_core(pic, pic_cadr(pic, pair), file, p);
2015-06-25 04:50:20 -04:00
return;
}
2016-02-20 14:34:26 -05:00
else if (EQ(tag, "syntax-quote")) {
2015-06-25 04:50:20 -04:00
xfprintf(pic, file, "#'");
2016-02-21 02:04:34 -05:00
write_core(pic, pic_cadr(pic, pair), file, p);
2015-06-25 04:50:20 -04:00
return;
}
2016-02-20 14:34:26 -05:00
else if (EQ(tag, "syntax-unquote")) {
2015-06-25 04:50:20 -04:00
xfprintf(pic, file, "#,");
2016-02-21 02:04:34 -05:00
write_core(pic, pic_cadr(pic, pair), file, p);
2015-06-25 04:50:20 -04:00
return;
}
2016-02-20 14:34:26 -05:00
else if (EQ(tag, "syntax-unquote-splicing")) {
2015-06-25 04:50:20 -04:00
xfprintf(pic, file, "#,@");
2016-02-21 02:04:34 -05:00
write_core(pic, pic_cadr(pic, pair), file, p);
2015-06-25 04:50:20 -04:00
return;
}
2016-02-20 14:34:26 -05:00
else if (EQ(tag, "syntax-quasiquote")) {
2015-06-25 04:50:20 -04:00
xfprintf(pic, file, "#`");
2016-02-21 02:04:34 -05:00
write_core(pic, pic_cadr(pic, pair), file, p);
2015-06-25 04:50:20 -04:00
return;
}
}
xfprintf(pic, file, "(");
2016-02-21 02:04:34 -05:00
write_pair_help(pic, pair, file, p);
2015-06-25 04:50:20 -04:00
xfprintf(pic, file, ")");
}
static void
2016-02-21 02:04:34 -05:00
write_vec(pic_state *pic, pic_value vec, xFILE *file, struct writer_control *p)
2014-08-25 00:38:09 -04:00
{
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) {
2016-02-21 02:04:34 -05:00
write_core(pic, pic_vec_ref(pic, vec, i), file, p);
2016-02-19 07:56:45 -05:00
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-21 02:04:34 -05:00
write_dict(pic_state *pic, pic_value dict, xFILE *file, struct writer_control *p)
2015-06-25 04:50:20 -04:00
{
2016-02-20 01:31:14 -05:00
pic_value key, val;
2016-02-18 10:39:13 -05:00
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)) {
2016-02-22 12:37:33 -05:00
xfprintf(pic, file, " '%s ", pic_sym(pic, key));
2016-02-21 02:04:34 -05:00
write_core(pic, val, file, p);
2015-06-25 04:50:20 -04:00
}
xfprintf(pic, file, ")");
2014-08-25 00:38:09 -04:00
}
static void
2016-02-21 02:04:34 -05:00
write_core(pic_state *pic, pic_value obj, xFILE *file, struct writer_control *p)
2014-08-25 00:38:09 -04:00
{
2016-02-21 04:01:41 -05:00
pic_value labels = p->labels;
int i;
2014-08-25 00:38:09 -04:00
/* shared objects */
if (is_shared_object(pic, obj, p)) {
2016-02-21 04:01:41 -05:00
if (pic_weak_has(pic, labels, obj)) {
xfprintf(pic, file, "#%d#", pic_int(pic, pic_weak_ref(pic, labels, obj)));
2014-08-25 00:38:09 -04:00
return;
}
2016-02-21 04:01:41 -05:00
i = p->cnt++;
xfprintf(pic, file, "#%d=", i);
pic_weak_set(pic, labels, obj, pic_int_value(pic, i));
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-20 01:59:06 -05:00
xfprintf(pic, file, "#<identifier %s>", pic_str(pic, pic_id_name(pic, 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_SYMBOL:
2016-02-22 12:37:33 -05:00
xfprintf(pic, file, "%s", pic_sym(pic, obj));
2014-08-25 00:38:09 -04:00
break;
2016-02-21 04:28:59 -05:00
case PIC_TYPE_FLOAT:
write_float(pic, obj, file);
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:
2016-02-21 02:04:34 -05:00
write_char(pic, obj, file, p);
2014-08-25 00:38:09 -04:00
break;
case PIC_TYPE_STRING:
2016-02-21 02:04:34 -05:00
write_str(pic, obj, file, p);
2014-08-25 00:38:09 -04:00
break;
case PIC_TYPE_PAIR:
2016-02-21 02:04:34 -05:00
write_pair(pic, obj, file, p);
2014-08-25 00:38:09 -04:00
break;
case PIC_TYPE_VECTOR:
2016-02-21 02:04:34 -05:00
write_vec(pic, obj, file, p);
2014-08-25 00:38:09 -04:00
break;
case PIC_TYPE_DICT:
2016-02-21 02:04:34 -05:00
write_dict(pic, obj, file, p);
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 (is_shared_object(pic, obj, p)) {
2016-02-21 04:01:41 -05:00
pic_weak_del(pic, labels, obj);
2015-06-25 05:51:21 -04:00
}
}
}
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;
2016-02-21 04:01:41 -05:00
writer_control_init(pic, &p, mode, op);
2014-08-25 00:38:09 -04:00
2016-02-21 02:04:34 -05:00
traverse(pic, obj, &p);
2014-08-25 00:38:09 -04:00
2016-02-21 02:04:34 -05:00
write_core(pic, obj, file, &p);
2014-08-25 00:38:09 -04:00
}
pic_value
pic_write(pic_state *pic, pic_value obj)
{
2016-02-20 02:51:24 -05:00
return pic_fwrite(pic, obj, pic_fileno(pic, pic_stdout(pic)));
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)
{
2016-02-20 02:51:24 -05:00
return pic_fdisplay(pic, obj, pic_fileno(pic, pic_stdout(pic)));
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
2016-02-20 02:51:24 -05:00
pic_vfprintf(pic_state *pic, pic_value port, const char *fmt, va_list ap)
2014-08-25 00:38:09 -04:00
{
2016-02-20 02:51:24 -05:00
xFILE *file = pic_fileno(pic, port);
2016-02-19 13:26:52 -05:00
pic_value str;
2014-08-25 00:38:09 -04:00
str = pic_vstrf_value(pic, fmt, ap);
2014-08-25 00:38:09 -04:00
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
}
2016-02-20 02:51:24 -05:00
void
pic_fprintf(pic_state *pic, pic_value port, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
pic_vfprintf(pic, port, fmt, ap);
va_end(ap);
}
void
pic_printf(pic_state *pic, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
pic_vfprintf(pic, pic_stdout(pic), fmt, ap);
va_end(ap);
}
2014-08-25 00:38:09 -04:00
static pic_value
pic_write_write(pic_state *pic)
{
2016-02-20 02:51:24 -05:00
pic_value v, port = pic_stdout(pic);
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "o|p", &v, &port);
2016-02-20 02:51:24 -05:00
write(pic, v, pic_fileno(pic, port), 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)
{
2016-02-20 02:51:24 -05:00
pic_value v, port = pic_stdout(pic);
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "o|p", &v, &port);
2016-02-20 02:51:24 -05:00
write(pic, v, pic_fileno(pic, port), 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)
{
2016-02-20 02:51:24 -05:00
pic_value v, port = pic_stdout(pic);
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "o|p", &v, &port);
2016-02-20 02:51:24 -05:00
write(pic, v, pic_fileno(pic, port), 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)
{
2016-02-20 02:51:24 -05:00
pic_value v, port = pic_stdout(pic);
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "o|p", &v, &port);
2016-02-20 02:51:24 -05:00
write(pic, v, pic_fileno(pic, port), 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
}