From 7c6ba805815b0b9699d2913ab5c9b0ae23b98852 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 7 Dec 2013 07:05:06 -0800 Subject: [PATCH] add library operation methods --- include/picrin.h | 3 +++ include/picrin/lib.h | 2 ++ src/lib.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 src/lib.c diff --git a/include/picrin.h b/include/picrin.h index 9022d9a6..79772d22 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -122,6 +122,9 @@ pic_value pic_apply_argv(pic_state *pic, struct pic_proc *, size_t, ...); struct pic_proc *pic_codegen(pic_state *, pic_value); pic_value pic_macroexpand(pic_state *, pic_value); +void pic_make_library(pic_state *, pic_value); +void pic_in_library(pic_state *, pic_value); + void pic_abort(pic_state *, const char *); void pic_raise(pic_state *, pic_value); void pic_error(pic_state *, const char *); diff --git a/include/picrin/lib.h b/include/picrin/lib.h index 348b6739..30ee5a5d 100644 --- a/include/picrin/lib.h +++ b/include/picrin/lib.h @@ -7,4 +7,6 @@ struct pic_lib { struct xhash *exports; }; +#define pic_lib_ptr(o) ((struct pic_lib *)pic_ptr(o)) + #endif diff --git a/src/lib.c b/src/lib.c new file mode 100644 index 00000000..f55199b4 --- /dev/null +++ b/src/lib.c @@ -0,0 +1,29 @@ +#include "picrin.h" +#include "picrin/lib.h" +#include "picrin/pair.h" +#include "picrin/macro.h" +#include "xhash/xhash.h" + +void +pic_make_library(pic_state *pic, pic_value name) +{ + struct pic_lib *lib; + + lib = (struct pic_lib *)pic_obj_alloc(pic, sizeof(struct pic_lib), PIC_TT_LIB); + lib->senv = pic_minimal_syntactic_env(pic); + lib->exports = xh_new(); + + pic->lib_tbl = pic_acons(pic, name, pic_obj_value(lib), pic->lib_tbl); +} + +void +pic_in_library(pic_state *pic, pic_value name) +{ + pic_value v; + + v = pic_assoc(pic, name, pic->lib_tbl); + if (pic_false_p(v)) { + pic_error(pic, "library not found"); + } + pic->lib = pic_lib_ptr(pic_cdr(pic, v)); +}