savepoint: savepoint which seems to have no problematic memory leaks.

This commit is contained in:
NunoSempere 2023-05-02 13:07:40 -04:00
parent ce12b90eb5
commit d072f3e1f6
2 changed files with 19 additions and 12 deletions

BIN
mumble

Binary file not shown.

View File

@ -5,14 +5,19 @@
#include <string.h> #include <string.h>
#include "mpc/mpc.h" #include "mpc/mpc.h"
#define VERBOSE 1 #define VERBOSE 0
#define LISPVAL_ASSERT(cond, err) \ #define LISPVAL_ASSERT(cond, err) \
if (!(cond)) { \ if (!(cond)) { \
return lispval_err(err); \ return lispval_err(err); \
} }
#define printfln(...) do { \ #define printfln(...) do { \
printf ("\n@ %s (%d): ", __FILE__, __LINE__); \ if(VERBOSE == 2) { \
printf (__VA_ARGS__); \ printf ("\n@ %s (%d): ", __FILE__, __LINE__); \
printf (__VA_ARGS__); \
} else {\
printf("\n"); \
printf (__VA_ARGS__); \
} \
} while (0) } while (0)
// Types // Types
@ -133,6 +138,7 @@ void delete_lispval(lispval* v)
switch (v->type) { switch (v->type) {
case LISPVAL_NUM: case LISPVAL_NUM:
if(VERBOSE) printfln("Freeing num"); if(VERBOSE) printfln("Freeing num");
if(VERBOSE) printfln("Freed num");
break; break;
case LISPVAL_ERR: case LISPVAL_ERR:
if(VERBOSE) printfln("Freeing err"); if(VERBOSE) printfln("Freeing err");
@ -316,11 +322,11 @@ lispval* read_lispval(mpc_ast_t* t)
// Print // Print
void print_lispval_tree(lispval* v, int indent_level) void print_lispval_tree(lispval* v, int indent_level)
{ {
char* indent = " ";/*malloc(sizeof(char) * (indent_level + 1)); char* indent = malloc(sizeof(char) * (indent_level + 1));
for (int i = 0; i < indent_level; i++) { for (int i = 0; i < indent_level; i++) {
indent[i] = ' '; indent[i] = ' ';
} }
indent[indent_level] = '\0';*/ indent[indent_level] = '\0';
switch (v->type) { switch (v->type) {
case LISPVAL_NUM: case LISPVAL_NUM:
@ -351,10 +357,10 @@ void print_lispval_tree(lispval* v, int indent_level)
printfln("Error: unknown lispval type\n"); printfln("Error: unknown lispval type\n");
// printfln("%s", v->sym); // printfln("%s", v->sym);
} }
// printfln("Freeing indent"); if(VERBOSE) printfln("Freeing indent");
// if (indent!=NULL) free(indent); if (indent!=NULL) free(indent);
printfln("Freed indent"); indent = NULL;
// indent = NULL; if(VERBOSE) printfln("Freed indent");
} }
void print_lispval_parenthesis(lispval* v) void print_lispval_parenthesis(lispval* v)
@ -679,7 +685,8 @@ lispval* evaluate_lispval(lispval* l, lispenv* env)
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] = // l->cell[i] =
lispval* new = evaluate_lispval(l->cell[i], env); lispval* new = evaluate_lispval(l->cell[i], env);
delete_lispval(l->cell[i]); // delete_lispval(l->cell[i]);
// ^ gave me a "double free" error.
l->cell[i] = new; l->cell[i] = new;
} }
} }
@ -786,9 +793,9 @@ int main(int argc, char** argv)
print_lispval_tree(answer, 0); print_lispval_tree(answer, 0);
printf("\n"); printf("\n");
} }
// ^ I do not understand how the memory in l is freed.
delete_lispval(answer);
// delete_lispval(l); // delete_lispval(l);
// delete_lispval(answer);
// Clean up environment // Clean up environment
destroy_lispenv(env); destroy_lispenv(env);
} else { } else {