From 1d83aa65a45485fdeb0312ccc122d8fd5e566d8b Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 17 May 2025 11:01:20 -0400 Subject: [PATCH] Well it mostly works now. Fixed some memory leaks. Implemented some memory leaks. May need to look into garbage collection in the future. --- src/ast.c | 10 +--------- src/exec.c | 35 ++++++++++++++++++++++++++--------- src/grammar.y | 1 + src/main.c | 10 +++++++--- src/scope.c | 3 +-- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/ast.c b/src/ast.c index 10f1be6..96e1379 100644 --- a/src/ast.c +++ b/src/ast.c @@ -55,16 +55,8 @@ void ast_destroy(AST* ast) { // If there're no more `AST`s linked to the scope, free. if (ast->scope) { - log_dbgf("%p: there is scope.", ast->scope); ast->scope->uses--; - log_dbgf( - "%p: were %d uses, now %d", ast->scope, ast->scope->uses + 1, - ast->scope->uses - ); - if (!ast->scope->uses) { - log_dbgf("%p: no more uses, gonna free", ast->scope); - scope_destroy_psv(ast->scope); - } + if (!ast->scope->uses) { scope_destroy_psv(ast->scope); } } free(ast); diff --git a/src/exec.c b/src/exec.c index 6c167be..78103f8 100644 --- a/src/exec.c +++ b/src/exec.c @@ -4,6 +4,7 @@ #include "include/ast.h" #include "include/builtin.h" +#include "include/dlist.h" #include "include/exec.h" #include "include/htab.h" #include "include/scope.h" @@ -15,18 +16,32 @@ AST* exec_start(AST* ast) { Scope* global = scope_init(NULL); global->uses = 1; - // Maybe root ast here should set global as scope? + // Keep track of built-in function ASTs, as they arent part of the main + // tree. + // DList* builtins = dlist_init(); + + for (int i = 0; i < BUILTIN_FNS_LN; i++) { + + // AST* builtin_ast = + // ast_init(AST_TYPE_BIF, ast_bif_data_init(BUILTIN_FNS[i].fn)); + + // dlist_append(builtins, builtin_ast); - for (int i = 0; i < BUILTIN_FNS_LN; i++) htab_ins( global->here, BUILTIN_FNS[i].name, ast_init(AST_TYPE_BIF, ast_bif_data_init(BUILTIN_FNS[i].fn)) ); + } log_dbg("Completed startup sequence."); AST* res = exec_exp(ast, global); - log_dbgf("global addr %p uses %d", global, global->uses); + + // Clean up built-in function ASTs. + /*for (int i = 0; i < builtins->ln; i++) ast_destroy(builtins->buf[i]); + dlist_destroy(builtins); + */ + scope_destroy_psv(global); return res; } @@ -35,11 +50,14 @@ AST* exec_exp(AST* ast, Scope* parent) { switch (ast->type) { case AST_TYPE_BLOCK: return exec_block(ast, parent); case AST_TYPE_CALL: return exec_call(ast, parent); - case AST_TYPE_NUM: return ast; - case AST_TYPE_VREF: return exec_vref(ast, parent); - case AST_TYPE_VDEF: return exec_vdef(ast, parent); - case AST_TYPE_FDEF: return exec_fdef(ast, parent); - default: printf("what\n"); exit(1); + case AST_TYPE_NUM: + return ast_init( + AST_TYPE_NUM, ast_num_data_init(*(ASTNumData*)ast->data) + ); + case AST_TYPE_VREF: return exec_vref(ast, parent); + case AST_TYPE_VDEF: return exec_vdef(ast, parent); + case AST_TYPE_FDEF: return exec_fdef(ast, parent); + default: printf("what\n"); exit(1); } } @@ -153,7 +171,6 @@ inline void exec_new_scope(AST* ast, Scope* inherit) { // Update linked status. scope->uses++; - // if (inherit) inherit->uses++; } inline void exec_inherit_scope(AST* ast, Scope* inherit) { diff --git a/src/grammar.y b/src/grammar.y index 8e9ea09..9854f38 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -85,6 +85,7 @@ inputend: %empty | input { root = ast_init(AST_TYPE_BLOCK, ast_block_data_init((AST**) $1->buf, $1->ln)); + dlist_destroypsv($1); } ; diff --git a/src/main.c b/src/main.c index fe5c600..f788a1d 100644 --- a/src/main.c +++ b/src/main.c @@ -19,7 +19,10 @@ int main(int argc, char** argv) { if (argc - 1 && strlen(argv[1]) > 0 && (inp = argv[1]) && !yyparse()) { log_dbg("Parsed successfully!\n"); - ast_print(exec_start(root)); + ast_print(root); + AST* eval = exec_start(root); + ast_print(eval); + ast_destroy(eval); ast_destroy(root); exit(0); } @@ -55,8 +58,9 @@ int main(int argc, char** argv) { ast_print(root); #endif - ast_print(exec_start(root)); - + AST* eval = exec_start(root); + ast_print(eval); + ast_destroy(eval); ast_destroy(root); } diff --git a/src/scope.c b/src/scope.c index 51d3d90..4076e04 100644 --- a/src/scope.c +++ b/src/scope.c @@ -24,12 +24,11 @@ void scope_destroy(Scope* scope) { } void scope_destroy_psv(Scope* scope) { - log_dbgf("%p got here", scope); if (!scope) return; + log_dbgf("%p: destroying", scope); htab_destroy(scope->here); scope->inherit = NULL; free(scope); - log_dbgf("%p got here 2", scope); } inline void scope_add(Scope* scope, char* key, void* val) {