feat(parser): ast_subshell

This commit is contained in:
matteo 2026-01-31 19:45:40 +01:00
parent 19addf8e6f
commit 3e42b6fd00

View file

@ -372,28 +372,45 @@ struct ast *parse_shell_command(struct lexer_context *ctx)
struct token *token = PEEK_TOKEN(); struct token *token = PEEK_TOKEN();
struct ast *result = NULL; struct ast *result = NULL;
// Grouping // '{'
// '(' or '{' if (token->type == TOKEN_LEFT_BRACKET)
if (token->type == TOKEN_LEFT_BRACKET || token->type == TOKEN_LEFT_PAREN)
{ {
POP_TOKEN(); POP_TOKEN();
result = parse_compound_list(ctx); result = parse_compound_list(ctx);
if (result == NULL) if (result == NULL)
return NULL; return NULL;
// ')' or '}' // '}'
token = PEEK_TOKEN(); token = PEEK_TOKEN();
if (token->type == TOKEN_LEFT_BRACKET if (token->type == TOKEN_LEFT_BRACKET)
|| token->type == TOKEN_LEFT_PAREN)
{ {
ast_free(&result); ast_free(&result);
perror("Syntax error: bracket/parenthesis mismatch"); perror("Syntax error: bracket mismatch");
return NULL; return NULL;
} }
POP_TOKEN(); POP_TOKEN();
return result; return result;
} }
// '('
else if (token->type == TOKEN_LEFT_PAREN)
{
POP_TOKEN();
result = parse_compound_list(ctx);
if (result == NULL)
return NULL;
// ')'
token = PEEK_TOKEN();
if (token->type == TOKEN_LEFT_PAREN)
{
ast_free(&result);
perror("Syntax error: parenthesis mismatch");
return NULL;
}
POP_TOKEN();
return ast_create_subshell(result);
}
else if (is_first(*token, RULE_IF)) else if (is_first(*token, RULE_IF))
{ {
return parse_if_rule(ctx); return parse_if_rule(ctx);