Well it mostly works now.

Fixed some memory leaks. Implemented some memory leaks.

May need to look into garbage collection in the future.
This commit is contained in:
Jacob Signorovitch 2025-05-17 11:01:20 -04:00
parent 3f30662cde
commit 1d83aa65a4
5 changed files with 36 additions and 23 deletions

View File

@ -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);

View File

@ -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,7 +50,10 @@ 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_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);
@ -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) {

View File

@ -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);
}
;

View File

@ -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);
}

View File

@ -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) {