feat: export handling, except exec

This commit is contained in:
Matteo Flebus 2026-01-30 19:37:05 +01:00
parent 1367598047
commit aa45e3d30f
5 changed files with 35 additions and 16 deletions

View file

@ -175,7 +175,7 @@ struct ast *parse_command(struct lexer_context *ctx)
* @return: NULL
*/
static void *err_s_com(struct list *command_elements, struct list *redirections,
struct list *assignments);
struct list *assignments)
{
list_deep_destroy(command_elements);
list_deep_destroy(redirections);
@ -183,7 +183,10 @@ static void *err_s_com(struct list *command_elements, struct list *redirections,
return NULL;
}
static ast *parse_export(struct lexer_context *ctx)
/* @brief: used when export keyword is found, and expects an assignment after.
* @return: an ast_assignment with the field [global] set to true.
*/
static struct ast *parse_export(struct lexer_context *ctx)
{
struct token *token = PEEK_TOKEN();
if (token->type != TOKEN_EXPORT)
@ -191,13 +194,20 @@ static ast *parse_export(struct lexer_context *ctx)
fprintf(stderr, "expected the export keyword in parse_export");
return NULL;
}
// export
POP_TOKEN();
token = PEEK_TOKEN();
if (token->type != TOKEN_ASSIGNMENT_WORD)
{
fprintf(stderr, "in parser: export must be followed by 'x=y'");
return NULL;
}
// assignment
POP_TOKEN();
return ast_create_assignment(token->data, true);
}
@ -217,7 +227,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
struct ast *prefix = parse_prefix(ctx);
if (prefix == NULL)
{
return err_s_com(command_elements, redirections);
return err_s_com(command_elements, redirections, assignments);
}
if (prefix->type == AST_ASSIGNMENT)
{
@ -241,7 +251,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
if (token->type == TOKEN_EXPORT)
{
struct ast *assignment_export = parse_export(ctx);
if (assignment == NULL)
if (assignment_export == NULL)
return err_s_com(command_elements, redirections, assignments);
assignments = list_append(assignments, assignment_export);
@ -253,8 +263,8 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
command_elements = list_append(command_elements, command);
POP_TOKEN();
token = PEEK_TOKEN();
}
token = PEEK_TOKEN();
// Eventual elements
while (is_first(*token, RULE_ELEMENT))
{