Compare commits
17 Commits
3a737c05d5
...
6380bcc442
Author | SHA1 | Date | |
---|---|---|---|
6380bcc442 | |||
9de9005059 | |||
4cb2bf7165 | |||
251b1b82b1 | |||
8faf73771b | |||
4d8cbc066b | |||
5968997d19 | |||
eb3b9d6366 | |||
ff4b01acea | |||
9eec8e5e1a | |||
234f614886 | |||
8ac844c2b0 | |||
56d1054b74 | |||
d7dc6c802e | |||
e2fa229c1d | |||
5fb6ebef28 | |||
bbdcad024f |
20
src/ast.c
20
src/ast.c
@ -162,46 +162,26 @@ const char *ast_node_id_to_cstr(node_id_t id) {
|
|||||||
__builtin_unreachable();
|
__builtin_unreachable();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Helper function to print a single AST node with indentation
|
|
||||||
*
|
|
||||||
* @param node The node to print
|
|
||||||
* @param indent Current indentation level
|
|
||||||
*/
|
|
||||||
static void ast_node_print_internal(ast_node_t *node, int indent) {
|
static void ast_node_print_internal(ast_node_t *node, int indent) {
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print indentation
|
|
||||||
for (int i = 0; i < indent; i++) {
|
for (int i = 0; i < indent; i++) {
|
||||||
printf(" ");
|
printf(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print node type
|
|
||||||
printf("%s", ast_node_id_to_cstr(node->id));
|
printf("%s", ast_node_id_to_cstr(node->id));
|
||||||
|
|
||||||
// Print token value if available
|
|
||||||
if (node->token_entry && node->token_entry->token.value) {
|
if (node->token_entry && node->token_entry->token.value) {
|
||||||
printf(" \"%s\"", node->token_entry->token.value);
|
printf(" \"%s\"", node->token_entry->token.value);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
// Recursively print all children with increased indentation
|
|
||||||
for (size_t i = 0; i < node->len; i++) {
|
for (size_t i = 0; i < node->len; i++) {
|
||||||
ast_node_print_internal(node->children[i], indent + 1);
|
ast_node_print_internal(node->children[i], indent + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Prints an AST starting from the given node
|
|
||||||
*
|
|
||||||
* Prints a representation of the AST with indentation to show structure.
|
|
||||||
* Each node's type is shown, and if a node has an associated token value,
|
|
||||||
* that value is printed in quotes.
|
|
||||||
*
|
|
||||||
* @param node The root node of the AST to print
|
|
||||||
*/
|
|
||||||
void ast_node_print(ast_node_t *node) {
|
void ast_node_print(ast_node_t *node) {
|
||||||
ast_node_print_internal(node, 0);
|
ast_node_print_internal(node, 0);
|
||||||
}
|
}
|
||||||
|
10
src/ast.h
10
src/ast.h
@ -106,7 +106,15 @@ void ast_node_free(ast_node_t *node);
|
|||||||
*/
|
*/
|
||||||
error_t *ast_node_add_child(ast_node_t *node, ast_node_t *child);
|
error_t *ast_node_add_child(ast_node_t *node, ast_node_t *child);
|
||||||
|
|
||||||
const char *ast_node_id_to_cstr(node_id_t id);
|
/**
|
||||||
|
* @brief Prints an AST starting from the given node
|
||||||
|
*
|
||||||
|
* Prints a representation of the AST with indentation to show structure.
|
||||||
|
* Each node's type is shown, and if a node has an associated token value,
|
||||||
|
* that value is printed in quotes.
|
||||||
|
*
|
||||||
|
* @param node The root node of the AST to print
|
||||||
|
*/
|
||||||
void ast_node_print(ast_node_t *node);
|
void ast_node_print(ast_node_t *node);
|
||||||
|
|
||||||
#endif // INCLUDE_SRC_AST_H_
|
#endif // INCLUDE_SRC_AST_H_
|
||||||
|
Loading…
x
Reference in New Issue
Block a user