add library operation methods

This commit is contained in:
Yuichi Nishiwaki 2013-12-07 07:05:06 -08:00
parent 1ac5b6458c
commit 7c6ba80581
3 changed files with 34 additions and 0 deletions

View File

@ -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); struct pic_proc *pic_codegen(pic_state *, pic_value);
pic_value pic_macroexpand(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_abort(pic_state *, const char *);
void pic_raise(pic_state *, pic_value); void pic_raise(pic_state *, pic_value);
void pic_error(pic_state *, const char *); void pic_error(pic_state *, const char *);

View File

@ -7,4 +7,6 @@ struct pic_lib {
struct xhash *exports; struct xhash *exports;
}; };
#define pic_lib_ptr(o) ((struct pic_lib *)pic_ptr(o))
#endif #endif

29
src/lib.c Normal file
View File

@ -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));
}