picrin/src/write.c

462 lines
9.6 KiB
C
Raw Normal View History

2014-01-17 06:58:31 -05:00
/**
* See Copyright Notice in picrin.h
*/
2014-07-26 05:03:51 -04:00
#include <math.h>
#include "picrin.h"
#include "picrin/port.h"
#include "picrin/pair.h"
#include "picrin/string.h"
#include "picrin/vector.h"
#include "picrin/blob.h"
#include "picrin/macro.h"
static bool
2014-02-17 13:42:33 -05:00
is_tagged(pic_state *pic, pic_sym tag, 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-17 13:42:33 -05:00
&& pic_eq_p(pic_car(pic, pair), pic_symbol_value(tag));
}
static bool
is_quote(pic_state *pic, pic_value pair)
{
return is_tagged(pic, pic->sQUOTE, pair);
}
static bool
is_unquote(pic_state *pic, pic_value pair)
{
2014-02-17 13:42:33 -05:00
return is_tagged(pic, pic->sUNQUOTE, pair);
}
static bool
is_unquote_splicing(pic_state *pic, pic_value pair)
{
2014-02-17 13:42:33 -05:00
return is_tagged(pic, pic->sUNQUOTE_SPLICING, pair);
}
static bool
is_quasiquote(pic_state *pic, pic_value pair)
{
2014-02-17 13:42:33 -05:00
return is_tagged(pic, pic->sQUASIQUOTE, pair);
}
struct writer_control {
pic_state *pic;
2014-02-25 07:07:32 -05:00
xFILE *file;
2014-03-03 10:09:32 -05:00
int mode;
2014-03-25 02:29:26 -04:00
xhash labels; /* object -> int */
xhash visited; /* object -> int */
int cnt;
};
2014-03-03 10:09:32 -05:00
#define WRITE_MODE 1
#define DISPLAY_MODE 2
static void
writer_control_init(struct writer_control *p, pic_state *pic, xFILE *file, int mode)
{
p->pic = pic;
p->file = file;
2014-03-03 10:09:32 -05:00
p->mode = mode;
p->cnt = 0;
2014-03-25 02:29:26 -04:00
xh_init_ptr(&p->labels, sizeof(int));
xh_init_ptr(&p->visited, sizeof(int));
}
2014-02-17 13:45:00 -05:00
static void
writer_control_destroy(struct writer_control *p)
{
2014-03-25 02:29:26 -04:00
xh_destroy(&p->labels);
xh_destroy(&p->visited);
2014-02-17 13:45:00 -05:00
}
static void
traverse_shared(struct writer_control *p, pic_value obj)
{
xh_entry *e;
size_t i;
2014-03-25 02:29:26 -04:00
int c;
switch (pic_type(obj)) {
case PIC_TT_PAIR:
case PIC_TT_VECTOR:
2014-08-01 05:48:14 -04:00
e = xh_get_ptr(&p->labels, pic_obj_ptr(obj));
if (e == NULL) {
2014-03-25 02:29:26 -04:00
c = -1;
2014-08-01 05:48:14 -04:00
xh_put_ptr(&p->labels, pic_obj_ptr(obj), &c);
}
2014-03-25 02:29:26 -04:00
else if (xh_val(e, int) == -1) {
c = p->cnt++;
2014-08-01 05:48:14 -04:00
xh_put_ptr(&p->labels, pic_obj_ptr(obj), &c);
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
write_pair(struct writer_control *p, struct pic_pair *pair)
2014-01-16 07:32:22 -05:00
{
xh_entry *e;
2014-03-25 02:29:26 -04:00
int c;
write_core(p, pair->car);
2014-01-16 07:32:22 -05:00
if (pic_nil_p(pair->cdr)) {
return;
}
else if (pic_pair_p(pair->cdr)) {
/* shared objects */
2014-08-01 05:48:14 -04:00
if ((e = xh_get_ptr(&p->labels, pic_obj_ptr(pair->cdr))) && xh_val(e, int) != -1) {
xfprintf(p->file, " . ");
2014-08-01 05:48:14 -04:00
if ((xh_get_ptr(&p->visited, pic_obj_ptr(pair->cdr)))) {
2014-03-25 02:29:26 -04:00
xfprintf(p->file, "#%d#", xh_val(e, int));
return;
}
else {
2014-03-25 02:29:26 -04:00
xfprintf(p->file, "#%d=", xh_val(e, int));
c = 1;
2014-08-01 05:48:14 -04:00
xh_put_ptr(&p->visited, pic_obj_ptr(pair->cdr), &c);
}
}
else {
xfprintf(p->file, " ");
}
write_pair(p, pic_pair_ptr(pair->cdr));
2014-01-16 07:32:22 -05:00
return;
}
else {
xfprintf(p->file, " . ");
write_core(p, pair->cdr);
}
2014-01-16 07:32:22 -05:00
}
static void
2014-02-25 07:07:32 -05:00
write_str(pic_state *pic, struct pic_string *str, xFILE *file)
2014-01-16 07:32:22 -05:00
{
size_t i;
const char *cstr = pic_str_cstr(str);
2014-01-16 07:32:22 -05:00
2014-01-30 04:15:59 -05:00
UNUSED(pic);
for (i = 0; i < pic_strlen(str); ++i) {
2014-01-16 07:32:22 -05:00
if (cstr[i] == '"' || cstr[i] == '\\') {
xfputc('\\', file);
2014-01-16 07:32:22 -05:00
}
xfputc(cstr[i], file);
2014-01-16 07:32:22 -05:00
}
}
static void
write_core(struct writer_control *p, pic_value obj)
{
pic_state *pic = p->pic;
2014-02-25 07:07:32 -05:00
xFILE *file = p->file;
size_t i;
xh_entry *e;
2014-03-25 02:29:26 -04:00
int c;
2014-07-26 05:03:51 -04:00
float f;
/* shared objects */
if (pic_vtype(obj) == PIC_VTYPE_HEAP
2014-08-01 05:48:14 -04:00
&& (e = xh_get_ptr(&p->labels, pic_obj_ptr(obj)))
2014-03-25 02:29:26 -04:00
&& xh_val(e, int) != -1) {
2014-08-01 05:48:14 -04:00
if ((xh_get_ptr(&p->visited, pic_obj_ptr(obj)))) {
2014-03-25 02:29:26 -04:00
xfprintf(file, "#%d#", xh_val(e, int));
return;
}
else {
2014-03-25 02:29:26 -04:00
xfprintf(file, "#%d=", xh_val(e, int));
c = 1;
2014-08-01 05:48:14 -04:00
xh_put_ptr(&p->visited, pic_obj_ptr(obj), &c);
}
}
switch (pic_type(obj)) {
case PIC_TT_NIL:
xfprintf(file, "()");
break;
case PIC_TT_BOOL:
if (pic_true_p(obj))
xfprintf(file, "#t");
else
xfprintf(file, "#f");
break;
case PIC_TT_PAIR:
if (is_quote(pic, obj)) {
xfprintf(file, "'");
write_core(p, pic_list_ref(pic, obj, 1));
break;
}
else if (is_unquote(pic, obj)) {
xfprintf(file, ",");
write_core(p, pic_list_ref(pic, obj, 1));
break;
}
else if (is_unquote_splicing(pic, obj)) {
xfprintf(file, ",@");
write_core(p, pic_list_ref(pic, obj, 1));
break;
}
else if (is_quasiquote(pic, obj)) {
xfprintf(file, "`");
write_core(p, pic_list_ref(pic, obj, 1));
break;
}
xfprintf(file, "(");
write_pair(p, pic_pair_ptr(obj));
xfprintf(file, ")");
break;
case PIC_TT_SYMBOL:
xfprintf(file, "%s", pic_symbol_name(pic, pic_sym(obj)));
break;
case PIC_TT_CHAR:
2014-03-03 10:09:32 -05:00
if (p->mode == DISPLAY_MODE) {
xfputc(pic_char(obj), file);
break;
}
switch (pic_char(obj)) {
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;
}
break;
case PIC_TT_FLOAT:
2014-07-26 05:03:51 -04:00
f = pic_float(obj);
if (isnan(f)) {
xfprintf(file, signbit(f) ? "-nan.0" : "+nan.0");
} else if (isinf(f)) {
xfprintf(file, signbit(f) ? "-inf.0" : "+inf.0");
} else {
xfprintf(file, "%f", pic_float(obj));
}
break;
case PIC_TT_INT:
xfprintf(file, "%d", pic_int(obj));
break;
case PIC_TT_EOF:
xfprintf(file, "#<eof-object>");
break;
case PIC_TT_UNDEF:
xfprintf(file, "#<undef>");
break;
case PIC_TT_STRING:
2014-03-03 10:09:32 -05:00
if (p->mode == DISPLAY_MODE) {
xfprintf(file, "%s", pic_str_cstr(pic_str_ptr(obj)));
break;
}
xfprintf(file, "\"");
write_str(pic, pic_str_ptr(obj), file);
xfprintf(file, "\"");
break;
case PIC_TT_VECTOR:
xfprintf(file, "#(");
for (i = 0; i < pic_vec_ptr(obj)->len; ++i) {
write_core(p, pic_vec_ptr(obj)->data[i]);
if (i + 1 < pic_vec_ptr(obj)->len) {
xfprintf(file, " ");
}
}
xfprintf(file, ")");
break;
case PIC_TT_BLOB:
xfprintf(file, "#u8(");
for (i = 0; i < pic_blob_ptr(obj)->len; ++i) {
xfprintf(file, "%d", pic_blob_ptr(obj)->data[i]);
if (i + 1 < pic_blob_ptr(obj)->len) {
xfprintf(file, " ");
}
}
xfprintf(file, ")");
break;
2014-07-26 22:10:59 -04:00
default:
xfprintf(file, "#<%s %p>", pic_type_repr(pic_type(obj)), pic_ptr(obj));
}
}
static void
2014-02-25 07:07:32 -05:00
write(pic_state *pic, pic_value obj, xFILE *file)
{
struct writer_control p;
writer_control_init(&p, pic, file, WRITE_MODE);
traverse_shared(&p, obj); /* FIXME */
write_core(&p, obj);
writer_control_destroy(&p);
}
static void
2014-02-25 07:07:32 -05:00
write_simple(pic_state *pic, pic_value obj, xFILE *file)
{
struct writer_control p;
writer_control_init(&p, pic, file, WRITE_MODE);
/* no traverse here! */
write_core(&p, obj);
2014-02-17 13:45:00 -05:00
writer_control_destroy(&p);
}
2014-02-17 13:40:03 -05:00
static void
2014-02-25 07:07:32 -05:00
write_shared(pic_state *pic, pic_value obj, xFILE *file)
2014-02-17 13:40:03 -05:00
{
struct writer_control p;
2014-02-17 13:40:03 -05:00
writer_control_init(&p, pic, file, WRITE_MODE);
2014-02-17 13:40:03 -05:00
traverse_shared(&p, obj);
2014-02-17 13:40:03 -05:00
write_core(&p, obj);
2014-02-17 13:45:00 -05:00
writer_control_destroy(&p);
2014-02-17 13:40:03 -05:00
}
2014-03-03 10:09:32 -05:00
static void
display(pic_state *pic, pic_value obj, xFILE *file)
{
struct writer_control p;
2014-03-03 10:09:32 -05:00
writer_control_init(&p, pic, file, DISPLAY_MODE);
2014-03-03 10:09:32 -05:00
traverse_shared(&p, obj); /* FIXME */
2014-03-03 10:09:32 -05:00
write_core(&p, obj);
2014-03-03 10:09:32 -05:00
writer_control_destroy(&p);
2014-03-03 10:09:32 -05:00
}
2014-02-10 23:38:42 -05:00
pic_value
2014-03-03 10:28:39 -05:00
pic_write(pic_state *pic, pic_value obj)
{
2014-03-03 10:28:39 -05:00
return pic_fwrite(pic, obj, xstdout);
2014-02-10 10:47:57 -05:00
}
2014-02-10 23:38:42 -05:00
pic_value
2014-03-03 10:28:39 -05:00
pic_fwrite(pic_state *pic, pic_value obj, xFILE *file)
2014-02-10 10:47:57 -05:00
{
write(pic, obj, file);
2014-02-10 10:47:57 -05:00
xfflush(file);
2014-02-10 23:38:42 -05:00
return obj;
}
2014-03-03 10:28:39 -05:00
pic_value
pic_display(pic_state *pic, pic_value obj)
{
return pic_fdisplay(pic, obj, xstdout);
}
pic_value
pic_fdisplay(pic_state *pic, pic_value obj, xFILE *file)
{
display(pic, obj, file);
xfflush(file);
return obj;
}
2014-03-03 08:43:59 -05:00
void
pic_printf(pic_state *pic, const char *fmt, ...)
{
va_list ap;
pic_str *str;
va_start(ap, fmt);
str = pic_str_ptr(pic_car(pic, pic_vformat(pic, fmt, ap)));
va_end(ap);
2014-06-25 04:24:42 -04:00
xprintf("%s", pic_str_cstr(str));
xfflush(xstdout);
2014-03-03 08:43:59 -05:00
}
static pic_value
2014-02-17 14:24:41 -05:00
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);
return pic_none_value();
}
static pic_value
2014-02-17 14:24:41 -05:00
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_simple(pic, v, port->file);
return pic_none_value();
}
2014-02-17 13:40:03 -05:00
static pic_value
2014-02-17 14:24:41 -05:00
pic_write_write_shared(pic_state *pic)
2014-02-17 13:40:03 -05:00
{
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-03-03 10:09:32 -05: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);
display(pic, v, port->file);
return pic_none_value();
}
void
pic_init_write(pic_state *pic)
{
2014-07-27 01:47:14 -04:00
pic_deflibrary (pic, "(scheme write)") {
2014-02-17 14:24:41 -05: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);
2014-03-03 10:09:32 -05:00
pic_defun(pic, "display", pic_write_display);
}
}