struct pic_port * -> pic_value

This commit is contained in:
Yuichi Nishiwaki 2016-02-20 16:51:24 +09:00
parent 215e159598
commit d851273f60
11 changed files with 103 additions and 84 deletions

View File

@ -12,7 +12,7 @@ file_error(pic_state *pic, const char *msg)
pic_error(pic, "file", msg, pic_nil_value(pic));
}
static struct pic_port *
static pic_value
open_file(pic_state *pic, const char *fname, const char *mode)
{
FILE *fp;
@ -30,7 +30,7 @@ pic_file_open_input_file(pic_state *pic)
pic_get_args(pic, "z", &fname);
return pic_obj_value(open_file(pic, fname, "r"));
return open_file(pic, fname, "r");
}
pic_value
@ -40,7 +40,7 @@ pic_file_open_output_file(pic_state *pic)
pic_get_args(pic, "z", &fname);
return pic_obj_value(open_file(pic, fname, "w"));
return open_file(pic, fname, "w");
}
pic_value

View File

@ -9,9 +9,8 @@
static pic_value
pic_load_load(pic_state *pic)
{
pic_value envid;
pic_value envid, port;
char *fn;
struct pic_port *port;
FILE *fp;
pic_get_args(pic, "z|o", &fn, &envid);

View File

@ -320,7 +320,7 @@ xf_socket_close(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie))
return 0;
}
static struct pic_port *
static pic_value
make_socket_port(pic_state *pic, struct pic_socket_t *sock, const char *mode)
{
xFILE *fp;
@ -346,7 +346,7 @@ pic_socket_socket_input_port(pic_state *pic)
sock = pic_socket_data(pic, obj);
ensure_socket_is_open(pic, sock);
return pic_obj_value(make_socket_port(pic, sock, "r"));
return make_socket_port(pic, sock, "r");
}
static pic_value
@ -361,7 +361,7 @@ pic_socket_socket_output_port(pic_state *pic)
sock = pic_socket_data(pic, obj);
ensure_socket_is_open(pic, sock);
return pic_obj_value(make_socket_port(pic, sock, "w"));
return make_socket_port(pic, sock, "w");
}
static pic_value

View File

@ -28,7 +28,7 @@ pic_warnf(pic_state *pic, const char *fmt, ...)
err = pic_vstrf_value(pic, fmt, ap);
va_end(ap);
xfprintf(pic, pic_stderr(pic)->file, "warn: %s\n", pic_str(pic, err));
pic_fprintf(pic, pic_stderr(pic), "warn: %s\n", pic_str(pic, err));
}
void

View File

@ -51,7 +51,6 @@ typedef struct {
} pic_value;
#endif
struct pic_port;
struct pic_error;
typedef void *(*pic_allocf)(void *userdata, void *ptr, size_t n);
@ -275,8 +274,8 @@ void *pic_default_allocf(void *, void *, size_t);
pic_errorf(pic, "expected " #type ", but got ~s", v); \
}
struct pic_port *pic_make_port(pic_state *, xFILE *file);
void pic_close_port(pic_state *, struct pic_port *port);
pic_value pic_make_port(pic_state *, xFILE *file);
void pic_close_port(pic_state *, pic_value port);
#define pic_void(exec) \
pic_void_(PIC_GENSYM(ai), exec)
@ -286,14 +285,14 @@ void pic_close_port(pic_state *, struct pic_port *port);
pic_leave(pic, ai); \
} while (0)
pic_value pic_read(pic_state *, struct pic_port *);
pic_value pic_read(pic_state *, pic_value port);
pic_value pic_read_cstr(pic_state *, const char *);
pic_value pic_expand(pic_state *, pic_value program, pic_value env);
pic_value pic_eval(pic_state *, pic_value program, const char *lib);
void pic_load(pic_state *, struct pic_port *);
void pic_load(pic_state *, pic_value port);
void pic_load_cstr(pic_state *, const char *);
pic_value pic_make_var(pic_state *, pic_value init, pic_value conv);
@ -347,14 +346,16 @@ void pic_warnf(pic_state *, const char *, ...);
pic_value pic_get_backtrace(pic_state *);
void pic_print_backtrace(pic_state *, xFILE *);
#define pic_stdin(pic) pic_port_ptr(pic_funcall(pic, "picrin.base", "current-input-port", 0))
#define pic_stdout(pic) pic_port_ptr(pic_funcall(pic, "picrin.base", "current-output-port", 0))
#define pic_stderr(pic) pic_port_ptr(pic_funcall(pic, "picrin.base", "current-error-port", 0))
#define pic_stdin(pic) pic_funcall(pic, "picrin.base", "current-input-port", 0)
#define pic_stdout(pic) pic_funcall(pic, "picrin.base", "current-output-port", 0)
#define pic_stderr(pic) pic_funcall(pic, "picrin.base", "current-error-port", 0)
xFILE *pic_fileno(pic_state *, pic_value port);
pic_value pic_write(pic_state *, pic_value); /* returns given obj */
pic_value pic_fwrite(pic_state *, pic_value, xFILE *);
void pic_printf(pic_state *, const char *, ...);
void pic_fprintf(pic_state *, struct pic_port *, const char *, ...);
void pic_fprintf(pic_state *, pic_value port, const char *, ...);
pic_value pic_display(pic_state *, pic_value);
pic_value pic_fdisplay(pic_state *, pic_value, xFILE *);

