reorganize files

This commit is contained in:
Yuichi Nishiwaki 2017-03-28 23:09:40 +09:00
parent 39b6103548
commit b58a81ca39
36 changed files with 149 additions and 217 deletions

View File

@ -8,15 +8,11 @@ LIBPICRIN_SRCS = \
lib/debug.c\ lib/debug.c\
lib/dict.c\ lib/dict.c\
lib/error.c\ lib/error.c\
lib/eval.c\
lib/gc.c\ lib/gc.c\
lib/lib.c\
lib/load.c\
lib/number.c\ lib/number.c\
lib/pair.c\ lib/pair.c\
lib/port.c\ lib/port.c\
lib/proc.c\ lib/proc.c\
lib/read.c\
lib/record.c\ lib/record.c\
lib/state.c\ lib/state.c\
lib/string.c\ lib/string.c\
@ -25,7 +21,11 @@ LIBPICRIN_SRCS = \
lib/var.c\ lib/var.c\
lib/vector.c\ lib/vector.c\
lib/weak.c\ lib/weak.c\
lib/write.c lib/ext/eval.c\
lib/ext/lib.c\
lib/ext/load.c\
lib/ext/read.c\
lib/ext/write.c
LIBPICRIN_OBJS = $(LIBPICRIN_SRCS:.c=.o) LIBPICRIN_OBJS = $(LIBPICRIN_SRCS:.c=.o)
PICRIN_SRCS = \ PICRIN_SRCS = \
@ -47,7 +47,7 @@ REPL_ISSUE_TESTS = $(wildcard t/issue/*.sh)
TEST_RUNNER = picrin TEST_RUNNER = picrin
CFLAGS += -I./lib/include -Wall -Wextra CFLAGS += -I./lib -I./lib/include -Wall -Wextra
LDFLAGS += -lm LDFLAGS += -lm
prefix ?= /usr/local prefix ?= /usr/local
@ -77,7 +77,7 @@ src/init_contrib.c:
lib/boot.c: piclib/boot.scm lib/boot.c: piclib/boot.scm
bin/picrin-bootstrap tools/mkboot.scm < piclib/boot.scm > lib/boot.c bin/picrin-bootstrap tools/mkboot.scm < piclib/boot.scm > lib/boot.c
$(LIBPICRIN_OBJS) $(PICRIN_OBJS) $(CONTRIB_OBJS): lib/include/picrin.h lib/include/picrin/*.h $(LIBPICRIN_OBJS) $(PICRIN_OBJS) $(CONTRIB_OBJS): lib/include/picrin.h lib/include/picrin/*.h lib/khash.h lib/object.h lib/state.h lib/vm.h
doc: docs/*.rst docs/contrib.rst doc: docs/*.rst docs/contrib.rst
$(MAKE) -C docs html $(MAKE) -C docs html
@ -94,8 +94,8 @@ test: test-contribs test-nostdlib test-issue
test-contribs: picrin $(CONTRIB_TESTS) test-contribs: picrin $(CONTRIB_TESTS)
test-nostdlib: test-nostdlib: lib/boot.c
$(CC) -I./lib/include -D'PIC_USE_LIBC=0' -D'PIC_USE_STDIO=0' -D'PIC_USE_WRITE=0' -ffreestanding -nostdlib -Os -fPIC -shared -std=c89 -pedantic -Wall -Wextra -Werror -o libpicrin-tiny.so $(LIBPICRIN_SRCS) etc/libc_polyfill.c -fno-stack-protector $(CC) -I./lib -I./lib/include -D'PIC_USE_LIBC=0' -D'PIC_USE_STDIO=0' -D'PIC_USE_WRITE=0' -ffreestanding -nostdlib -Os -fPIC -shared -std=c89 -pedantic -Wall -Wextra -Werror -o libpicrin-tiny.so $(LIBPICRIN_SRCS) etc/libc_polyfill.c -fno-stack-protector
strip libpicrin-tiny.so strip libpicrin-tiny.so
ls -lh libpicrin-tiny.so ls -lh libpicrin-tiny.so
rm -f libpicrin-tiny.so rm -f libpicrin-tiny.so

View File

@ -1,6 +1,6 @@
#include "picrin.h" #include "picrin.h"
#include "picrin/private/object.h" #include "object.h"
#include "picrin/private/state.h" #include "state.h"
struct fullcont { struct fullcont {
jmp_buf jmp; jmp_buf jmp;

View File

@ -3,8 +3,7 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h" #include "object.h"
#include "picrin/private/object.h"
pic_value pic_value
pic_blob_value(pic_state *pic, const unsigned char *buf, int len) pic_blob_value(pic_state *pic, const unsigned char *buf, int len)

View File

@ -3,7 +3,7 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/private/object.h" #include "object.h"
#if PIC_NAN_BOXING #if PIC_NAN_BOXING
@ -226,7 +226,7 @@ pic_bool_not(pic_state *pic)
pic_get_args(pic, "o", &v); pic_get_args(pic, "o", &v);
return pic_false_p(pic, v) ? pic_true_value(pic) : pic_false_value(pic); return pic_bool_value(pic, pic_false_p(pic, v));
} }
static pic_value static pic_value
@ -236,7 +236,7 @@ pic_bool_boolean_p(pic_state *pic)
pic_get_args(pic, "o", &v); pic_get_args(pic, "o", &v);
return (pic_true_p(pic, v) || pic_false_p(pic, v)) ? pic_true_value(pic) : pic_false_value(pic); return pic_bool_value(pic, pic_bool_p(pic, v));
} }
static pic_value static pic_value
@ -264,9 +264,7 @@ pic_init_bool(pic_state *pic)
pic_defun(pic, "eq?", pic_bool_eq_p); pic_defun(pic, "eq?", pic_bool_eq_p);
pic_defun(pic, "eqv?", pic_bool_eqv_p); pic_defun(pic, "eqv?", pic_bool_eqv_p);
pic_defun(pic, "equal?", pic_bool_equal_p); pic_defun(pic, "equal?", pic_bool_equal_p);
pic_defun(pic, "not", pic_bool_not); pic_defun(pic, "not", pic_bool_not);
pic_defun(pic, "boolean?", pic_bool_boolean_p); pic_defun(pic, "boolean?", pic_bool_boolean_p);
pic_defun(pic, "boolean=?", pic_bool_boolean_eq_p); pic_defun(pic, "boolean=?", pic_bool_boolean_eq_p);
} }

View File

@ -3,8 +3,7 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h" #include "object.h"
#include "picrin/private/object.h"
static pic_value static pic_value
pic_char_char_p(pic_state *pic) pic_char_char_p(pic_state *pic)
@ -13,7 +12,7 @@ pic_char_char_p(pic_state *pic)
pic_get_args(pic, "o", &v); pic_get_args(pic, "o", &v);
return pic_char_p(pic, v) ? pic_true_value(pic) : pic_false_value(pic); return pic_bool_value(pic, pic_char_p(pic, v));
} }
static pic_value static pic_value

View File

@ -3,8 +3,8 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/private/object.h" #include "object.h"
#include "picrin/private/state.h" #include "state.h"
struct cont { struct cont {
PIC_JMPBUF *jmp; PIC_JMPBUF *jmp;

View File

@ -1,5 +1,9 @@
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h" #include "picrin.h"
#include "picrin/private/object.h" #include "object.h"
bool bool
pic_data_p(pic_state *pic, pic_value obj, const pic_data_type *type) pic_data_p(pic_state *pic, pic_value obj, const pic_data_type *type)

View File

@ -3,9 +3,8 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h" #include "object.h"
#include "picrin/private/object.h" #include "state.h"
#include "picrin/private/state.h"
pic_value pic_value
pic_get_backtrace(pic_state *pic) pic_get_backtrace(pic_state *pic)

View File

@ -3,8 +3,7 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h" #include "object.h"
#include "picrin/private/object.h"
KHASH_DEFINE(dict, symbol *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal) KHASH_DEFINE(dict, symbol *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal)

View File

@ -3,9 +3,8 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h" #include "object.h"
#include "picrin/private/object.h" #include "state.h"
#include "picrin/private/state.h"
void void
pic_panic(pic_state *pic, const char *msg) pic_panic(pic_state *pic, const char *msg)
@ -14,10 +13,6 @@ pic_panic(pic_state *pic, const char *msg)
pic->panicf(pic, msg); pic->panicf(pic, msg);
} }
#if PIC_USE_STDIO
fprintf(stderr, "picrin panic!: %s\n", msg);
#endif
PIC_ABORT(pic); PIC_ABORT(pic);
PIC_UNREACHABLE(); PIC_UNREACHABLE();
@ -296,7 +291,7 @@ pic_error_error_object_type(pic_state *pic)
void void
pic_init_error(pic_state *pic) pic_init_error(pic_state *pic)
{ {
pic_defvar(pic, "current-exception-handlers", pic_nil_value(pic), pic_false_value(pic)); pic_defvar(pic, "current-exception-handlers", pic_nil_value(pic));
pic_defun(pic, "with-exception-handler", pic_error_with_exception_handler); pic_defun(pic, "with-exception-handler", pic_error_with_exception_handler);
pic_defun(pic, "raise", pic_error_raise); pic_defun(pic, "raise", pic_error_raise);
pic_defun(pic, "raise-continuable", pic_error_raise_continuable); pic_defun(pic, "raise-continuable", pic_error_raise_continuable);

View File

@ -4,9 +4,9 @@
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h" #include "picrin/extra.h"
#include "picrin/private/object.h" #include "object.h"
#include "picrin/private/vm.h" #include "state.h"
#include "picrin/private/state.h" #include "vm.h"
static pic_value pic_compile(pic_state *, pic_value); static pic_value pic_compile(pic_state *, pic_value);

View File

@ -4,8 +4,8 @@
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h" #include "picrin/extra.h"
#include "picrin/private/object.h" #include "object.h"
#include "picrin/private/state.h" #include "state.h"
KHASH_DEFINE(env, struct identifier *, symbol *, kh_ptr_hash_func, kh_ptr_hash_equal) KHASH_DEFINE(env, struct identifier *, symbol *, kh_ptr_hash_func, kh_ptr_hash_equal)
KHASH_DEFINE(ltable, const char *, struct lib, kh_str_hash_func, kh_str_cmp_func) KHASH_DEFINE(ltable, const char *, struct lib, kh_str_hash_func, kh_str_cmp_func)

View File

@ -4,7 +4,7 @@
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h" #include "picrin/extra.h"
#include "picrin/private/object.h" #include "object.h"
#undef EOF #undef EOF
#define EOF (-1) #define EOF (-1)

View File

@ -4,7 +4,7 @@
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h" #include "picrin/extra.h"
#include "picrin/private/object.h" #include "object.h"
struct writer_control { struct writer_control {
int mode; int mode;

View File

@ -3,8 +3,8 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/private/object.h" #include "object.h"
#include "picrin/private/state.h" #include "state.h"
#define PAGE_UNITS ((PIC_HEAP_PAGE_SIZE - offsetof(struct heap_page, basep)) / sizeof(union header)) #define PAGE_UNITS ((PIC_HEAP_PAGE_SIZE - offsetof(struct heap_page, basep)) / sizeof(union header))

View File

@ -74,7 +74,7 @@ void pic_gc(pic_state *);
int pic_get_args(pic_state *, const char *fmt, ...); int pic_get_args(pic_state *, const char *fmt, ...);
void pic_defun(pic_state *, const char *name, pic_func_t f); void pic_defun(pic_state *, const char *name, pic_func_t f);
void pic_defvar(pic_state *, const char *name, pic_value v, pic_value conv); void pic_defvar(pic_state *, const char *name, pic_value v);
void pic_define(pic_state *, const char *lib, const char *name, pic_value v); void pic_define(pic_state *, const char *lib, const char *name, pic_value v);
pic_value pic_ref(pic_state *, const char *lib, const char *name); pic_value pic_ref(pic_state *, const char *lib, const char *name);
void pic_set(pic_state *, const char *lib, const char *name, pic_value v); void pic_set(pic_state *, const char *lib, const char *name, pic_value v);
@ -179,6 +179,7 @@ enum {
#define pic_float_p(pic,v) (pic_type(pic,v) == PIC_TYPE_FLOAT) #define pic_float_p(pic,v) (pic_type(pic,v) == PIC_TYPE_FLOAT)
#define pic_char_p(pic,v) (pic_type(pic,v) == PIC_TYPE_CHAR) #define pic_char_p(pic,v) (pic_type(pic,v) == PIC_TYPE_CHAR)
#define pic_eof_p(pic, v) (pic_type(pic, v) == PIC_TYPE_EOF) #define pic_eof_p(pic, v) (pic_type(pic, v) == PIC_TYPE_EOF)
#define pic_bool_p(pic,v) (pic_type(pic,v) == PIC_TYPE_TRUE || pic_type(pic,v) == PIC_TYPE_FALSE)
#define pic_true_p(pic,v) (pic_type(pic,v) == PIC_TYPE_TRUE) #define pic_true_p(pic,v) (pic_type(pic,v) == PIC_TYPE_TRUE)
#define pic_false_p(pic,v) (pic_type(pic,v) == PIC_TYPE_FALSE) #define pic_false_p(pic,v) (pic_type(pic,v) == PIC_TYPE_FALSE)
#define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ID || pic_type(pic, v) == PIC_TYPE_SYMBOL) #define pic_id_p(pic, v) (pic_type(pic, v) == PIC_TYPE_ID || pic_type(pic, v) == PIC_TYPE_SYMBOL)
@ -270,6 +271,10 @@ int pic_str_hash(pic_state *, pic_value str);
#define PIC_SEEK_END 1 #define PIC_SEEK_END 1
#define PIC_SEEK_SET 2 #define PIC_SEEK_SET 2
#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)
pic_value pic_funopen(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 *)); pic_value pic_funopen(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 *));
size_t pic_fread(pic_state *, void *ptr, size_t size, size_t count, pic_value port); size_t pic_fread(pic_state *, void *ptr, size_t size, size_t count, pic_value port);
size_t pic_fwrite(pic_state *, const void *ptr, size_t size, size_t count, pic_value port); size_t pic_fwrite(pic_state *, const void *ptr, size_t size, size_t count, pic_value port);
@ -291,6 +296,32 @@ int pic_printf(pic_state *, const char *fmt, ...);
int pic_fprintf(pic_state *, pic_value port, const char *fmt, ...); int pic_fprintf(pic_state *, pic_value port, const char *fmt, ...);
int pic_vfprintf(pic_state *, pic_value port, const char *fmt, va_list ap); int pic_vfprintf(pic_state *, pic_value port, const char *fmt, va_list ap);
pic_value pic_fmemopen(pic_state *, const char *buf, int len, const char *mode); /* deprecated */
int pic_fgetbuf(pic_state *, pic_value port, const char **buf, int *len); /* deprecated */
/* debug */
void pic_warnf(pic_state *, const char *, ...);
pic_value pic_get_backtrace(pic_state *);
/* utility macros */
#define pic_for_each(var, list, it) \
for (it = (list); ! pic_nil_p(pic, it); it = pic_cdr(pic, it)) \
if ((var = pic_car(pic, it)), true)
#define pic_push(pic, item, place) (place = pic_cons(pic, item, place))
#define pic_pop(pic, place) (place = pic_cdr(pic, place))
#define pic_void(pic, exec) pic_void_(pic, PIC_GENSYM(ai), exec)
#define pic_void_(pic,ai,exec) do { \
size_t ai = pic_enter(pic); \
exec; \
pic_leave(pic, ai); \
} while (0)
#if defined(__cplusplus) #if defined(__cplusplus)
} }

