fix: added new ast type and moved parsing functions to a different header
This commit is contained in:
parent
7b773641a1
commit
37fcdf99be
4 changed files with 96 additions and 22 deletions
|
|
@ -29,35 +29,28 @@ static bool isterminator(char c)
|
|||
/* Parses a simple list of words (command and arguments)
|
||||
* and returns the resulting ast
|
||||
*/
|
||||
static struct ast *parse_simple_command(void)
|
||||
{
|
||||
struct list *cmd_elements = NULL;
|
||||
char *token = get_token();
|
||||
if (token == NULL) // just in case ?
|
||||
return NULL;
|
||||
|
||||
while (token != NULL && !isterminator(token[0]))
|
||||
{
|
||||
cmd_elements = list_append(cmd_elements, token);
|
||||
token = get_token();
|
||||
}
|
||||
|
||||
if (token == NULL)
|
||||
return NULL; // TODO handle error
|
||||
|
||||
struct ast *result = ast_create_cmd(cmd_elements);
|
||||
return result;
|
||||
}
|
||||
|
||||
// === Functions
|
||||
|
||||
struct ast *get_ast()
|
||||
{
|
||||
struct ast *result = NULL;
|
||||
struct list *result_list = NULL;
|
||||
struct ast *current_node = NULL;
|
||||
|
||||
// char *token = get_token();
|
||||
char *token = peek_token();
|
||||
if (token != NULL)
|
||||
{
|
||||
puts("Internal error: cannot get the following token");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (token[0] != EOF)
|
||||
{
|
||||
struct ast *cmd = parse_simple_command();
|
||||
result_list = list_append(result_list, cmd);
|
||||
}
|
||||
|
||||
struct ast *result = ast_create_list(result_list);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
|||
35
src/parser/parsing_utils.c
Normal file
35
src/parser/parsing_utils.c
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
struct ast *parse_simple_command(void)
|
||||
{
|
||||
struct list *cmd_elements = NULL;
|
||||
char *token = pop_token();
|
||||
if (token == NULL) // just in case ?
|
||||
{
|
||||
puts("Internal error: cannot get the following token");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (token != NULL && !isterminator(token[0]))
|
||||
{
|
||||
cmd_elements = list_append(cmd_elements, token);
|
||||
token = pop_token();
|
||||
}
|
||||
|
||||
if (token == NULL)
|
||||
{
|
||||
puts("Internal error: cannot get the following token");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct ast *result = ast_create_cmd(cmd_elements);
|
||||
return result;
|
||||
}
|
||||
|
||||
struct ast *parse_if_rule(void)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct ast *parse_shell_command(void)
|
||||
{
|
||||
return parse_if_rule();
|
||||
}
|
||||
24
src/parser/parsing_utils.h
Normal file
24
src/parser/parsing_utils.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/* @brief Parses a simple list of words (command and arguments)
|
||||
* and returns the resulting ast
|
||||
*/
|
||||
struct ast *parse_simple_command(void);
|
||||
|
||||
/*
|
||||
*/
|
||||
struct ast *parse_if_rule(void);
|
||||
|
||||
/*
|
||||
*/
|
||||
struct ast *parse_shell_command(void);
|
||||
|
||||
/*
|
||||
*/
|
||||
struct ast* parse_compound_list(void);
|
||||
|
||||
/*
|
||||
*/
|
||||
struct ast* parse_and_or(void);
|
||||
|
||||
/*
|
||||
*/
|
||||
struct ast* else_clause(void);
|
||||
Loading…
Add table
Add a link
Reference in a new issue