aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorc+12023-11-21 13:58:06 -0500
committerc+12023-11-21 13:58:06 -0500
commita2a5e863dc9beb4bc7a73a7361d8e208599134c9 (patch)
tree13acb631f31b7850842c59149a0902280fd023a9
parentc3c0f8f78085bdc4698263513aea0c9b2d33bae4 (diff)
Memory leaks alleviated.
-rw-r--r--src/include/tree.h14
-rw-r--r--src/main.c4
-rw-r--r--src/tree.c72
3 files changed, 64 insertions, 26 deletions
diff --git a/src/include/tree.h b/src/include/tree.h
index ba1f5a5..3d3bcbd 100644
--- a/src/include/tree.h
+++ b/src/include/tree.h
@@ -51,32 +51,32 @@ typedef struct TREE {
/* Tags. */
struct TREE_DATA_TAG {
char* val;
- struct TREE_DATA_TAG* nxt;
+ struct TREE* nxt; /* TAG* */
} tag;
/* Definition arguments. */
struct TREE_DATA_DARG {
- struct TREE_DATA_TAG* tag;
- struct TREE_DATA_DARG* nxt;
+ struct TREE* tag; /* TAG* */
+ struct TREE* nxt; /* DARG */
} darg;
/* Call arguments. */
struct TREE_DATA_CARG {
struct TREE* val;
- struct TREE_DATA_CARG* nxt;
+ struct TREE* nxt; /* CARG */
} carg;
/* Definitions. */
struct TREE_DATA_DEF {
- struct TREE_DATA_TAG* tag;
- struct TREE_DATA_DARG* arg;
+ struct TREE* tag; /* TAG */
+ struct TREE* arg; /* DARG */
struct TREE* val;
} def;
/* Calls. */
struct TREE_DATA_CAL {
char* target;
- struct TREE_DATA_CARG* arg;
+ struct TREE* arg; /* CARG */
} cal;
} data;
diff --git a/src/main.c b/src/main.c
index 773134e..710cbe0 100644
--- a/src/main.c
+++ b/src/main.c
@@ -52,11 +52,9 @@ int main(int argc, char* argv[]) {
tree_print(parser->tree, 0);
/* Clean up parser stuff. */
+ token_destroy(lexer->tokenl);
tree_destroy(parser->tree);
parser_destroy(parser);
-
- /* clean up lexer stuff */
- if (lexer->tokenl) { token_destroy(lexer->tokenl); } /* temp until parser eats tokens */
lexer_destroy(lexer);
free(src);
diff --git a/src/tree.c b/src/tree.c
index 3dfa761..83f36bc 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -3,7 +3,7 @@
tree_t* tree_init(int type) {
tree_t* tree;
- tree = emalloc(sizeof(struct TREE));
+ tree = emalloc(sizeof(tree_t));
tree->type = type;
@@ -48,6 +48,44 @@ tree_t* tree_init(int type) {
}
void tree_destroy(tree_t* tree) {
+ if (!tree) { return; }
+
+ switch (tree->type) {
+ case TREE_TYPE_BLOCK:
+ tree_destroy(tree->data.block.val);
+ tree_destroy(tree->data.block.nxt);
+ break;
+ case TREE_TYPE_EXPR:
+ tree_destroy(tree->data.expr.val);
+ break;
+ case TREE_TYPE_LINT:
+ break;
+ case TREE_TYPE_LSTR:
+ free(tree->data.lstr.val);
+ break;
+ case TREE_TYPE_TAG:
+ free(tree->data.tag.val);
+ tree_destroy(tree->data.tag.nxt);
+ break;
+ case TREE_TYPE_DARG:
+ free(tree->data.darg.tag);
+ tree_destroy(tree->data.darg.nxt);
+ break;
+ case TREE_TYPE_CARG:
+ free(tree->data.carg.val);
+ tree_destroy(tree->data.carg.nxt);
+ break;
+ case TREE_TYPE_DEF:
+ free(tree->data.def.tag);
+ free(tree->data.def.arg);
+ tree_destroy(tree->data.def.val);
+ break;
+ case TREE_TYPE_CAL:
+ free(tree->data.cal.target);
+ tree_destroy(tree->data.cal.arg);
+ break;
+ }
+
free(tree);
}
@@ -55,42 +93,44 @@ void tree_print(tree_t* tree, int nest) {
char* spaces;
int i;
- for (spaces = ecalloc(nest + 1, sizeof(char)), i = 0; i < nest; spaces[i++] = ' ');
- spaces[i] = '\0';
+ /* Aaahhh. */
+ for(i=0,spaces=ecalloc(nest+1,sizeof(char)),spaces[nest]='\0';i<=nest-1;spaces[i++]=' ');
-#define NEST(TEXT) log_raw("%s" TEXT "\n", spaces);
+ #define NEST(TEXT) log_raw("%s"TEXT"\n",spaces);
+ #define NEST2(TEXT) log_raw("%s"TEXT,spaces);
if (!tree) {
NEST("NULL");
- return;
+ goto tree_print_exit;
}
switch (tree->type) {
case TREE_TYPE_BLOCK:
NEST("[block]");
- NEST(" - val:");
- tree_print(tree->data.block.val, nest + 3);
- NEST(" - nxt:");
- tree_print(tree->data.block.nxt, nest + 3);
+ NEST("val:");
+ tree_print(tree->data.block.val, nest + 1);
+ NEST("nxt:");
+ tree_print(tree->data.block.nxt, nest + 1);
break;
case TREE_TYPE_EXPR:
NEST("[expression]");
- NEST(" - val:");
- tree_print(tree->data.expr.val, nest + 3);
- break;
+ NEST("val:");
+ tree_print(tree->data.expr.val, nest + 1);
+ break;
case TREE_TYPE_LINT:
- log_raw("%s[lint]: %d\n", spaces, tree->data.lint.val);
+ NEST("[lint]");
+ NEST("val:");
+ log_raw("%s %d\n", spaces, tree->data.lint.val);
break;
case TREE_TYPE_LSTR:
- log_raw("%s[lstr]: %s\n", spaces, tree->data.lstr.val);
- break;
case TREE_TYPE_TAG:
case TREE_TYPE_DARG:
case TREE_TYPE_CARG:
case TREE_TYPE_DEF:
case TREE_TYPE_CAL:
- NEST("??");
+ NEST("???");
break;
}
+ tree_print_exit: free(spaces); return;
}