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

@ -89,6 +89,18 @@ static bool init_firsts_map(void)
return true; return true;
} }
static void add_first_redir(void)
{
add_first(RULE_REDIRECTION, TOKEN_IONUMBER);
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_DOUBLE_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT_AMP);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT_AMP);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT_PIPE);
}
// === Functions // === Functions
int grammar_init(void) int grammar_init(void)
@ -128,15 +140,9 @@ int grammar_init(void)
add_firsts(RULE_CASE_CLAUSE, first(RULE_CASE_ITEM)); add_firsts(RULE_CASE_CLAUSE, first(RULE_CASE_ITEM));
// Redirection // Redirection
add_first(RULE_REDIRECTION, TOKEN_IONUMBER); add_first_redir();
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_DOUBLE_RIGHT);
add_first(RULE_REDIRECTION, TOKEN_REDIR_LEFT_AMP);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT_AMP);
add_first(RULE_REDIRECTION, TOKEN_REDIR_RIGHT_PIPE);
// %RIP Matteo 30/01/2026 // %RIP Matteo 30/01/2026
// %RAX Guillem 30/01/2026 hehe
// Element // Element
add_first(RULE_ELEMENT, TOKEN_WORD); add_first(RULE_ELEMENT, TOKEN_WORD);
@ -153,6 +159,7 @@ int grammar_init(void)
// Simple command // Simple command
add_firsts(RULE_SIMPLE_COMMAND, first(RULE_PREFIX)); add_firsts(RULE_SIMPLE_COMMAND, first(RULE_PREFIX));
add_first(RULE_SIMPLE_COMMAND, TOKEN_WORD); add_first(RULE_SIMPLE_COMMAND, TOKEN_WORD);
add_first(RULE_SIMPLE_COMMAND, TOKEN_EXPORT);
// Funcdec // Funcdec
add_first(RULE_FUNCDEC, TOKEN_WORD); add_first(RULE_FUNCDEC, TOKEN_WORD);

View file

@ -71,7 +71,7 @@ struct ast *parse_prefix(struct lexer_context *ctx)
if (token->type == TOKEN_ASSIGNMENT_WORD) if (token->type == TOKEN_ASSIGNMENT_WORD)
{ {
token = POP_TOKEN(); token = POP_TOKEN();
return ast_create_assignment(token->data); return ast_create_assignment(token->data, false);
} }
else if (is_first(*token, RULE_REDIRECTION)) else if (is_first(*token, RULE_REDIRECTION))
return parse_redirection(ctx); return parse_redirection(ctx);

View file

@ -175,7 +175,7 @@ struct ast *parse_command(struct lexer_context *ctx)
* @return: NULL * @return: NULL
*/ */
static void *err_s_com(struct list *command_elements, struct list *redirections, 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(command_elements);
list_deep_destroy(redirections); list_deep_destroy(redirections);
@ -183,7 +183,10 @@ static void *err_s_com(struct list *command_elements, struct list *redirections,
return NULL; 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(); struct token *token = PEEK_TOKEN();
if (token->type != TOKEN_EXPORT) 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"); fprintf(stderr, "expected the export keyword in parse_export");
return NULL; return NULL;
} }
// export
POP_TOKEN(); POP_TOKEN();
token = PEEK_TOKEN(); token = PEEK_TOKEN();
if (token->type != TOKEN_ASSIGNMENT_WORD) if (token->type != TOKEN_ASSIGNMENT_WORD)
{ {
fprintf(stderr, "in parser: export must be followed by 'x=y'"); fprintf(stderr, "in parser: export must be followed by 'x=y'");
return NULL; return NULL;
} }
// assignment
POP_TOKEN();
return ast_create_assignment(token->data, true); 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); struct ast *prefix = parse_prefix(ctx);
if (prefix == NULL) if (prefix == NULL)
{ {
return err_s_com(command_elements, redirections); return err_s_com(command_elements, redirections, assignments);
} }
if (prefix->type == AST_ASSIGNMENT) if (prefix->type == AST_ASSIGNMENT)
{ {
@ -241,7 +251,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
if (token->type == TOKEN_EXPORT) if (token->type == TOKEN_EXPORT)
{ {
struct ast *assignment_export = parse_export(ctx); struct ast *assignment_export = parse_export(ctx);
if (assignment == NULL) if (assignment_export == NULL)
return err_s_com(command_elements, redirections, assignments); return err_s_com(command_elements, redirections, assignments);
assignments = list_append(assignments, assignment_export); 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); command_elements = list_append(command_elements, command);
POP_TOKEN(); POP_TOKEN();
token = PEEK_TOKEN();
} }
token = PEEK_TOKEN();
// Eventual elements // Eventual elements
while (is_first(*token, RULE_ELEMENT)) while (is_first(*token, RULE_ELEMENT))
{ {

View file

@ -34,7 +34,7 @@ static void init_assignments(struct ast_assignment *ast_assignment,
ast_assignment->value = strdup(split_pos + 1); ast_assignment->value = strdup(split_pos + 1);
} }
struct ast *ast_create_assignment(char *assignment) struct ast *ast_create_assignment(char *assignment, bool global)
{ {
struct ast_assignment *assignment_data = struct ast_assignment *assignment_data =
calloc(1, sizeof(struct ast_assignment)); calloc(1, sizeof(struct ast_assignment));
@ -42,6 +42,7 @@ struct ast *ast_create_assignment(char *assignment)
return NULL; return NULL;
init_assignments(assignment_data, assignment); init_assignments(assignment_data, assignment);
assignment_data->global = global;
return ast_create(AST_ASSIGNMENT, assignment_data); return ast_create(AST_ASSIGNMENT, assignment_data);
} }

View file

@ -7,11 +7,12 @@ struct ast_assignment
{ {
char *name; char *name;
char *value; char *value;
bool global;
}; };
bool ast_is_assignment(struct ast *node); bool ast_is_assignment(struct ast *node);
struct ast_assignment *ast_get_assignment(struct ast *node); struct ast_assignment *ast_get_assignment(struct ast *node);
struct ast *ast_create_assignment(char *assignment); struct ast *ast_create_assignment(char *assignment, bool global);
void ast_free_assignment(struct ast_assignment *assignment_data); void ast_free_assignment(struct ast_assignment *assignment_data);
#endif /* ! AST_ASSIGNMENT_H */ #endif /* ! AST_ASSIGNMENT_H */