From 5faa179b63e49b6f0668f794094a4c77c5249a98 Mon Sep 17 00:00:00 2001 From: Matteo Flebus Date: Thu, 22 Jan 2026 19:06:58 +0100 Subject: [PATCH] fix(parser): rule if now works --- src/execution/execution.h | 2 +- src/parser/parsing_utils.c | 29 +++++++++++++++++------------ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/execution/execution.h b/src/execution/execution.h index 872f0ad..c3e0080 100644 --- a/src/execution/execution.h +++ b/src/execution/execution.h @@ -2,8 +2,8 @@ #define EXECUTION_H #include "../utils/ast/ast.h" -#include "../utils/lists/lists.h" #include "../utils/hash_map/hash_map.h" +#include "../utils/lists/lists.h" /** * @brief Execute the AST diff --git a/src/parser/parsing_utils.c b/src/parser/parsing_utils.c index 3be4f05..2273654 100644 --- a/src/parser/parsing_utils.c +++ b/src/parser/parsing_utils.c @@ -78,16 +78,16 @@ struct ast *parse_list(struct lexer_context *ctx) while (token->type == TOKEN_SEMICOLON) { token = POP_TOKEN(); - // if (!isterminator(token)) // Follow(list) + // if (!isterminator(token)) // Follow(list) // { - current_node = parse_and_or(ctx); - if (current_node == NULL) - { - // TODO free list - // There must be a function for that - return NULL; - } - result_list = list_append(result_list, current_node); + current_node = parse_and_or(ctx); + if (current_node == NULL) + { + // TODO free list + // There must be a function for that + return NULL; + } + result_list = list_append(result_list, current_node); // } token = PEEK_TOKEN(); } @@ -247,31 +247,34 @@ struct ast *parse_compound_list(struct lexer_context *ctx) while (token->type == TOKEN_NEWLINE) { token = POP_TOKEN(); + token = PEEK_TOKEN(); } // and_or current_cmd = parse_and_or(ctx); if (current_cmd == NULL) return NULL; - list_append(result_list, current_cmd); + result_list = list_append(result_list, current_cmd); // Following commands token = PEEK_TOKEN(); while (token->type == TOKEN_SEMICOLON || token->type == TOKEN_NEWLINE) { POP_TOKEN(); + token = PEEK_TOKEN(); // Skip newlines while (token->type == TOKEN_NEWLINE) { token = POP_TOKEN(); + token = PEEK_TOKEN(); } // and_or current_cmd = parse_and_or(ctx); if (current_cmd == NULL) return NULL; - list_append(result_list, current_cmd); + result_list = list_append(result_list, current_cmd); token = PEEK_TOKEN(); } @@ -280,12 +283,14 @@ struct ast *parse_compound_list(struct lexer_context *ctx) if (token->type == TOKEN_SEMICOLON) { token = POP_TOKEN(); + token = PEEK_TOKEN(); } // Skip newlines while (token->type == TOKEN_NEWLINE) { token = POP_TOKEN(); + token = PEEK_TOKEN(); } struct ast *result = ast_create_list(result_list); @@ -333,8 +338,8 @@ struct ast *parse_else_clause(struct lexer_context *ctx) if (token->type == TOKEN_ELSE) { + token = POP_TOKEN(); // eat else result = parse_compound_list(ctx); - token = POP_TOKEN(); // Forward } if (result == NULL)