From 0115ede4de05318535277549f5a40dd842ae9d30 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 7 Dec 2013 07:11:22 -0800 Subject: [PATCH] change APIs of library functions --- include/picrin.h | 4 ++-- src/lib.c | 25 ++++++++++++++++++++----- src/state.c | 4 ++-- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/include/picrin.h b/include/picrin.h index 79772d22..d4262b45 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -122,8 +122,8 @@ 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_make_library(pic_state *, const char *); +void pic_in_library(pic_state *, const char *); void pic_abort(pic_state *, const char *); void pic_raise(pic_state *, pic_value); diff --git a/src/lib.c b/src/lib.c index f55199b4..9c136b28 100644 --- a/src/lib.c +++ b/src/lib.c @@ -4,26 +4,41 @@ #include "picrin/macro.h" #include "xhash/xhash.h" +static pic_value +lib_spec(pic_state *pic, const char *name) +{ + pic_value vs; + int r; + + r = pic_parse_cstr(pic, name, &vs); + if (r != 1) { + pic_error(pic, "invalid library spec given"); + } + return pic_car(pic, vs); +} + void -pic_make_library(pic_state *pic, pic_value name) +pic_make_library(pic_state *pic, const char *name) { struct pic_lib *lib; + pic_value spec = lib_spec(pic, name); 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); + pic->lib_tbl = pic_acons(pic, spec, pic_obj_value(lib), pic->lib_tbl); } void -pic_in_library(pic_state *pic, pic_value name) +pic_in_library(pic_state *pic, const char *name) { - pic_value v; + pic_value v, spec = lib_spec(pic, name); - v = pic_assoc(pic, name, pic->lib_tbl); + v = pic_assoc(pic, spec, pic->lib_tbl); if (pic_false_p(v)) { pic_error(pic, "library not found"); } + pic->lib = pic_lib_ptr(pic_cdr(pic, v)); } diff --git a/src/state.c b/src/state.c index 9afa08c1..c322e5e7 100644 --- a/src/state.c +++ b/src/state.c @@ -119,8 +119,8 @@ pic_open(int argc, char *argv[], char **envp) pic_gc_arena_restore(pic, ai); /* set library */ - pic_make_library(pic, pic_symbol_value(pic_intern_cstr(pic, "user"))); - pic_in_library(pic, pic_symbol_value(pic_intern_cstr(pic, "user"))); + pic_make_library(pic, "user"); + pic_in_library(pic, "user"); pic_init_core(pic);