move pic_resolve to macro.c

This commit is contained in:
Yuichi Nishiwaki 2016-02-05 21:01:36 +09:00
parent e96465f724
commit 0499b5ffb0
4 changed files with 41 additions and 43 deletions

View File

@ -9,48 +9,6 @@
* macro expander * macro expander
*/ */
static pic_sym *
lookup(pic_state *pic, pic_value var, struct pic_env *env)
{
khiter_t it;
pic_assert_type(pic, var, var);
while (env != NULL) {
it = kh_get(env, &env->map, pic_ptr(var));
if (it != kh_end(&env->map)) {
return kh_val(&env->map, it);
}
env = env->up;
}
return NULL;
}
pic_sym *
pic_resolve(pic_state *pic, pic_value var, struct pic_env *env)
{
pic_sym *uid;
assert(env != NULL);
pic_assert_type(pic, var, var);
while ((uid = lookup(pic, var, env)) == NULL) {
if (pic_sym_p(var)) {
break;
}
env = pic_id_ptr(var)->env;
var = pic_id_ptr(var)->var;
}
if (uid == NULL) {
while (env->up != NULL) {
env = env->up;
}
uid = pic_add_variable(pic, env, var);
}
return uid;
}
static void static void
define_macro(pic_state *pic, pic_sym *uid, struct pic_proc *mac) define_macro(pic_state *pic, pic_sym *uid, struct pic_proc *mac)
{ {

View File

@ -38,7 +38,6 @@ struct pic_irep {
void pic_irep_incref(pic_state *, struct pic_irep *); void pic_irep_incref(pic_state *, struct pic_irep *);
void pic_irep_decref(pic_state *, struct pic_irep *); void pic_irep_decref(pic_state *, struct pic_irep *);
pic_sym *pic_resolve(pic_state *, pic_value, struct pic_env *);
pic_value pic_expand(pic_state *, pic_value, struct pic_env *); pic_value pic_expand(pic_state *, pic_value, struct pic_env *);
pic_value pic_analyze(pic_state *, pic_value); pic_value pic_analyze(pic_state *, pic_value);
struct pic_irep *pic_codegen(pic_state *, pic_value); struct pic_irep *pic_codegen(pic_state *, pic_value);

View File

@ -37,6 +37,7 @@ pic_sym *pic_uniq(pic_state *, pic_value);
pic_sym *pic_add_variable(pic_state *, struct pic_env *, pic_value); pic_sym *pic_add_variable(pic_state *, struct pic_env *, pic_value);
void pic_put_variable(pic_state *, struct pic_env *, pic_value, pic_sym *); void pic_put_variable(pic_state *, struct pic_env *, pic_value, pic_sym *);
pic_sym *pic_find_variable(pic_state *, struct pic_env *, pic_value); pic_sym *pic_find_variable(pic_state *, struct pic_env *, pic_value);
pic_sym *pic_resolve(pic_state *, pic_value, struct pic_env *);
bool pic_var_p(pic_value); bool pic_var_p(pic_value);
pic_sym *pic_var_name(pic_state *, pic_value); pic_sym *pic_var_name(pic_state *, pic_value);

View File

@ -99,6 +99,46 @@ pic_find_variable(pic_state PIC_UNUSED(*pic), struct pic_env *env, pic_value var
return kh_val(&env->map, it); return kh_val(&env->map, it);
} }
static pic_sym *
lookup(void *var, struct pic_env *env)
{
khiter_t it;
while (env != NULL) {
it = kh_get(env, &env->map, var);
if (it != kh_end(&env->map)) {
return kh_val(&env->map, it);
}
env = env->up;
}
return NULL;
}
pic_sym *
pic_resolve(pic_state *pic, pic_value var, struct pic_env *env)
{
pic_sym *uid;
assert(env != NULL);
pic_assert_type(pic, var, var);
while ((uid = lookup(pic_ptr(var), env)) == NULL) {
if (pic_sym_p(var)) {
break;
}
env = pic_id_ptr(var)->env;
var = pic_id_ptr(var)->var;
}
if (uid == NULL) {
while (env->up != NULL) {
env = env->up;
}
uid = pic_add_variable(pic, env, var);
}
return uid;
}
static pic_value static pic_value
pic_macro_identifier_p(pic_state *pic) pic_macro_identifier_p(pic_state *pic)
{ {