feat(parser): and_or_rule in progress

This commit is contained in:
Matteo Flebus 2026-01-22 19:57:49 +01:00
parent 5faa179b63
commit 82f35cfa08
4 changed files with 57 additions and 2 deletions

View file

@ -98,6 +98,14 @@ static void set_token_keyword(struct token *tok, char *begin, ssize_t size)
{ {
tok->type = TOKEN_ELIF; 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. // no keywords found.
if (tok->type == TOKEN_NULL) if (tok->type == TOKEN_NULL)

View file

@ -56,7 +56,9 @@ enum token_type
TOKEN_THEN, TOKEN_THEN,
TOKEN_ELSE, TOKEN_ELSE,
TOKEN_FI, TOKEN_FI,
TOKEN_ELIF TOKEN_ELIF,
TOKEN_AND,
TOKEN_OR
}; };
struct token struct token

View file

@ -13,6 +13,20 @@
// === Static functions // === 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 /* Returns true if c is a command terminator, false otherwise
static bool isterminator(struct token *token) 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) 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) struct ast *parse_pipeline(struct lexer_context *ctx)

View file

@ -7,6 +7,7 @@
enum ast_and_or_type enum ast_and_or_type
{ {
AST_AND_OR_NULL,
AST_AND_OR_TYPE_AND, AST_AND_OR_TYPE_AND,
AST_AND_OR_TYPE_OR AST_AND_OR_TYPE_OR
}; };