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
#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

View file

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