save symbol names in xhash

This commit is contained in:
Yuichi Nishiwaki 2014-02-13 00:14:03 +09:00
parent 907a6a1207
commit 923a762411
3 changed files with 21 additions and 35 deletions

View File

@ -98,10 +98,10 @@ typedef struct {
pic_sym sADD, sSUB, sMUL, sDIV, sMINUS; pic_sym sADD, sSUB, sMUL, sDIV, sMINUS;
pic_sym sEQ, sLT, sLE, sGT, sGE, sNOT; pic_sym sEQ, sLT, sLE, sGT, sGE, sNOT;
xhash *sym_tbl; xhash *syms; /* name to symbol */
const char **sym_pool; xhash *sym_names; /* symbol to name */
size_t slen, scapa; int sym_cnt;
int uniq_sym_count; int uniq_sym_cnt;
xhash *global_tbl; xhash *global_tbl;
pic_value *globals; pic_value *globals;

View File

@ -52,11 +52,10 @@ pic_open(int argc, char *argv[], char **envp)
init_heap(pic->heap); init_heap(pic->heap);
/* symbol table */ /* symbol table */
pic->sym_tbl = xh_new_str(); pic->syms = xh_new_str();
pic->sym_pool = (const char **)calloc(PIC_SYM_POOL_SIZE, sizeof(const char *)); pic->sym_names = xh_new_int();
pic->slen = 0; pic->sym_cnt = 0;
pic->scapa = pic->slen + PIC_SYM_POOL_SIZE; pic->uniq_sym_cnt = 0;
pic->uniq_sym_count = 0;
/* global variables */ /* global variables */
pic->global_tbl = xh_new_int(); pic->global_tbl = xh_new_int();
@ -129,7 +128,7 @@ pic_open(int argc, char *argv[], char **envp)
void void
pic_close(pic_state *pic) pic_close(pic_state *pic)
{ {
size_t i; xh_iter it;
/* free global stacks */ /* free global stacks */
free(pic->stbase); free(pic->stbase);
@ -137,7 +136,7 @@ pic_close(pic_state *pic)
free(pic->rescue); free(pic->rescue);
free(pic->globals); free(pic->globals);
xh_destroy(pic->sym_tbl); xh_destroy(pic->syms);
xh_destroy(pic->global_tbl); xh_destroy(pic->global_tbl);
pic->glen = 0; pic->glen = 0;
@ -157,10 +156,10 @@ pic_close(pic_state *pic)
free(pic->heap); free(pic->heap);
/* free symbol names */ /* free symbol names */
for (i = 0; i < pic->slen; ++i) { for (xh_begin(pic->sym_names, &it); ! xh_isend(&it); xh_next(&it)) {
free((void *)pic->sym_pool[i]); free((void *)it.e->val);
} }
free(pic->sym_pool); free(pic->sym_names);
PIC_BLK_DECREF(pic, pic->blk); PIC_BLK_DECREF(pic, pic->blk);

View File

@ -14,32 +14,23 @@ pic_intern_cstr(pic_state *pic, const char *str)
xh_entry *e; xh_entry *e;
pic_sym id; pic_sym id;
e = xh_get(pic->sym_tbl, str); e = xh_get(pic->syms, str);
if (e) { if (e) {
return e->val; return e->val;
} }
str = pic_strdup(pic, str); str = pic_strdup(pic, str);
if (pic->slen >= pic->scapa) { id = pic->sym_cnt++;
xh_put(pic->syms, str, id);
#if DEBUG xh_put_int(pic->sym_names, id, (long)str);
puts("sym_pool realloced");
#endif
pic->scapa *= 2;
pic->sym_pool = pic_realloc(pic, pic->sym_pool, sizeof(const char *) * pic->scapa);
}
id = pic->slen++;
pic->sym_pool[id] = str;
xh_put(pic->sym_tbl, str, id);
return id; return id;
} }
pic_sym pic_sym
pic_gensym(pic_state *pic, pic_sym base) pic_gensym(pic_state *pic, pic_sym base)
{ {
int s = ++pic->uniq_sym_count; int s = ++pic->uniq_sym_cnt;
char *str; char *str;
pic_sym uniq; pic_sym uniq;
@ -47,12 +38,8 @@ pic_gensym(pic_state *pic, pic_sym base)
sprintf(str, "%s@%d", pic_symbol_name(pic, base), s); sprintf(str, "%s@%d", pic_symbol_name(pic, base), s);
/* don't put the symbol to pic->sym_tbl to keep it uninterned */ /* don't put the symbol to pic->sym_tbl to keep it uninterned */
if (pic->slen >= pic->scapa) { uniq = pic->sym_cnt++;
pic->scapa *= 2; xh_put_int(pic->sym_names, uniq, (long)str);
pic->sym_pool = pic_realloc(pic, pic->sym_pool, sizeof(const char *) * pic->scapa);
}
uniq = pic->slen++;
pic->sym_pool[uniq] = str;
return uniq; return uniq;
} }
@ -68,7 +55,7 @@ pic_interned_p(pic_state *pic, pic_sym sym)
const char * const char *
pic_symbol_name(pic_state *pic, pic_sym sym) pic_symbol_name(pic_state *pic, pic_sym sym)
{ {
return pic->sym_pool[sym]; return (const char *)xh_get_int(pic->sym_names, sym)->val;
} }
static pic_value static pic_value