fix: merge conflict
This commit is contained in:
commit
69ee869af0
6 changed files with 118 additions and 40 deletions
|
|
@ -7,6 +7,24 @@
|
|||
#include "../utils/lists/lists.h"
|
||||
#include "grammar.h"
|
||||
|
||||
// === Static functions
|
||||
|
||||
enum ast_and_or_type and_or_tok_to_ast(enum token_type tok_type)
|
||||
{
|
||||
switch (tok_type)
|
||||
{
|
||||
case TOKEN_AND:
|
||||
return AST_AND_OR_TYPE_AND;
|
||||
case TOKEN_OR:
|
||||
return AST_AND_OR_TYPE_OR;
|
||||
default:
|
||||
fprintf(stderr, "and_or impossible to init, wrong token type");
|
||||
return AST_AND_OR_NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// === Functions
|
||||
|
||||
struct ast *parse_list(struct lexer_context *ctx)
|
||||
{
|
||||
struct list *result_list = NULL;
|
||||
|
|
@ -41,7 +59,37 @@ struct ast *parse_list(struct lexer_context *ctx)
|
|||
|
||||
struct ast *parse_and_or(struct lexer_context *ctx)
|
||||
{
|
||||
return parse_pipeline(ctx);
|
||||
struct ast *result = parse_pipeline(ctx);
|
||||
struct token *token = PEEK_TOKEN();
|
||||
|
||||
if (token->type == TOKEN_AND || token->type == TOKEN_OR)
|
||||
{
|
||||
// set left part
|
||||
|
||||
struct ast *left = result;
|
||||
|
||||
// eat and_or token
|
||||
token = POP_TOKEN();
|
||||
|
||||
// set type
|
||||
enum ast_and_or_type type = and_or_tok_to_ast(token->type);
|
||||
|
||||
token = PEEK_TOKEN();
|
||||
|
||||
// skip newlines
|
||||
while (token->type == TOKEN_NEWLINE)
|
||||
{
|
||||
token = POP_TOKEN();
|
||||
token = PEEK_TOKEN();
|
||||
}
|
||||
|
||||
// right part
|
||||
struct ast *right = parse_pipeline(ctx);
|
||||
|
||||
result = ast_create_and_or(left, right, type);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
struct ast *parse_pipeline(struct lexer_context *ctx)
|
||||
|
|
@ -190,31 +238,34 @@ struct ast *parse_compound_list(struct lexer_context *ctx)
|
|||
while (token->type == TOKEN_NEWLINE)
|
||||
{
|
||||
token = POP_TOKEN();
|
||||
token = PEEK_TOKEN();
|
||||
}
|
||||
|
||||
// and_or
|
||||
current_cmd = parse_and_or(ctx);
|
||||
if (current_cmd == NULL)
|
||||
return NULL;
|
||||
list_append(result_list, current_cmd);
|
||||
result_list = list_append(result_list, current_cmd);
|
||||
|
||||
// Following commands
|
||||
token = PEEK_TOKEN();
|
||||
while (token->type == TOKEN_SEMICOLON || token->type == TOKEN_NEWLINE)
|
||||
{
|
||||
POP_TOKEN();
|
||||
token = PEEK_TOKEN();
|
||||
|
||||
// Skip newlines
|
||||
while (token->type == TOKEN_NEWLINE)
|
||||
{
|
||||
token = POP_TOKEN();
|
||||
token = PEEK_TOKEN();
|
||||
}
|
||||
|
||||
// and_or
|
||||
current_cmd = parse_and_or(ctx);
|
||||
if (current_cmd == NULL)
|
||||
return NULL;
|
||||
list_append(result_list, current_cmd);
|
||||
result_list = list_append(result_list, current_cmd);
|
||||
|
||||
token = PEEK_TOKEN();
|
||||
}
|
||||
|
|
@ -223,12 +274,14 @@ struct ast *parse_compound_list(struct lexer_context *ctx)
|
|||
if (token->type == TOKEN_SEMICOLON)
|
||||
{
|
||||
token = POP_TOKEN();
|
||||
token = PEEK_TOKEN();
|
||||
}
|
||||
|
||||
// Skip newlines
|
||||
while (token->type == TOKEN_NEWLINE)
|
||||
{
|
||||
token = POP_TOKEN();
|
||||
token = PEEK_TOKEN();
|
||||
}
|
||||
|
||||
struct ast *result = ast_create_list(result_list);
|
||||
|
|
@ -276,8 +329,8 @@ struct ast *parse_else_clause(struct lexer_context *ctx)
|
|||
|
||||
if (token->type == TOKEN_ELSE)
|
||||
{
|
||||
token = POP_TOKEN(); // eat else
|
||||
result = parse_compound_list(ctx);
|
||||
token = POP_TOKEN(); // Forward
|
||||
}
|
||||
|
||||
if (result == NULL)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue