add parent/child env distinction when defining a new sym

This commit is contained in:
NunoSempere 2023-05-07 13:01:23 -04:00
parent 21a0a4edab
commit cd75a060c3

View File

@ -335,7 +335,7 @@ lispval* get_from_lispenv(char* sym, lispenv* env)
// and this explains shadowing! // and this explains shadowing!
} }
void insert_in_lispenv(char* sym, lispval* v, lispenv* env) void insert_in_current_lispenv(char* sym, lispval* v, lispenv* env)
{ {
int found = 0; int found = 0;
for (int i = 0; i < env->count; i++) { for (int i = 0; i < env->count; i++) {
@ -358,7 +358,17 @@ void insert_in_lispenv(char* sym, lispval* v, lispenv* env)
} }
} }
lispenv* clone_lispenv(lispenv* origin_env){ void insert_in_parentmost_lispenv(char* sym, lispval* v, lispenv* env)
{
// note that you could have two chains of envs, though hopefully not.
while (env->parent != NULL) {
env = env->parent;
}
insert_in_current_lispenv(sym, v, env);
}
lispenv* clone_lispenv(lispenv* origin_env)
{
lispenv* new_env = malloc(sizeof(lispenv)); lispenv* new_env = malloc(sizeof(lispenv));
new_env->count = origin_env->count; new_env->count = origin_env->count;
new_env->parent = origin_env->parent; new_env->parent = origin_env->parent;
@ -366,7 +376,7 @@ lispenv* clone_lispenv(lispenv* origin_env){
new_env->syms = malloc(sizeof(char*) * origin_env->count); new_env->syms = malloc(sizeof(char*) * origin_env->count);
new_env->vals = malloc(sizeof(lispval*) * origin_env->count); new_env->vals = malloc(sizeof(lispval*) * origin_env->count);
for(int i=0;i<origin_env->count; i++){ for (int i = 0; i < origin_env->count; i++) {
new_env->syms[i] = malloc(strlen(origin_env->syms[i]) + 1); new_env->syms[i] = malloc(strlen(origin_env->syms[i]) + 1);
strcpy(new_env->syms[i], origin_env->syms[i]); strcpy(new_env->syms[i], origin_env->syms[i]);
new_env->vals[i] = clone_lispval(origin_env->vals[i]); new_env->vals[i] = clone_lispval(origin_env->vals[i]);
@ -374,7 +384,6 @@ lispenv* clone_lispenv(lispenv* origin_env){
return new_env; return new_env;
} }
// Read ast into a lispval object // Read ast into a lispval object
lispval* lispval_append_child(lispval* parent, lispval* child) lispval* lispval_append_child(lispval* parent, lispval* child)
{ {
@ -729,7 +738,7 @@ lispval* builtin_def(lispval* v, lispenv* env)
print_lispval_tree(values, 0); print_lispval_tree(values, 0);
if (VERBOSE) if (VERBOSE)
printf("\n"); printf("\n");
insert_in_lispenv(symbols->cell[i]->sym, values->cell[i], env); insert_in_current_lispenv(symbols->cell[i]->sym, values->cell[i], env);
} }
return lispval_sexpr(); // () return lispval_sexpr(); // ()
} }
@ -834,7 +843,7 @@ void lispenv_add_builtin(char* builtin_func_name, lispbuiltin func, lispenv* env
lispval* f = lispval_builtin_func(func, builtin_func_name); lispval* f = lispval_builtin_func(func, builtin_func_name);
if (VERBOSE) if (VERBOSE)
print_lispval_tree(f, 0); print_lispval_tree(f, 0);
insert_in_lispenv(builtin_func_name, f, env); insert_in_current_lispenv(builtin_func_name, f, env);
delete_lispval(f); delete_lispval(f);
} }
void lispenv_add_builtins(lispenv* env) void lispenv_add_builtins(lispenv* env)