From 82f35cfa082f27939946f935711cb65ee6e05f7e Mon Sep 17 00:00:00 2001 From: Matteo Flebus Date: Thu, 22 Jan 2026 19:57:49 +0100 Subject: [PATCH] feat(parser): and_or_rule in progress --- src/lexer/lexer_utils.c | 8 +++++++ src/lexer/lexer_utils.h | 4 +++- src/parser/parsing_utils.c | 46 +++++++++++++++++++++++++++++++++++++- src/utils/ast/ast_and_or.h | 1 + 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/lexer/lexer_utils.c b/src/lexer/lexer_utils.c index 0a0a23d..4206c4c 100644 --- a/src/lexer/lexer_utils.c +++ b/src/lexer/lexer_utils.c @@ -98,6 +98,14 @@ static void set_token_keyword(struct token *tok, char *begin, ssize_t size) { tok->type = TOKEN_ELIF; } + else if (strncmp(begin, "&&", size) == 0) + { + tok->type = TOKEN_AND; + } + else if (strncmp(begin, "||", size) == 0) + { + tok->type = TOKEN_OR; + } // no keywords found. if (tok->type == TOKEN_NULL) diff --git a/src/lexer/lexer_utils.h b/src/lexer/lexer_utils.h index 729e912..83bc772 100644 --- a/src/lexer/lexer_utils.h +++ b/src/lexer/lexer_utils.h @@ -56,7 +56,9 @@ enum token_type TOKEN_THEN, TOKEN_ELSE, TOKEN_FI, - TOKEN_ELIF + TOKEN_ELIF, + TOKEN_AND, + TOKEN_OR }; struct token diff --git a/src/parser/parsing_utils.c b/src/parser/parsing_utils.c index 2273654..2daeb3d 100644 --- a/src/parser/parsing_utils.c +++ b/src/parser/parsing_utils.c @@ -13,6 +13,20 @@ // === 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; + } +} + /* Returns true if c is a command terminator, false otherwise static bool isterminator(struct token *token) { @@ -98,7 +112,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) diff --git a/src/utils/ast/ast_and_or.h b/src/utils/ast/ast_and_or.h index 7bbe76c..344cfbf 100644 --- a/src/utils/ast/ast_and_or.h +++ b/src/utils/ast/ast_and_or.h @@ -7,6 +7,7 @@ enum ast_and_or_type { + AST_AND_OR_NULL, AST_AND_OR_TYPE_AND, AST_AND_OR_TYPE_OR };