From 2e59b6ab0423dfa0c65fab85a31bd7a1c6996c69 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Jun 2015 01:11:04 +0900 Subject: [PATCH 1/9] rename xfile.[ch] to file.[ch] --- extlib/benz/{xfile.c => file.c} | 0 extlib/benz/include/picrin.h | 2 +- extlib/benz/include/picrin/{xfile.h => file.h} | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename extlib/benz/{xfile.c => file.c} (100%) rename extlib/benz/include/picrin/{xfile.h => file.h} (100%) diff --git a/extlib/benz/xfile.c b/extlib/benz/file.c similarity index 100% rename from extlib/benz/xfile.c rename to extlib/benz/file.c diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index d0dedfa5..6bda3be1 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -42,7 +42,7 @@ extern "C" { #include "picrin/xvect.h" #include "picrin/xhash.h" -#include "picrin/xfile.h" +#include "picrin/file.h" #include "picrin/value.h" diff --git a/extlib/benz/include/picrin/xfile.h b/extlib/benz/include/picrin/file.h similarity index 100% rename from extlib/benz/include/picrin/xfile.h rename to extlib/benz/include/picrin/file.h From 3df7d1dd710f1626d5693bc5b9faac9d26d704dc Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Jun 2015 02:05:56 +0900 Subject: [PATCH 2/9] use pic_malloc and pic_free in file.c --- extlib/benz/debug.c | 10 +- extlib/benz/error.c | 2 +- extlib/benz/file.c | 114 ++++++++++----------- extlib/benz/include/picrin.h | 5 +- extlib/benz/include/picrin/file.h | 58 +++++------ extlib/benz/number.c | 2 +- extlib/benz/port.c | 38 +++---- extlib/benz/read.c | 164 +++++++++++++++--------------- extlib/benz/state.c | 2 +- extlib/benz/string.c | 22 ++-- extlib/benz/write.c | 109 ++++++++++---------- 11 files changed, 261 insertions(+), 265 deletions(-) diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index d61e9380..55b652c2 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -38,7 +38,7 @@ pic_print_backtrace(pic_state *pic, xFILE *file) assert(! pic_invalid_p(pic->err)); if (! pic_error_p(pic->err)) { - xfprintf(file, "raise: "); + xfprintf(pic, file, "raise: "); pic_fwrite(pic, pic->err, file); } else { struct pic_error *e; @@ -46,14 +46,14 @@ pic_print_backtrace(pic_state *pic, xFILE *file) e = pic_error_ptr(pic->err); if (e->type != pic_intern_cstr(pic, "")) { pic_fwrite(pic, pic_obj_value(e->type), file); - xfprintf(file, " "); + xfprintf(pic, file, " "); } - xfprintf(file, "error: "); + xfprintf(pic, file, "error: "); pic_fwrite(pic, pic_obj_value(e->msg), file); - xfprintf(file, "\n"); + xfprintf(pic, file, "\n"); /* TODO: print error irritants */ - xfputs(pic_str_cstr(pic, e->stack), file); + xfputs(pic, pic_str_cstr(pic, e->stack), file); } } diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 6fa5309d..1386ab63 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -27,7 +27,7 @@ pic_warnf(pic_state *pic, const char *fmt, ...) err_line = pic_xvformat(pic, fmt, ap); va_end(ap); - xfprintf(pic_stderr(pic)->file, "warn: %s\n", pic_str_cstr(pic, pic_str_ptr(pic_car(pic, err_line)))); + xfprintf(pic, pic_stderr(pic)->file, "warn: %s\n", pic_str_cstr(pic, pic_str_ptr(pic_car(pic, err_line)))); } void diff --git a/extlib/benz/file.c b/extlib/benz/file.c index b28bf060..7a4908f3 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -93,18 +93,15 @@ xFILE *xfunopen(void *cookie, int (*read)(void *, char *, int), int (*write)(voi return fp; } -int xfclose(xFILE *fp) { - extern void free(void *); /* FIXME */ - - xfflush(fp); +int xfclose(pic_state *pic, xFILE *fp) { + xfflush(pic, fp); fp->flag = 0; if (fp->base != fp->buf) - free(fp->base); + pic_free(pic, fp->base); return fp->vtable.close(fp->vtable.cookie); } -int x_fillbuf(xFILE *fp) { - extern void *malloc(size_t); /* FIXME */ +int x_fillbuf(pic_state *pic, xFILE *fp) { int bufsize; if ((fp->flag & (X_READ|X_EOF|X_ERR)) != X_READ) @@ -112,7 +109,7 @@ int x_fillbuf(xFILE *fp) { if (fp->base == NULL) { if ((fp->flag & X_UNBUF) == 0) { /* no buffer yet */ - if ((fp->base = malloc(XBUFSIZ)) == NULL) { + if ((fp->base = pic_malloc(pic, XBUFSIZ)) == NULL) { /* can't get buffer, try unbuffered */ fp->flag |= X_UNBUF; } @@ -138,8 +135,7 @@ int x_fillbuf(xFILE *fp) { return (unsigned char) *fp->ptr++; } -int x_flushbuf(int x, xFILE *fp) { - extern void *malloc(size_t); /* FIXME */ +int x_flushbuf(pic_state *pic, int x, xFILE *fp) { int num_written=0, bufsize=0; char c = x; @@ -147,7 +143,7 @@ int x_flushbuf(int x, xFILE *fp) { return EOF; if (fp->base == NULL && ((fp->flag & X_UNBUF) == 0)) { /* no buffer yet */ - if ((fp->base = malloc(XBUFSIZ)) == NULL) { + if ((fp->base = pic_malloc(pic, XBUFSIZ)) == NULL) { /* couldn't allocate a buffer, so try unbuffered */ fp->flag |= X_UNBUF; } else { @@ -190,7 +186,7 @@ int x_flushbuf(int x, xFILE *fp) { } } -int xfflush(xFILE *f) { +int xfflush(pic_state *pic, xFILE *f) { int retval; int i; @@ -198,48 +194,48 @@ int xfflush(xFILE *f) { if (f == NULL) { /* flush all output streams */ for (i = 0; i < XOPEN_MAX; i++) { - if ((x_iob[i].flag & X_WRITE) && (xfflush(&x_iob[i]) == -1)) + if ((x_iob[i].flag & X_WRITE) && (xfflush(pic, &x_iob[i]) == -1)) retval = -1; } } else { if ((f->flag & X_WRITE) == 0) return -1; - x_flushbuf(EOF, f); + x_flushbuf(pic, EOF, f); if (f->flag & X_ERR) retval = -1; } return retval; } -int xfputc(int x, xFILE *fp) { - return xputc(x, fp); +int xfputc(pic_state *pic, int x, xFILE *fp) { + return xputc(pic, x, fp); } -int xfgetc(xFILE *fp) { - return xgetc(fp); +int xfgetc(pic_state *pic, xFILE *fp) { + return xgetc(pic, fp); } -int xfputs(const char *s, xFILE *stream) { +int xfputs(pic_state *pic, const char *s, xFILE *stream) { const char *ptr = s; while(*ptr != '\0') { - if (xputc(*ptr, stream) == EOF) + if (xputc(pic, *ptr, stream) == EOF) return EOF; ++ptr; } return (int)(ptr - s); } -char *xfgets(char *s, int size, xFILE *stream) { +char *xfgets(pic_state *pic, char *s, int size, xFILE *stream) { int c; char *buf; - xfflush(NULL); + xfflush(pic, NULL); if (size == 0) { return NULL; } buf = s; - while (--size > 0 && (c = xgetc(stream)) != EOF) { + while (--size > 0 && (c = xgetc(pic, stream)) != EOF) { if ((*buf++ = c) == '\n') break; } @@ -248,28 +244,28 @@ char *xfgets(char *s, int size, xFILE *stream) { return (c == EOF && buf == s) ? NULL : s; } -int xputs(const char *s) { +int xputs(pic_state *pic, const char *s) { int i = 1; while(*s != '\0') { - if (xputchar(*s++) == EOF) + if (xputchar(pic, *s++) == EOF) return EOF; i++; } - if (xputchar('\n') == EOF) { + if (xputchar(pic, '\n') == EOF) { return EOF; } return i; } -char *xgets(char *s) { +char *xgets(pic_state *pic, char *s) { int c; char *buf; - xfflush(NULL); + xfflush(pic, NULL); buf = s; - while ((c = xgetchar()) != EOF && c != '\n') { + while ((c = xgetchar(pic)) != EOF && c != '\n') { *buf++ = c; } *buf = '\0'; @@ -287,7 +283,7 @@ int xungetc(int c, xFILE *fp) { return *--fp->ptr = uc; } -size_t xfread(void *ptr, size_t size, size_t count, xFILE *fp) { +size_t xfread(pic_state *pic, void *ptr, size_t size, size_t count, xFILE *fp) { char *bptr = ptr; long nbytes; int c; @@ -298,7 +294,7 @@ size_t xfread(void *ptr, size_t size, size_t count, xFILE *fp) { fp->ptr += fp->cnt; bptr += fp->cnt; nbytes -= fp->cnt; - if ((c = x_fillbuf(fp)) == EOF) { + if ((c = x_fillbuf(pic, fp)) == EOF) { return (size * count - nbytes) / size; } else { xungetc(c, fp); @@ -310,7 +306,7 @@ size_t xfread(void *ptr, size_t size, size_t count, xFILE *fp) { return count; } -size_t xfwrite(const void *ptr, size_t size, size_t count, xFILE *fp) { +size_t xfwrite(pic_state *pic, const void *ptr, size_t size, size_t count, xFILE *fp) { const char *bptr = ptr; long nbytes; @@ -320,7 +316,7 @@ size_t xfwrite(const void *ptr, size_t size, size_t count, xFILE *fp) { fp->ptr += fp->cnt; bptr += fp->cnt; nbytes -= fp->cnt; - if (x_flushbuf(EOF, fp) == EOF) { + if (x_flushbuf(pic, EOF, fp) == EOF) { return (size * count - nbytes) / size; } } @@ -330,10 +326,10 @@ size_t xfwrite(const void *ptr, size_t size, size_t count, xFILE *fp) { return count; } -long xfseek(xFILE *fp, long offset, int whence) { +long xfseek(pic_state *pic, xFILE *fp, long offset, int whence) { long s; - xfflush(fp); + xfflush(pic, fp); fp->ptr = fp->base; fp->cnt = 0; @@ -344,36 +340,36 @@ long xfseek(xFILE *fp, long offset, int whence) { return 0; } -long xftell(xFILE *fp) { - return xfseek(fp, 0, XSEEK_CUR); +long xftell(pic_state *pic, xFILE *fp) { + return xfseek(pic, fp, 0, XSEEK_CUR); } -void xrewind(xFILE *fp) { - xfseek(fp, 0, XSEEK_SET); +void xrewind(pic_state *pic, xFILE *fp) { + xfseek(pic, fp, 0, XSEEK_SET); xclearerr(fp); } -int xprintf(const char *fmt, ...) { +int xprintf(pic_state *pic, const char *fmt, ...) { va_list ap; int n; va_start(ap, fmt); - n = xvfprintf(xstdout, fmt, ap); + n = xvfprintf(pic, xstdout, fmt, ap); va_end(ap); return n; } -int xfprintf(xFILE *stream, const char *fmt, ...) { +int xfprintf(pic_state *pic, xFILE *stream, const char *fmt, ...) { va_list ap; int n; va_start(ap, fmt); - n = xvfprintf(stream, fmt, ap); + n = xvfprintf(pic, stream, fmt, ap); va_end(ap); return n; } -static int print_int(xFILE *stream, long x, int base) { +static int print_int(pic_state *pic, xFILE *stream, long x, int base) { static const char digits[] = "0123456789abcdef"; char buf[20]; int i, c, neg; @@ -395,12 +391,12 @@ static int print_int(xFILE *stream, long x, int base) { c = i; while (i-- > 0) { - xputc(buf[i], stream); + xputc(pic, buf[i], stream); } return c; } -int xvfprintf(xFILE *stream, const char *fmt, va_list ap) { +int xvfprintf(pic_state *pic, xFILE *stream, const char *fmt, va_list ap) { const char *p; char *sval; int ival; @@ -412,7 +408,7 @@ int xvfprintf(xFILE *stream, const char *fmt, va_list ap) { for (p = fmt; *p; p++) { if (*p != '%') { - xputc(*p, stream); + xputc(pic, *p, stream); cnt++; continue; } @@ -420,42 +416,42 @@ int xvfprintf(xFILE *stream, const char *fmt, va_list ap) { case 'd': case 'i': ival = va_arg(ap, int); - cnt += print_int(stream, ival, 10); + cnt += print_int(pic, stream, ival, 10); break; #if PIC_ENABLE_FLOAT case 'f': dval = va_arg(ap, double); - cnt += print_int(stream, dval, 10); - xputc('.', stream); + cnt += print_int(pic, stream, dval, 10); + xputc(pic, '.', stream); cnt++; if ((ival = fabs((dval - floor(dval)) * 1e4) + 0.5) == 0) { - cnt += xfputs("0000", stream); + cnt += xfputs(pic, "0000", stream); } else { int i; for (i = 0; i < 3 - (int)log10(ival); ++i) { - xputc('0', stream); + xputc(pic, '0', stream); cnt++; } - cnt += print_int(stream, ival, 10); + cnt += print_int(pic, stream, ival, 10); } break; #endif case 's': sval = va_arg(ap, char*); - cnt += xfputs(sval, stream); + cnt += xfputs(pic, sval, stream); break; case 'p': vp = va_arg(ap, void*); - cnt += xfputs("0x", stream); - cnt += print_int(stream, (long)vp, 16); + cnt += xfputs(pic, "0x", stream); + cnt += print_int(pic, stream, (long)vp, 16); break; case '%': - xputc(*(p-1), stream); + xputc(pic, *(p-1), stream); cnt++; break; default: - xputc('%', stream); - xputc(*(p-1), stream); + xputc(pic, '%', stream); + xputc(pic, *(p-1), stream); cnt += 2; break; } diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 6bda3be1..8eb641fe 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -42,7 +42,6 @@ extern "C" { #include "picrin/xvect.h" #include "picrin/xhash.h" -#include "picrin/file.h" #include "picrin/value.h" @@ -72,6 +71,8 @@ typedef struct { typedef void *(*pic_allocf)(void *, size_t); +typedef struct xFILE xFILE; + typedef struct { int argc; char **argv, **envp; @@ -254,6 +255,7 @@ struct pic_port *pic_stderr(pic_state *); 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 *, ...); pic_value pic_display(pic_state *, pic_value); pic_value pic_fdisplay(pic_state *, pic_value, xFILE *); @@ -281,6 +283,7 @@ pic_value pic_fdisplay(pic_state *, pic_value, xFILE *); #include "picrin/read.h" #include "picrin/vector.h" #include "picrin/reg.h" +#include "picrin/file.h" #if defined(__cplusplus) } diff --git a/extlib/benz/include/picrin/file.h b/extlib/benz/include/picrin/file.h index eff7d269..189bd3de 100644 --- a/extlib/benz/include/picrin/file.h +++ b/extlib/benz/include/picrin/file.h @@ -1,5 +1,5 @@ -#ifndef XFILE_H -#define XFILE_H +#ifndef PICRIN_FILE_H +#define PICRIN_FILE_H #if defined(__cplusplus) extern "C" { @@ -7,10 +7,6 @@ extern "C" { #include -#ifndef NULL -# define NULL 0 -#endif - #ifndef EOF # define EOF (-1) #endif @@ -18,7 +14,7 @@ extern "C" { #define XBUFSIZ 1024 #define XOPEN_MAX 1024 -typedef struct { +struct xFILE { /* buffer */ char buf[1]; /* fallback buffer */ long cnt; /* characters left */ @@ -33,7 +29,7 @@ typedef struct { int (*close)(void *); } vtable; int flag; /* mode of the file access */ -} xFILE; +}; extern xFILE x_iob[XOPEN_MAX]; @@ -55,30 +51,30 @@ enum _flags { #define xferror(p) (((p)->flag & X_ERR) != 0) #define xfileno(p) ((p)->fd) -#define xgetc(p) \ +#define xgetc(pic, p) \ ((--(p)->cnt >= 0) \ ? (unsigned char) *(p)->ptr++ \ - : x_fillbuf(p)) -#define xputc(x, p) \ + : x_fillbuf((pic), p)) +#define xputc(pic, x, p) \ ((--(p)->cnt >= 0 && !(((p)->flag & X_LNBUF) && (x) == '\n')) \ ? *(p)->ptr++ = (x) \ - : x_flushbuf(x, (p))) -#define xgetchar() xgetc(xstdin) -#define xputchar(x) xputc((x), xstdout) + : x_flushbuf((pic), (x), (p))) +#define xgetchar(pic) xgetc((pic), xstdin) +#define xputchar(pic, x) xputc((pic), (x), xstdout) /* resource aquisition */ xFILE *xfunopen(void *cookie, int (*read)(void *, char *, int), int (*write)(void *, const char *, int), long (*seek)(void *, long, int), int (*close)(void *)); xFILE *xfopen(const char *, const char *); -int xfclose(xFILE *); +int xfclose(pic_state *, xFILE *); /* buffer management */ -int x_fillbuf(xFILE *); -int x_flushbuf(int, xFILE *); -int xfflush(xFILE *); +int x_fillbuf(pic_state *, xFILE *); +int x_flushbuf(pic_state *, int, xFILE *); +int xfflush(pic_state *, xFILE *); /* direct IO */ -size_t xfread(void *, size_t, size_t, xFILE *); -size_t xfwrite(const void *, size_t, size_t, xFILE *); +size_t xfread(pic_state *, void *, size_t, size_t, xFILE *); +size_t xfwrite(pic_state *, const void *, size_t, size_t, xFILE *); enum { XSEEK_CUR, @@ -87,22 +83,22 @@ enum { }; /* indicator positioning */ -long xfseek(xFILE *, long, int); -long xftell(xFILE *); -void xrewind(xFILE *); +long xfseek(pic_state *, xFILE *, long, int); +long xftell(pic_state *, xFILE *); +void xrewind(pic_state *, xFILE *); /* character IO */ -int xfputc(int, xFILE *); -int xfgetc(xFILE *); -int xfputs(const char *, xFILE *); -char *xfgets(char *, int, xFILE *); -int xputs(const char *); +int xfputc(pic_state *, int, xFILE *); +int xfgetc(pic_state *, xFILE *); +int xfputs(pic_state *, const char *, xFILE *); +char *xfgets(pic_state *, char *, int, xFILE *); +int xputs(pic_state *, const char *); int xungetc(int, xFILE *); /* formatted I/O */ -int xprintf(const char *, ...); -int xfprintf(xFILE *, const char *, ...); -int xvfprintf(xFILE *, const char *, va_list); +int xprintf(pic_state *, const char *, ...); +int xfprintf(pic_state *, xFILE *, const char *, ...); +int xvfprintf(pic_state *, xFILE *, const char *, va_list); #if defined(__cplusplus) } diff --git a/extlib/benz/number.c b/extlib/benz/number.c index a0cf35ba..52eed9b3 100644 --- a/extlib/benz/number.c +++ b/extlib/benz/number.c @@ -574,7 +574,7 @@ pic_number_number_to_string(pic_state *pic) else { struct pic_port *port = pic_open_output_string(pic); - xfprintf(port->file, "%f", f); + xfprintf(pic, port->file, "%f", f); str = pic_get_output_string(pic, port); diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 5eed06cc..8a0d7df4 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -178,7 +178,7 @@ pic_get_output_string(pic_state *pic, struct pic_port *port) pic_errorf(pic, "get-output-string: port is not made by open-output-string"); } - xfflush(port->file); + xfflush(pic, port->file); s = port->file->vtable.cookie; @@ -191,7 +191,7 @@ pic_close_port(pic_state *pic, struct pic_port *port) if ((port->flags & PIC_PORT_OPEN) == 0) { return; } - if (xfclose(port->file) == EOF) { + if (xfclose(pic, port->file) == EOF) { pic_errorf(pic, "close-port: failure"); } port->flags &= ~PIC_PORT_OPEN; @@ -431,7 +431,7 @@ pic_port_get_output_bytevector(pic_state *pic) pic_errorf(pic, "get-output-bytevector: port is not made by open-output-bytevector"); } - xfflush(port->file); + xfflush(pic, port->file); s = port->file->vtable.cookie; @@ -451,7 +451,7 @@ pic_port_read_char(pic_state *pic) assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, "read-char"); - if ((c = xfgetc(port->file)) == EOF) { + if ((c = xfgetc(pic, port->file)) == EOF) { return pic_eof_object(); } else { @@ -469,7 +469,7 @@ pic_port_peek_char(pic_state *pic) assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, "peek-char"); - if ((c = xfgetc(port->file)) == EOF) { + if ((c = xfgetc(pic, port->file)) == EOF) { return pic_eof_object(); } else { @@ -490,8 +490,8 @@ pic_port_read_line(pic_state *pic) assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, "read-line"); buf = pic_open_output_string(pic); - while ((c = xfgetc(port->file)) != EOF && c != '\n') { - xfputc(c, buf->file); + while ((c = xfgetc(pic, port->file)) != EOF && c != '\n') { + xfputc(pic, c, buf->file); } str = pic_get_output_string(pic, buf); @@ -529,10 +529,10 @@ pic_port_read_string(pic_state *pic){ c = EOF; buf = pic_open_output_string(pic); for(i = 0; i < k; ++i) { - if((c = xfgetc(port->file)) == EOF){ + if((c = xfgetc(pic, port->file)) == EOF){ break; } - xfputc(c, buf->file); + xfputc(pic, c, buf->file); } str = pic_get_output_string(pic, buf); @@ -552,7 +552,7 @@ pic_port_read_byte(pic_state *pic){ pic_get_args(pic, "|p", &port); assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, "read-u8"); - if ((c = xfgetc(port->file)) == EOF) { + if ((c = xfgetc(pic, port->file)) == EOF) { return pic_eof_object(); } @@ -569,7 +569,7 @@ pic_port_peek_byte(pic_state *pic) assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, "peek-u8"); - c = xfgetc(port->file); + c = xfgetc(pic, port->file); if (c == EOF) { return pic_eof_object(); } @@ -605,7 +605,7 @@ pic_port_read_blob(pic_state *pic) blob = pic_make_blob(pic, k); - i = xfread(blob->data, sizeof(char), k, port->file); + i = xfread(pic, blob->data, sizeof(char), k, port->file); if (i == 0) { return pic_eof_object(); } @@ -644,7 +644,7 @@ pic_port_read_blob_ip(pic_state *pic) len = end - start; buf = pic_calloc(pic, len, sizeof(char)); - i = xfread(buf, sizeof(char), len, port->file); + i = xfread(pic, buf, sizeof(char), len, port->file); memcpy(bv->data + start, buf, i); pic_free(pic, buf); @@ -665,7 +665,7 @@ pic_port_newline(pic_state *pic) assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, "newline"); - xfputs("\n", port->file); + xfputs(pic, "\n", port->file); return pic_undef_value(); } @@ -679,7 +679,7 @@ pic_port_write_char(pic_state *pic) assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, "write-char"); - xfputc(c, port->file); + xfputc(pic, c, port->file); return pic_undef_value(); } @@ -703,7 +703,7 @@ pic_port_write_string(pic_state *pic) assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, "write-string"); for (i = start; i < end && str[i] != '\0'; ++i) { - xfputc(str[i], port->file); + xfputc(pic, str[i], port->file); } return pic_undef_value(); } @@ -718,7 +718,7 @@ pic_port_write_byte(pic_state *pic) assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_BINARY, "write-u8"); - xfputc(i, port->file); + xfputc(pic, i, port->file); return pic_undef_value(); } @@ -743,7 +743,7 @@ pic_port_write_blob(pic_state *pic) assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_BINARY, "write-bytevector"); for (i = start; i < end; ++i) { - xfputc(blob->data[i], port->file); + xfputc(pic, blob->data[i], port->file); } return pic_undef_value(); } @@ -757,7 +757,7 @@ pic_port_flush(pic_state *pic) assert_port_profile(port, PIC_PORT_OUT, "flush-output-port"); - xfflush(port->file); + xfflush(pic, port->file); return pic_undef_value(); } diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 6dbea00a..45325ebf 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -18,39 +18,39 @@ read_error(pic_state *pic, const char *msg) } static int -skip(struct pic_port *port, int c) +skip(pic_state *pic, struct pic_port *port, int c) { while (isspace(c)) { - c = xfgetc(port->file); + c = xfgetc(pic, port->file); } return c; } static int -next(struct pic_port *port) +next(pic_state *pic, struct pic_port *port) { - return xfgetc(port->file); + return xfgetc(pic, port->file); } static int -peek(struct pic_port *port) +peek(pic_state *pic, struct pic_port *port) { int c; - xungetc((c = xfgetc(port->file)), port->file); + xungetc((c = xfgetc(pic, port->file)), port->file); return c; } static bool -expect(struct pic_port *port, const char *str) +expect(pic_state *pic, struct pic_port *port, const char *str) { int c; while ((c = (int)*str++) != 0) { - if (c != peek(port)) + if (c != peek(pic, port)) return false; - next(port); + next(pic, port); } return true; @@ -89,7 +89,7 @@ static pic_value read_comment(pic_state PIC_UNUSED(*pic), struct pic_port *port, int c) { do { - c = next(port); + c = next(pic, port); } while (! (c == EOF || c == '\n')); return pic_invalid_value(); @@ -101,11 +101,11 @@ read_block_comment(pic_state PIC_UNUSED(*pic), struct pic_port *port, int PIC_UN int x, y; int i = 1; - y = next(port); + y = next(pic, port); while (y != EOF && i > 0) { x = y; - y = next(port); + y = next(pic, port); if (x == '|' && y == '#') { i--; } @@ -120,7 +120,7 @@ read_block_comment(pic_state PIC_UNUSED(*pic), struct pic_port *port, int PIC_UN static pic_value read_datum_comment(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) { - read(pic, port, next(port)); + read(pic, port, next(pic, port)); return pic_invalid_value(); } @@ -128,15 +128,15 @@ read_datum_comment(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) static pic_value read_directive(pic_state *pic, struct pic_port *port, int c) { - switch (peek(port)) { + switch (peek(pic, port)) { case 'n': - if (expect(port, "no-fold-case")) { + if (expect(pic, port, "no-fold-case")) { pic->reader->typecase = PIC_CASE_DEFAULT; return pic_invalid_value(); } break; case 'f': - if (expect(port, "fold-case")) { + if (expect(pic, port, "fold-case")) { pic->reader->typecase = PIC_CASE_FOLD; return pic_invalid_value(); } @@ -149,13 +149,13 @@ read_directive(pic_state *pic, struct pic_port *port, int c) static pic_value read_quote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) { - return pic_list2(pic, pic_obj_value(pic->sQUOTE), read(pic, port, next(port))); + return pic_list2(pic, pic_obj_value(pic->sQUOTE), read(pic, port, next(pic, port))); } static pic_value read_quasiquote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) { - return pic_list2(pic, pic_obj_value(pic->sQUASIQUOTE), read(pic, port, next(port))); + return pic_list2(pic, pic_obj_value(pic->sQUASIQUOTE), read(pic, port, next(pic, port))); } static pic_value @@ -163,23 +163,23 @@ read_unquote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) { pic_sym *tag = pic->sUNQUOTE; - if (peek(port) == '@') { + if (peek(pic, port) == '@') { tag = pic->sUNQUOTE_SPLICING; - next(port); + next(pic, port); } - return pic_list2(pic, pic_obj_value(tag), read(pic, port, next(port))); + return pic_list2(pic, pic_obj_value(tag), read(pic, port, next(pic, port))); } static pic_value read_syntax_quote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) { - return pic_list2(pic, pic_obj_value(pic->sSYNTAX_QUOTE), read(pic, port, next(port))); + return pic_list2(pic, pic_obj_value(pic->sSYNTAX_QUOTE), read(pic, port, next(pic, port))); } static pic_value read_syntax_quasiquote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) { - return pic_list2(pic, pic_obj_value(pic->sSYNTAX_QUASIQUOTE), read(pic, port, next(port))); + return pic_list2(pic, pic_obj_value(pic->sSYNTAX_QUASIQUOTE), read(pic, port, next(pic, port))); } static pic_value @@ -187,11 +187,11 @@ read_syntax_unquote(pic_state *pic, struct pic_port *port, int PIC_UNUSED(c)) { pic_sym *tag = pic->sSYNTAX_UNQUOTE; - if (peek(port) == '@') { + if (peek(pic, port) == '@') { tag = pic->sSYNTAX_UNQUOTE_SPLICING; - next(port); + next(pic, port); } - return pic_list2(pic, pic_obj_value(tag), read(pic, port, next(port))); + return pic_list2(pic, pic_obj_value(tag), read(pic, port, next(pic, port))); } static pic_value @@ -206,8 +206,8 @@ read_symbol(pic_state *pic, struct pic_port *port, int c) buf[0] = case_fold(pic, c); buf[1] = 0; - while (! isdelim(peek(port))) { - c = next(port); + while (! isdelim(peek(pic, port))) { + c = next(pic, port); len += 1; buf = pic_realloc(pic, buf, len + 1); buf[len - 1] = case_fold(pic, c); @@ -230,8 +230,8 @@ read_uinteger(pic_state *pic, struct pic_port *port, int c) } u = c - '0'; - while (isdigit(c = peek(port))) { - u = u * 10 + next(port) - '0'; + while (isdigit(c = peek(pic, port))) { + u = u * 10 + next(pic, port) - '0'; } return u; @@ -242,19 +242,19 @@ read_suffix(pic_state *pic, struct pic_port *port) { int c, s = 1; - c = peek(port); + c = peek(pic, port); if (c != 'e' && c != 'E') { return 0; } - next(port); + next(pic, port); - switch ((c = next(port))) { + switch ((c = next(pic, port))) { case '-': s = -1; case '+': - c = next(port); + c = next(pic, port); default: return s * read_uinteger(pic, port, c); } @@ -271,13 +271,13 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c) u = read_uinteger(pic, port, c); - switch (peek(port)) { + switch (peek(pic, port)) { #if PIC_ENABLE_FLOAT case '.': - next(port); + next(pic, port); g = 0, e = 0; - while (isdigit(c = peek(port))) { - g = g * 10 + (next(port) - '0'); + while (isdigit(c = peek(pic, port))) { + g = g * 10 + (next(pic, port) - '0'); e++; } f = u + g * pow(10, -e); @@ -348,8 +348,8 @@ read_minus(pic_state *pic, struct pic_port *port, int c) { pic_value sym; - if (isdigit(peek(port))) { - return negate(read_unsigned(pic, port, next(port))); + if (isdigit(peek(pic, port))) { + return negate(read_unsigned(pic, port, next(pic, port))); } else { sym = read_symbol(pic, port, c); @@ -370,8 +370,8 @@ read_plus(pic_state *pic, struct pic_port *port, int c) { pic_value sym; - if (isdigit(peek(port))) { - return read_unsigned(pic, port, next(port)); + if (isdigit(peek(pic, port))) { + return read_unsigned(pic, port, next(pic, port)); } else { sym = read_symbol(pic, port, c); @@ -390,8 +390,8 @@ read_plus(pic_state *pic, struct pic_port *port, int c) static pic_value read_true(pic_state *pic, struct pic_port *port, int c) { - if ((c = peek(port)) == 'r') { - if (! expect(port, "rue")) { + if ((c = peek(pic, port)) == 'r') { + if (! expect(pic, port, "rue")) { read_error(pic, "unexpected character while reading #true"); } } else if (! isdelim(c)) { @@ -404,8 +404,8 @@ read_true(pic_state *pic, struct pic_port *port, int c) static pic_value read_false(pic_state *pic, struct pic_port *port, int c) { - if ((c = peek(port)) == 'a') { - if (! expect(port, "alse")) { + if ((c = peek(pic, port)) == 'a') { + if (! expect(pic, port, "alse")) { read_error(pic, "unexpected character while reading #false"); } } else if (! isdelim(c)) { @@ -418,29 +418,29 @@ read_false(pic_state *pic, struct pic_port *port, int c) static pic_value read_char(pic_state *pic, struct pic_port *port, int c) { - c = next(port); + c = next(pic, port); - if (! isdelim(peek(port))) { + if (! isdelim(peek(pic, port))) { switch (c) { default: read_error(pic, "unexpected character after char literal"); - case 'a': c = '\a'; if (! expect(port, "lerm")) goto fail; break; - case 'b': c = '\b'; if (! expect(port, "ackspace")) goto fail; break; - case 'd': c = 0x7F; if (! expect(port, "elete")) goto fail; break; - case 'e': c = 0x1B; if (! expect(port, "scape")) goto fail; break; + case 'a': c = '\a'; if (! expect(pic, port, "lerm")) goto fail; break; + case 'b': c = '\b'; if (! expect(pic, port, "ackspace")) goto fail; break; + case 'd': c = 0x7F; if (! expect(pic, port, "elete")) goto fail; break; + case 'e': c = 0x1B; if (! expect(pic, port, "scape")) goto fail; break; case 'n': - if ((c = peek(port)) == 'e') { + if ((c = peek(pic, port)) == 'e') { c = '\n'; - if (! expect(port, "ewline")) + if (! expect(pic, port, "ewline")) goto fail; } else { c = '\0'; - if (! expect(port, "ull")) + if (! expect(pic, port, "ull")) goto fail; } break; - case 'r': c = '\r'; if (! expect(port, "eturn")) goto fail; break; - case 's': c = ' '; if (! expect(port, "pace")) goto fail; break; - case 't': c = '\t'; if (! expect(port, "ab")) goto fail; break; + case 'r': c = '\r'; if (! expect(pic, port, "eturn")) goto fail; break; + case 's': c = ' '; if (! expect(pic, port, "pace")) goto fail; break; + case 't': c = '\t'; if (! expect(pic, port, "ab")) goto fail; break; } } @@ -463,9 +463,9 @@ read_string(pic_state *pic, struct pic_port *port, int c) /* TODO: intraline whitespaces */ - while ((c = next(port)) != '"') { + while ((c = next(pic, port)) != '"') { if (c == '\\') { - switch (c = next(port)) { + switch (c = next(pic, port)) { case 'a': c = '\a'; break; case 'b': c = '\b'; break; case 't': c = '\t'; break; @@ -498,9 +498,9 @@ read_pipe(pic_state *pic, struct pic_port *port, int c) size = 256; buf = pic_malloc(pic, size); cnt = 0; - while ((c = next(port)) != '|') { + while ((c = next(pic, port)) != '|') { if (c == '\\') { - switch ((c = next(port))) { + switch ((c = next(pic, port))) { case 'a': c = '\a'; break; case 'b': c = '\b'; break; case 't': c = '\t'; break; @@ -508,7 +508,7 @@ read_pipe(pic_state *pic, struct pic_port *port, int c) case 'r': c = '\r'; break; case 'x': i = 0; - while ((HEX_BUF[i++] = (char)next(port)) != ';') { + while ((HEX_BUF[i++] = (char)next(pic, port)) != ';') { if (i >= sizeof HEX_BUF) read_error(pic, "expected ';'"); } @@ -539,7 +539,7 @@ read_blob(pic_state *pic, struct pic_port *port, int c) nbits = 0; - while (isdigit(c = next(port))) { + while (isdigit(c = next(pic, port))) { nbits = 10 * nbits + c - '0'; } @@ -553,8 +553,8 @@ read_blob(pic_state *pic, struct pic_port *port, int c) len = 0; dat = NULL; - c = next(port); - while ((c = skip(port, c)) != ')') { + c = next(pic, port); + while ((c = skip(pic, port, c)) != ')') { n = read_uinteger(pic, port, c); if (n < 0 || (1 << nbits) <= n) { read_error(pic, "invalid element in bytevector literal"); @@ -562,7 +562,7 @@ read_blob(pic_state *pic, struct pic_port *port, int c) len += 1; dat = pic_realloc(pic, dat, len); dat[len - 1] = (unsigned char)n; - c = next(port); + c = next(pic, port); } blob = pic_make_blob(pic, len); @@ -577,8 +577,8 @@ read_blob(pic_state *pic, struct pic_port *port, int c) static pic_value read_undef_or_blob(pic_state *pic, struct pic_port *port, int c) { - if ((c = peek(port)) == 'n') { - if (! expect(port, "ndefined")) { + if ((c = peek(pic, port)) == 'n') { + if (! expect(pic, port, "ndefined")) { read_error(pic, "unexpected character while reading #undefined"); } return pic_undef_value(); @@ -597,16 +597,16 @@ read_pair(pic_state *pic, struct pic_port *port, int c) retry: - c = skip(port, ' '); + c = skip(pic, port, ' '); if (c == tCLOSE) { return pic_nil_value(); } - if (c == '.' && isdelim(peek(port))) { - cdr = read(pic, port, next(port)); + if (c == '.' && isdelim(peek(pic, port))) { + cdr = read(pic, port, next(pic, port)); closing: - if ((c = skip(port, ' ')) != tCLOSE) { + if ((c = skip(pic, port, ' ')) != tCLOSE) { if (pic_invalid_p(read_nullable(pic, port, c))) { goto closing; } @@ -642,7 +642,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) pic_value val; int c; - switch ((c = skip(port, ' '))) { + switch ((c = skip(pic, port, ' '))) { case '(': { pic_value tmp; @@ -661,7 +661,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) { bool vect; - if (peek(port) == '(') { + if (peek(pic, port) == '(') { vect = true; } else { vect = false; @@ -714,7 +714,7 @@ read_label(pic_state *pic, struct pic_port *port, int c) i = 0; do { i = i * 10 + c - '0'; - } while (isdigit(c = next(port))); + } while (isdigit(c = next(pic, port))); if (c == '=') { return read_label_set(pic, port, i); @@ -734,7 +734,7 @@ read_unmatch(pic_state *pic, struct pic_port PIC_UNUSED(*port), int PIC_UNUSED(c static pic_value read_dispatch(pic_state *pic, struct pic_port *port, int c) { - c = next(port); + c = next(pic, port); if (c == EOF) { read_error(pic, "unexpected EOF"); @@ -750,7 +750,7 @@ read_dispatch(pic_state *pic, struct pic_port *port, int c) static pic_value read_nullable(pic_state *pic, struct pic_port *port, int c) { - c = skip(port, c); + c = skip(pic, port, c); if (c == EOF) { read_error(pic, "unexpected EOF"); @@ -772,7 +772,7 @@ read(pic_state *pic, struct pic_port *port, int c) val = read_nullable(pic, port, c); if (pic_invalid_p(val)) { - c = next(port); + c = next(pic, port); goto retry; } @@ -860,10 +860,10 @@ pic_value pic_read(pic_state *pic, struct pic_port *port) { pic_value val; - int c = next(port); + int c = next(pic, port); retry: - c = skip(port, c); + c = skip(pic, port, c); if (c == EOF) { return pic_eof_object(); @@ -872,7 +872,7 @@ pic_read(pic_state *pic, struct pic_port *port) val = read_nullable(pic, port, c); if (pic_invalid_p(val)) { - c = next(port); + c = next(pic, port); goto retry; } diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 64b58b25..7a355e47 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -437,7 +437,7 @@ pic_close(pic_state *pic) pic_gc_run(pic); /* flush all xfiles */ - xfflush(NULL); + xfflush(pic, NULL); /* free heaps */ pic_heap_close(pic, pic->heap); diff --git a/extlib/benz/string.c b/extlib/benz/string.c index 1e1e083c..9d1060c3 100644 --- a/extlib/benz/string.c +++ b/extlib/benz/string.c @@ -310,7 +310,7 @@ pic_xvfformat(pic_state *pic, xFILE *file, const char *fmt, va_list ap) while ((c = *fmt++)) { switch (c) { default: - xfputc(c, file); + xfputc(pic, c, file); break; case '%': c = *fmt++; @@ -318,26 +318,26 @@ pic_xvfformat(pic_state *pic, xFILE *file, const char *fmt, va_list ap) goto exit; switch (c) { default: - xfputc(c, file); + xfputc(pic, c, file); break; case '%': - xfputc('%', file); + xfputc(pic, '%', file); break; case 'c': - xfprintf(file, "%c", va_arg(ap, int)); + xfprintf(pic, file, "%c", va_arg(ap, int)); break; case 's': - xfprintf(file, "%s", va_arg(ap, const char *)); + xfprintf(pic, file, "%s", va_arg(ap, const char *)); break; case 'd': - xfprintf(file, "%d", va_arg(ap, int)); + xfprintf(pic, file, "%d", va_arg(ap, int)); break; case 'p': - xfprintf(file, "%p", va_arg(ap, void *)); + xfprintf(pic, file, "%p", va_arg(ap, void *)); break; #if PIC_ENABLE_FLOAT case 'f': - xfprintf(file, "%f", va_arg(ap, double)); + xfprintf(pic, file, "%f", va_arg(ap, double)); break; #endif } @@ -348,13 +348,13 @@ pic_xvfformat(pic_state *pic, xFILE *file, const char *fmt, va_list ap) goto exit; switch (c) { default: - xfputc(c, file); + xfputc(pic, c, file); break; case '~': - xfputc('~', file); + xfputc(pic, '~', file); break; case '%': - xfputc('\n', file); + xfputc(pic, '\n', file); break; case 'a': irrs = pic_cons(pic, pic_fdisplay(pic, va_arg(ap, pic_value), file), irrs); diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 374d54e2..e98e027c 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -111,6 +111,7 @@ static void write_core(struct writer_control *p, pic_value); static void write_pair(struct writer_control *p, struct pic_pair *pair) { + pic_state *pic = p->pic; xh_entry *e; int c; @@ -123,27 +124,27 @@ write_pair(struct writer_control *p, struct pic_pair *pair) /* shared objects */ if ((e = xh_get_ptr(&p->labels, pic_obj_ptr(pair->cdr))) && xh_val(e, int) != -1) { - xfprintf(p->file, " . "); + xfprintf(pic, p->file, " . "); if ((xh_get_ptr(&p->visited, pic_obj_ptr(pair->cdr)))) { - xfprintf(p->file, "#%d#", xh_val(e, int)); + xfprintf(pic, p->file, "#%d#", xh_val(e, int)); return; } else { - xfprintf(p->file, "#%d=", xh_val(e, int)); + xfprintf(pic, p->file, "#%d=", xh_val(e, int)); c = 1; xh_put_ptr(&p->visited, pic_obj_ptr(pair->cdr), &c); } } else { - xfprintf(p->file, " "); + xfprintf(pic, p->file, " "); } write_pair(p, pic_pair_ptr(pair->cdr)); return; } else { - xfprintf(p->file, " . "); + xfprintf(pic, p->file, " . "); write_core(p, pair->cdr); } } @@ -156,9 +157,9 @@ write_str(pic_state *pic, struct pic_string *str, xFILE *file) for (i = 0; i < pic_str_len(str); ++i) { if (cstr[i] == '"' || cstr[i] == '\\') { - xfputc('\\', file); + xfputc(pic, '\\', file); } - xfputc(cstr[i], file); + xfputc(pic, cstr[i], file); } } @@ -179,11 +180,11 @@ write_core(struct writer_control *p, pic_value obj) && (e = xh_get_ptr(&p->labels, pic_obj_ptr(obj))) && xh_val(e, int) != -1) { if ((xh_get_ptr(&p->visited, pic_obj_ptr(obj)))) { - xfprintf(file, "#%d#", xh_val(e, int)); + xfprintf(pic, file, "#%d#", xh_val(e, int)); return; } else { - xfprintf(file, "#%d=", xh_val(e, int)); + xfprintf(pic, file, "#%d=", xh_val(e, int)); c = 1; xh_put_ptr(&p->visited, pic_obj_ptr(obj), &c); } @@ -191,122 +192,122 @@ write_core(struct writer_control *p, pic_value obj) switch (pic_type(obj)) { case PIC_TT_UNDEF: - xfprintf(file, "#undefined"); + xfprintf(pic, file, "#undefined"); break; case PIC_TT_NIL: - xfprintf(file, "()"); + xfprintf(pic, file, "()"); break; case PIC_TT_BOOL: if (pic_true_p(obj)) - xfprintf(file, "#t"); + xfprintf(pic, file, "#t"); else - xfprintf(file, "#f"); + xfprintf(pic, file, "#f"); break; case PIC_TT_PAIR: if (is_quote(pic, obj)) { - xfprintf(file, "'"); + xfprintf(pic, file, "'"); write_core(p, pic_list_ref(pic, obj, 1)); break; } else if (is_unquote(pic, obj)) { - xfprintf(file, ","); + xfprintf(pic, file, ","); write_core(p, pic_list_ref(pic, obj, 1)); break; } else if (is_unquote_splicing(pic, obj)) { - xfprintf(file, ",@"); + xfprintf(pic, file, ",@"); write_core(p, pic_list_ref(pic, obj, 1)); break; } else if (is_quasiquote(pic, obj)) { - xfprintf(file, "`"); + xfprintf(pic, file, "`"); write_core(p, pic_list_ref(pic, obj, 1)); break; } - xfprintf(file, "("); + xfprintf(pic, file, "("); write_pair(p, pic_pair_ptr(obj)); - xfprintf(file, ")"); + xfprintf(pic, file, ")"); break; case PIC_TT_SYMBOL: - xfprintf(file, "%s", pic_symbol_name(pic, pic_sym_ptr(obj))); + xfprintf(pic, file, "%s", pic_symbol_name(pic, pic_sym_ptr(obj))); break; case PIC_TT_CHAR: if (p->mode == DISPLAY_MODE) { - xfputc(pic_char(obj), file); + xfputc(pic, 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; + default: xfprintf(pic, file, "#\\%c", pic_char(obj)); break; + case '\a': xfprintf(pic, file, "#\\alarm"); break; + case '\b': xfprintf(pic, file, "#\\backspace"); break; + case 0x7f: xfprintf(pic, file, "#\\delete"); break; + case 0x1b: xfprintf(pic, file, "#\\escape"); break; + case '\n': xfprintf(pic, file, "#\\newline"); break; + case '\r': xfprintf(pic, file, "#\\return"); break; + case ' ': xfprintf(pic, file, "#\\space"); break; + case '\t': xfprintf(pic, file, "#\\tab"); break; } break; #if PIC_ENABLE_FLOAT case PIC_TT_FLOAT: f = pic_float(obj); if (isnan(f)) { - xfprintf(file, signbit(f) ? "-nan.0" : "+nan.0"); + xfprintf(pic, file, signbit(f) ? "-nan.0" : "+nan.0"); } else if (isinf(f)) { - xfprintf(file, signbit(f) ? "-inf.0" : "+inf.0"); + xfprintf(pic, file, signbit(f) ? "-inf.0" : "+inf.0"); } else { - xfprintf(file, "%f", pic_float(obj)); + xfprintf(pic, file, "%f", pic_float(obj)); } break; #endif case PIC_TT_INT: - xfprintf(file, "%d", pic_int(obj)); + xfprintf(pic, file, "%d", pic_int(obj)); break; case PIC_TT_EOF: - xfprintf(file, "#.(eof-object)"); + xfprintf(pic, file, "#.(eof-object)"); break; case PIC_TT_STRING: if (p->mode == DISPLAY_MODE) { - xfprintf(file, "%s", pic_str_cstr(pic, pic_str_ptr(obj))); + xfprintf(pic, file, "%s", pic_str_cstr(pic, pic_str_ptr(obj))); break; } - xfprintf(file, "\""); + xfprintf(pic, file, "\""); write_str(pic, pic_str_ptr(obj), file); - xfprintf(file, "\""); + xfprintf(pic, file, "\""); break; case PIC_TT_VECTOR: - xfprintf(file, "#("); + xfprintf(pic, 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(pic, file, " "); } } - xfprintf(file, ")"); + xfprintf(pic, file, ")"); break; case PIC_TT_BLOB: - xfprintf(file, "#u8("); + xfprintf(pic, file, "#u8("); for (i = 0; i < pic_blob_ptr(obj)->len; ++i) { - xfprintf(file, "%d", pic_blob_ptr(obj)->data[i]); + xfprintf(pic, file, "%d", pic_blob_ptr(obj)->data[i]); if (i + 1 < pic_blob_ptr(obj)->len) { - xfprintf(file, " "); + xfprintf(pic, file, " "); } } - xfprintf(file, ")"); + xfprintf(pic, file, ")"); break; case PIC_TT_DICT: - xfprintf(file, "#.(dictionary"); + xfprintf(pic, file, "#.(dictionary"); for (it = xh_begin(&pic_dict_ptr(obj)->hash); it != NULL; it = xh_next(it)) { - xfprintf(file, " '%s ", pic_symbol_name(pic, xh_key(it, pic_sym *))); + xfprintf(pic, file, " '%s ", pic_symbol_name(pic, xh_key(it, pic_sym *))); write_core(p, xh_val(it, pic_value)); } - xfprintf(file, ")"); + xfprintf(pic, file, ")"); break; case PIC_TT_ID: - xfprintf(file, "#", pic_symbol_name(pic, pic_var_name(pic, obj))); + xfprintf(pic, file, "#", pic_symbol_name(pic, pic_var_name(pic, obj))); break; default: - xfprintf(file, "#<%s %p>", pic_type_repr(pic_type(obj)), pic_ptr(obj)); + xfprintf(pic, file, "#<%s %p>", pic_type_repr(pic_type(obj)), pic_ptr(obj)); break; } } @@ -377,7 +378,7 @@ pic_value pic_fwrite(pic_state *pic, pic_value obj, xFILE *file) { write(pic, obj, file); - xfflush(file); + xfflush(pic, file); return obj; } @@ -391,7 +392,7 @@ pic_value pic_fdisplay(pic_state *pic, pic_value obj, xFILE *file) { display(pic, obj, file); - xfflush(file); + xfflush(pic, file); return obj; } @@ -408,8 +409,8 @@ pic_printf(pic_state *pic, const char *fmt, ...) va_end(ap); - xfprintf(file, "%s", pic_str_cstr(pic, str)); - xfflush(file); + xfprintf(pic, file, "%s", pic_str_cstr(pic, str)); + xfflush(pic, file); } static pic_value From e43a9c78818a64ca08504ff232891a1eab92e5e6 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Jun 2015 02:29:17 +0900 Subject: [PATCH 3/9] pass pic_state object to vtable functions --- extlib/benz/file.c | 24 ++++++++++++++---------- extlib/benz/include/picrin/file.h | 10 +++++----- extlib/benz/port.c | 18 ++++++++---------- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/extlib/benz/file.c b/extlib/benz/file.c index 7a4908f3..23a78f45 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -1,6 +1,7 @@ #include "picrin.h" -static int file_read(void *cookie, char *ptr, int size) { +static int +file_read(pic_state PIC_UNUSED(*pic), void *cookie, char *ptr, int size) { FILE *file = cookie; int r; @@ -16,7 +17,8 @@ static int file_read(void *cookie, char *ptr, int size) { return r; } -static int file_write(void *cookie, const char *ptr, int size) { +static int +file_write(pic_state PIC_UNUSED(*pic), void *cookie, const char *ptr, int size) { FILE *file = cookie; int r; @@ -28,7 +30,8 @@ static int file_write(void *cookie, const char *ptr, int size) { return r; } -static long file_seek(void *cookie, long pos, int whence) { +static long +file_seek(pic_state PIC_UNUSED(*pic), void *cookie, long pos, int whence) { switch (whence) { case XSEEK_CUR: whence = SEEK_CUR; @@ -43,7 +46,8 @@ static long file_seek(void *cookie, long pos, int whence) { return fseek(cookie, pos, whence); } -static int file_close(void *cookie) { +static int +file_close(pic_state PIC_UNUSED(*pic), void *cookie) { return fclose(cookie); } @@ -70,7 +74,7 @@ xFILE x_iob[XOPEN_MAX] = { { { 0 }, 0, NULL, NULL, FILE_VTABLE, X_WRITE | X_UNBUF } }; -xFILE *xfunopen(void *cookie, int (*read)(void *, char *, int), int (*write)(void *, const char *, int), long (*seek)(void *, long, int), int (*close)(void *)) { +xFILE *xfunopen(void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *)) { xFILE *fp; for (fp = x_iob; fp < x_iob + XOPEN_MAX; fp++) @@ -98,7 +102,7 @@ int xfclose(pic_state *pic, xFILE *fp) { fp->flag = 0; if (fp->base != fp->buf) pic_free(pic, fp->base); - return fp->vtable.close(fp->vtable.cookie); + return fp->vtable.close(pic, fp->vtable.cookie); } int x_fillbuf(pic_state *pic, xFILE *fp) { @@ -121,7 +125,7 @@ int x_fillbuf(pic_state *pic, xFILE *fp) { bufsize = (fp->flag & X_UNBUF) ? sizeof(fp->buf) : XBUFSIZ; fp->ptr = fp->base; - fp->cnt = fp->vtable.read(fp->vtable.cookie, fp->ptr, bufsize); + fp->cnt = fp->vtable.read(pic, fp->vtable.cookie, fp->ptr, bufsize); if (--fp->cnt < 0) { if (fp->cnt == -1) @@ -157,7 +161,7 @@ int x_flushbuf(pic_state *pic, int x, xFILE *fp) { fp->cnt = 0; if (x == EOF) return EOF; - num_written = fp->vtable.write(fp->vtable.cookie, (const char *) &c, 1); + num_written = fp->vtable.write(pic, fp->vtable.cookie, (const char *) &c, 1); bufsize = 1; } else { /* buffered write */ @@ -168,7 +172,7 @@ int x_flushbuf(pic_state *pic, int x, xFILE *fp) { bufsize = (int)(fp->ptr - fp->base); while(bufsize - num_written > 0) { int t; - t = fp->vtable.write(fp->vtable.cookie, fp->base + num_written, bufsize - num_written); + t = fp->vtable.write(pic, fp->vtable.cookie, fp->base + num_written, bufsize - num_written); if (t < 0) break; num_written += t; @@ -334,7 +338,7 @@ long xfseek(pic_state *pic, xFILE *fp, long offset, int whence) { fp->ptr = fp->base; fp->cnt = 0; - if ((s = fp->vtable.seek(fp->vtable.cookie, offset, whence)) != 0) + if ((s = fp->vtable.seek(pic, fp->vtable.cookie, offset, whence)) != 0) return s; fp->flag &= ~X_EOF; return 0; diff --git a/extlib/benz/include/picrin/file.h b/extlib/benz/include/picrin/file.h index 189bd3de..2b761184 100644 --- a/extlib/benz/include/picrin/file.h +++ b/extlib/benz/include/picrin/file.h @@ -23,10 +23,10 @@ struct xFILE { /* operators */ struct { void *cookie; - int (*read)(void *, char *, int); - int (*write)(void *, const char *, int); - long (*seek)(void *, long, int); - int (*close)(void *); + int (*read)(pic_state *, void *, char *, int); + int (*write)(pic_state *, void *, const char *, int); + long (*seek)(pic_state *, void *, long, int); + int (*close)(pic_state *, void *); } vtable; int flag; /* mode of the file access */ }; @@ -63,7 +63,7 @@ enum _flags { #define xputchar(pic, x) xputc((pic), (x), xstdout) /* resource aquisition */ -xFILE *xfunopen(void *cookie, int (*read)(void *, char *, int), int (*write)(void *, const char *, int), long (*seek)(void *, long, int), int (*close)(void *)); +xFILE *xfunopen(void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *)); xFILE *xfopen(const char *, const char *); int xfclose(pic_state *, xFILE *); diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 8a0d7df4..867626f8 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -54,13 +54,12 @@ DEFINE_STANDARD_PORT_ACCESSOR(pic_stdout, "current-output-port") DEFINE_STANDARD_PORT_ACCESSOR(pic_stderr, "current-error-port") struct strfile { - pic_state *pic; char *buf; long pos, end, capa; }; static int -string_read(void *cookie, char *ptr, int size) +string_read(pic_state PIC_UNUSED(*pic), void *cookie, char *ptr, int size) { struct strfile *m = cookie; @@ -72,13 +71,13 @@ string_read(void *cookie, char *ptr, int size) } static int -string_write(void *cookie, const char *ptr, int size) +string_write(pic_state *pic, void *cookie, const char *ptr, int size) { struct strfile *m = cookie; if (m->pos + size >= m->capa) { m->capa = (m->pos + size) * 2; - m->buf = pic_realloc(m->pic, m->buf, (size_t)m->capa); + m->buf = pic_realloc(pic, m->buf, (size_t)m->capa); } memcpy(m->buf + m->pos, ptr, size); m->pos += size; @@ -88,7 +87,7 @@ string_write(void *cookie, const char *ptr, int size) } static long -string_seek(void *cookie, long pos, int whence) +string_seek(pic_state PIC_UNUSED(*pic), void *cookie, long pos, int whence) { struct strfile *m = cookie; @@ -108,12 +107,12 @@ string_seek(void *cookie, long pos, int whence) } static int -string_close(void *cookie) +string_close(pic_state *pic, void *cookie) { struct strfile *m = cookie; - pic_free(m->pic, m->buf); - pic_free(m->pic, m); + pic_free(pic, m->buf); + pic_free(pic, m); return 0; } @@ -124,7 +123,6 @@ string_open(pic_state *pic, const char *data, size_t size) xFILE *file; m = pic_malloc(pic, sizeof(struct strfile)); - m->pic = pic; m->buf = pic_malloc(pic, size); m->pos = 0; m->end = size; @@ -139,7 +137,7 @@ string_open(pic_state *pic, const char *data, size_t size) } if (file == NULL) { - string_close(m); + string_close(pic, m); pic_error(pic, "could not open new output string/bytevector port", pic_nil_value()); } return file; From 78bd3047f80efecc45fef791930c768d6d516938 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Jun 2015 03:06:57 +0900 Subject: [PATCH 4/9] don't use global mutable variable! --- contrib/05.r7rs/src/file.c | 2 +- contrib/05.r7rs/src/load.c | 2 +- extlib/benz/file.c | 16 ++++++++-------- extlib/benz/include/picrin.h | 11 ++++++----- extlib/benz/include/picrin/file.h | 16 ++++++++-------- extlib/benz/port.c | 4 ++-- extlib/benz/state.c | 6 ++++++ 7 files changed, 32 insertions(+), 25 deletions(-) diff --git a/contrib/05.r7rs/src/file.c b/contrib/05.r7rs/src/file.c index 36e50e86..c354c179 100644 --- a/contrib/05.r7rs/src/file.c +++ b/contrib/05.r7rs/src/file.c @@ -20,7 +20,7 @@ generic_open_file(pic_state *pic, const char *fname, char *mode, short flags) struct pic_port *port; xFILE *file; - file = xfopen(fname, mode); + file = xfopen(pic, fname, mode); if (! file) { file_error(pic, "could not open file"); } diff --git a/contrib/05.r7rs/src/load.c b/contrib/05.r7rs/src/load.c index 385767d8..8f519327 100644 --- a/contrib/05.r7rs/src/load.c +++ b/contrib/05.r7rs/src/load.c @@ -10,7 +10,7 @@ pic_load(pic_state *pic, const char *filename) struct pic_port *port; xFILE *file; - file = xfopen(filename, "r"); + file = xfopen(pic, filename, "r"); if (file == NULL) { pic_errorf(pic, "could not open file: %s", filename); } diff --git a/extlib/benz/file.c b/extlib/benz/file.c index 23a78f45..146f63e6 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -51,7 +51,7 @@ file_close(pic_state PIC_UNUSED(*pic), void *cookie) { return fclose(cookie); } -xFILE *xfopen(const char *name, const char *mode) { +xFILE *xfopen(pic_state *pic, const char *name, const char *mode) { FILE *fp; if ((fp = fopen(name, mode)) == NULL) { @@ -60,28 +60,28 @@ xFILE *xfopen(const char *name, const char *mode) { switch (*mode) { case 'r': - return xfunopen(fp, file_read, NULL, file_seek, file_close); + return xfunopen(pic, fp, file_read, NULL, file_seek, file_close); default: - return xfunopen(fp, NULL, file_write, file_seek, file_close); + return xfunopen(pic, fp, NULL, file_write, file_seek, file_close); } } #define FILE_VTABLE { 0, file_read, file_write, file_seek, file_close } -xFILE x_iob[XOPEN_MAX] = { +const xFILE x_iob[XOPEN_MAX] = { { { 0 }, 0, NULL, NULL, FILE_VTABLE, X_READ }, { { 0 }, 0, NULL, NULL, FILE_VTABLE, X_WRITE | X_LNBUF }, { { 0 }, 0, NULL, NULL, FILE_VTABLE, X_WRITE | X_UNBUF } }; -xFILE *xfunopen(void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *)) { +xFILE *xfunopen(pic_state *pic, void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *)) { xFILE *fp; - for (fp = x_iob; fp < x_iob + XOPEN_MAX; fp++) + for (fp = pic->files; fp < pic->files + XOPEN_MAX; fp++) if ((fp->flag & (X_READ | X_WRITE)) == 0) break; /* found free slot */ - if (fp >= x_iob + XOPEN_MAX) /* no free slots */ + if (fp >= pic->files + XOPEN_MAX) /* no free slots */ return NULL; fp->cnt = 0; @@ -198,7 +198,7 @@ int xfflush(pic_state *pic, xFILE *f) { if (f == NULL) { /* flush all output streams */ for (i = 0; i < XOPEN_MAX; i++) { - if ((x_iob[i].flag & X_WRITE) && (xfflush(pic, &x_iob[i]) == -1)) + if ((pic->files[i].flag & X_WRITE) && (xfflush(pic, &pic->files[i]) == -1)) retval = -1; } } else { diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 8eb641fe..f250305a 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -46,6 +46,9 @@ extern "C" { #include "picrin/value.h" typedef struct pic_code pic_code; +typedef struct pic_state pic_state; + +#include "picrin/file.h" typedef struct pic_jmpbuf { PIC_JMPBUF buf; @@ -71,9 +74,7 @@ typedef struct { typedef void *(*pic_allocf)(void *, size_t); -typedef struct xFILE xFILE; - -typedef struct { +struct pic_state { int argc; char **argv, **envp; @@ -137,6 +138,7 @@ typedef struct { struct pic_reg *attrs; struct pic_reader *reader; + xFILE files[XOPEN_MAX]; bool gc_enable; struct pic_heap *heap; @@ -148,7 +150,7 @@ typedef struct { pic_code *iseq; /* for pic_apply_trampoline */ char *native_stack_start; -} pic_state; +}; typedef pic_value (*pic_func_t)(pic_state *); @@ -283,7 +285,6 @@ pic_value pic_fdisplay(pic_state *, pic_value, xFILE *); #include "picrin/read.h" #include "picrin/vector.h" #include "picrin/reg.h" -#include "picrin/file.h" #if defined(__cplusplus) } diff --git a/extlib/benz/include/picrin/file.h b/extlib/benz/include/picrin/file.h index 2b761184..22af7f8e 100644 --- a/extlib/benz/include/picrin/file.h +++ b/extlib/benz/include/picrin/file.h @@ -14,7 +14,7 @@ extern "C" { #define XBUFSIZ 1024 #define XOPEN_MAX 1024 -struct xFILE { +typedef struct { /* buffer */ char buf[1]; /* fallback buffer */ long cnt; /* characters left */ @@ -29,13 +29,13 @@ struct xFILE { int (*close)(pic_state *, void *); } vtable; int flag; /* mode of the file access */ -}; +} xFILE; -extern xFILE x_iob[XOPEN_MAX]; +#define xstdin (&pic->files[0]) +#define xstdout (&pic->files[1]) +#define xstderr (&pic->files[2]) -#define xstdin (x_iob[0].vtable.cookie || (x_iob[0].vtable.cookie = stdin ), &x_iob[0]) -#define xstdout (x_iob[1].vtable.cookie || (x_iob[1].vtable.cookie = stdout), &x_iob[1]) -#define xstderr (x_iob[2].vtable.cookie || (x_iob[2].vtable.cookie = stderr), &x_iob[2]) +extern const xFILE x_iob[XOPEN_MAX]; enum _flags { X_READ = 01, @@ -63,8 +63,8 @@ enum _flags { #define xputchar(pic, x) xputc((pic), (x), xstdout) /* resource aquisition */ -xFILE *xfunopen(void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *)); -xFILE *xfopen(const char *, const char *); +xFILE *xfunopen(pic_state *, void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *)); +xFILE *xfopen(pic_state *, const char *, const char *); int xfclose(pic_state *, xFILE *); /* buffer management */ diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 867626f8..c14ce632 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -131,9 +131,9 @@ string_open(pic_state *pic, const char *data, size_t size) if (data != NULL) { memcpy(m->buf, data, size); - file = xfunopen(m, string_read, NULL, string_seek, string_close); + file = xfunopen(pic, m, string_read, NULL, string_seek, string_close); } else { - file = xfunopen(m, NULL, string_write, string_seek, string_close); + file = xfunopen(pic, m, NULL, string_write, string_seek, string_close); } if (file == NULL) { diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 7a355e47..fc0877ff 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -254,6 +254,12 @@ pic_open(int argc, char *argv[], char **envp, pic_allocf allocf) /* raised error object */ pic->err = pic_invalid_value(); + /* file pool */ + memcpy(pic->files, x_iob, sizeof pic->files); + pic->files[0].vtable.cookie = stdin; + pic->files[1].vtable.cookie = stdout; + pic->files[2].vtable.cookie = stderr; + /* parameter table */ pic->ptable = pic_nil_value(); From db0767c93185024341654fa84f2581995d081cb9 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Jun 2015 03:10:11 +0900 Subject: [PATCH 5/9] don't malloc pic->iseq --- extlib/benz/include/picrin.h | 5 ++--- extlib/benz/include/picrin/irep.h | 4 ++-- extlib/benz/state.c | 12 ------------ 3 files changed, 4 insertions(+), 17 deletions(-) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index f250305a..fcafe4c2 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -45,9 +45,9 @@ extern "C" { #include "picrin/value.h" -typedef struct pic_code pic_code; typedef struct pic_state pic_state; +#include "picrin/irep.h" #include "picrin/file.h" typedef struct pic_jmpbuf { @@ -139,6 +139,7 @@ struct pic_state { struct pic_reader *reader; xFILE files[XOPEN_MAX]; + pic_code iseq[2]; /* for pic_apply_trampoline */ bool gc_enable; struct pic_heap *heap; @@ -148,7 +149,6 @@ struct pic_state { pic_value err; - pic_code *iseq; /* for pic_apply_trampoline */ char *native_stack_start; }; @@ -272,7 +272,6 @@ pic_value pic_fdisplay(pic_state *, pic_value, xFILE *); #include "picrin/dict.h" #include "picrin/error.h" #include "picrin/gc.h" -#include "picrin/irep.h" #include "picrin/lib.h" #include "picrin/macro.h" #include "picrin/pair.h" diff --git a/extlib/benz/include/picrin/irep.h b/extlib/benz/include/picrin/irep.h index 319d1b31..a71d14f6 100644 --- a/extlib/benz/include/picrin/irep.h +++ b/extlib/benz/include/picrin/irep.h @@ -49,7 +49,7 @@ enum pic_opcode { OP_STOP }; -struct pic_code { +typedef struct { enum pic_opcode insn; union { int i; @@ -59,7 +59,7 @@ struct pic_code { int idx; } r; } u; -}; +} pic_code; #define PIC_INIT_CODE_I(code, op, ival) do { \ code.insn = op; \ diff --git a/extlib/benz/state.c b/extlib/benz/state.c index fc0877ff..3f6f5ba9 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -216,13 +216,6 @@ pic_open(int argc, char *argv[], char **envp, pic_allocf allocf) goto EXIT_ARENA; } - /* trampoline iseq */ - pic->iseq = allocf(NULL, 2 * sizeof(pic_code)); - - if (! pic->iseq) { - goto EXIT_ISEQ; - } - /* memory heap */ pic->heap = pic_heap_open(pic); @@ -393,8 +386,6 @@ pic_open(int argc, char *argv[], char **envp, pic_allocf allocf) return pic; - EXIT_ISEQ: - allocf(pic->arena, 0); EXIT_ARENA: allocf(pic->xp, 0); EXIT_XP: @@ -456,9 +447,6 @@ pic_close(pic_state *pic) allocf(pic->cibase, 0); allocf(pic->xpbase, 0); - /* free trampoline iseq */ - allocf(pic->iseq, 0); - /* free global stacks */ xh_destroy(&pic->syms); From 10f81512d85b064579456187d98c5daf5a08d4d2 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Jun 2015 03:14:55 +0900 Subject: [PATCH 6/9] don't malloc pic_reader --- extlib/benz/include/picrin.h | 5 ++-- extlib/benz/include/picrin/read.h | 8 +++--- extlib/benz/read.c | 47 ++++++++++++++----------------- extlib/benz/state.c | 4 +-- 4 files changed, 29 insertions(+), 35 deletions(-) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index fcafe4c2..a95a6935 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -49,6 +49,7 @@ typedef struct pic_state pic_state; #include "picrin/irep.h" #include "picrin/file.h" +#include "picrin/read.h" typedef struct pic_jmpbuf { PIC_JMPBUF buf; @@ -137,7 +138,7 @@ struct pic_state { pic_value libs; struct pic_reg *attrs; - struct pic_reader *reader; + pic_reader reader; xFILE files[XOPEN_MAX]; pic_code iseq[2]; /* for pic_apply_trampoline */ @@ -277,11 +278,9 @@ pic_value pic_fdisplay(pic_state *, pic_value, xFILE *); #include "picrin/pair.h" #include "picrin/port.h" #include "picrin/proc.h" -#include "picrin/read.h" #include "picrin/record.h" #include "picrin/string.h" #include "picrin/symbol.h" -#include "picrin/read.h" #include "picrin/vector.h" #include "picrin/reg.h" diff --git a/extlib/benz/include/picrin/read.h b/extlib/benz/include/picrin/read.h index a3f01100..d9b0bb6e 100644 --- a/extlib/benz/include/picrin/read.h +++ b/extlib/benz/include/picrin/read.h @@ -11,7 +11,7 @@ extern "C" { typedef pic_value (*pic_reader_t)(pic_state *, struct pic_port *port, int c); -struct pic_reader { +typedef struct { enum pic_typecase { PIC_CASE_DEFAULT, PIC_CASE_FOLD @@ -19,10 +19,10 @@ struct pic_reader { xhash labels; pic_reader_t table[256]; pic_reader_t dispatch[256]; -}; +} pic_reader; -struct pic_reader *pic_reader_open(pic_state *); -void pic_reader_close(pic_state *, struct pic_reader *); +void pic_reader_init(pic_state *); +void pic_reader_destroy(pic_state *); #if defined(__cplusplus) } diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 45325ebf..bcadecaa 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -79,7 +79,7 @@ strcaseeq(const char *s1, const char *s2) static int case_fold(pic_state *pic, int c) { - if (pic->reader->typecase == PIC_CASE_FOLD) { + if (pic->reader.typecase == PIC_CASE_FOLD) { c = tolower(c); } return c; @@ -131,13 +131,13 @@ read_directive(pic_state *pic, struct pic_port *port, int c) switch (peek(pic, port)) { case 'n': if (expect(pic, port, "no-fold-case")) { - pic->reader->typecase = PIC_CASE_DEFAULT; + pic->reader.typecase = PIC_CASE_DEFAULT; return pic_invalid_value(); } break; case 'f': if (expect(pic, port, "fold-case")) { - pic->reader->typecase = PIC_CASE_FOLD; + pic->reader.typecase = PIC_CASE_FOLD; return pic_invalid_value(); } break; @@ -649,7 +649,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) val = pic_cons(pic, pic_undef_value(), pic_undef_value()); - xh_put_int(&pic->reader->labels, i, &val); + xh_put_int(&pic->reader.labels, i, &val); tmp = read(pic, port, c); pic_pair_ptr(val)->car = pic_car(pic, tmp); @@ -672,7 +672,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) val = pic_obj_value(pic_make_vec(pic, 0)); - xh_put_int(&pic->reader->labels, i, &val); + xh_put_int(&pic->reader.labels, i, &val); tmp = pic_vec_ptr(read(pic, port, c)); PIC_SWAP(pic_value *, tmp->data, pic_vec_ptr(val)->data); @@ -687,7 +687,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) { val = read(pic, port, c); - xh_put_int(&pic->reader->labels, i, &val); + xh_put_int(&pic->reader.labels, i, &val); return val; } @@ -699,7 +699,7 @@ read_label_ref(pic_state *pic, struct pic_port PIC_UNUSED(*port), int i) { xh_entry *e; - e = xh_get_int(&pic->reader->labels, i); + e = xh_get_int(&pic->reader.labels, i); if (! e) { read_error(pic, "label of given index not defined"); } @@ -740,11 +740,11 @@ read_dispatch(pic_state *pic, struct pic_port *port, int c) read_error(pic, "unexpected EOF"); } - if (pic->reader->dispatch[c] == NULL) { + if (pic->reader.dispatch[c] == NULL) { read_error(pic, "invalid character at the seeker head"); } - return pic->reader->dispatch[c](pic, port, c); + return pic->reader.dispatch[c](pic, port, c); } static pic_value @@ -756,11 +756,11 @@ read_nullable(pic_state *pic, struct pic_port *port, int c) read_error(pic, "unexpected EOF"); } - if (pic->reader->table[c] == NULL) { + if (pic->reader.table[c] == NULL) { read_error(pic, "invalid character at the seeker head"); } - return pic->reader->table[c](pic, port, c); + return pic->reader.table[c](pic, port, c); } static pic_value @@ -780,7 +780,7 @@ read(pic_state *pic, struct pic_port *port, int c) } static void -reader_table_init(struct pic_reader *reader) +reader_table_init(pic_reader *reader) { int c; @@ -826,34 +826,29 @@ reader_table_init(struct pic_reader *reader) } } -struct pic_reader * -pic_reader_open(pic_state *pic) +void +pic_reader_init(pic_state *pic) { - struct pic_reader *reader; int c; - reader = pic_malloc(pic, sizeof(struct pic_reader)); - reader->typecase = PIC_CASE_DEFAULT; - xh_init_int(&reader->labels, sizeof(pic_value)); + pic->reader.typecase = PIC_CASE_DEFAULT; + xh_init_int(&pic->reader.labels, sizeof(pic_value)); for (c = 0; c < 256; ++c) { - reader->table[c] = NULL; + pic->reader.table[c] = NULL; } for (c = 0; c < 256; ++c) { - reader->dispatch[c] = NULL; + pic->reader.dispatch[c] = NULL; } - reader_table_init(reader); - - return reader; + reader_table_init(&pic->reader); } void -pic_reader_close(pic_state *pic, struct pic_reader *reader) +pic_reader_destroy(pic_state *pic) { - xh_destroy(&reader->labels); - pic_free(pic, reader); + xh_destroy(&pic->reader.labels); } pic_value diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 3f6f5ba9..6c9545fb 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -364,7 +364,7 @@ pic_open(int argc, char *argv[], char **envp, pic_allocf allocf) pic->cp->in = pic->cp->out = NULL; /* reader */ - pic->reader = pic_reader_open(pic); + pic_reader_init(pic); /* parameter table */ pic->ptable = pic_cons(pic, pic_obj_value(pic_make_dict(pic)), pic->ptable); @@ -440,7 +440,7 @@ pic_close(pic_state *pic) pic_heap_close(pic, pic->heap); /* free reader struct */ - pic_reader_close(pic, pic->reader); + pic_reader_destroy(pic); /* free runtime context */ allocf(pic->stbase, 0); From 19c09ba6433c3026b63934259a005e1709b8dffb Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Jun 2015 03:23:07 +0900 Subject: [PATCH 7/9] move contents of util.h to compat.h --- extlib/benz/include/picrin.h | 7 +-- extlib/benz/include/picrin/compat.h | 75 +++++++++++++++++++++++++ extlib/benz/include/picrin/util.h | 86 ----------------------------- 3 files changed, 76 insertions(+), 92 deletions(-) delete mode 100644 extlib/benz/include/picrin/util.h diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index a95a6935..0c2ef296 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -33,13 +33,8 @@ extern "C" { #include #include "picrin/config.h" -#include "picrin/util.h" + #include "picrin/compat.h" - -#if PIC_ENABLE_FLOAT -# include -#endif - #include "picrin/xvect.h" #include "picrin/xhash.h" diff --git a/extlib/benz/include/picrin/compat.h b/extlib/benz/include/picrin/compat.h index cca83d95..24f82691 100644 --- a/extlib/benz/include/picrin/compat.h +++ b/extlib/benz/include/picrin/compat.h @@ -9,6 +9,77 @@ extern "C" { #endif +#if __STDC_VERSION__ >= 199901L +# include +#else +# define bool char +# define true 1 +# define false 0 +#endif + +#if __STDC_VERSION__ >= 199901L +# include +#elif ! defined(offsetof) +# define offsetof(s,m) ((size_t)&(((s *)NULL)->m)) +#endif + +#if __STDC_VERSION__ >= 201112L +# include +# define PIC_NORETURN noreturn +#elif __GNUC__ || __clang__ +# define PIC_NORETURN __attribute__((noreturn)) +#else +# define PIC_NORETURN +#endif + +#if __STDC_VERSION__ >= 199901L +# define PIC_INLINE static inline +#elif __GNUC__ || __clang__ +# define PIC_INLINE static __inline__ +#else +# define PIC_INLINE static +#endif + +#define PIC_FALLTHROUGH ((void)0) + +#if __GNUC__ || __clang__ +# define PIC_UNUSED(v) __attribute__((unused)) v +#else +# define PIC_UNUSED(v) v +#endif + +#define PIC_GENSYM2_(x,y) PIC_G##x##_##y##_ +#define PIC_GENSYM1_(x,y) PIC_GENSYM2_(x,y) +#if defined(__COUNTER__) +# define PIC_GENSYM(x) PIC_GENSYM1_(__COUNTER__,x) +#else +# define PIC_GENSYM(x) PIC_GENSYM1_(__LINE__,x) +#endif + +#if __GNUC__ +# define GCC_VERSION (__GNUC__ * 10000 \ + + __GNUC_MINOR__ * 100 \ + + __GNUC_PATCHLEVEL__) +#endif +#if GCC_VERSION >= 40500 || __clang__ +# define PIC_UNREACHABLE() (__builtin_unreachable()) +#else +# define PIC_UNREACHABLE() (assert(false)) +#endif +#if __GNUC__ +# undef GCC_VERSION +#endif + +#define PIC_SWAP(type,a,b) \ + PIC_SWAP_HELPER_(type, PIC_GENSYM(tmp), a, b) +#define PIC_SWAP_HELPER_(type,tmp,a,b) \ + do { \ + type tmp = (a); \ + (a) = (b); \ + (b) = tmp; \ + } while (0) + + #if PIC_ENABLE_LIBC #include @@ -134,6 +205,10 @@ strcpy(char *dst, const char *src) #endif +#if PIC_ENABLE_FLOAT +# include +#endif + #if defined(__cplusplus) } #endif diff --git a/extlib/benz/include/picrin/util.h b/extlib/benz/include/picrin/util.h deleted file mode 100644 index ad816c70..00000000 --- a/extlib/benz/include/picrin/util.h +++ /dev/null @@ -1,86 +0,0 @@ -/** - * See Copyright Notice in picrin.h - */ - -#ifndef PICRIN_UTIL_H -#define PICRIN_UTIL_H - -#if defined(__cplusplus) -extern "C" { -#endif - -#if __STDC_VERSION__ >= 199901L -# include -#else -# define bool char -# define true 1 -# define false 0 -#endif - -#if __STDC_VERSION__ >= 199901L -# include -#elif ! defined(offsetof) -# define offsetof(s,m) ((size_t)&(((s *)NULL)->m)) -#endif - -#if __STDC_VERSION__ >= 201112L -# include -# define PIC_NORETURN noreturn -#elif __GNUC__ || __clang__ -# define PIC_NORETURN __attribute__((noreturn)) -#else -# define PIC_NORETURN -#endif - -#if __STDC_VERSION__ >= 199901L -# define PIC_INLINE static inline -#elif __GNUC__ || __clang__ -# define PIC_INLINE static __inline__ -#else -# define PIC_INLINE static -#endif - -#define PIC_FALLTHROUGH ((void)0) - -#if __GNUC__ || __clang__ -# define PIC_UNUSED(v) __attribute__((unused)) v -#else -# define PIC_UNUSED(v) v -#endif - -#define PIC_GENSYM2_(x,y) PIC_G##x##_##y##_ -#define PIC_GENSYM1_(x,y) PIC_GENSYM2_(x,y) -#if defined(__COUNTER__) -# define PIC_GENSYM(x) PIC_GENSYM1_(__COUNTER__,x) -#else -# define PIC_GENSYM(x) PIC_GENSYM1_(__LINE__,x) -#endif - -#if __GNUC__ -# define GCC_VERSION (__GNUC__ * 10000 \ - + __GNUC_MINOR__ * 100 \ - + __GNUC_PATCHLEVEL__) -#endif -#if GCC_VERSION >= 40500 || __clang__ -# define PIC_UNREACHABLE() (__builtin_unreachable()) -#else -# define PIC_UNREACHABLE() (assert(false)) -#endif -#if __GNUC__ -# undef GCC_VERSION -#endif - -#define PIC_SWAP(type,a,b) \ - PIC_SWAP_HELPER_(type, PIC_GENSYM(tmp), a, b) -#define PIC_SWAP_HELPER_(type,tmp,a,b) \ - do { \ - type tmp = (a); \ - (a) = (b); \ - (b) = tmp; \ - } while (0) - -#if defined(__cplusplus) -} -#endif - -#endif From 4bc765da653cccd1e90a1ca55a896f04c10af662 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Jun 2015 03:27:03 +0900 Subject: [PATCH 8/9] move include of gc.h --- extlib/benz/include/picrin.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 0c2ef296..5deca03e 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -45,6 +45,7 @@ typedef struct pic_state pic_state; #include "picrin/irep.h" #include "picrin/file.h" #include "picrin/read.h" +#include "picrin/gc.h" typedef struct pic_jmpbuf { PIC_JMPBUF buf; @@ -267,7 +268,6 @@ pic_value pic_fdisplay(pic_state *, pic_value, xFILE *); #include "picrin/data.h" #include "picrin/dict.h" #include "picrin/error.h" -#include "picrin/gc.h" #include "picrin/lib.h" #include "picrin/macro.h" #include "picrin/pair.h" From cf037f27dbe0aee6dca12fbdef9a91ffc4dbc4a4 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Jun 2015 03:31:24 +0900 Subject: [PATCH 9/9] remove library-name --- extlib/benz/lib.c | 13 ------------- piclib/picrin/base.scm | 1 - 2 files changed, 14 deletions(-) diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index 7ffa66fe..227eea7f 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -168,18 +168,6 @@ pic_lib_library_export(pic_state *pic) return pic_undef_value(); } -static pic_value -pic_lib_library_name(pic_state *pic) -{ - pic_value lib; - - pic_get_args(pic, "o", &lib); - - pic_assert_type(pic, lib, lib); - - return pic_lib_ptr(lib)->name; -} - static pic_value pic_lib_library_exports(pic_state *pic) { @@ -215,7 +203,6 @@ pic_init_lib(pic_state *pic) { pic_defun(pic, "make-library", pic_lib_make_library); pic_defun(pic, "find-library", pic_lib_find_library); - pic_defun(pic, "library-name", pic_lib_library_name); pic_defun(pic, "library-exports", pic_lib_library_exports); pic_defun(pic, "library-environment", pic_lib_library_environment); diff --git a/piclib/picrin/base.scm b/piclib/picrin/base.scm index a9d6d7fa..f0d988a5 100644 --- a/piclib/picrin/base.scm +++ b/piclib/picrin/base.scm @@ -255,7 +255,6 @@ (export make-library find-library current-library - library-name library-exports library-environment)