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,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");