Compare commits

..

17 Commits

Author SHA1 Message Date
6380bcc442 Slightly change the valid test input file
All checks were successful
Validate the build / validate-build (push) Successful in 25s
2025-04-02 12:04:42 +02:00
9de9005059 add functionality to main to parse and print the ast 2025-04-02 12:04:42 +02:00
4cb2bf7165 Fix parse_directive grammar rule 2025-04-02 12:04:42 +02:00
251b1b82b1 Add registers and fix section primitive parser 2025-04-02 12:04:42 +02:00
8faf73771b Fix parser loops in parse_any and parse_consecutive 2025-04-02 12:04:42 +02:00
4d8cbc066b Add more grammar rules to the parser 2025-04-02 12:04:42 +02:00
5968997d19 Add a parser combinator to parse a delimited list 2025-04-02 12:04:42 +02:00
eb3b9d6366 TODO: REVIEW ME AND WRITE PROPER MESSAGE
Fix lexer issue where consuming n tokens always fails if there are n
tokens and always succeeds if they aren't n tokens
2025-04-02 12:04:42 +02:00
ff4b01acea Use new validator function for parse_token calls on all primitives
Also adds new validated primitives for NODE_SECTION and NODE_REGISTER
2025-04-02 12:04:42 +02:00
9eec8e5e1a Fix incorrect error returned in parse_consecutive 2025-04-02 12:04:42 +02:00
234f614886 Add basic parser combinators 2025-04-02 12:04:42 +02:00
8ac844c2b0 Add "primitive" parsers for all the semantic tokens in the lexer grammar 2025-04-02 12:04:42 +02:00
56d1054b74 Fix parse_token to add the correct information to a parse node 2025-04-02 12:04:42 +02:00
d7dc6c802e Partial parser implementation 2025-04-02 12:04:42 +02:00
e2fa229c1d Add basic parser utilities 2025-04-02 12:04:36 +02:00
5fb6ebef28 Add functions to skip over trivia in a tokenlist 2025-04-02 11:59:24 +02:00
bbdcad024f Add function to print the AST 2025-04-02 11:50:25 +02:00
2 changed files with 9 additions and 21 deletions

View File

@ -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);
} }

View File

@ -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_