added debug statements
This commit is contained in:
parent
914935aafb
commit
c6d8651ab6
|
@ -1,11 +1,12 @@
|
||||||
|
|
||||||
#include <ast/ast.h>
|
#include <ast/ast.h>
|
||||||
#include <bits/posix2_lim.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/log.h>
|
#include <sys/log.h>
|
||||||
|
|
||||||
struct AST_Node_t *AST_new_node(enum AST_SyntaxElement_t kind, const char* value) {
|
struct AST_Node_t *AST_new_node(enum AST_SyntaxElement_t kind, const char* value) {
|
||||||
|
DEBUG("creating new AST node: %d \"%s\"", kind, value);
|
||||||
|
|
||||||
struct AST_Node_t *node = malloc(sizeof(struct AST_Node_t));
|
struct AST_Node_t *node = malloc(sizeof(struct AST_Node_t));
|
||||||
|
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
|
@ -23,6 +24,8 @@ struct AST_Node_t *AST_new_node(enum AST_SyntaxElement_t kind, const char* value
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* AST_node_to_string(struct AST_Node_t* node) {
|
const char* AST_node_to_string(struct AST_Node_t* node) {
|
||||||
|
DEBUG("converting AST node to string: %p", node);
|
||||||
|
|
||||||
const char* string = "unknown";
|
const char* string = "unknown";
|
||||||
|
|
||||||
switch (node->kind) {
|
switch (node->kind) {
|
||||||
|
@ -47,12 +50,16 @@ const char* AST_node_to_string(struct AST_Node_t* node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void AST_push_node(struct AST_Node_t *owner, struct AST_Node_t *child) {
|
void AST_push_node(struct AST_Node_t *owner, struct AST_Node_t *child) {
|
||||||
|
DEBUG("Adding new node %p to %p", child, owner);
|
||||||
|
|
||||||
// if there are no children for now
|
// if there are no children for now
|
||||||
if (owner->child_count == 0) {
|
if (owner->child_count == 0) {
|
||||||
|
DEBUG("Allocating new children array");
|
||||||
owner->children = malloc(sizeof(struct AST_Node_t *));
|
owner->children = malloc(sizeof(struct AST_Node_t *));
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
size_t size = sizeof(struct AST_Node_t *) * (owner->child_count + 1);
|
DEBUG("Rellocating old children array");
|
||||||
|
const size_t size = sizeof(struct AST_Node_t *) * (owner->child_count + 1);
|
||||||
owner->children = realloc(owner->children, size);
|
owner->children = realloc(owner->children, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,6 +71,8 @@ void AST_push_node(struct AST_Node_t *owner, struct AST_Node_t *child) {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AST_Node_t *AST_get_node(struct AST_Node_t *owner, size_t idx) {
|
struct AST_Node_t *AST_get_node(struct AST_Node_t *owner, size_t idx) {
|
||||||
|
DEBUG("retrvieng node %d from %p", idx, owner);
|
||||||
|
|
||||||
if (owner == NULL) {
|
if (owner == NULL) {
|
||||||
PANIC("AST owner node is NULL");
|
PANIC("AST owner node is NULL");
|
||||||
}
|
}
|
||||||
|
@ -82,22 +91,28 @@ struct AST_Node_t *AST_get_node(struct AST_Node_t *owner, size_t idx) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void AST_delete_node(struct AST_Node_t *node) {
|
void AST_delete_node(struct AST_Node_t *node) {
|
||||||
|
DEBUG("Deleting AST node: %p", node);
|
||||||
|
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
PANIC("Node to free is NULL");
|
PANIC("Node to free is NULL");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node->children != NULL) {
|
if (node->children == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < node->child_count; i++) {
|
for (size_t i = 0; i < node->child_count; i++) {
|
||||||
AST_delete_node(node->children[i]);
|
AST_delete_node(node->children[i]);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void __AST_visit_nodes_recurse2(struct AST_Node_t *root,
|
static void __AST_visit_nodes_recurse2(struct AST_Node_t *root,
|
||||||
void (*for_each)(struct AST_Node_t *node,
|
void (*for_each)(struct AST_Node_t *node,
|
||||||
size_t depth),
|
size_t depth),
|
||||||
size_t depth) {
|
size_t depth) {
|
||||||
(for_each)(root, 0);
|
DEBUG("Recursive visit of %p at %d with %p", root, depth, for_each);
|
||||||
|
|
||||||
|
(for_each)(root, depth);
|
||||||
|
|
||||||
for (size_t i = 0; i < root->child_count; i++) {
|
for (size_t i = 0; i < root->child_count; i++) {
|
||||||
__AST_visit_nodes_recurse2(root->children[i], for_each, depth + 1);
|
__AST_visit_nodes_recurse2(root->children[i], for_each, depth + 1);
|
||||||
|
@ -107,35 +122,44 @@ static void __AST_visit_nodes_recurse2(struct AST_Node_t *root,
|
||||||
void AST_visit_nodes_recurse(struct AST_Node_t *root,
|
void AST_visit_nodes_recurse(struct AST_Node_t *root,
|
||||||
void (*for_each)(struct AST_Node_t *node,
|
void (*for_each)(struct AST_Node_t *node,
|
||||||
size_t depth)) {
|
size_t depth)) {
|
||||||
|
DEBUG("Starting recursive visit of %p with %p", root, for_each);
|
||||||
__AST_visit_nodes_recurse2(root, for_each, 0);
|
__AST_visit_nodes_recurse2(root, for_each, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AST_fprint_graphviz_node_definition(FILE* stream, struct AST_Node_t* node) {
|
static void __AST_fprint_graphviz_node_definition(FILE* stream, struct AST_Node_t* node) {
|
||||||
|
DEBUG("Printing graphviz definition of %p", node);
|
||||||
|
|
||||||
fprintf(stream, "\tnode%p [label=\"%s\"]\n", (void*) node, AST_node_to_string(node));
|
fprintf(stream, "\tnode%p [label=\"%s\"]\n", (void*) node, AST_node_to_string(node));
|
||||||
|
|
||||||
if (node->children != NULL) {
|
if (node->children == NULL) {
|
||||||
for (size_t i = 0; i < node->child_count; i++) {
|
return;
|
||||||
AST_fprint_graphviz_node_definition(stream, node->children[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < node->child_count; i++) {
|
||||||
|
__AST_fprint_graphviz_node_definition(stream, node->children[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AST_fprint_graphviz_node_connection(FILE* stream, struct AST_Node_t* node) {
|
static void __AST_fprint_graphviz_node_connection(FILE* stream, struct AST_Node_t* node) {
|
||||||
|
DEBUG("Printing graphviz connection of %p", node);
|
||||||
|
|
||||||
|
if (node->children == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (node->children != NULL) {
|
|
||||||
for (size_t i = 0; i < node->child_count; i++) {
|
for (size_t i = 0; i < node->child_count; i++) {
|
||||||
fprintf(stream, "\tnode%p -- node%p\n", (void*) node, (void*) node->children[i]);
|
fprintf(stream, "\tnode%p -- node%p\n", (void*) node, (void*) node->children[i]);
|
||||||
AST_fprint_graphviz_node_connection(stream, node->children[i]);
|
__AST_fprint_graphviz_node_connection(stream, node->children[i]);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AST_fprint_graphviz(FILE* stream, struct AST_Node_t* root) {
|
void AST_fprint_graphviz(FILE* stream, struct AST_Node_t* root) {
|
||||||
|
DEBUG("Starting print of graphviz graph of %p", root);
|
||||||
|
|
||||||
fprintf(stream, "graph {\n");
|
fprintf(stream, "graph {\n");
|
||||||
|
|
||||||
AST_fprint_graphviz_node_definition(stream, root);
|
__AST_fprint_graphviz_node_definition(stream, root);
|
||||||
AST_fprint_graphviz_node_connection(stream, root);
|
__AST_fprint_graphviz_node_connection(stream, root);
|
||||||
|
|
||||||
fprintf(stream, "}\n");
|
fprintf(stream, "}\n");
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/log.h>
|
#include <sys/log.h>
|
||||||
#include <yacc/parser.tab.h>
|
|
||||||
|
|
||||||
#define LOG_LEVEL LOG_LEVEL_DEBUG
|
#define LOG_LEVEL LOG_LEVEL_DEBUG
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue