diff --git a/src/env.c b/src/env.c index de97f6e..8e567c8 100644 --- a/src/env.c +++ b/src/env.c @@ -76,18 +76,22 @@ struct Environment envForLambda(const Object *params, const Object *arg_forms, void addToEnv(struct Environment *env, const char *name, const Object obj) { int i; - for(i = 0; i < env->size; i++) { - if(env->strings[i] == NULL) { - env->strings[i] = calloc(sizeof(char), strlen(name) + 1); - strncpy(env->strings[i], name, strlen(name)); - env->objects[i] = cloneObject(obj); - return; - } - if(strcmp(env->strings[i], name) == 0) { - cleanObject(&env->objects[i]); - env->objects[i] = cloneObject(obj); - return; + struct Environment *temp_env = env; + while(temp_env) { + for(i = 0; i < temp_env->size; i++) { + if(temp_env->strings[i] == NULL) { + temp_env->strings[i] = calloc(sizeof(char), strlen(name) + 1); + strncpy(temp_env->strings[i], name, strlen(name)); + temp_env->objects[i] = cloneObject(obj); + return; + } + if(strcmp(temp_env->strings[i], name) == 0) { + cleanObject(&temp_env->objects[i]); + temp_env->objects[i] = cloneObject(obj); + return; + } } + temp_env = temp_env->outer; } printd("Reallocating environment\n");