add pic_core_syntactic_env
This commit is contained in:
parent
b4d0bdbd3c
commit
ef04e3f02c
|
@ -56,10 +56,7 @@ typedef struct {
|
||||||
pic_value *globals;
|
pic_value *globals;
|
||||||
size_t glen, gcapa;
|
size_t glen, gcapa;
|
||||||
|
|
||||||
/* positive for variables, negative for macros (bitwise-not) */
|
struct pic_senv *global_senv;
|
||||||
struct xhash *var_tbl;
|
|
||||||
struct pic_syntax **stx;
|
|
||||||
size_t xlen, xcapa;
|
|
||||||
|
|
||||||
struct pic_irep **irep;
|
struct pic_irep **irep;
|
||||||
size_t ilen, icapa;
|
size_t ilen, icapa;
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
struct pic_senv {
|
struct pic_senv {
|
||||||
PIC_OBJECT_HEADER
|
PIC_OBJECT_HEADER
|
||||||
struct pic_senv *up;
|
struct pic_senv *up;
|
||||||
|
/* positive for variables, negative for macros (bitwise-not) */
|
||||||
struct xhash *tbl;
|
struct xhash *tbl;
|
||||||
struct pic_syntax **stx;
|
struct pic_syntax **stx;
|
||||||
size_t xlen, xcapa;
|
size_t xlen, xcapa;
|
||||||
|
@ -42,6 +43,8 @@ struct pic_sc {
|
||||||
#define pic_senv(v) ((struct pic_senv *)pic_ptr(v))
|
#define pic_senv(v) ((struct pic_senv *)pic_ptr(v))
|
||||||
#define pic_senv_p(v) (pic_type(v) == PIC_TT_SENV)
|
#define pic_senv_p(v) (pic_type(v) == PIC_TT_SENV)
|
||||||
|
|
||||||
|
struct pic_senv *pic_core_syntactic_env(pic_state *pic);
|
||||||
|
|
||||||
struct pic_syntax *pic_syntax_new(pic_state *, int kind, pic_sym sym);
|
struct pic_syntax *pic_syntax_new(pic_state *, int kind, pic_sym sym);
|
||||||
struct pic_syntax *pic_syntax_new_macro(pic_state *, pic_sym, struct pic_proc *, struct pic_senv *senv);
|
struct pic_syntax *pic_syntax_new_macro(pic_state *, pic_sym, struct pic_proc *, struct pic_senv *senv);
|
||||||
|
|
||||||
|
|
12
src/gc.c
12
src/gc.c
|
@ -458,8 +458,8 @@ gc_mark_phase(pic_state *pic)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* macros */
|
/* macros */
|
||||||
for (i = 0; i < pic->xlen; ++i) {
|
if (pic->global_senv) {
|
||||||
gc_mark_object(pic, (struct pic_object *)pic->stx[i]);
|
gc_mark_object(pic, (struct pic_object *)pic->global_senv);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* pool */
|
/* pool */
|
||||||
|
@ -522,11 +522,9 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj)
|
||||||
}
|
}
|
||||||
case PIC_TT_SENV: {
|
case PIC_TT_SENV: {
|
||||||
struct pic_senv *senv = (struct pic_senv *)obj;
|
struct pic_senv *senv = (struct pic_senv *)obj;
|
||||||
if (senv->up) {
|
xh_destory(senv->tbl);
|
||||||
xh_destory(senv->tbl);
|
if (senv->stx)
|
||||||
if (senv->stx)
|
pic_free(pic, senv->stx);
|
||||||
pic_free(pic, senv->stx);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case PIC_TT_SYNTAX: {
|
case PIC_TT_SYNTAX: {
|
||||||
|
|
44
src/macro.c
44
src/macro.c
|
@ -29,20 +29,42 @@ new_uniq_sym(pic_state *pic, pic_sym base)
|
||||||
return uniq;
|
return uniq;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct pic_senv *
|
struct pic_senv *
|
||||||
new_global_senv(pic_state *pic)
|
pic_core_syntactic_env(pic_state *pic)
|
||||||
{
|
{
|
||||||
struct pic_senv *senv;
|
struct pic_senv *senv;
|
||||||
|
|
||||||
senv = (struct pic_senv *)pic_obj_alloc(pic, sizeof(struct pic_senv), PIC_TT_SENV);
|
senv = (struct pic_senv *)pic_obj_alloc(pic, sizeof(struct pic_senv), PIC_TT_SENV);
|
||||||
senv->up = NULL;
|
senv->up = NULL;
|
||||||
senv->tbl = pic->var_tbl;
|
senv->tbl = xh_new();
|
||||||
senv->stx = pic->stx;
|
senv->stx = (struct pic_syntax **)pic_calloc(pic, PIC_MACROS_SIZE, sizeof(struct pic_syntax *));
|
||||||
senv->xlen = pic->xlen;
|
senv->xlen = 0;
|
||||||
senv->xcapa = pic->xcapa;
|
senv->xcapa = PIC_MACROS_SIZE;
|
||||||
|
|
||||||
|
#define register_core_syntax(pic,senv,kind,name) do { \
|
||||||
|
senv->stx[senv->xlen] = pic_syntax_new(pic, kind, pic_intern_cstr(pic, name)); \
|
||||||
|
xh_put(senv->tbl, name, ~senv->xlen); \
|
||||||
|
senv->xlen++; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
register_core_syntax(pic, senv, PIC_STX_DEFINE, "define");
|
||||||
|
register_core_syntax(pic, senv, PIC_STX_SET, "set!");
|
||||||
|
register_core_syntax(pic, senv, PIC_STX_QUOTE, "quote");
|
||||||
|
register_core_syntax(pic, senv, PIC_STX_LAMBDA, "lambda");
|
||||||
|
register_core_syntax(pic, senv, PIC_STX_IF, "if");
|
||||||
|
register_core_syntax(pic, senv, PIC_STX_BEGIN, "begin");
|
||||||
|
register_core_syntax(pic, senv, PIC_STX_DEFMACRO, "define-macro");
|
||||||
|
register_core_syntax(pic, senv, PIC_STX_DEFSYNTAX, "define-syntax");
|
||||||
|
|
||||||
return senv;
|
return senv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static struct pic_senv *
|
||||||
|
new_global_senv(pic_state *pic)
|
||||||
|
{
|
||||||
|
return pic->global_senv;
|
||||||
|
}
|
||||||
|
|
||||||
static struct pic_senv *
|
static struct pic_senv *
|
||||||
new_local_senv(pic_state *pic, pic_value formals, struct pic_senv *up)
|
new_local_senv(pic_state *pic, pic_value formals, struct pic_senv *up)
|
||||||
{
|
{
|
||||||
|
@ -136,13 +158,13 @@ pic_defsyntax(pic_state *pic, const char *name, struct pic_proc *macro, struct p
|
||||||
{
|
{
|
||||||
int idx;
|
int idx;
|
||||||
|
|
||||||
idx = pic->xlen;
|
idx = pic->global_senv->xlen;
|
||||||
if (idx >= pic->xcapa) {
|
if (idx >= pic->global_senv->xcapa) {
|
||||||
pic_abort(pic, "macro table overflow");
|
pic_abort(pic, "macro table overflow");
|
||||||
}
|
}
|
||||||
pic->stx[idx] = pic_syntax_new_macro(pic, pic_intern_cstr(pic, name), macro, mac_env);
|
pic->global_senv->stx[idx] = pic_syntax_new_macro(pic, pic_intern_cstr(pic, name), macro, mac_env);
|
||||||
xh_put(pic->var_tbl, name, ~idx);
|
xh_put(pic->global_senv->tbl, name, ~idx);
|
||||||
pic->xlen++;
|
pic->global_senv->xlen++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
24
src/state.c
24
src/state.c
|
@ -65,11 +65,9 @@ pic_open(int argc, char *argv[], char **envp)
|
||||||
pic->glen = 0;
|
pic->glen = 0;
|
||||||
pic->gcapa = PIC_GLOBALS_SIZE;
|
pic->gcapa = PIC_GLOBALS_SIZE;
|
||||||
|
|
||||||
/* identifier table */
|
/* syntactic env */
|
||||||
pic->var_tbl = xh_new();
|
pic->global_senv = NULL; /* prevent gc from hanging during marking phase */
|
||||||
pic->stx = (struct pic_syntax **)calloc(PIC_MACROS_SIZE, sizeof(struct pic_syntax *));
|
pic->global_senv = pic_core_syntactic_env(pic);
|
||||||
pic->xlen = 0;
|
|
||||||
pic->xcapa = PIC_MACROS_SIZE;
|
|
||||||
|
|
||||||
/* pool */
|
/* pool */
|
||||||
pic->pool = (pic_value *)calloc(PIC_POOL_SIZE, sizeof(pic_value));
|
pic->pool = (pic_value *)calloc(PIC_POOL_SIZE, sizeof(pic_value));
|
||||||
|
@ -117,22 +115,6 @@ pic_open(int argc, char *argv[], char **envp)
|
||||||
register_core_symbol(pic, sGE, ">=");
|
register_core_symbol(pic, sGE, ">=");
|
||||||
pic_gc_arena_restore(pic, ai);
|
pic_gc_arena_restore(pic, ai);
|
||||||
|
|
||||||
#define register_core_syntax(pic,kind,name) do { \
|
|
||||||
pic->stx[pic->xlen] = pic_syntax_new(pic, kind, pic_intern_cstr(pic, name)); \
|
|
||||||
xh_put(pic->var_tbl, name, ~pic->xlen); \
|
|
||||||
pic->xlen++; \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
register_core_syntax(pic, PIC_STX_DEFINE, "define");
|
|
||||||
register_core_syntax(pic, PIC_STX_SET, "set!");
|
|
||||||
register_core_syntax(pic, PIC_STX_QUOTE, "quote");
|
|
||||||
register_core_syntax(pic, PIC_STX_LAMBDA, "lambda");
|
|
||||||
register_core_syntax(pic, PIC_STX_IF, "if");
|
|
||||||
register_core_syntax(pic, PIC_STX_BEGIN, "begin");
|
|
||||||
register_core_syntax(pic, PIC_STX_DEFMACRO, "define-macro");
|
|
||||||
register_core_syntax(pic, PIC_STX_DEFSYNTAX, "define-syntax");
|
|
||||||
pic_gc_arena_restore(pic, ai);
|
|
||||||
|
|
||||||
pic_init_core(pic);
|
pic_init_core(pic);
|
||||||
|
|
||||||
return pic;
|
return pic;
|
||||||
|
|
Loading…
Reference in New Issue