From 94555e4497df4d170ed1608ce5b58a809637a296 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Thu, 29 Oct 2020 10:26:44 -0400 Subject: [PATCH] Adding to env checks outer env for existing Object --- src/env.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) 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");