tweak: some memory leak tweaks.

This commit is contained in:
NunoSempere 2023-05-02 11:54:48 -04:00
parent a0734df444
commit 59c51e0027
2 changed files with 22 additions and 4 deletions

BIN
mumble

Binary file not shown.

View File

@ -124,6 +124,7 @@ lispval* lispval_qexpr(void)
// Destructor // Destructor
void delete_lispval(lispval* v) void delete_lispval(lispval* v)
{ {
if(v == NULL) return;
switch (v->type) { switch (v->type) {
case LISPVAL_NUM: case LISPVAL_NUM:
if(VERBOSE) printf("\nFreed num"); if(VERBOSE) printf("\nFreed num");
@ -202,6 +203,7 @@ lispval* get_from_lispenv(char* sym, lispenv* env){
for(int i=0; i<env->count; i++){ for(int i=0; i<env->count; i++){
if(strcmp(env->syms[i], sym) == 0){ if(strcmp(env->syms[i], sym) == 0){
return clone_lispval(env->vals[i]); return clone_lispval(env->vals[i]);
// return env->vals[i];
} }
} }
return lispval_err("Error: unbound symbol"); return lispval_err("Error: unbound symbol");
@ -652,26 +654,39 @@ lispval* evaluate_lispval(lispval* l, lispenv* env)
// Check if this is a symbol // Check if this is a symbol
if(l->type == LISPVAL_SYM){ if(l->type == LISPVAL_SYM){
get_from_lispenv(l->sym, env); // Unclear how I want to structure this so as to not get memory errors.
return get_from_lispenv(l->sym, env);
} }
// Evaluate the children if needed // Evaluate the children if needed
for (int i = 0; i < l->count; i++) { for (int i = 0; i < l->count; i++) {
if (l->cell[i]->type == LISPVAL_SEXPR || l->cell[i]->type == LISPVAL_SYM) { if (l->cell[i]->type == LISPVAL_SEXPR || l->cell[i]->type == LISPVAL_SYM) {
l->cell[i] = evaluate_lispval(l->cell[i], env); // l->cell[i] =
lispval* new = evaluate_lispval(l->cell[i], env);
delete_lispval(l->cell[i]);
l->cell[i] = new;
} }
} }
// Check if any are errors. // Check if any are errors.
lispval* err = NULL;
for (int i = 0; i < l->count; i++) { for (int i = 0; i < l->count; i++) {
if (l->cell[i]->type == LISPVAL_ERR) { if (l->cell[i]->type == LISPVAL_ERR) {
return clone_lispval(l->cell[i]); err = clone_lispval(l->cell[i]);
} }
} }
if (err != NULL){
/*
for (int i = 0; i < l->count; i++) {
delete_lispval(l->cell[i]);
}
*/
return err;
}
// Check if the first element is an operation. // Check if the first element is an operation.
if (l->count >= 2 && ((l->cell[0])->type == LISPVAL_FUNC)) { if (l->count >= 2 && ((l->cell[0])->type == LISPVAL_FUNC)) {
lispval* temp = clone_lispval(l->cell[0]); lispval* temp = clone_lispval(l->cell[0]);
lispval* f = pop_lispval(l, 0); lispval* f = pop_lispval(temp, 0);
lispval* operands = temp; lispval* operands = temp;
// lispval* operation = clone_lispval(l->cell[0]); // lispval* operation = clone_lispval(l->cell[0]);
// lispval* operands = lispval_sexpr(); // lispval* operands = lispval_sexpr();
@ -756,6 +771,9 @@ int main(int argc, char** argv)
} }
delete_lispval(l); delete_lispval(l);
delete_lispval(answer); delete_lispval(answer);
// Clean up environment
destroy_lispenv(env);
} else { } else {
/* Otherwise Print the Error */ /* Otherwise Print the Error */
mpc_err_print(result.error); mpc_err_print(result.error);