diff --git a/src/parser/grammar_basic.c b/src/parser/grammar_basic.c index b90cf68..b7f74c0 100644 --- a/src/parser/grammar_basic.c +++ b/src/parser/grammar_basic.c @@ -9,7 +9,7 @@ // === 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) { @@ -47,7 +47,7 @@ struct ast *parse_list(struct lexer_context *ctx) if (current_node == NULL) { // TODO free list - // There must be a function for that + // There must be a function for that return NULL; } 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) { - // set left part + // Set left part struct ast *left = result; // eat and_or token token = POP_TOKEN(); - // set type + // Set type enum ast_and_or_type type = and_or_tok_to_ast(token->type); token = PEEK_TOKEN(); - // skip newlines + // Skip newlines while (token->type == TOKEN_NEWLINE) { token = POP_TOKEN(); token = PEEK_TOKEN(); } - // right part + // Right part struct ast *right = parse_pipeline(ctx); result = ast_create_and_or(left, right, type); + if (result == NULL) + { + ast_free(&left); + ast_free(&right); + return NULL; + } } return result; @@ -204,6 +210,7 @@ struct ast *parse_if_rule(struct lexer_context *ctx) return NULL; } + // Fi keyword token = POP_TOKEN(); if (token->type != TOKEN_FI) { @@ -214,6 +221,7 @@ struct ast *parse_if_rule(struct lexer_context *ctx) return NULL; } + // Result struct ast *result = ast_create_if(condition_content, then_content, else_content); if (result == NULL) @@ -241,7 +249,7 @@ struct ast *parse_compound_list(struct lexer_context *ctx) token = PEEK_TOKEN(); } - // and_or + // And/or current_cmd = parse_and_or(ctx); if (current_cmd == NULL) return NULL; @@ -261,7 +269,7 @@ struct ast *parse_compound_list(struct lexer_context *ctx) token = PEEK_TOKEN(); } - // and_or + // And/or current_cmd = parse_and_or(ctx); if (current_cmd == NULL) return NULL; @@ -299,7 +307,7 @@ struct ast *parse_else_clause(struct lexer_context *ctx) token = POP_TOKEN(); struct ast *condition = parse_compound_list(ctx); - // 'then' + // Then keyword token = POP_TOKEN(); if (token->type != TOKEN_THEN) { diff --git a/src/parser/grammar_basic.h b/src/parser/grammar_basic.h index 0990e1b..840f05d 100644 --- a/src/parser/grammar_basic.h +++ b/src/parser/grammar_basic.h @@ -15,7 +15,7 @@ struct ast *parse_list(struct lexer_context *ctx); /* @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); diff --git a/src/utils/ast/ast.c b/src/utils/ast/ast.c index e336a73..75e1e7f 100644 --- a/src/utils/ast/ast.c +++ b/src/utils/ast/ast.c @@ -10,7 +10,7 @@ void ast_free(struct ast **node) { - if (*node == NULL) + if (node == NULL || *node == NULL) return; // ast void does not need to be freed. if (ast_is_if(*node))