picrin/extlib/benz/macro.c

113 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"
2016-02-06 09:15:53 -05:00
KHASH_DEFINE(env, pic_id *, pic_sym *, kh_ptr_hash_func, kh_ptr_hash_equal)
2014-08-25 00:38:09 -04:00
struct pic_env *
pic_make_env(pic_state *pic, struct pic_env *up)
2014-08-25 00:38:09 -04:00
{
struct pic_env *env;
assert(up != NULL);
env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TT_ENV);
env->up = up;
env->prefix = NULL;
kh_init(env, &env->map);
return env;
}
struct pic_env *
pic_make_topenv(pic_state *pic, pic_str *prefix)
{
struct pic_env *env;
env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TT_ENV);
env->up = NULL;
env->prefix = prefix;
2015-06-24 18:26:48 -04:00
kh_init(env, &env->map);
return env;
2014-08-25 00:38:09 -04:00
}
2015-06-09 09:26:21 -04:00
pic_sym *
2016-02-06 09:15:53 -05:00
pic_add_identifier(pic_state *pic, pic_id *id, struct pic_env *env)
{
const char *name;
pic_sym *uid;
pic_str *str;
2016-02-06 09:15:53 -05:00
name = pic_identifier_name(pic, id);
2016-02-06 09:15:53 -05:00
if (env->up == NULL && pic_sym_p(pic_obj_value(id))) { /* toplevel & public */
str = pic_format(pic, "%s/%s", pic_str_cstr(pic, env->prefix), name);
} else {
str = pic_format(pic, ".%s.%d", name, pic->ucnt++);
}
uid = pic_intern_str(pic, str);
2016-02-06 09:15:53 -05:00
return pic_put_identifier(pic, id, uid, env);
}
pic_sym *
2016-02-06 09:15:53 -05:00
pic_put_identifier(pic_state *pic, pic_id *id, pic_sym *uid, struct pic_env *env)
{
2015-06-24 18:26:48 -04:00
khiter_t it;
int ret;
2016-02-06 09:15:53 -05:00
it = kh_put(env, &env->map, id, &ret);
2015-06-24 18:26:48 -04:00
kh_val(&env->map, it) = uid;
return uid;
}
pic_sym *
2016-02-06 09:15:53 -05:00
pic_find_identifier(pic_state PIC_UNUSED(*pic), pic_id *id, struct pic_env *env)
{
2015-06-24 18:26:48 -04:00
khiter_t it;
2016-02-06 09:15:53 -05:00
it = kh_get(env, &env->map, id);
2015-06-24 18:26:48 -04:00
if (it == kh_end(&env->map)) {
return NULL;
2014-08-25 00:38:09 -04:00
}
2015-06-24 18:26:48 -04:00
return kh_val(&env->map, it);
}
2016-02-05 07:01:36 -05:00
static pic_sym *
2016-02-06 09:15:53 -05:00
lookup(pic_state *pic, pic_id *id, struct pic_env *env)
2016-02-05 07:01:36 -05:00
{
2016-02-06 09:15:53 -05:00
pic_sym *uid = NULL;
2016-02-05 07:01:36 -05:00
while (env != NULL) {
2016-02-06 09:15:53 -05:00
uid = pic_find_identifier(pic, id, env);
if (uid != NULL) {
break;
2016-02-05 07:01:36 -05:00
}
env = env->up;
}
2016-02-06 09:15:53 -05:00
return uid;
2016-02-05 07:01:36 -05:00
}
pic_sym *
2016-02-06 09:15:53 -05:00
pic_lookup_identifier(pic_state *pic, pic_id *id, struct pic_env *env)
2016-02-05 07:01:36 -05:00
{
pic_sym *uid;
2016-02-06 09:15:53 -05:00
while ((uid = lookup(pic, id, env)) == NULL) {
if (pic_sym_p(pic_obj_value(id))) {
2016-02-05 07:01:36 -05:00
break;
}
2016-02-06 09:15:53 -05:00
env = id->u.id.env; /* do not overwrite id first */
id = id->u.id.id;
2016-02-05 07:01:36 -05:00
}
if (uid == NULL) {
while (env->up != NULL) {
env = env->up;
}
2016-02-06 09:15:53 -05:00
uid = pic_add_identifier(pic, id, env);
2016-02-05 07:01:36 -05:00
}
return uid;
}