feat(lexer + parser): export handling

This commit is contained in:
Matteo Flebus 2026-01-30 19:13:27 +01:00
parent bada9c6a29
commit 115377edfe
3 changed files with 80 additions and 50 deletions

View file

@ -72,6 +72,8 @@ static void set_token_keyword(struct token *tok, char *begin, ssize_t size)
tok->type = TOKEN_ELSE;
else if (strncmp(begin, "elif", size) == 0 && size == 4)
tok->type = TOKEN_ELIF;
else if (strncmp(begin, "export", size) == 0 && size == 6)
tok->type = TOKEN_ELIF;
// no keywords found.
if (tok->type == TOKEN_NULL)

View file

@ -72,7 +72,8 @@ enum token_type
TOKEN_FOR,
TOKEN_WHILE,
TOKEN_UNTIL,
TOKEN_CASE
TOKEN_CASE,
TOKEN_EXPORT
};
struct token

View file

@ -174,14 +174,33 @@ struct ast *parse_command(struct lexer_context *ctx)
/* @brief: frees command_elements and redirections lists (helper func)
* @return: NULL
*/
static void *err_simple_command(struct list *command_elements,
struct list *redirections)
static void *err_s_com(struct list *command_elements, struct list *redirections,
struct list *assignments);
{
list_deep_destroy(command_elements);
list_deep_destroy(redirections);
list_deep_destroy(assignments);
return NULL;
}
static ast *parse_export(struct lexer_context *ctx)
{
struct token *token = PEEK_TOKEN();
if (token->type != TOKEN_EXPORT)
{
fprintf(stderr, "expected the export keyword in parse_export");
return NULL;
}
POP_TOKEN();
token = PEEK_TOKEN();
if (token->type != TOKEN_ASSIGNMENT_WORD)
{
fprintf(stderr, "in parser: export must be followed by 'x=y'");
return NULL;
}
return ast_create_assignment(token->data, true);
}
struct ast *parse_simple_command(struct lexer_context *ctx)
{
struct list *command_elements = NULL;
@ -198,7 +217,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
struct ast *prefix = parse_prefix(ctx);
if (prefix == NULL)
{
return err_simple_command(command_elements, redirections);
return err_s_com(command_elements, redirections);
}
if (prefix->type == AST_ASSIGNMENT)
{
@ -214,16 +233,21 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
if (token->type != TOKEN_WORD)
{
if (!has_prefix)
if (!has_prefix && token->type != TOKEN_EXPORT)
{
perror("Expected a command but got a different token type");
return err_simple_command(command_elements, redirections);
return err_s_com(command_elements, redirections, assignments);
}
// else : only prefixes
}
else
if (token->type == TOKEN_EXPORT)
{
if (token->type == TOKEN_WORD)
struct ast *assignment_export = parse_export(ctx);
if (assignment == NULL)
return err_s_com(command_elements, redirections, assignments);
assignments = list_append(assignments, assignment_export);
}
}
else // TOKEN WORD
{
char *command = strdup(token->data);
command_elements = list_append(command_elements, command);
@ -238,7 +262,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
struct ast *element = parse_element(ctx);
if (element == NULL)
{
return err_simple_command(command_elements, redirections);
return err_s_com(command_elements, redirections, assignments);
}
// Get element type
@ -261,19 +285,18 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
{
perror("Internal error: unexpected return value from "
"parse_element in parse_simple_command");
return err_simple_command(command_elements, redirections);
return err_s_com(command_elements, redirections, assignments);
}
// Forward
token = PEEK_TOKEN();
}
}
struct ast *result =
ast_create_command(command_elements, redirections, assignments);
if (result == NULL)
{
return err_simple_command(command_elements, redirections);
return err_s_com(command_elements, redirections, assignments);
}
return result;
}
@ -290,6 +313,10 @@ struct ast *parse_element(struct lexer_context *ctx)
{
return parse_redirection(ctx);
}
else if (token->type == TOKEN_EXPORT)
{
return parse_export(ctx);
}
else
{
perror("Syntax error: unexpected token at parse_element");
@ -455,8 +482,8 @@ struct ast *parse_else_clause(struct lexer_context *ctx)
token = POP_TOKEN();
if (token->type != TOKEN_THEN)
{
perror(
"Expected the 'then' keyword but got a different token type");
perror("Expected the 'then' keyword but got a different token "
"type");
return NULL;
}