View File

@ -23,31 +23,9 @@ pic_value pic_eval(pic_state *, pic_value program, const char *lib);
void pic_load(pic_state *, pic_value port); void pic_load(pic_state *, pic_value port);
void pic_load_cstr(pic_state *, const char *); void pic_load_cstr(pic_state *, const char *);
#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)
#if PIC_USE_STDIO #if PIC_USE_STDIO
pic_value pic_fopen(pic_state *, FILE *, const char *mode); pic_value pic_fopen(pic_state *, FILE *, const char *mode);
#endif #endif
pic_value pic_fmemopen(pic_state *, const char *buf, int len, const char *mode);
int pic_fgetbuf(pic_state *, pic_value port, const char **buf, int *len);
/* utility macros */
#define pic_for_each(var, list, it) \
for (it = (list); ! pic_nil_p(pic, it); it = pic_cdr(pic, it)) \
if ((var = pic_car(pic, it)), true)
#define pic_push(pic, item, place) (place = pic_cons(pic, item, place))
#define pic_pop(pic, place) (place = pic_cdr(pic, place))
#define pic_void(pic, exec) pic_void_(pic, PIC_GENSYM(ai), exec)
#define pic_void_(pic,ai,exec) do { \
size_t ai = pic_enter(pic); \
exec; \
pic_leave(pic, ai); \
} while (0)
#define pic_deflibrary(pic, lib) do { \ #define pic_deflibrary(pic, lib) do { \
if (! pic_find_library(pic, lib)) { \ if (! pic_find_library(pic, lib)) { \
@ -78,8 +56,6 @@ int pic_fgetbuf(pic_state *, pic_value port, const char **buf, int *len);
/* for debug */ /* for debug */
void pic_warnf(pic_state *, const char *, ...);
pic_value pic_get_backtrace(pic_state *);
#if PIC_USE_WRITE #if PIC_USE_WRITE
void pic_print_error(pic_state *, pic_value port, pic_value err); void pic_print_error(pic_state *, pic_value port, pic_value err);
#endif #endif

View File

@ -1,39 +0,0 @@
#ifndef PICRIN_FILE_H
#define PICRIN_FILE_H
#if defined(__cplusplus)
extern "C" {
#endif
struct file {
/* buffer */
char buf[1]; /* fallback buffer */
long cnt; /* characters left */
char *ptr; /* next character position */
char *base; /* location of the buffer */
/* operators */
struct {
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 *);
} vtable;
int flag; /* mode of the file access */
};
enum {
FILE_READ = 01,
FILE_WRITE = 02,
FILE_UNBUF = 04,
FILE_EOF = 010,
FILE_ERR = 020,
FILE_LNBUF = 040
};
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -1,28 +0,0 @@
/**
* See Copyright Notice in picrin.h
*/
#ifndef PICRIN_GC_H
#define PICRIN_GC_H
#if defined(__cplusplus)
extern "C" {
#endif
#if PIC_BITMAP_GC
# define OBJECT_HEADER \
unsigned char tt;
#else
# define OBJECT_HEADER \
unsigned char tt; \
char gc_mark;
#endif
struct heap *pic_heap_open(pic_state *);
void pic_heap_close(pic_state *, struct heap *);
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -3,7 +3,6 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h"
static pic_value static pic_value
pic_number_number_p(pic_state *pic) pic_number_number_p(pic_state *pic)
@ -334,33 +333,20 @@ pic_number_string_to_number(pic_state *pic)
void void
pic_init_number(pic_state *pic) pic_init_number(pic_state *pic)
{ {
size_t ai = pic_enter(pic);
pic_defun(pic, "number?", pic_number_number_p); pic_defun(pic, "number?", pic_number_number_p);
pic_leave(pic, ai);
pic_defun(pic, "exact?", pic_number_exact_p); pic_defun(pic, "exact?", pic_number_exact_p);
pic_defun(pic, "inexact?", pic_number_inexact_p); pic_defun(pic, "inexact?", pic_number_inexact_p);
pic_leave(pic, ai);
pic_defun(pic, "inexact", pic_number_inexact); pic_defun(pic, "inexact", pic_number_inexact);
pic_defun(pic, "exact", pic_number_exact); pic_defun(pic, "exact", pic_number_exact);
pic_leave(pic, ai);
pic_defun(pic, "=", pic_number_eq); pic_defun(pic, "=", pic_number_eq);
pic_defun(pic, "<", pic_number_lt); pic_defun(pic, "<", pic_number_lt);
pic_defun(pic, ">", pic_number_gt); pic_defun(pic, ">", pic_number_gt);
pic_defun(pic, "<=", pic_number_le); pic_defun(pic, "<=", pic_number_le);
pic_defun(pic, ">=", pic_number_ge); pic_defun(pic, ">=", pic_number_ge);
pic_leave(pic, ai);
pic_defun(pic, "+", pic_number_add); pic_defun(pic, "+", pic_number_add);
pic_defun(pic, "-", pic_number_sub); pic_defun(pic, "-", pic_number_sub);
pic_defun(pic, "*", pic_number_mul); pic_defun(pic, "*", pic_number_mul);
pic_defun(pic, "/", pic_number_div); pic_defun(pic, "/", pic_number_div);
pic_leave(pic, ai);
pic_defun(pic, "number->string", pic_number_number_to_string); pic_defun(pic, "number->string", pic_number_number_to_string);
pic_defun(pic, "string->number", pic_number_string_to_number); pic_defun(pic, "string->number", pic_number_string_to_number);
pic_leave(pic, ai);
} }

View File

@ -9,15 +9,16 @@
extern "C" { extern "C" {
#endif #endif
#include "picrin/private/file.h" #include "khash.h"
#include "picrin/private/khash.h"
#include "picrin/private/gc.h"
typedef struct identifier symbol; #if PIC_BITMAP_GC
# define OBJECT_HEADER \
KHASH_DECLARE(env, struct identifier *, symbol *) unsigned char tt;
KHASH_DECLARE(dict, symbol *, pic_value) #else
KHASH_DECLARE(weak, struct object *, pic_value) # define OBJECT_HEADER \
unsigned char tt; \
char gc_mark;
#endif
struct object; /* defined in gc.c */ struct object; /* defined in gc.c */
@ -34,6 +35,10 @@ struct identifier {
struct env *env; struct env *env;
}; };
typedef struct identifier symbol;
KHASH_DECLARE(env, struct identifier *, symbol *)
struct env { struct env {
OBJECT_HEADER OBJECT_HEADER
khash_t(env) map; khash_t(env) map;
@ -58,11 +63,15 @@ struct string {
struct rope *rope; struct rope *rope;
}; };
KHASH_DECLARE(dict, symbol *, pic_value)
struct dict { struct dict {
OBJECT_HEADER OBJECT_HEADER
khash_t(dict) hash; khash_t(dict) hash;
}; };
KHASH_DECLARE(weak, struct object *, pic_value)
struct weak { struct weak {
OBJECT_HEADER OBJECT_HEADER
khash_t(weak) hash; khash_t(weak) hash;
@ -118,9 +127,33 @@ struct error {
struct string *stack; struct string *stack;
}; };
enum {
FILE_READ = 01,
FILE_WRITE = 02,
FILE_UNBUF = 04,
FILE_EOF = 010,
FILE_ERR = 020,
FILE_LNBUF = 040
};
struct port { struct port {
OBJECT_HEADER OBJECT_HEADER
struct file file; struct file {
/* buffer */
char buf[1]; /* fallback buffer */
long cnt; /* characters left */
char *ptr; /* next character position */
char *base; /* location of the buffer */
/* operators */
struct {
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 *);
} vtable;
int flag; /* mode of the file access */
} file;
}; };
struct checkpoint { struct checkpoint {
@ -189,7 +222,7 @@ pic_value pic_make_identifier(pic_state *, pic_value id, pic_value env);
pic_value pic_make_proc(pic_state *, pic_func_t, int, pic_value *); pic_value pic_make_proc(pic_state *, pic_func_t, int, pic_value *);
pic_value pic_make_proc_irep(pic_state *, struct irep *, struct context *); pic_value pic_make_proc_irep(pic_state *, struct irep *, struct context *);
pic_value pic_make_env(pic_state *, pic_value env); pic_value pic_make_env(pic_state *, pic_value env);
pic_value pic_make_rec(pic_state *, pic_value type, pic_value datum); pic_value pic_make_record(pic_state *, pic_value type, pic_value datum);
pic_value pic_add_identifier(pic_state *, pic_value id, pic_value env); pic_value pic_add_identifier(pic_state *, pic_value id, pic_value env);
void pic_put_identifier(pic_state *, pic_value id, pic_value uid, pic_value env); void pic_put_identifier(pic_state *, pic_value id, pic_value uid, pic_value env);

View File

@ -3,8 +3,7 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h" #include "object.h"
#include "picrin/private/object.h"
pic_value pic_value
pic_cons(pic_state *pic, pic_value car, pic_value cdr) pic_cons(pic_state *pic, pic_value car, pic_value cdr)

View File

@ -3,10 +3,8 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h" #include "object.h"
#include "picrin/private/state.h" #include "state.h"
#include "picrin/private/object.h"
#include "picrin/private/file.h"
#ifndef EOF #ifndef EOF
# define EOF (-1) # define EOF (-1)
@ -758,32 +756,18 @@ pic_port_flush(pic_state *pic)
return pic_undef_value(pic); return pic_undef_value(pic);
} }
static pic_value
coerce_port(pic_state *pic)
{
pic_value port;
pic_get_args(pic, "p", &port);
return port;
}
#if PIC_USE_STDIO
# define DEFINE_PORT(pic, name, file, mode) \
pic_defvar(pic, name, pic_fopen(pic, file, mode), coerce)
#else
# define DEFINE_PORT(pic, name, file, mode) \
pic_defvar(pic, name, pic_fopen_null(pic, mode), coerce)
#endif
void void
pic_init_port(pic_state *pic) pic_init_port(pic_state *pic)
{ {
pic_value coerce = pic_lambda(pic, coerce_port, 0); #if PIC_USE_STDIO
pic_defvar(pic, "current-input-port", pic_fopen(pic, stdin, "r"));
DEFINE_PORT(pic, "current-input-port", stdin, "r"); pic_defvar(pic, "current-output-port", pic_fopen(pic, stdout, "w"));
DEFINE_PORT(pic, "current-output-port", stdout, "w"); pic_defvar(pic, "current-error-port", pic_fopen(pic, stdout, "w"));
DEFINE_PORT(pic, "current-error-port", stderr, "w"); #else
pic_defvar(pic, "current-input-port", pic_fopen_null(pic, "r"));
pic_defvar(pic, "current-output-port", pic_fopen_null(pic, "w"));
pic_defvar(pic, "current-error-port", pic_fopen_null(pic, "w"));
#endif
pic_defun(pic, "port?", pic_port_port_p); pic_defun(pic, "port?", pic_port_port_p);
pic_defun(pic, "input-port?", pic_port_input_port_p); pic_defun(pic, "input-port?", pic_port_input_port_p);

View File

@ -3,10 +3,9 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h" #include "object.h"
#include "picrin/private/object.h" #include "state.h"
#include "picrin/private/vm.h" #include "vm.h"
#include "picrin/private/state.h"
#define MIN(x,y) ((x) < (y) ? (x) : (y)) #define MIN(x,y) ((x) < (y) ? (x) : (y))
@ -827,9 +826,9 @@ pic_defun(pic_state *pic, const char *name, pic_func_t f)
} }
void void
pic_defvar(pic_state *pic, const char *name, pic_value init, pic_value conv) pic_defvar(pic_state *pic, const char *name, pic_value init)
{ {
pic_define(pic, pic_current_library(pic), name, pic_make_var(pic, init, conv)); pic_define(pic, pic_current_library(pic), name, pic_make_var(pic, init, pic_false_value(pic)));
pic_export(pic, pic_intern_cstr(pic, name)); pic_export(pic, pic_intern_cstr(pic, name));
} }

View File

@ -3,10 +3,10 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/private/object.h" #include "object.h"
pic_value pic_value
pic_make_rec(pic_state *pic, pic_value type, pic_value datum) pic_make_record(pic_state *pic, pic_value type, pic_value datum)
{ {
struct record *rec; struct record *rec;
@ -24,7 +24,7 @@ pic_rec_make_record(pic_state *pic)
pic_get_args(pic, "oo", &type, &datum); pic_get_args(pic, "oo", &type, &datum);
return pic_make_rec(pic, type, datum); return pic_make_record(pic, type, datum);
} }
static pic_value static pic_value

View File

@ -4,8 +4,8 @@
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h" #include "picrin/extra.h"
#include "picrin/private/object.h" #include "object.h"
#include "picrin/private/state.h" #include "state.h"
static pic_value static pic_value
pic_features(pic_state *pic) pic_features(pic_state *pic)

View File

@ -9,9 +9,8 @@
extern "C" { extern "C" {
#endif #endif
#include "picrin/private/khash.h" #include "khash.h"
#include "picrin/private/vm.h" #include "vm.h"
#include "picrin/private/gc.h"
struct lib { struct lib {
struct string *name; struct string *name;
@ -69,6 +68,9 @@ struct pic_state {
pic_panicf panicf; pic_panicf panicf;
}; };
struct heap *pic_heap_open(pic_state *);
void pic_heap_close(pic_state *, struct heap *);
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif

View File

@ -3,8 +3,7 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h" #include "object.h"
#include "picrin/private/object.h"
struct rope { struct rope {
int refcnt; int refcnt;

View File

@ -3,9 +3,8 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h" #include "object.h"
#include "picrin/private/object.h" #include "state.h"
#include "picrin/private/state.h"
#define kh_pic_str_hash(a) (pic_str_hash(pic, pic_obj_value(a))) #define kh_pic_str_hash(a) (pic_str_hash(pic, pic_obj_value(a)))
#define kh_pic_str_cmp(a, b) (pic_str_cmp(pic, pic_obj_value(a), pic_obj_value(b)) == 0) #define kh_pic_str_cmp(a, b) (pic_str_cmp(pic, pic_obj_value(a), pic_obj_value(b)) == 0)

View File

@ -3,7 +3,7 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/private/object.h" #include "object.h"
#if PIC_NAN_BOXING #if PIC_NAN_BOXING

View File

@ -3,9 +3,8 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h" #include "object.h"
#include "picrin/private/object.h" #include "state.h"
#include "picrin/private/state.h"
static pic_value static pic_value
var_conv(pic_state *pic, pic_value val, pic_value conv) var_conv(pic_state *pic, pic_value val, pic_value conv)

View File

@ -3,8 +3,7 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/extra.h" #include "object.h"
#include "picrin/private/object.h"
pic_value pic_value
pic_make_vec(pic_state *pic, int len, pic_value *argv) pic_make_vec(pic_state *pic, int len, pic_value *argv)

View File

@ -3,7 +3,7 @@
*/ */
#include "picrin.h" #include "picrin.h"
#include "picrin/private/object.h" #include "object.h"
KHASH_DEFINE(weak, struct object *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal) KHASH_DEFINE(weak, struct object *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal)