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;
|
2015-06-25 05:43:06 -04:00
|
|
|
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
|
|
|
|
|
2015-06-25 05:43:06 -04:00
|
|
|
#define OP_WRITE 1
|
|
|
|
#define OP_WRITE_SHARED 2
|
|
|
|
#define OP_WRITE_SIMPLE 3
|
|
|
|
|
2016-06-19 15:49:01 -04:00
|
|
|
#if PIC_USE_WRITE
|
|
|
|
static void write_value(pic_state *pic, pic_value obj, pic_value port, int mode, int op);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_int(pic_state *pic, pic_value port, long x, int base)
|
|
|
|
{
|
|
|
|
static const char digits[] = "0123456789abcdef";
|
|
|
|
char buf[20];
|
|
|
|
int i, neg;
|
|
|
|
|
|
|
|
neg = 0;
|
|
|
|
if (x < 0) {
|
|
|
|
neg = 1;
|
|
|
|
x = -x;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
buf[i++] = digits[x % base];
|
|
|
|
} while ((x /= base) != 0);
|
|
|
|
|
|
|
|
if (neg) {
|
|
|
|
buf[i++] = '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
while (i-- > 0) {
|
|
|
|
pic_fputc(pic, buf[i], port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pic_vfprintf(pic_state *pic, pic_value port, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
char *sval;
|
|
|
|
int ival;
|
|
|
|
void *vp;
|
|
|
|
long start = pic_fseek(pic, port, 0, PIC_SEEK_CUR);
|
|
|
|
|
|
|
|
for (p = fmt; *p; p++) {
|
|
|
|
|
|
|
|
#if PIC_USE_WRITE
|
|
|
|
if (*p == '~') {
|
|
|
|
switch (*++p) {
|
|
|
|
default:
|
|
|
|
pic_fputc(pic, *(p-1), port);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
pic_fputc(pic, '\n', port);
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
write_value(pic, va_arg(ap, pic_value), port, DISPLAY_MODE, OP_WRITE);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
write_value(pic, va_arg(ap, pic_value), port, WRITE_MODE, OP_WRITE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (*p != '%') {
|
|
|
|
pic_fputc(pic, *p, port);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (*++p) {
|
|
|
|
case 'd':
|
|
|
|
case 'i':
|
|
|
|
ival = va_arg(ap, int);
|
|
|
|
print_int(pic, port, ival, 10);
|
|
|
|
break;
|
|
|
|
case 'f': {
|
|
|
|
char buf[64];
|
|
|
|
PIC_DOUBLE_TO_CSTRING(va_arg(ap, double), buf);
|
|
|
|
pic_fputs(pic, buf, port);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'c':
|
|
|
|
ival = va_arg(ap, int);
|
|
|
|
pic_fputc(pic, ival, port);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sval = va_arg(ap, char*);
|
|
|
|
pic_fputs(pic, sval, port);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
vp = va_arg(ap, void*);
|
|
|
|
pic_fputs(pic, "0x", port);
|
|
|
|
print_int(pic, port, (long)vp, 16);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
pic_fputc(pic, *(p-1), port);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pic_fputc(pic, '%', port);
|
|
|
|
pic_fputc(pic, *(p-1), port);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pic_fseek(pic, port, 0, PIC_SEEK_CUR) - start;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pic_fprintf(pic_state *pic, pic_value port, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
n = pic_vfprintf(pic, port, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pic_printf(pic_state *pic, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
n = pic_vfprintf(pic, pic_stdout(pic), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if PIC_USE_WRITE
|
|
|
|
|
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;
|
2015-06-25 05:43:06 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-02-21 03:35:28 -05: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;
|
|
|
|
|
2016-02-21 03:35:28 -05:00
|
|
|
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)) {
|
2016-02-21 03:35:28 -05:00
|
|
|
/* first time */
|
2016-02-21 04:01:41 -05:00
|
|
|
pic_weak_set(pic, shared, obj, pic_int_value(pic, 0));
|
2016-02-21 03:35:28 -05:00
|
|
|
|
|
|
|
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);
|
2016-02-21 03:35:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* second time */
|
2016-02-21 04:01:41 -05:00
|
|
|
pic_weak_set(pic, shared, obj, pic_int_value(pic, 1));
|
2016-02-21 03:35:28 -05:00
|
|
|
}
|
|
|
|
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;
|
2016-02-21 03:35:28 -05:00
|
|
|
|
|
|
|
if (! pic_obj_p(pic, obj)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-21 04:01:41 -05:00
|
|
|
if (! pic_weak_has(pic, shared, obj)) {
|
2016-02-21 03:35:28 -05:00
|
|
|
return false;
|
|
|
|
}
|
2016-02-21 04:01:41 -05:00
|
|
|
return pic_int(pic, pic_weak_ref(pic, shared, obj)) > 0;
|
2016-02-21 03:35:28 -05:00
|
|
|
}
|
|
|
|
|
2015-06-25 04:50:20 -04:00
|
|
|
static void
|
2016-06-19 15:49:01 -04:00
|
|
|
write_blob(pic_state *pic, pic_value blob, pic_value port)
|
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
|
|
|
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "#u8(");
|
2016-02-19 09:22:41 -05:00
|
|
|
for (i = 0; i < len; ++i) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "%d", buf[i]);
|
2016-02-19 09:22:41 -05:00
|
|
|
if (i + 1 < len) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, " ");
|
2015-06-25 04:50:20 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, ")");
|
2015-06-25 04:50:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-06-19 15:49:01 -04:00
|
|
|
write_char(pic_state *pic, pic_value ch, pic_value port, 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) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fputc(pic, c, port);
|
2015-06-25 04:50:20 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (c) {
|
2016-06-19 15:49:01 -04:00
|
|
|
default: pic_fprintf(pic, port, "#\\%c", c); break;
|
|
|
|
case '\a': pic_fprintf(pic, port, "#\\alarm"); break;
|
|
|
|
case '\b': pic_fprintf(pic, port, "#\\backspace"); break;
|
|
|
|
case 0x7f: pic_fprintf(pic, port, "#\\delete"); break;
|
|
|
|
case 0x1b: pic_fprintf(pic, port, "#\\escape"); break;
|
|
|
|
case '\n': pic_fprintf(pic, port, "#\\newline"); break;
|
|
|
|
case '\r': pic_fprintf(pic, port, "#\\return"); break;
|
|
|
|
case ' ': pic_fprintf(pic, port, "#\\space"); break;
|
|
|
|
case '\t': pic_fprintf(pic, port, "#\\tab"); break;
|
2015-06-25 04:50:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-06-19 15:49:01 -04:00
|
|
|
write_str(pic_state *pic, pic_value str, pic_value port, struct writer_control *p)
|
2015-06-25 04:50:20 -04:00
|
|
|
{
|
2015-08-26 06:04:27 -04:00
|
|
|
int i;
|
2016-02-18 09:49:16 -05:00
|
|
|
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) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "%s", pic_str(pic, str));
|
2015-06-25 04:50:20 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "\"");
|
2016-02-18 06:15:42 -05:00
|
|
|
for (i = 0; i < pic_str_len(pic, str); ++i) {
|
2015-06-25 04:50:20 -04:00
|
|
|
if (cstr[i] == '"' || cstr[i] == '\\') {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fputc(pic, '\\', port);
|
2015-06-25 04:50:20 -04:00
|
|
|
}
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fputc(pic, cstr[i], port);
|
2015-06-25 04:50:20 -04:00
|
|
|
}
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "\"");
|
2015-06-25 04:50:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-06-19 15:49:01 -04:00
|
|
|
write_float(pic_state *pic, pic_value flo, pic_value port)
|
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) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "+nan.0");
|
2015-07-19 13:19:41 -04:00
|
|
|
} else if (f == 1.0 / 0.0) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "+inf.0");
|
2015-07-19 13:19:41 -04:00
|
|
|
} else if (f == -1.0 / 0.0) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "-inf.0");
|
2015-06-25 04:50:20 -04:00
|
|
|
} else {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "%f", f);
|
2015-06-25 04:50:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-19 15:49:01 -04:00
|
|
|
static void write_core(pic_state *, pic_value, pic_value port, struct writer_control *p);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
|
|
|
static void
|
2016-06-19 15:49:01 -04:00
|
|
|
write_pair_help(pic_state *pic, pic_value pair, pic_value port, struct writer_control *p)
|
2014-08-25 00:38:09 -04:00
|
|
|
{
|
2016-02-21 03:35:28 -05:00
|
|
|
pic_value cdr = pic_cdr(pic, pair);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-06-19 15:49:01 -04:00
|
|
|
write_core(pic, pic_car(pic, pair), port, p);
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2016-02-21 03:35:28 -05:00
|
|
|
if (pic_nil_p(pic, cdr)) {
|
2014-08-25 00:38:09 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-02-21 03:35:28 -05:00
|
|
|
else if (pic_pair_p(pic, cdr) && ! is_shared_object(pic, cdr, p)) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, " ");
|
|
|
|
write_pair_help(pic, cdr, port, p);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
else {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, " . ");
|
|
|
|
write_core(pic, cdr, port, 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-06-19 15:49:01 -04:00
|
|
|
write_pair(pic_state *pic, pic_value pair, pic_value port, 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")) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "'");
|
|
|
|
write_core(pic, pic_cadr(pic, pair), port, p);
|
2015-06-25 04:50:20 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-02-20 14:34:26 -05:00
|
|
|
else if (EQ(tag, "unquote")) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, ",");
|
|
|
|
write_core(pic, pic_cadr(pic, pair), port, p);
|
2015-06-25 04:50:20 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-02-20 14:34:26 -05:00
|
|
|
else if (EQ(tag, "unquote-splicing")) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, ",@");
|
|
|
|
write_core(pic, pic_cadr(pic, pair), port, p);
|
2015-06-25 04:50:20 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-02-20 14:34:26 -05:00
|
|
|
else if (EQ(tag, "quasiquote")) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "`");
|
|
|
|
write_core(pic, pic_cadr(pic, pair), port, p);
|
2015-06-25 04:50:20 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-02-20 14:34:26 -05:00
|
|
|
else if (EQ(tag, "syntax-quote")) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "#'");
|
|
|
|
write_core(pic, pic_cadr(pic, pair), port, p);
|
2015-06-25 04:50:20 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-02-20 14:34:26 -05:00
|
|
|
else if (EQ(tag, "syntax-unquote")) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "#,");
|
|
|
|
write_core(pic, pic_cadr(pic, pair), port, 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")) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "#,@");
|
|
|
|
write_core(pic, pic_cadr(pic, pair), port, p);
|
2015-06-25 04:50:20 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-02-20 14:34:26 -05:00
|
|
|
else if (EQ(tag, "syntax-quasiquote")) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "#`");
|
|
|
|
write_core(pic, pic_cadr(pic, pair), port, p);
|
2015-06-25 04:50:20 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "(");
|
|
|
|
write_pair_help(pic, pair, port, p);
|
|
|
|
pic_fprintf(pic, port, ")");
|
2015-06-25 04:50:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-06-19 15:49:01 -04:00
|
|
|
write_vec(pic_state *pic, pic_value vec, pic_value port, 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
|
|
|
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "#(");
|
2016-02-19 07:56:45 -05:00
|
|
|
for (i = 0; i < len; ++i) {
|
2016-06-19 15:49:01 -04:00
|
|
|
write_core(pic, pic_vec_ref(pic, vec, i), port, p);
|
2016-02-19 07:56:45 -05:00
|
|
|
if (i + 1 < len) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, " ");
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, ")");
|
2015-06-25 04:50:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-06-19 15:49:01 -04:00
|
|
|
write_dict(pic_state *pic, pic_value dict, pic_value port, 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
|
|
|
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "#.(dictionary");
|
2016-02-18 10:39:13 -05:00
|
|
|
while (pic_dict_next(pic, dict, &it, &key, &val)) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, " '%s ", pic_sym(pic, key));
|
|
|
|
write_core(pic, val, port, p);
|
2015-06-25 04:50:20 -04:00
|
|
|
}
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, ")");
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-06-19 15:49:01 -04:00
|
|
|
write_core(pic_state *pic, pic_value obj, pic_value port, 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 */
|
2016-02-21 03:35:28 -05:00
|
|
|
if (is_shared_object(pic, obj, p)) {
|
2016-02-21 04:01:41 -05:00
|
|
|
if (pic_weak_has(pic, labels, obj)) {
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "#%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++;
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "#%d=", i);
|
2016-02-21 04:01:41 -05:00
|
|
|
pic_weak_set(pic, labels, obj, pic_int_value(pic, i));
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
2016-02-18 06:15:42 -05:00
|
|
|
switch (pic_type(pic, obj)) {
|
2016-02-18 09:25:45 -05:00
|
|
|
case PIC_TYPE_UNDEF:
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "#undefined");
|
2014-08-25 00:38:09 -04:00
|
|
|
break;
|
2016-02-18 09:25:45 -05:00
|
|
|
case PIC_TYPE_NIL:
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "()");
|
2014-08-25 00:38:09 -04:00
|
|
|
break;
|
2016-02-18 09:25:45 -05:00
|
|
|
case PIC_TYPE_TRUE:
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "#t");
|
2014-08-25 00:38:09 -04:00
|
|
|
break;
|
2016-02-18 09:25:45 -05:00
|
|
|
case PIC_TYPE_FALSE:
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "#f");
|
2016-02-18 09:25:45 -05:00
|
|
|
break;
|
|
|
|
case PIC_TYPE_ID:
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "#<identifier %s>", pic_str(pic, pic_id_name(pic, obj)));
|
2014-08-25 00:38:09 -04:00
|
|
|
break;
|
2016-02-18 09:25:45 -05:00
|
|
|
case PIC_TYPE_EOF:
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "#.(eof-object)");
|
2014-08-25 00:38:09 -04:00
|
|
|
break;
|
2016-02-18 09:25:45 -05:00
|
|
|
case PIC_TYPE_INT:
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "%d", pic_int(pic, obj));
|
2014-08-25 00:38:09 -04:00
|
|
|
break;
|
2016-02-18 09:25:45 -05:00
|
|
|
case PIC_TYPE_SYMBOL:
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "%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:
|
2016-06-19 15:49:01 -04:00
|
|
|
write_float(pic, obj, port);
|
2016-02-21 04:28:59 -05:00
|
|
|
break;
|
2016-02-18 09:25:45 -05:00
|
|
|
case PIC_TYPE_BLOB:
|
2016-06-19 15:49:01 -04:00
|
|
|
write_blob(pic, obj, port);
|
2015-06-25 04:50:20 -04:00
|
|
|
break;
|
2016-02-18 09:25:45 -05:00
|
|
|
case PIC_TYPE_CHAR:
|
2016-06-19 15:49:01 -04:00
|
|
|
write_char(pic, obj, port, p);
|
2014-08-25 00:38:09 -04:00
|
|
|
break;
|
2016-02-18 09:25:45 -05:00
|
|
|
case PIC_TYPE_STRING:
|
2016-06-19 15:49:01 -04:00
|
|
|
write_str(pic, obj, port, p);
|
2014-08-25 00:38:09 -04:00
|
|
|
break;
|
2016-02-18 09:25:45 -05:00
|
|
|
case PIC_TYPE_PAIR:
|
2016-06-19 15:49:01 -04:00
|
|
|
write_pair(pic, obj, port, p);
|
2014-08-25 00:38:09 -04:00
|
|
|
break;
|
2016-02-18 09:25:45 -05:00
|
|
|
case PIC_TYPE_VECTOR:
|
2016-06-19 15:49:01 -04:00
|
|
|
write_vec(pic, obj, port, p);
|
2014-08-25 00:38:09 -04:00
|
|
|
break;
|
2016-02-18 09:25:45 -05:00
|
|
|
case PIC_TYPE_DICT:
|
2016-06-19 15:49:01 -04:00
|
|
|
write_dict(pic, obj, port, p);
|
2015-06-09 13:16:38 -04:00
|
|
|
break;
|
2014-08-25 00:38:09 -04:00
|
|
|
default:
|
2016-06-19 15:49:01 -04:00
|
|
|
pic_fprintf(pic, port, "#<%s %p>", pic_typename(pic, pic_type(pic, obj)), pic_obj_ptr(obj));
|
2014-08-25 00:38:09 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-06-25 05:43:06 -04:00
|
|
|
if (p->op == OP_WRITE) {
|
2016-02-21 03:35:28 -05:00
|
|
|
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
|
2016-06-19 15:49:01 -04:00
|
|
|
write_value(pic_state *pic, pic_value obj, pic_value port, 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-06-19 15:49:01 -04:00
|
|
|
write_core(pic, obj, port, &p);
|
2016-02-20 02:51:24 -05:00
|
|
|
}
|
|
|
|
|
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-06-19 15:49:01 -04:00
|
|
|
write_value(pic, v, port, WRITE_MODE, OP_WRITE);
|
2016-02-18 06:15:42 -05:00
|
|
|
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-06-19 15:49:01 -04:00
|
|
|
write_value(pic, v, port, WRITE_MODE, OP_WRITE_SIMPLE);
|
2016-02-18 06:15:42 -05:00
|
|
|
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-06-19 15:49:01 -04:00
|
|
|
write_value(pic, v, port, WRITE_MODE, OP_WRITE_SHARED);
|
2016-02-18 06:15:42 -05:00
|
|
|
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-06-19 15:49:01 -04:00
|
|
|
write_value(pic, v, port, DISPLAY_MODE, OP_WRITE);
|
2016-02-18 06:15:42 -05:00
|
|
|
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
|
|
|
}
|
2016-02-22 14:37:58 -05:00
|
|
|
|
|
|
|
#endif
|