Adding to env checks outer env for existing Object

This commit is contained in:
Sage Vaillancourt 2020-10-29 10:26:44 -04:00
parent cb87fbdae6
commit 94555e4497
1 changed files with 15 additions and 11 deletions

View File

@ -76,19 +76,23 @@ struct Environment envForLambda(const Object *params, const Object *arg_forms,
void addToEnv(struct Environment *env, const char *name, const Object obj) void addToEnv(struct Environment *env, const char *name, const Object obj)
{ {
int i; int i;
for(i = 0; i < env->size; i++) { struct Environment *temp_env = env;
if(env->strings[i] == NULL) { while(temp_env) {
env->strings[i] = calloc(sizeof(char), strlen(name) + 1); for(i = 0; i < temp_env->size; i++) {
strncpy(env->strings[i], name, strlen(name)); if(temp_env->strings[i] == NULL) {
env->objects[i] = cloneObject(obj); 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; return;
} }
if(strcmp(env->strings[i], name) == 0) { if(strcmp(temp_env->strings[i], name) == 0) {
cleanObject(&env->objects[i]); cleanObject(&temp_env->objects[i]);
env->objects[i] = cloneObject(obj); temp_env->objects[i] = cloneObject(obj);
return; return;
} }
} }
temp_env = temp_env->outer;
}
printd("Reallocating environment\n"); printd("Reallocating environment\n");
const int inc = 5; const int inc = 5;