add pic_intern

This commit is contained in:
Yuichi Nishiwaki 2014-03-01 00:13:11 +09:00
parent 81f839ea04
commit 0b8c7a8ccb
2 changed files with 16 additions and 6 deletions

View File

@ -160,6 +160,7 @@ void pic_defvar(pic_state *, const char *, pic_value);
bool pic_equal_p(pic_state *, pic_value, pic_value);
pic_sym pic_intern(pic_state *, const char *, size_t);
pic_sym pic_intern_cstr(pic_state *, const char *);
const char *pic_symbol_name(pic_state *, pic_sym);
pic_sym pic_gensym(pic_state *, pic_sym);

View File

@ -10,24 +10,33 @@
#include "picrin/string.h"
pic_sym
pic_intern_cstr(pic_state *pic, const char *str)
pic_intern(pic_state *pic, const char *str, size_t len)
{
char *cstr;
xh_entry *e;
pic_sym id;
e = xh_get(pic->syms, str);
cstr = (char *)pic_malloc(pic, len + 1);
cstr[len] = '\0';
memcpy(cstr, str, len);
e = xh_get(pic->syms, cstr);
if (e) {
return e->val;
}
str = pic_strdup(pic, str);
id = pic->sym_cnt++;
xh_put(pic->syms, str, id);
xh_put_int(pic->sym_names, id, (long)str);
xh_put(pic->syms, cstr, id);
xh_put_int(pic->sym_names, id, (long)cstr);
return id;
}
pic_sym
pic_intern_cstr(pic_state *pic, const char *str)
{
return pic_intern(pic, str, strlen(str));
}
pic_sym
pic_gensym(pic_state *pic, pic_sym base)
{