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>
|
|
|
|
|
2014-01-16 07:31:46 -05:00
|
|
|
#include "picrin.h"
|
|
|
|
#include "picrin/port.h"
|
|
|
|
#include "picrin/pair.h"
|
2014-02-22 01:20:53 -05:00
|
|
|
#include "picrin/string.h"
|
|
|
|
#include "picrin/vector.h"
|
2014-01-16 07:31:46 -05:00
|
|
|
#include "picrin/blob.h"
|
|
|
|
#include "picrin/macro.h"
|
|
|
|
|
2014-02-12 09:07:12 -05:00
|
|
|
static bool
|
2014-02-17 13:42:33 -05:00
|
|
|
is_tagged(pic_state *pic, pic_sym tag, pic_value pair)
|
2014-02-12 09:07:12 -05:00
|
|
|
{
|
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);
|
2014-02-12 09:07:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-02-12 09:07:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-02-12 09:07:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-02-12 09:07:12 -05:00
|
|
|
}
|
|
|
|
|
2014-02-17 13:39:32 -05:00
|
|
|
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 */
|
2014-02-17 13:39:32 -05:00
|
|
|
int cnt;
|
|
|
|
};
|
|
|
|
|
2014-03-03 10:09:32 -05:00
|
|
|
#define WRITE_MODE 1
|
|
|
|
#define DISPLAY_MODE 2
|
|
|
|
|
2014-07-26 23:36:02 -04:00
|
|
|
static void
|
|
|
|
writer_control_init(struct writer_control *p, pic_state *pic, xFILE *file, int mode)
|
2014-02-17 13:39:32 -05:00
|
|
|
{
|
|
|
|
p->pic = pic;
|
|
|
|
p->file = file;
|
2014-03-03 10:09:32 -05:00
|
|
|
p->mode = mode;
|
2014-02-17 13:39:32 -05:00
|
|
|
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:39:32 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-17 13:39:32 -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;
|
2014-02-17 13:39:32 -05:00
|
|
|
|
|
|
|
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));
|
2014-02-17 13:39:32 -05:00
|
|
|
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-02-17 13:39:32 -05:00
|
|
|
}
|
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);
|
2014-02-17 13:39:32 -05:00
|
|
|
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;
|
2014-03-25 02:29:26 -04:00
|
|
|
int c;
|
2014-02-17 13:39:32 -05:00
|
|
|
|
|
|
|
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 */
|
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) {
|
2014-02-17 13:39:32 -05:00
|
|
|
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));
|
2014-02-17 13:39:32 -05:00
|
|
|
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);
|
2014-02-17 13:39:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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-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
|
|
|
{
|
2014-01-30 04:14:33 -05:00
|
|
|
size_t i;
|
2014-02-27 07:54:37 -05:00
|
|
|
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);
|
|
|
|
|
2014-02-27 07:54:37 -05:00
|
|
|
for (i = 0; i < pic_strlen(str); ++i) {
|
2014-01-16 07:32:22 -05:00
|
|
|
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;
|
2014-02-25 07:07:32 -05:00
|
|
|
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;
|
2014-03-25 02:29:26 -04:00
|
|
|
int c;
|
2014-07-26 05:03:51 -04:00
|
|
|
float f;
|
2014-02-17 13:39:32 -05:00
|
|
|
|
|
|
|
/* 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));
|
2014-02-17 13:39:32 -05:00
|
|
|
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);
|
2014-02-17 13:39:32 -05:00
|
|
|
}
|
|
|
|
}
|
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:
|
2014-03-03 10:09:32 -05:00
|
|
|
if (p->mode == DISPLAY_MODE) {
|
|
|
|
xfputc(pic_char(obj), file);
|
|
|
|
break;
|
|
|
|
}
|
2014-01-16 07:31:46 -05:00
|
|
|
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-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));
|
|
|
|
}
|
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_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;
|
|
|
|
}
|
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;
|
2014-07-26 22:10:59 -04:00
|
|
|
default:
|
|
|
|
xfprintf(file, "#<%s %p>", pic_type_repr(pic_type(obj)), pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 14:22:58 -05:00
|
|
|
static void
|
2014-02-25 07:07:32 -05:00
|
|
|
write(pic_state *pic, pic_value obj, xFILE *file)
|
2014-02-17 14:22:58 -05:00
|
|
|
{
|
2014-07-26 23:36:02 -04:00
|
|
|
struct writer_control p;
|
2014-02-17 14:22:58 -05:00
|
|
|
|
2014-07-26 23:36:02 -04:00
|
|
|
writer_control_init(&p, pic, file, WRITE_MODE);
|
2014-02-17 14:22:58 -05:00
|
|
|
|
2014-07-26 23:36:02 -04:00
|
|
|
traverse_shared(&p, obj); /* FIXME */
|
2014-02-17 14:22:58 -05:00
|
|
|
|
2014-07-26 23:36:02 -04:00
|
|
|
write_core(&p, obj);
|
2014-02-17 14:22:58 -05:00
|
|
|
|
2014-07-26 23:36:02 -04:00
|
|
|
writer_control_destroy(&p);
|
2014-02-17 14:22:58 -05:00
|
|
|
}
|
|
|
|
|
2014-02-17 13:39:32 -05:00
|
|
|
static void
|
2014-02-25 07:07:32 -05:00
|
|
|
write_simple(pic_state *pic, pic_value obj, xFILE *file)
|
2014-02-17 13:39:32 -05:00
|
|
|
{
|
2014-07-26 23:36:02 -04:00
|
|
|
struct writer_control p;
|
2014-02-17 13:39:32 -05:00
|
|
|
|
2014-07-26 23:36:02 -04:00
|
|
|
writer_control_init(&p, pic, file, WRITE_MODE);
|
2014-02-17 13:39:32 -05:00
|
|
|
|
|
|
|
/* no traverse here! */
|
|
|
|
|
2014-07-26 23:36:02 -04:00
|
|
|
write_core(&p, obj);
|
2014-02-17 13:45:00 -05:00
|
|
|
|
2014-07-26 23:36:02 -04:00
|
|
|
writer_control_destroy(&p);
|
2014-02-17 13:39:32 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-07-26 23:36:02 -04:00
|
|
|
struct writer_control p;
|
2014-02-17 13:40:03 -05:00
|
|
|
|
2014-07-26 23:36:02 -04:00
|
|
|
writer_control_init(&p, pic, file, WRITE_MODE);
|
2014-02-17 13:40:03 -05:00
|
|
|
|
2014-07-26 23:36:02 -04:00
|
|
|
traverse_shared(&p, obj);
|
2014-02-17 13:40:03 -05:00
|
|
|
|
2014-07-26 23:36:02 -04:00
|
|
|
write_core(&p, obj);
|
2014-02-17 13:45:00 -05:00
|
|
|
|
2014-07-26 23:36:02 -04: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)
|
|
|
|
{
|
2014-07-26 23:36:02 -04:00
|
|
|
struct writer_control p;
|
2014-03-03 10:09:32 -05:00
|
|
|
|
2014-07-26 23:36:02 -04:00
|
|
|
writer_control_init(&p, pic, file, DISPLAY_MODE);
|
2014-03-03 10:09:32 -05:00
|
|
|
|
2014-07-26 23:36:02 -04:00
|
|
|
traverse_shared(&p, obj); /* FIXME */
|
2014-03-03 10:09:32 -05:00
|
|
|
|
2014-07-26 23:36:02 -04:00
|
|
|
write_core(&p, obj);
|
2014-03-03 10:09:32 -05:00
|
|
|
|
2014-07-26 23:36:02 -04: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-01-16 07:31:46 -05:00
|
|
|
{
|
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
|
|
|
{
|
2014-02-17 14:22:58 -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-01-16 07:31:46 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-17 14:22:58 -05:00
|
|
|
static pic_value
|
2014-02-17 14:24:41 -05:00
|
|
|
pic_write_write(pic_state *pic)
|
2014-02-17 14:22:58 -05:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2014-01-16 07:31:46 -05:00
|
|
|
static pic_value
|
2014-02-17 14:24:41 -05:00
|
|
|
pic_write_write_simple(pic_state *pic)
|
2014-01-16 07:31:46 -05:00
|
|
|
{
|
|
|
|
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
|
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();
|
|
|
|
}
|
|
|
|
|
2014-01-16 07:31:46 -05:00
|
|
|
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);
|
2014-01-16 07:31:46 -05:00
|
|
|
}
|
|
|
|
}
|