2014-01-17 06:58:31 -05:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
2013-10-10 04:22:25 -04:00
|
|
|
#include <string.h>
|
2013-10-13 03:01:40 -04:00
|
|
|
#include <stdlib.h>
|
2014-01-12 02:03:36 -05:00
|
|
|
#include <math.h>
|
2013-10-10 04:22:25 -04:00
|
|
|
|
|
|
|
#include "picrin.h"
|
2014-02-22 01:20:53 -05:00
|
|
|
#include "picrin/string.h"
|
2013-10-20 01:05:35 -04:00
|
|
|
|
2013-10-28 13:11:31 -04:00
|
|
|
pic_sym
|
2014-02-28 10:13:11 -05:00
|
|
|
pic_intern(pic_state *pic, const char *str, size_t len)
|
2013-10-20 01:05:35 -04:00
|
|
|
{
|
2014-02-28 10:13:11 -05:00
|
|
|
char *cstr;
|
2014-02-06 11:15:17 -05:00
|
|
|
xh_entry *e;
|
2013-10-28 13:11:31 -04:00
|
|
|
pic_sym id;
|
2013-10-20 01:05:35 -04:00
|
|
|
|
2014-02-28 10:13:11 -05:00
|
|
|
cstr = (char *)pic_malloc(pic, len + 1);
|
|
|
|
cstr[len] = '\0';
|
|
|
|
memcpy(cstr, str, len);
|
|
|
|
|
2014-03-25 02:29:26 -04:00
|
|
|
e = xh_get(&pic->syms, cstr);
|
2013-10-28 13:11:31 -04:00
|
|
|
if (e) {
|
2014-03-25 02:29:26 -04:00
|
|
|
return xh_val(e, pic_sym);
|
2013-10-20 01:05:35 -04:00
|
|
|
}
|
|
|
|
|
2014-02-12 10:14:03 -05:00
|
|
|
id = pic->sym_cnt++;
|
2014-03-25 02:29:26 -04:00
|
|
|
xh_put(&pic->syms, cstr, &id);
|
2014-06-14 07:59:31 -04:00
|
|
|
xh_put_int(&pic->sym_names, id, &cstr);
|
2013-10-28 13:11:31 -04:00
|
|
|
return id;
|
2013-10-20 01:05:35 -04:00
|
|
|
}
|
|
|
|
|
2014-02-28 10:13:11 -05:00
|
|
|
pic_sym
|
|
|
|
pic_intern_cstr(pic_state *pic, const char *str)
|
|
|
|
{
|
|
|
|
return pic_intern(pic, str, strlen(str));
|
|
|
|
}
|
|
|
|
|
2014-01-12 02:03:36 -05:00
|
|
|
pic_sym
|
|
|
|
pic_gensym(pic_state *pic, pic_sym base)
|
|
|
|
{
|
2014-03-26 01:49:48 -04:00
|
|
|
int uid = pic->uniq_sym_cnt++, len;
|
2014-01-12 02:03:36 -05:00
|
|
|
char *str;
|
|
|
|
pic_sym uniq;
|
|
|
|
|
2014-03-26 01:49:48 -04:00
|
|
|
len = snprintf(NULL, 0, "%s@%d", pic_symbol_name(pic, base), uid);
|
|
|
|
str = pic_alloc(pic, len + 1);
|
|
|
|
sprintf(str, "%s@%d", pic_symbol_name(pic, base), uid);
|
2014-01-12 02:03:36 -05:00
|
|
|
|
2014-02-12 23:52:11 -05:00
|
|
|
/* don't put the symbol to pic->syms to keep it uninterned */
|
2014-02-12 10:14:03 -05:00
|
|
|
uniq = pic->sym_cnt++;
|
2014-06-14 07:59:31 -04:00
|
|
|
xh_put_int(&pic->sym_names, uniq, &str);
|
2014-01-12 02:03:36 -05:00
|
|
|
|
|
|
|
return uniq;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
pic_interned_p(pic_state *pic, pic_sym sym)
|
|
|
|
{
|
|
|
|
return sym == pic_intern_cstr(pic, pic_symbol_name(pic, sym));
|
|
|
|
}
|
|
|
|
|
2013-10-28 13:11:31 -04:00
|
|
|
const char *
|
|
|
|
pic_symbol_name(pic_state *pic, pic_sym sym)
|
2013-10-20 01:05:35 -04:00
|
|
|
{
|
2014-06-14 07:59:31 -04:00
|
|
|
return xh_val(xh_get_int(&pic->sym_names, sym), const char *);
|
2013-10-10 04:22:25 -04:00
|
|
|
}
|
2013-10-28 13:49:38 -04:00
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_symbol_symbol_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
2014-01-30 13:03:36 -05:00
|
|
|
return pic_bool_value(pic_sym_p(v));
|
2013-10-28 13:49:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_symbol_symbol_to_string(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
2014-01-30 13:03:36 -05:00
|
|
|
if (! pic_sym_p(v)) {
|
2013-10-28 13:49:38 -04:00
|
|
|
pic_error(pic, "symbol->string: expected symbol");
|
|
|
|
}
|
|
|
|
|
2013-11-17 10:28:42 -05:00
|
|
|
return pic_obj_value(pic_str_new_cstr(pic, pic_symbol_name(pic, pic_sym(v))));
|
2013-10-28 13:49:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_symbol_string_to_symbol(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
|
|
|
if (! pic_str_p(v)) {
|
|
|
|
pic_error(pic, "string->symbol: expected string");
|
|
|
|
}
|
|
|
|
|
2014-02-27 07:54:37 -05:00
|
|
|
return pic_symbol_value(pic_intern_cstr(pic, pic_str_cstr(pic_str_ptr(v))));
|
2013-10-28 13:49:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_init_symbol(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_defun(pic, "symbol?", pic_symbol_symbol_p);
|
|
|
|
pic_defun(pic, "symbol->string", pic_symbol_symbol_to_string);
|
|
|
|
pic_defun(pic, "string->symbol", pic_symbol_string_to_symbol);
|
|
|
|
}
|