2014-01-17 06:58:31 -05:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
2014-01-16 07:31:46 -05:00
|
|
|
#include "picrin.h"
|
|
|
|
#include "picrin/port.h"
|
|
|
|
#include "picrin/pair.h"
|
|
|
|
#include "picrin/blob.h"
|
|
|
|
#include "picrin/macro.h"
|
|
|
|
|
2014-02-08 03:15:34 -05:00
|
|
|
static void write(pic_state *, pic_value, XFILE *file);
|
2014-01-16 07:32:22 -05:00
|
|
|
|
|
|
|
static void
|
2014-02-08 03:15:34 -05:00
|
|
|
write_pair(pic_state *pic, struct pic_pair *pair, XFILE *file)
|
2014-01-16 07:32:22 -05:00
|
|
|
{
|
2014-02-08 03:15:34 -05:00
|
|
|
write(pic, pair->car, file);
|
2014-01-16 07:32:22 -05:00
|
|
|
|
|
|
|
if (pic_nil_p(pair->cdr)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (pic_pair_p(pair->cdr)) {
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, " ");
|
|
|
|
write_pair(pic, pic_pair_ptr(pair->cdr), file);
|
2014-01-16 07:32:22 -05:00
|
|
|
return;
|
|
|
|
}
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, " . ");
|
|
|
|
write(pic, pair->cdr, file);
|
2014-01-16 07:32:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-02-08 03:15:34 -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-01-16 07:32:22 -05:00
|
|
|
const char *cstr = str->str;
|
|
|
|
|
2014-01-30 04:15:59 -05:00
|
|
|
UNUSED(pic);
|
|
|
|
|
2014-01-16 07:32:22 -05:00
|
|
|
for (i = 0; i < str->len; ++i) {
|
|
|
|
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-08 03:15:34 -05:00
|
|
|
write(pic_state *pic, pic_value obj, XFILE *file)
|
2014-01-16 07:31:46 -05:00
|
|
|
{
|
2014-01-30 04:14:33 -05:00
|
|
|
size_t i;
|
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-08 03:15:34 -05:00
|
|
|
xfprintf(file, "(");
|
|
|
|
write_pair(pic, pic_pair_ptr(obj), file);
|
|
|
|
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:
|
|
|
|
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-02-08 03:15:34 -05:00
|
|
|
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_PROC:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<proc %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_PORT:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<port %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_STRING:
|
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-08 03:15:34 -05:00
|
|
|
write(pic, pic_vec_ptr(obj)->data[i], file);
|
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;
|
|
|
|
case PIC_TT_ERROR:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<error %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_ENV:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<env %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_CONT:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<cont %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_SENV:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<senv %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_SYNTAX:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<syntax %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_SC:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<sc %p: ", pic_ptr(obj));
|
|
|
|
write(pic, pic_sc(obj)->expr, file);
|
|
|
|
xfprintf(file, ">");
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_LIB:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<lib %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
|
|
|
case PIC_TT_VAR:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<var %p>", pic_ptr(obj));
|
2014-01-16 07:31:46 -05:00
|
|
|
break;
|
2014-01-18 02:51:54 -05:00
|
|
|
case PIC_TT_IREP:
|
2014-02-08 03:15:34 -05:00
|
|
|
xfprintf(file, "#<irep %p>", pic_ptr(obj));
|
2014-01-18 02:51:54 -05:00
|
|
|
break;
|
2014-01-16 07:31:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_debug(pic_state *pic, pic_value obj)
|
|
|
|
{
|
2014-02-08 03:15:34 -05:00
|
|
|
write(pic, obj, xstdout);
|
|
|
|
xfflush(xstdout);
|
2014-01-16 07:31:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_port_write_simple(pic_state *pic)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
write(pic, v, port->file);
|
2014-01-16 07:31:46 -05:00
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_init_write(pic_state *pic)
|
|
|
|
{
|
2014-02-01 01:41:30 -05:00
|
|
|
pic_deflibrary ("(scheme write)") {
|
2014-01-16 07:31:46 -05:00
|
|
|
pic_defun(pic, "write-simple", pic_port_write_simple);
|
|
|
|
}
|
|
|
|
}
|