feat(parser): parse_list() implemented

This commit is contained in:
Matteo Flebus 2026-01-16 20:12:29 +01:00
parent f33498fb13
commit ed42f0b93d
2 changed files with 50 additions and 16 deletions

View file

@ -17,9 +17,6 @@
struct ast *get_ast() struct ast *get_ast()
{ {
// struct list *result_list = NULL;
// struct ast *current_node = NULL;
struct token *token = PEEK_TOKEN(); struct token *token = PEEK_TOKEN();
if (token->type == TOKEN_EOF) if (token->type == TOKEN_EOF)
@ -31,25 +28,21 @@ struct ast *get_ast()
else if (token->type == TOKEN_NEWLINE) else if (token->type == TOKEN_NEWLINE)
{ {
token = pop_token(); token = pop_token();
// TODO return ast_create_void();
// return ast EMPTY.
} }
else // TOKEN WORD else // TOKEN WORD
{ {
// TODO current_node = parse_list();
// call parse_list
current_node = parse_simple_command();
result_list = list_append(result_list, current_node);
} }
/* /*
if (token == NULL) if (token == NULL)
{ {
puts("Internal error: cannot get the following token"); puts("Internal error: cannot get the following token");
puts("Hint: EOF might be missing"); puts("Hint: EOF might be missing");
return NULL; return NULL;
} }
*/ */
struct ast *result = ast_create_list(result_list); struct ast *result = ast_create_list(result_list);
return result; return result;

View file

@ -28,6 +28,23 @@ static bool isterminator(struct token *token)
} }
} }
/* @brief: returns true if token is an end of list indicator.
*/
static bool is_end_of_list(struct token *token)
{
if (token == NULL)
return false;
switch (token->type)
{
case TOKEN_NEWLINE:
case TOKEN_EOF:
return true;
default:
return false;
}
}
// === Functions // === Functions
/* Parses a simple list of words (command and arguments) /* Parses a simple list of words (command and arguments)
@ -49,6 +66,30 @@ struct ast *parse_simple_command(void)
return result; return result;
} }
struct ast *parse_list(void)
{
struct list *result_list = NULL;
struct ast *current_node = NULL;
struct token *token = PEEK_TOKEN();
while (!is_end_of_list(token))
{
if (token->type == TOKEN_SEMICOLON)
{
result_list = list_append(result_list, current_node);
}
else
{
// TODO use parse_and_or() instead.
current_node = parse_simple_command();
}
token = PEEK_TOKEN();
}
return ast_create_list(result_list);
}
struct ast *parse_if_rule(void) struct ast *parse_if_rule(void)
{ {
// If condition // If condition