fix(parser): rule if now works

This commit is contained in:
Matteo Flebus 2026-01-22 19:06:58 +01:00
parent 2f2509f1c9
commit 5faa179b63
2 changed files with 18 additions and 13 deletions

View file

@ -2,8 +2,8 @@
#define EXECUTION_H #define EXECUTION_H
#include "../utils/ast/ast.h" #include "../utils/ast/ast.h"
#include "../utils/lists/lists.h"
#include "../utils/hash_map/hash_map.h" #include "../utils/hash_map/hash_map.h"
#include "../utils/lists/lists.h"
/** /**
* @brief Execute the AST * @brief Execute the AST

View file

@ -247,31 +247,34 @@ struct ast *parse_compound_list(struct lexer_context *ctx)
while (token->type == TOKEN_NEWLINE) while (token->type == TOKEN_NEWLINE)
{ {
token = POP_TOKEN(); token = POP_TOKEN();
token = PEEK_TOKEN();
} }
// and_or // and_or
current_cmd = parse_and_or(ctx); current_cmd = parse_and_or(ctx);
if (current_cmd == NULL) if (current_cmd == NULL)
return NULL; return NULL;
list_append(result_list, current_cmd); result_list = list_append(result_list, current_cmd);
// Following commands // Following commands
token = PEEK_TOKEN(); token = PEEK_TOKEN();
while (token->type == TOKEN_SEMICOLON || token->type == TOKEN_NEWLINE) while (token->type == TOKEN_SEMICOLON || token->type == TOKEN_NEWLINE)
{ {
POP_TOKEN(); POP_TOKEN();
token = PEEK_TOKEN();
// Skip newlines // Skip newlines
while (token->type == TOKEN_NEWLINE) while (token->type == TOKEN_NEWLINE)
{ {
token = POP_TOKEN(); token = POP_TOKEN();
token = PEEK_TOKEN();
} }
// and_or // and_or
current_cmd = parse_and_or(ctx); current_cmd = parse_and_or(ctx);
if (current_cmd == NULL) if (current_cmd == NULL)
return NULL; return NULL;
list_append(result_list, current_cmd); result_list = list_append(result_list, current_cmd);
token = PEEK_TOKEN(); token = PEEK_TOKEN();
} }
@ -280,12 +283,14 @@ struct ast *parse_compound_list(struct lexer_context *ctx)
if (token->type == TOKEN_SEMICOLON) if (token->type == TOKEN_SEMICOLON)
{ {
token = POP_TOKEN(); token = POP_TOKEN();
token = PEEK_TOKEN();
} }
// Skip newlines // Skip newlines
while (token->type == TOKEN_NEWLINE) while (token->type == TOKEN_NEWLINE)
{ {
token = POP_TOKEN(); token = POP_TOKEN();
token = PEEK_TOKEN();
} }
struct ast *result = ast_create_list(result_list); 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) if (token->type == TOKEN_ELSE)
{ {
token = POP_TOKEN(); // eat else
result = parse_compound_list(ctx); result = parse_compound_list(ctx);
token = POP_TOKEN(); // Forward
} }
if (result == NULL) if (result == NULL)