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; tok->type = TOKEN_ELSE;
else if (strncmp(begin, "elif", size) == 0 && size == 4) else if (strncmp(begin, "elif", size) == 0 && size == 4)
tok->type = TOKEN_ELIF; tok->type = TOKEN_ELIF;
else if (strncmp(begin, "export", size) == 0 && size == 6)
tok->type = TOKEN_ELIF;
// no keywords found. // no keywords found.
if (tok->type == TOKEN_NULL) if (tok->type == TOKEN_NULL)

View file

@ -72,7 +72,8 @@ enum token_type
TOKEN_FOR, TOKEN_FOR,
TOKEN_WHILE, TOKEN_WHILE,
TOKEN_UNTIL, TOKEN_UNTIL,
TOKEN_CASE TOKEN_CASE,
TOKEN_EXPORT
}; };
struct token 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) /* @brief: frees command_elements and redirections lists (helper func)
* @return: NULL * @return: NULL
*/ */
static void *err_simple_command(struct list *command_elements, static void *err_s_com(struct list *command_elements, struct list *redirections,
struct list *redirections) struct list *assignments);
{ {
list_deep_destroy(command_elements); list_deep_destroy(command_elements);
list_deep_destroy(redirections); list_deep_destroy(redirections);
list_deep_destroy(assignments);
return NULL; 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 ast *parse_simple_command(struct lexer_context *ctx)
{ {
struct list *command_elements = NULL; struct list *command_elements = NULL;
@ -198,7 +217,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
struct ast *prefix = parse_prefix(ctx); struct ast *prefix = parse_prefix(ctx);
if (prefix == NULL) if (prefix == NULL)
{ {
return err_simple_command(command_elements, redirections); return err_s_com(command_elements, redirections);
} }
if (prefix->type == AST_ASSIGNMENT) if (prefix->type == AST_ASSIGNMENT)
{ {
@ -214,16 +233,21 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
if (token->type != TOKEN_WORD) 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"); 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 if (token->type == TOKEN_EXPORT)
}
else
{ {
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); char *command = strdup(token->data);
command_elements = list_append(command_elements, command); 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); struct ast *element = parse_element(ctx);
if (element == NULL) if (element == NULL)
{ {
return err_simple_command(command_elements, redirections); return err_s_com(command_elements, redirections, assignments);
} }
// Get element type // Get element type
@ -261,19 +285,18 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
{ {
perror("Internal error: unexpected return value from " perror("Internal error: unexpected return value from "
"parse_element in parse_simple_command"); "parse_element in parse_simple_command");
return err_simple_command(command_elements, redirections); return err_s_com(command_elements, redirections, assignments);
} }
// Forward // Forward
token = PEEK_TOKEN(); token = PEEK_TOKEN();
} }
}
struct ast *result = struct ast *result =
ast_create_command(command_elements, redirections, assignments); ast_create_command(command_elements, redirections, assignments);
if (result == NULL) if (result == NULL)
{ {
return err_simple_command(command_elements, redirections); return err_s_com(command_elements, redirections, assignments);
} }
return result; return result;
} }
@ -290,6 +313,10 @@ struct ast *parse_element(struct lexer_context *ctx)
{ {
return parse_redirection(ctx); return parse_redirection(ctx);
} }
else if (token->type == TOKEN_EXPORT)
{
return parse_export(ctx);
}
else else
{ {
perror("Syntax error: unexpected token at parse_element"); perror("Syntax error: unexpected token at parse_element");
@ -455,8 +482,8 @@ struct ast *parse_else_clause(struct lexer_context *ctx)
token = POP_TOKEN(); token = POP_TOKEN();
if (token->type != TOKEN_THEN) if (token->type != TOKEN_THEN)
{ {
perror( perror("Expected the 'then' keyword but got a different token "
"Expected the 'then' keyword but got a different token type"); "type");
return NULL; return NULL;
} }