feat(parser): and_or_rule in progress
This commit is contained in:
parent
5faa179b63
commit
82f35cfa08
4 changed files with 57 additions and 2 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue