picrin/src/port.c

290 lines
5.5 KiB
C
Raw Normal View History

2013-10-10 04:48:01 -04:00
#include <stdio.h>
#include <stdlib.h>
#include "picrin.h"
2013-10-15 00:21:40 -04:00
#include "picrin/proc.h"
2013-10-17 09:42:47 -04:00
#include "picrin/port.h"
2013-11-04 22:38:23 -05:00
#include "picrin/blob.h"
2013-10-10 04:48:01 -04:00
2013-10-17 09:42:20 -04:00
static void write_pair(pic_state *pic, struct pic_pair *pair);
static void write_str(pic_state *pic, struct pic_string *str);
2013-10-17 09:42:20 -04:00
2013-10-17 04:57:27 -04:00
static void
write(pic_state *pic, pic_value obj)
2013-10-10 04:48:01 -04:00
{
2013-10-29 02:51:37 -04:00
int i;
2013-10-10 04:48:01 -04:00
switch (pic_type(obj)) {
case PIC_TT_NIL:
printf("()");
break;
2013-10-16 00:17:01 -04:00
case PIC_TT_BOOL:
if (pic_true_p(obj))
printf("#t");
else
printf("#f");
break;
2013-10-10 04:48:01 -04:00
case PIC_TT_PAIR:
printf("(");
2013-10-17 09:42:20 -04:00
write_pair(pic, pic_pair_ptr(obj));
2013-10-10 04:48:01 -04:00
printf(")");
break;
case PIC_TT_SYMBOL:
2013-10-28 13:11:31 -04:00
printf("%s", pic_symbol_name(pic, pic_sym(obj)));
2013-10-10 04:48:01 -04:00
break;
2013-11-04 21:37:18 -05:00
case PIC_TT_CHAR:
printf("#\\%c", pic_char(obj));
break;
case PIC_TT_FLOAT:
2013-10-27 11:21:24 -04:00
printf("%f", pic_float(obj));
break;
case PIC_TT_INT:
printf("%d", pic_int(obj));
2013-10-11 11:15:46 -04:00
break;
2013-10-22 03:02:20 -04:00
case PIC_TT_EOF:
printf("#<eof-object>");
break;
2013-10-12 01:40:01 -04:00
case PIC_TT_UNDEF:
printf("#<undef>");
break;
case PIC_TT_PROC:
printf("#<proc %p>", pic_proc_ptr(obj));
break;
2013-10-17 09:42:47 -04:00
case PIC_TT_PORT:
printf("#<port %p>", pic_port_ptr(obj));
break;
2013-10-20 19:48:55 -04:00
case PIC_TT_STRING:
printf("\"");
write_str(pic, pic_str_ptr(obj));
printf("\"");
2013-10-20 19:48:55 -04:00
break;
2013-10-29 02:51:37 -04:00
case PIC_TT_VECTOR:
printf("#(");
for (i = 0; i < pic_vec_ptr(obj)->len; ++i) {
write(pic, pic_vec_ptr(obj)->data[i]);
if (i + 1 < pic_vec_ptr(obj)->len) {
printf(" ");
}
}
printf(")");
break;
2013-11-04 22:38:23 -05:00
case PIC_TT_BLOB:
printf("#u8(");
for (i = 0; i < pic_blob_ptr(obj)->len; ++i) {
printf("%d", pic_blob_ptr(obj)->data[i]);
if (i + 1 < pic_blob_ptr(obj)->len) {
printf(" ");
}
}
printf(")");
break;
2013-10-23 13:02:07 -04:00
case PIC_TT_ENV:
printf("#<env %p>", pic_env_ptr(obj));
break;
2013-11-09 00:14:25 -05:00
case PIC_TT_CONT:
printf("#<cont %p>", pic_ptr(obj));
break;
2013-10-10 04:48:01 -04:00
}
}
2013-10-15 08:14:33 -04:00
2013-10-17 09:42:20 -04:00
static void
write_pair(pic_state *pic, struct pic_pair *pair)
{
write(pic, pair->car);
if (pic_nil_p(pair->cdr)) {
return;
}
if (pic_pair_p(pair->cdr)) {
printf(" ");
write_pair(pic, pic_pair_ptr(pair->cdr));
return;
}
printf(" . ");
write(pic, pair->cdr);
}
static void
write_str(pic_state *pic, struct pic_string *str)
{
int i;
const char *cstr = str->str;
for (i = 0; i < str->len; ++i) {
if (cstr[i] == '"' || cstr[i] == '\\') {
putchar('\\');
}
putchar(cstr[i]);
}
}
2013-10-17 04:57:27 -04:00
void
pic_debug(pic_state *pic, pic_value obj)
{
write(pic, obj);
}
2013-10-24 09:56:04 -04:00
static pic_value
pic_port_input_port_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
if (pic_port_p(v) && (pic_port_ptr(v)->flags & PIC_PORT_IN) != 0) {
return pic_true_value();
}
else {
return pic_false_value();
}
}
static pic_value
pic_port_output_port_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
if (pic_port_p(v) && (pic_port_ptr(v)->flags & PIC_PORT_OUT) != 0) {
return pic_true_value();
}
else {
return pic_false_value();
}
}
static pic_value
pic_port_textual_port_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
if (pic_port_p(v) && (pic_port_ptr(v)->flags & PIC_PORT_TEXT) != 0) {
return pic_true_value();
}
else {
return pic_false_value();
}
}
static pic_value
pic_port_binary_port_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
if (pic_port_p(v) && (pic_port_ptr(v)->flags & PIC_PORT_BINARY) != 0) {
return pic_true_value();
}
else {
return pic_false_value();
}
}
static pic_value
pic_port_port_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
return pic_bool_value(pic_port_p(v));
}
static pic_value
pic_port_input_port_open_p(pic_state *pic)
{
pic_value v;
struct pic_port *port;
pic_get_args(pic, "o", &v);
if (! pic_port_p(v))
return pic_false_value();
port = pic_port_ptr(v);
if ((port->flags & PIC_PORT_IN) == 0)
return pic_false_value();
return pic_bool_value(port->status == PIC_PORT_OPEN);
}
static pic_value
pic_port_output_port_open_p(pic_state *pic)
{
pic_value v;
struct pic_port *port;
pic_get_args(pic, "o", &v);
if (! pic_port_p(v))
return pic_false_value();
port = pic_port_ptr(v);
if ((port->flags & PIC_PORT_OUT) == 0)
return pic_false_value();
return pic_bool_value(port->status == PIC_PORT_OPEN);
}
2013-10-15 08:14:33 -04:00
static pic_value
pic_port_write(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
2013-10-17 04:57:27 -04:00
write(pic, v);
return pic_false_value();
2013-10-15 08:14:33 -04:00
}
static pic_value
pic_port_newline(pic_state *pic)
{
puts("");
return pic_false_value();
2013-10-15 08:14:33 -04:00
}
2013-10-22 03:02:20 -04:00
static pic_value
pic_port_eof_object_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
if (pic_vtype(v) == PIC_VTYPE_EOF) {
2013-10-22 03:02:20 -04:00
return pic_true_value();
}
else {
return pic_false_value();
}
}
static pic_value
pic_port_eof_object(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "");
2013-11-05 00:42:44 -05:00
pic_init_value(v, PIC_VTYPE_EOF);
2013-10-22 03:02:20 -04:00
return v;
}
2013-10-15 08:14:33 -04:00
void
pic_init_port(pic_state *pic)
{
2013-10-24 09:56:04 -04:00
pic_defun(pic, "input-port?", pic_port_input_port_p);
pic_defun(pic, "output-port?", pic_port_output_port_p);
pic_defun(pic, "textual-port?", pic_port_textual_port_p);
pic_defun(pic, "binary-port?", pic_port_binary_port_p);
pic_defun(pic, "port?", pic_port_port_p);
pic_defun(pic, "input-port-open?", pic_port_input_port_open_p);
pic_defun(pic, "output-port-open?", pic_port_output_port_open_p);
2013-10-15 08:14:33 -04:00
pic_defun(pic, "write", pic_port_write);
pic_defun(pic, "newline", pic_port_newline);
2013-10-22 03:02:20 -04:00
pic_defun(pic, "eof-object?", pic_port_eof_object_p);
pic_defun(pic, "eof-object", pic_port_eof_object);
2013-10-15 08:14:33 -04:00
}