View File

@ -144,10 +144,10 @@ struct pic_checkpoint {
#define pic_data_ptr(pic, o) ((struct pic_data *)pic_obj_ptr(o))
#define pic_proc_ptr(pic, o) ((struct pic_proc *)pic_obj_ptr(o))
#define pic_env_ptr(pic, o) ((struct pic_env *)pic_obj_ptr(o))
#define pic_port_ptr(pic, o) ((struct pic_port *)pic_obj_ptr(o))
#define pic_context_ptr(o) ((struct pic_context *)pic_obj_ptr(o))
#define pic_rec_ptr(v) ((struct pic_record *)pic_obj_ptr(v))
#define pic_error_ptr(v) ((struct pic_error *)pic_obj_ptr(v))
#define pic_port_ptr(v) ((struct pic_port *)pic_obj_ptr(v))
#define pic_obj_p(pic,v) (pic_vtype(pic,v) == PIC_IVAL_END)
#define pic_env_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ENV)

View File

@ -5,7 +5,7 @@
#include "picrin.h"
void
pic_load(pic_state *pic, struct pic_port *port)
pic_load(pic_state *pic, pic_value port)
{
pic_value form;
size_t ai = pic_enter(pic);
@ -20,7 +20,7 @@ pic_load(pic_state *pic, struct pic_port *port)
void
pic_load_cstr(pic_state *pic, const char *str)
{
struct pic_port *port = pic_make_port(pic, xfopen_buf(pic, str, strlen(str), "r"));
pic_value port = pic_make_port(pic, xfopen_buf(pic, str, strlen(str), "r"));
pic_try {
pic_load(pic, port);

View File

@ -8,23 +8,32 @@
#undef EOF
#define EOF (-1)
struct pic_port *
pic_value
pic_make_port(pic_state *pic, xFILE *file)
{
struct pic_port *port;
port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TYPE_PORT);
port->file = file;
return port;
return pic_obj_value(port);
}
xFILE *
pic_fileno(pic_state PIC_UNUSED(*pic), pic_value port)
{
return pic_port_ptr(pic, port)->file;
}
void
pic_close_port(pic_state *pic, struct pic_port *port)
pic_close_port(pic_state *pic, pic_value port)
{
if (port->file->flag == 0) {
xFILE *file = pic_fileno(pic, port);
if (file->flag == 0) {
return;
}
if (xfclose(pic, port->file) == EOF) {
if (xfclose(pic, file) == EOF) {
pic_errorf(pic, "close-port: failure");
}
}
@ -36,7 +45,7 @@ pic_port_input_port_p(pic_state *pic)
pic_get_args(pic, "o", &v);
if (pic_port_p(pic, v) && (pic_port_ptr(v)->file->flag & X_READ) != 0) {
if (pic_port_p(pic, v) && (pic_fileno(pic, v)->flag & X_READ) != 0) {
return pic_true_value(pic);
} else {
return pic_false_value(pic);
@ -50,7 +59,7 @@ pic_port_output_port_p(pic_state *pic)
pic_get_args(pic, "o", &v);
if (pic_port_p(pic, v) && (pic_port_ptr(v)->file->flag & X_WRITE) != 0) {
if (pic_port_p(pic, v) && (pic_fileno(pic, v)->flag & X_WRITE) != 0) {
return pic_true_value(pic);
}
else {
@ -89,17 +98,17 @@ pic_port_eof_object(pic_state *pic)
static pic_value
pic_port_port_open_p(pic_state *pic)
{
struct pic_port *port;
pic_value port;
pic_get_args(pic, "p", &port);
return pic_bool_value(pic, port->file->flag != 0);
return pic_bool_value(pic, pic_fileno(pic, port)->flag != 0);
}
static pic_value
pic_port_close_port(pic_state *pic)
{
struct pic_port *port;
pic_value port;
pic_get_args(pic, "p", &port);
@ -109,7 +118,7 @@ pic_port_close_port(pic_state *pic)
}
#define assert_port_profile(port, flags, caller) do { \
if ((port->file->flag & (flags)) != (flags)) { \
if ((pic_fileno(pic, port)->flag & (flags)) != (flags)) { \
switch (flags) { \
case X_WRITE: \
pic_errorf(pic, caller ": expected output port"); \
@ -117,7 +126,7 @@ pic_port_close_port(pic_state *pic)
pic_errorf(pic, caller ": expected input port"); \
} \
} \
if (port->file->flag == 0) { \
if (pic_fileno(pic, port)->flag == 0) { \
pic_errorf(pic, caller ": expected open port"); \
} \
} while (0)
@ -133,7 +142,7 @@ pic_port_open_input_bytevector(pic_state *pic)
buf = pic_blob(pic, blob, &len);
return pic_obj_value(pic_make_port(pic, xfopen_buf(pic, (char *)buf, len, "r")));
return pic_make_port(pic, xfopen_buf(pic, (char *)buf, len, "r"));
}
static pic_value
@ -141,13 +150,13 @@ pic_port_open_output_bytevector(pic_state *pic)
{
pic_get_args(pic, "");
return pic_obj_value(pic_make_port(pic, xfopen_buf(pic, NULL, 0, "w")));
return pic_make_port(pic, xfopen_buf(pic, NULL, 0, "w"));
}
static pic_value
pic_port_get_output_bytevector(pic_state *pic)
{
struct pic_port *port = pic_stdout(pic);
pic_value port = pic_stdout(pic);
const char *buf;
int len;
@ -155,7 +164,7 @@ pic_port_get_output_bytevector(pic_state *pic)
assert_port_profile(port, X_WRITE, "get-output-bytevector");
if (xfget_buf(pic, port->file, &buf, &len) < 0) {
if (xfget_buf(pic, pic_fileno(pic, port), &buf, &len) < 0) {
pic_errorf(pic, "port was not created by open-output-bytevector");
}
return pic_blob_value(pic, (unsigned char *)buf, len);
@ -163,12 +172,12 @@ pic_port_get_output_bytevector(pic_state *pic)
static pic_value
pic_port_read_u8(pic_state *pic){
struct pic_port *port = pic_stdin(pic);
pic_value port = pic_stdin(pic);
int c;
pic_get_args(pic, "|p", &port);
assert_port_profile(port, X_READ, "read-u8");
if ((c = xfgetc(pic, port->file)) == EOF) {
if ((c = xfgetc(pic, pic_fileno(pic, port))) == EOF) {
return pic_eof_object(pic);
}
@ -179,18 +188,18 @@ static pic_value
pic_port_peek_u8(pic_state *pic)
{
int c;
struct pic_port *port = pic_stdin(pic);
pic_value port = pic_stdin(pic);
pic_get_args(pic, "|p", &port);
assert_port_profile(port, X_READ, "peek-u8");
c = xfgetc(pic, port->file);
c = xfgetc(pic, pic_fileno(pic, port));
if (c == EOF) {
return pic_eof_object(pic);
}
else {
xungetc(c, port->file);
xungetc(c, pic_fileno(pic, port));
return pic_int_value(pic, c);
}
}
@ -198,7 +207,7 @@ pic_port_peek_u8(pic_state *pic)
static pic_value
pic_port_u8_ready_p(pic_state *pic)
{
struct pic_port *port = pic_stdin(pic);
pic_value port = pic_stdin(pic);
pic_get_args(pic, "|p", &port);
@ -211,7 +220,7 @@ pic_port_u8_ready_p(pic_state *pic)
static pic_value
pic_port_read_bytevector(pic_state *pic)
{
struct pic_port *port = pic_stdin(pic);
pic_value port = pic_stdin(pic);
unsigned char *buf;
int k, i;
@ -221,7 +230,7 @@ pic_port_read_bytevector(pic_state *pic)
buf = pic_blob(pic, pic_blob_value(pic, NULL, k), NULL);
i = xfread(pic, buf, sizeof(char), k, port->file);
i = xfread(pic, buf, sizeof(char), k, pic_fileno(pic, port));
if (i == 0) {
return pic_eof_object(pic);
}
@ -231,8 +240,7 @@ pic_port_read_bytevector(pic_state *pic)
static pic_value
pic_port_read_bytevector_ip(pic_state *pic)
{
struct pic_port *port;
pic_value bv;
pic_value bv, port;
unsigned char *buf;
int n, start, end, i, len;
@ -252,7 +260,7 @@ pic_port_read_bytevector_ip(pic_state *pic)
VALID_RANGE(pic, len, start, end);
assert_port_profile(port, X_READ, "read-bytevector!");
i = xfread(pic, buf + start, 1, end - start, port->file);
i = xfread(pic, buf + start, 1, end - start, pic_fileno(pic, port));
if (i == 0) {
return pic_eof_object(pic);
}
@ -263,13 +271,13 @@ static pic_value
pic_port_write_u8(pic_state *pic)
{
int i;
struct pic_port *port = pic_stdout(pic);
pic_value port = pic_stdout(pic);
pic_get_args(pic, "i|p", &i, &port);
assert_port_profile(port, X_WRITE, "write-u8");
xfputc(pic, i, port->file);
xfputc(pic, i, pic_fileno(pic, port));
return pic_undef_value(pic);
}
@ -277,7 +285,7 @@ static pic_value
pic_port_write_bytevector(pic_state *pic)
{
pic_value blob;
struct pic_port *port;
pic_value port;
unsigned char *buf;
int n, start, end, len, done;
@ -299,7 +307,7 @@ pic_port_write_bytevector(pic_state *pic)
done = 0;
while (done < end - start) {
done += xfwrite(pic, buf + start + done, 1, end - start - done, port->file);
done += xfwrite(pic, buf + start + done, 1, end - start - done, pic_fileno(pic, port));
/* FIXME: error check... */
}
return pic_undef_value(pic);
@ -308,28 +316,28 @@ pic_port_write_bytevector(pic_state *pic)
static pic_value
pic_port_flush(pic_state *pic)
{
struct pic_port *port = pic_stdout(pic);
pic_value port = pic_stdout(pic);
pic_get_args(pic, "|p", &port);
assert_port_profile(port, X_WRITE, "flush-output-port");
xfflush(pic, port->file);
xfflush(pic, pic_fileno(pic, port));
return pic_undef_value(pic);
}
static pic_value
coerce_port(pic_state *pic)
{
struct pic_port *port;
pic_value port;
pic_get_args(pic, "p", &port);
return pic_obj_value(port);
return port;
}
#define DEFINE_PORT(pic, name, file) \
pic_defvar(pic, name, pic_obj_value(pic_make_port(pic, file)), coerce)
pic_defvar(pic, name, pic_make_port(pic, file), coerce)
void
pic_init_port(pic_state *pic)

View File

@ -25,7 +25,7 @@
* s pic_value * string object
* b pic_value * bytevector object
* l pic_value * lambda object
* p struct pic_port ** port object
* p pic_value * port object
* d pic_value * dictionary object
* e struct pic_error ** error object
* r struct pic_record ** record object
@ -147,7 +147,6 @@ pic_get_args(pic_state *pic, const char *format, ...)
#define PTR_CASE(c, type, ctype) \
VAL_CASE(c, type, ctype, pic_## type ##_ptr(v))
PTR_CASE('p', port, struct pic_port *)
PTR_CASE('e', error, struct pic_error *)
PTR_CASE('r', rec, struct pic_record *)
@ -159,6 +158,7 @@ pic_get_args(pic_state *pic, const char *format, ...)
OBJ_CASE('b', blob)
OBJ_CASE('v', vec)
OBJ_CASE('d', dict)
OBJ_CASE('p', port)
default:
pic_errorf(pic, "pic_get_args: invalid argument specifier '%c' given", c);

View File

@ -818,11 +818,11 @@ pic_reader_destroy(pic_state *pic)
}
pic_value
pic_read(pic_state *pic, struct pic_port *port)
pic_read(pic_state *pic, pic_value port)
{
size_t ai = pic_enter(pic);
pic_value val;
xFILE *file = port->file;
xFILE *file = pic_fileno(pic, port);
int c;
while ((c = skip(pic, file, next(pic, file))) != EOF) {
@ -844,7 +844,7 @@ pic_read(pic_state *pic, struct pic_port *port)
pic_value
pic_read_cstr(pic_state *pic, const char *str)
{
struct pic_port *port = pic_make_port(pic, xfopen_buf(pic, str, strlen(str), "r"));
pic_value port = pic_make_port(pic, xfopen_buf(pic, str, strlen(str), "r"));
pic_value form;
pic_try {
@ -863,7 +863,7 @@ pic_read_cstr(pic_state *pic, const char *str)
static pic_value
pic_read_read(pic_state *pic)
{
struct pic_port *port = pic_stdin(pic);
pic_value port = pic_stdin(pic);
pic_get_args(pic, "|p", &port);

View File

@ -411,7 +411,7 @@ write(pic_state *pic, pic_value obj, xFILE *file, int mode, int op)
pic_value
pic_write(pic_state *pic, pic_value obj)
{
return pic_fwrite(pic, obj, pic_stdout(pic)->file);
return pic_fwrite(pic, obj, pic_fileno(pic, pic_stdout(pic)));
}
pic_value
@ -425,7 +425,7 @@ pic_fwrite(pic_state *pic, pic_value obj, xFILE *file)
pic_value
pic_display(pic_state *pic, pic_value obj)
{
return pic_fdisplay(pic, obj, pic_stdout(pic)->file);
return pic_fdisplay(pic, obj, pic_fileno(pic, pic_stdout(pic)));
}
pic_value
@ -437,63 +437,74 @@ pic_fdisplay(pic_state *pic, pic_value obj, xFILE *file)
}
void
pic_printf(pic_state *pic, const char *fmt, ...)
pic_vfprintf(pic_state *pic, pic_value port, const char *fmt, va_list ap)
{
xFILE *file = pic_stdout(pic)->file;
va_list ap;
xFILE *file = pic_fileno(pic, port);
pic_value str;
va_start(ap, fmt);
str = pic_vstrf_value(pic, fmt, ap);
va_end(ap);
xfprintf(pic, file, "%s", pic_str(pic, str));
xfflush(pic, file);
}
void
pic_fprintf(pic_state *pic, pic_value port, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
pic_vfprintf(pic, port, fmt, ap);
va_end(ap);
}
void
pic_printf(pic_state *pic, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
pic_vfprintf(pic, pic_stdout(pic), fmt, ap);
va_end(ap);
}
static pic_value
pic_write_write(pic_state *pic)
{
pic_value v;
struct pic_port *port = pic_stdout(pic);
pic_value v, port = pic_stdout(pic);
pic_get_args(pic, "o|p", &v, &port);
write(pic, v, port->file, WRITE_MODE, OP_WRITE);
write(pic, v, pic_fileno(pic, port), WRITE_MODE, OP_WRITE);
return pic_undef_value(pic);
}
static pic_value
pic_write_write_simple(pic_state *pic)
{
pic_value v;
struct pic_port *port = pic_stdout(pic);
pic_value v, port = pic_stdout(pic);
pic_get_args(pic, "o|p", &v, &port);
write(pic, v, port->file, WRITE_MODE, OP_WRITE_SIMPLE);
write(pic, v, pic_fileno(pic, port), WRITE_MODE, OP_WRITE_SIMPLE);
return pic_undef_value(pic);
}
static pic_value
pic_write_write_shared(pic_state *pic)
{
pic_value v;
struct pic_port *port = pic_stdout(pic);
pic_value v, port = pic_stdout(pic);
pic_get_args(pic, "o|p", &v, &port);
write(pic, v, port->file, WRITE_MODE, OP_WRITE_SHARED);
write(pic, v, pic_fileno(pic, port), WRITE_MODE, OP_WRITE_SHARED);
return pic_undef_value(pic);
}
static pic_value
pic_write_display(pic_state *pic)
{
pic_value v;
struct pic_port *port = pic_stdout(pic);
pic_value v, port = pic_stdout(pic);
pic_get_args(pic, "o|p", &v, &port);
write(pic, v, port->file, DISPLAY_MODE, OP_WRITE);
write(pic, v, pic_fileno(pic, port), DISPLAY_MODE, OP_WRITE);
return pic_undef_value(pic);
}