Compare commits

..

2 Commits

Author SHA1 Message Date
0177d86054 Modify main to use the new print ast functionality
All checks were successful
Validate the build / validate-build (push) Successful in 26s
2025-04-01 23:55:32 +02:00
faaf4111bb TODO: REVIEW THIS FUNCTION Add function to print AST 2025-04-01 23:54:31 +02:00
3 changed files with 134 additions and 2 deletions

122
src/ast.c
View File

@ -1,5 +1,6 @@
#include "ast.h" #include "ast.h"
#include "error.h" #include "error.h"
#include <assert.h>
#include <string.h> #include <string.h>
error_t *err_node_children_cap = &(error_t){ error_t *err_node_children_cap = &(error_t){
@ -83,3 +84,124 @@ error_t *ast_node_add_child(ast_node_t *node, ast_node_t *child) {
return nullptr; return nullptr;
} }
const char *ast_node_id_to_cstr(node_id_t id) {
switch (id) {
case NODE_INVALID:
return "NODE_INVALID";
case NODE_PROGRAM:
return "NODE_PROGRAM";
case NODE_STATEMENT:
return "NODE_STATEMENT";
case NODE_LABEL:
return "NODE_LABEL";
case NODE_DIRECTIVE:
return "NODE_DIRECTIVE";
case NODE_INSTRUCTION:
return "NODE_INSTRUCTION";
case NODE_OPERANDS:
return "NODE_OPERANDS";
case NODE_OPERAND:
return "NODE_OPERAND";
case NODE_IMMEDIATE:
return "NODE_IMMEDIATE";
case NODE_MEMORY:
return "NODE_MEMORY";
case NODE_NUMBER:
return "NODE_NUMBER";
case NODE_LABEL_REFERENCE:
return "NODE_LABEL_REFERENCE";
case NODE_MEMORY_EXPRESSION:
return "NODE_MEMORY_EXPRESSION";
case NODE_REGISTER_EXPRESSION:
return "NODE_REGISTER_EXPRESSION";
case NODE_REGISTER_INDEX:
return "NODE_REGISTER_INDEX";
case NODE_REGISTER_OFFSET:
return "NODE_REGISTER_OFFSET";
case NODE_PLUS_OR_MINUS:
return "NODE_PLUS_OR_MINUS";
case NODE_SECTION_DIRECTIVE:
return "NODE_SECTION_DIRECTIVE";
case NODE_REGISTER:
return "NODE_REGISTER";
case NODE_SECTION:
return "NODE_SECTION";
case NODE_IDENTIFIER:
return "NODE_IDENTIFIER";
case NODE_DECIMAL:
return "NODE_DECIMAL";
case NODE_HEXADECIMAL:
return "NODE_HEXADECIMAL";
case NODE_OCTAL:
return "NODE_OCTAL";
case NODE_BINARY:
return "NODE_BINARY";
case NODE_CHAR:
return "NODE_CHAR";
case NODE_STRING:
return "NODE_STRING";
case NODE_COLON:
return "NODE_COLON";
case NODE_COMMA:
return "NODE_COMMA";
case NODE_LBRACKET:
return "NODE_LBRACKET";
case NODE_RBRACKET:
return "NODE_RBRACKET";
case NODE_PLUS:
return "NODE_PLUS";
case NODE_MINUS:
return "NODE_MINUS";
case NODE_ASTERISK:
return "NODE_ASTERISK";
case NODE_DOT:
return "NODE_DOT";
}
assert(!"Unreachable, weird node id" && id);
__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) {
if (node == NULL) {
return;
}
// Print indentation
for (int i = 0; i < indent; i++) {
printf(" ");
}
// Print node type
printf("%s", ast_node_id_to_cstr(node->id));
// Print token value if available
if (node->token_entry && node->token_entry->token.value) {
printf(" \"%s\"", node->token_entry->token.value);
}
printf("\n");
// Recursively print all children with increased indentation
for (size_t i = 0; i < node->len; i++) {
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) {
ast_node_print_internal(node, 0);
}

View File

@ -106,4 +106,7 @@ 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);
void ast_node_print(ast_node_t *node);
#endif // INCLUDE_SRC_AST_H_ #endif // INCLUDE_SRC_AST_H_

View File

@ -32,13 +32,20 @@ void print_text(tokenlist_t *list) {
} }
} }
void parse_ast(tokenlist_t *list) { void print_ast(tokenlist_t *list) {
parse_result_t result = parse(list->head); parse_result_t result = parse(list->head);
if (result.err) { if (result.err) {
puts(result.err->message); puts(result.err->message);
error_free(result.err); error_free(result.err);
return; return;
} }
ast_node_print(result.node);
if (result.next != nullptr) {
puts("First unparsed token:");
lexer_token_print(&result.next->token);
}
ast_node_free(result.node); ast_node_free(result.node);
} }
@ -82,7 +89,7 @@ int main(int argc, char *argv[]) {
print_text(list); print_text(list);
break; break;
case MODE_AST: case MODE_AST:
parse_ast(list); print_ast(list);
break; break;
} }