picrin/extlib/benz/symbol.c

118 lines
2.2 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2015-06-24 18:05:41 -04:00
KHASH_DEFINE(s, const char *, pic_sym *, kh_str_hash_func, kh_str_hash_equal)
static pic_sym *
pic_make_symbol(pic_state *pic, pic_str *str)
{
2015-01-20 02:02:28 -05:00
pic_sym *sym;
2015-01-20 02:02:28 -05:00
sym = (pic_sym *)pic_obj_alloc(pic, sizeof(struct pic_symbol), PIC_TT_SYMBOL);
sym->str = str;
return sym;
}
2015-01-20 02:02:28 -05:00
pic_sym *
2015-01-18 12:21:10 -05:00
pic_intern(pic_state *pic, pic_str *str)
2014-08-25 00:38:09 -04:00
{
2015-06-24 18:05:41 -04:00
khash_t(s) *h = &pic->syms;
2015-01-20 02:02:28 -05:00
pic_sym *sym;
char *cstr;
2015-06-24 18:05:41 -04:00
khiter_t it;
int ret;
2014-08-25 00:38:09 -04:00
2015-06-24 18:05:41 -04:00
it = kh_put(s, h, pic_str_cstr(pic, str), &ret);
if (ret == 0) { /* if exists */
sym = kh_val(h, it);
pic_gc_protect(pic, pic_obj_value(sym));
return sym;
2014-08-25 00:38:09 -04:00
}
cstr = pic_malloc(pic, pic_str_len(str) + 1);
strcpy(cstr, pic_str_cstr(pic, str));
2015-06-24 18:05:41 -04:00
kh_key(h, it) = cstr;
2015-01-18 12:21:10 -05:00
sym = pic_make_symbol(pic, str);
2015-06-24 18:05:41 -04:00
kh_val(h, it) = sym;
return sym;
2014-08-25 00:38:09 -04:00
}
2015-01-20 02:02:28 -05:00
pic_sym *
2014-08-25 00:38:09 -04:00
pic_intern_cstr(pic_state *pic, const char *str)
{
2015-01-18 12:21:10 -05:00
return pic_intern(pic, pic_make_str(pic, str, strlen(str)));
2014-08-26 00:29:36 -04:00
}
2014-08-25 00:38:09 -04:00
const char *
2015-01-20 02:02:28 -05:00
pic_symbol_name(pic_state *pic, pic_sym *sym)
2014-08-25 00:38:09 -04:00
{
return pic_str_cstr(pic, sym->str);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_symbol_symbol_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
return pic_bool_value(pic_sym_p(v));
}
static pic_value
pic_symbol_symbol_eq_p(pic_state *pic)
{
2014-09-27 07:17:02 -04:00
size_t argc, i;
2014-08-25 00:38:09 -04:00
pic_value *argv;
pic_get_args(pic, "*", &argc, &argv);
for (i = 0; i < argc; ++i) {
if (! pic_sym_p(argv[i])) {
return pic_false_value();
}
if (! pic_eq_p(argv[i], argv[0])) {
return pic_false_value();
}
}
return pic_true_value();
}
static pic_value
pic_symbol_symbol_to_string(pic_state *pic)
{
2015-01-20 02:02:28 -05:00
pic_sym *sym;
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "m", &sym);
2014-08-25 00:38:09 -04:00
return pic_obj_value(sym->str);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_symbol_string_to_symbol(pic_state *pic)
{
pic_str *str;
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "s", &str);
2014-08-25 00:38:09 -04:00
2015-01-18 21:08:27 -05:00
return pic_obj_value(pic_intern(pic, str));
2014-08-25 00:38:09 -04:00
}
void
pic_init_symbol(pic_state *pic)
{
2015-06-04 00:53:41 -04:00
void pic_defun_vm(pic_state *, const char *, pic_sym *, pic_func_t);
2015-06-09 12:06:56 -04:00
pic_defun_vm(pic, "symbol?", pic->uSYMBOLP, pic_symbol_symbol_p);
2015-06-04 00:53:41 -04:00
2014-08-31 22:37:52 -04:00
pic_defun(pic, "symbol->string", pic_symbol_symbol_to_string);
pic_defun(pic, "string->symbol", pic_symbol_string_to_symbol);
2014-08-25 00:38:09 -04:00
2014-08-31 22:37:52 -04:00
pic_defun(pic, "symbol=?", pic_symbol_symbol_eq_p);
2014-08-25 00:38:09 -04:00
}