add pic_lib

This commit is contained in:
Yuichi Nishiwaki 2013-12-07 03:58:18 -08:00
parent acbcde3b6a
commit 931565d4ce
7 changed files with 31 additions and 2 deletions

10
include/picrin/lib.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef LIB_H__
#define LIB_H__
struct pic_lib {
PIC_OBJECT_HEADER
struct pic_senv *senv;
struct xhash *exports;
};
#endif

View File

@ -92,7 +92,8 @@ enum pic_tt {
PIC_TT_CONT, PIC_TT_CONT,
PIC_TT_SENV, PIC_TT_SENV,
PIC_TT_SYNTAX, PIC_TT_SYNTAX,
PIC_TT_SC PIC_TT_SC,
PIC_TT_LIB
}; };
#define PIC_OBJECT_HEADER \ #define PIC_OBJECT_HEADER \

View File

@ -664,7 +664,8 @@ codegen(codegen_state *state, pic_value obj, bool tailpos)
case PIC_TT_ERROR: case PIC_TT_ERROR:
case PIC_TT_SENV: case PIC_TT_SENV:
case PIC_TT_SYNTAX: case PIC_TT_SYNTAX:
case PIC_TT_SC: { case PIC_TT_SC:
case PIC_TT_LIB: {
pic_error(pic, "invalid expression given"); pic_error(pic, "invalid expression given");
} }
} }

View File

@ -9,6 +9,7 @@
#include "picrin/cont.h" #include "picrin/cont.h"
#include "picrin/error.h" #include "picrin/error.h"
#include "picrin/macro.h" #include "picrin/macro.h"
#include "picrin/lib.h"
#include "xhash/xhash.h" #include "xhash/xhash.h"
#if GC_DEBUG #if GC_DEBUG
@ -396,6 +397,11 @@ gc_mark_object(pic_state *pic, struct pic_object *obj)
gc_mark_object(pic, (struct pic_object *)sc->senv); gc_mark_object(pic, (struct pic_object *)sc->senv);
break; break;
} }
case PIC_TT_LIB: {
struct pic_lib *lib = (struct pic_lib *)obj;
gc_mark_object(pic, (struct pic_object *)lib->senv);
break;
}
case PIC_TT_NIL: case PIC_TT_NIL:
case PIC_TT_BOOL: case PIC_TT_BOOL:
case PIC_TT_FLOAT: case PIC_TT_FLOAT:
@ -529,6 +535,11 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj)
case PIC_TT_SC: { case PIC_TT_SC: {
break; break;
} }
case PIC_TT_LIB: {
struct pic_lib *lib = (struct pic_lib *)obj;
xh_destory(lib->exports);
break;
}
case PIC_TT_NIL: case PIC_TT_NIL:
case PIC_TT_BOOL: case PIC_TT_BOOL:
case PIC_TT_FLOAT: case PIC_TT_FLOAT:

View File

@ -415,6 +415,7 @@ macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv)
case PIC_TT_UNDEF: case PIC_TT_UNDEF:
case PIC_TT_SENV: case PIC_TT_SENV:
case PIC_TT_SYNTAX: case PIC_TT_SYNTAX:
case PIC_TT_LIB:
pic_error(pic, "unexpected value type"); pic_error(pic, "unexpected value type");
return pic_undef_value(); /* unreachable */ return pic_undef_value(); /* unreachable */
} }

View File

@ -109,6 +109,9 @@ write(pic_state *pic, pic_value obj)
write(pic, pic_sc(obj)->expr); write(pic, pic_sc(obj)->expr);
printf(">"); printf(">");
break; break;
case PIC_TT_LIB:
printf("#<library %p>", pic_ptr(obj));
break;
} }
} }

View File

@ -76,6 +76,8 @@ pic_type_repr(enum pic_tt tt)
return "senv"; return "senv";
case PIC_TT_SYNTAX: case PIC_TT_SYNTAX:
return "syntax"; return "syntax";
case PIC_TT_LIB:
return "lib";
} }
/* logic flaw */ /* logic flaw */
abort(); abort();