Fixed some memory bugs.
This commit is contained in:
parent
743d16f696
commit
3f30662cde
15
src/ast.c
15
src/ast.c
@ -53,7 +53,20 @@ void ast_destroy(AST* ast) {
|
|||||||
log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX);
|
log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX);
|
||||||
}
|
}
|
||||||
|
|
||||||
scope_destroy_psv(ast->scope);
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
free(ast);
|
free(ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
40
src/exec.c
40
src/exec.c
@ -13,6 +13,9 @@ AST* exec_start(AST* ast) {
|
|||||||
log_dbg("Started execution.");
|
log_dbg("Started execution.");
|
||||||
|
|
||||||
Scope* global = scope_init(NULL);
|
Scope* global = scope_init(NULL);
|
||||||
|
global->uses = 1;
|
||||||
|
|
||||||
|
// Maybe root ast here should set global as scope?
|
||||||
|
|
||||||
for (int i = 0; i < BUILTIN_FNS_LN; i++)
|
for (int i = 0; i < BUILTIN_FNS_LN; i++)
|
||||||
htab_ins(
|
htab_ins(
|
||||||
@ -22,7 +25,10 @@ AST* exec_start(AST* ast) {
|
|||||||
|
|
||||||
log_dbg("Completed startup sequence.");
|
log_dbg("Completed startup sequence.");
|
||||||
|
|
||||||
return exec_exp(ast, global);
|
AST* res = exec_exp(ast, global);
|
||||||
|
log_dbgf("global addr %p uses %d", global, global->uses);
|
||||||
|
scope_destroy_psv(global);
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* exec_exp(AST* ast, Scope* parent) {
|
AST* exec_exp(AST* ast, Scope* parent) {
|
||||||
@ -41,7 +47,10 @@ AST* exec_block(AST* ast, Scope* parent) {
|
|||||||
ASTBlockData* block = (ASTBlockData*)ast->data;
|
ASTBlockData* block = (ASTBlockData*)ast->data;
|
||||||
|
|
||||||
// Blocks create their own scope, shared among their expressions.
|
// Blocks create their own scope, shared among their expressions.
|
||||||
ast->scope = scope_init(parent);
|
// ast->scope = scope_init(parent);
|
||||||
|
|
||||||
|
// HERE
|
||||||
|
exec_new_scope(ast, parent);
|
||||||
|
|
||||||
// Loop through all but last ast.
|
// Loop through all but last ast.
|
||||||
for (int i = 0; i < block->ln - 1; i++)
|
for (int i = 0; i < block->ln - 1; i++)
|
||||||
@ -91,7 +100,11 @@ AST* exec_cf(AST* ast, size_t argc, AST** argv) {
|
|||||||
|
|
||||||
AST* exec_vdef(AST* ast, Scope* parent) {
|
AST* exec_vdef(AST* ast, Scope* parent) {
|
||||||
// Use parent's scope.
|
// Use parent's scope.
|
||||||
ast->scope = parent;
|
// ast->scope = parent;
|
||||||
|
|
||||||
|
// HERE
|
||||||
|
exec_inherit_scope(ast, parent);
|
||||||
|
|
||||||
ASTVDefData* data = (ASTVDefData*)ast->data;
|
ASTVDefData* data = (ASTVDefData*)ast->data;
|
||||||
AST* val = data->val;
|
AST* val = data->val;
|
||||||
char* key = data->name;
|
char* key = data->name;
|
||||||
@ -101,7 +114,10 @@ AST* exec_vdef(AST* ast, Scope* parent) {
|
|||||||
|
|
||||||
AST* exec_vref(AST* ast, Scope* parent) {
|
AST* exec_vref(AST* ast, Scope* parent) {
|
||||||
// Use parent's scope.
|
// Use parent's scope.
|
||||||
ast->scope = parent;
|
// ast->scope = parent;
|
||||||
|
|
||||||
|
// HERE
|
||||||
|
exec_inherit_scope(ast, parent);
|
||||||
log_dbg("attempting to reference var");
|
log_dbg("attempting to reference var");
|
||||||
ASTVrefData* vref = (ASTVrefData*)ast->data;
|
ASTVrefData* vref = (ASTVrefData*)ast->data;
|
||||||
|
|
||||||
@ -130,3 +146,19 @@ AST* exec_fdef(AST* ast, Scope* parent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void exec_print(double n) { printf("= %lf\n", n); }
|
void exec_print(double n) { printf("= %lf\n", n); }
|
||||||
|
|
||||||
|
inline void exec_new_scope(AST* ast, Scope* inherit) {
|
||||||
|
Scope* scope = scope_init(inherit);
|
||||||
|
ast->scope = scope;
|
||||||
|
|
||||||
|
// Update linked status.
|
||||||
|
scope->uses++;
|
||||||
|
// if (inherit) inherit->uses++;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void exec_inherit_scope(AST* ast, Scope* inherit) {
|
||||||
|
ast->scope = inherit;
|
||||||
|
|
||||||
|
// Update uses.
|
||||||
|
inherit->uses++;
|
||||||
|
}
|
||||||
|
@ -12,8 +12,7 @@ HTab* htab_init() {
|
|||||||
return htab;
|
return htab;
|
||||||
}
|
}
|
||||||
|
|
||||||
void htab_destroy(HTab* htab) { // free(htab);
|
void htab_destroy(HTab* htab) { free(htab); }
|
||||||
}
|
|
||||||
|
|
||||||
// Get the index of a key.
|
// Get the index of a key.
|
||||||
size_t geti(char* key) {
|
size_t geti(char* key) {
|
||||||
|
@ -23,4 +23,10 @@ AST* exec_fdef(AST* ast, Scope* parent);
|
|||||||
// Print the result of an execution.
|
// Print the result of an execution.
|
||||||
void exec_print(double n);
|
void exec_print(double n);
|
||||||
|
|
||||||
|
// Create a new scope and mark it as linked. Also update inherited scope.
|
||||||
|
void exec_new_scope(AST* ast, Scope* inherit);
|
||||||
|
|
||||||
|
// Inherit from another scope and mark it as linked.
|
||||||
|
void exec_inherit_scope(AST* ast, Scope* inherit);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -5,8 +5,9 @@
|
|||||||
|
|
||||||
// Represents the reverse linked tree of scope.
|
// Represents the reverse linked tree of scope.
|
||||||
typedef struct SCOPE_T {
|
typedef struct SCOPE_T {
|
||||||
HTab* here;
|
HTab* here; // This scope's hash table.
|
||||||
struct SCOPE_T* inherit;
|
struct SCOPE_T* inherit; // The scope to inherit from.
|
||||||
|
int uses; // How many `AST`s are linked to this scope.
|
||||||
} Scope;
|
} Scope;
|
||||||
|
|
||||||
// Create a new `Scope`. Creates new empty `HTab` for current scope.
|
// Create a new `Scope`. Creates new empty `HTab` for current scope.
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#include "include/scope.h"
|
#include "include/scope.h"
|
||||||
#include "include/htab.h"
|
#include "include/htab.h"
|
||||||
|
#include "include/util.h"
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
Scope* scope_init(Scope* inherit) {
|
Scope* scope_init(Scope* inherit) {
|
||||||
@ -7,6 +9,9 @@ Scope* scope_init(Scope* inherit) {
|
|||||||
|
|
||||||
scope->here = htab_init();
|
scope->here = htab_init();
|
||||||
scope->inherit = inherit;
|
scope->inherit = inherit;
|
||||||
|
scope->uses = 0;
|
||||||
|
|
||||||
|
log_dbgf("%p: new scope, inherits from %p", scope, inherit);
|
||||||
|
|
||||||
return scope;
|
return scope;
|
||||||
}
|
}
|
||||||
@ -19,10 +24,12 @@ void scope_destroy(Scope* scope) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void scope_destroy_psv(Scope* scope) {
|
void scope_destroy_psv(Scope* scope) {
|
||||||
|
log_dbgf("%p got here", scope);
|
||||||
if (!scope) return;
|
if (!scope) return;
|
||||||
htab_destroy(scope->here);
|
htab_destroy(scope->here);
|
||||||
scope->inherit = NULL;
|
scope->inherit = NULL;
|
||||||
free(scope);
|
free(scope);
|
||||||
|
log_dbgf("%p got here 2", scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void scope_add(Scope* scope, char* key, void* val) {
|
inline void scope_add(Scope* scope, char* key, void* val) {
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit cdf1d0297effc2736ee847e557b4275b4f02310b
|
Subproject commit 73237c5d224169c7b4d2ec8321f9ac92e8071708
|
Loading…
x
Reference in New Issue
Block a user