diff --git a/src/parser/parser.c b/src/parser/parser.c index b9c07e7..5edae88 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -17,9 +17,6 @@ struct ast *get_ast() { - // struct list *result_list = NULL; - // struct ast *current_node = NULL; - struct token *token = PEEK_TOKEN(); if (token->type == TOKEN_EOF) @@ -31,25 +28,21 @@ struct ast *get_ast() else if (token->type == TOKEN_NEWLINE) { token = pop_token(); - // TODO - // return ast EMPTY. + return ast_create_void(); } else // TOKEN WORD { - // TODO - // call parse_list - current_node = parse_simple_command(); - result_list = list_append(result_list, current_node); + current_node = parse_list(); } /* - if (token == NULL) - { - puts("Internal error: cannot get the following token"); - puts("Hint: EOF might be missing"); - return NULL; - } - */ + if (token == NULL) + { + puts("Internal error: cannot get the following token"); + puts("Hint: EOF might be missing"); + return NULL; + } + */ struct ast *result = ast_create_list(result_list); return result; diff --git a/src/parser/parsing_utils.c b/src/parser/parsing_utils.c index b0c6618..c07653e 100644 --- a/src/parser/parsing_utils.c +++ b/src/parser/parsing_utils.c @@ -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 /* Parses a simple list of words (command and arguments) @@ -49,6 +66,30 @@ struct ast *parse_simple_command(void) 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) { // If condition