fix: random fixes
This commit is contained in:
parent
69ee869af0
commit
32e182bd50
3 changed files with 19 additions and 11 deletions
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
// === Static functions
|
// === Static functions
|
||||||
|
|
||||||
enum ast_and_or_type and_or_tok_to_ast(enum token_type tok_type)
|
static enum ast_and_or_type and_or_tok_to_ast(enum token_type tok_type)
|
||||||
{
|
{
|
||||||
switch (tok_type)
|
switch (tok_type)
|
||||||
{
|
{
|
||||||
|
|
@ -47,7 +47,7 @@ struct ast *parse_list(struct lexer_context *ctx)
|
||||||
if (current_node == NULL)
|
if (current_node == NULL)
|
||||||
{
|
{
|
||||||
// TODO free list
|
// TODO free list
|
||||||
// There must be a function for that
|
// There must be a function for that
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
result_list = list_append(result_list, current_node);
|
result_list = list_append(result_list, current_node);
|
||||||
|
|
@ -64,29 +64,35 @@ struct ast *parse_and_or(struct lexer_context *ctx)
|
||||||
|
|
||||||
if (token->type == TOKEN_AND || token->type == TOKEN_OR)
|
if (token->type == TOKEN_AND || token->type == TOKEN_OR)
|
||||||
{
|
{
|
||||||
// set left part
|
// Set left part
|
||||||
|
|
||||||
struct ast *left = result;
|
struct ast *left = result;
|
||||||
|
|
||||||
// eat and_or token
|
// eat and_or token
|
||||||
token = POP_TOKEN();
|
token = POP_TOKEN();
|
||||||
|
|
||||||
// set type
|
// Set type
|
||||||
enum ast_and_or_type type = and_or_tok_to_ast(token->type);
|
enum ast_and_or_type type = and_or_tok_to_ast(token->type);
|
||||||
|
|
||||||
token = PEEK_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();
|
token = PEEK_TOKEN();
|
||||||
}
|
}
|
||||||
|
|
||||||
// right part
|
// Right part
|
||||||
struct ast *right = parse_pipeline(ctx);
|
struct ast *right = parse_pipeline(ctx);
|
||||||
|
|
||||||
result = ast_create_and_or(left, right, type);
|
result = ast_create_and_or(left, right, type);
|
||||||
|
if (result == NULL)
|
||||||
|
{
|
||||||
|
ast_free(&left);
|
||||||
|
ast_free(&right);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
@ -204,6 +210,7 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fi keyword
|
||||||
token = POP_TOKEN();
|
token = POP_TOKEN();
|
||||||
if (token->type != TOKEN_FI)
|
if (token->type != TOKEN_FI)
|
||||||
{
|
{
|
||||||
|
|
@ -214,6 +221,7 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Result
|
||||||
struct ast *result =
|
struct ast *result =
|
||||||
ast_create_if(condition_content, then_content, else_content);
|
ast_create_if(condition_content, then_content, else_content);
|
||||||
if (result == NULL)
|
if (result == NULL)
|
||||||
|
|
@ -241,7 +249,7 @@ struct ast *parse_compound_list(struct lexer_context *ctx)
|
||||||
token = PEEK_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;
|
||||||
|
|
@ -261,7 +269,7 @@ struct ast *parse_compound_list(struct lexer_context *ctx)
|
||||||
token = PEEK_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;
|
||||||
|
|
@ -299,7 +307,7 @@ struct ast *parse_else_clause(struct lexer_context *ctx)
|
||||||
token = POP_TOKEN();
|
token = POP_TOKEN();
|
||||||
struct ast *condition = parse_compound_list(ctx);
|
struct ast *condition = parse_compound_list(ctx);
|
||||||
|
|
||||||
// 'then'
|
// Then keyword
|
||||||
token = POP_TOKEN();
|
token = POP_TOKEN();
|
||||||
if (token->type != TOKEN_THEN)
|
if (token->type != TOKEN_THEN)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ struct ast *parse_list(struct lexer_context *ctx);
|
||||||
|
|
||||||
/* @brief Only parses a pipeline rule for the moment
|
/* @brief Only parses a pipeline rule for the moment
|
||||||
*
|
*
|
||||||
* @code and_or = pipeline ;
|
* @code and_or = pipeline { ( '&&' | '||' ) {'\n'} pipeline } ;
|
||||||
*/
|
*/
|
||||||
struct ast *parse_and_or(struct lexer_context *ctx);
|
struct ast *parse_and_or(struct lexer_context *ctx);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
void ast_free(struct ast **node)
|
void ast_free(struct ast **node)
|
||||||
{
|
{
|
||||||
if (*node == NULL)
|
if (node == NULL || *node == NULL)
|
||||||
return;
|
return;
|
||||||
// ast void does not need to be freed.
|
// ast void does not need to be freed.
|
||||||
if (ast_is_if(*node))
|
if (ast_is_if(*node))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue