feat: export handling, except exec
This commit is contained in:
parent
1367598047
commit
aa45e3d30f
5 changed files with 35 additions and 16 deletions
|
|
@ -89,6 +89,18 @@ static bool init_firsts_map(void)
|
|||
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
|
||||
|
||||
int grammar_init(void)
|
||||
|
|
@ -128,15 +140,9 @@ int grammar_init(void)
|
|||
add_firsts(RULE_CASE_CLAUSE, first(RULE_CASE_ITEM));
|
||||
|
||||
// Redirection
|
||||
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);
|
||||
add_first_redir();
|
||||
// %RIP Matteo 30/01/2026
|
||||
// %RAX Guillem 30/01/2026 hehe
|
||||
|
||||
// Element
|
||||
add_first(RULE_ELEMENT, TOKEN_WORD);
|
||||
|
|
@ -153,6 +159,7 @@ int grammar_init(void)
|
|||
// Simple command
|
||||
add_firsts(RULE_SIMPLE_COMMAND, first(RULE_PREFIX));
|
||||
add_first(RULE_SIMPLE_COMMAND, TOKEN_WORD);
|
||||
add_first(RULE_SIMPLE_COMMAND, TOKEN_EXPORT);
|
||||
|
||||
// Funcdec
|
||||
add_first(RULE_FUNCDEC, TOKEN_WORD);
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ struct ast *parse_prefix(struct lexer_context *ctx)
|
|||
if (token->type == TOKEN_ASSIGNMENT_WORD)
|
||||
{
|
||||
token = POP_TOKEN();
|
||||
return ast_create_assignment(token->data);
|
||||
return ast_create_assignment(token->data, false);
|
||||
}
|
||||
else if (is_first(*token, RULE_REDIRECTION))
|
||||
return parse_redirection(ctx);
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ static void init_assignments(struct ast_assignment *ast_assignment,
|
|||
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 =
|
||||
calloc(1, sizeof(struct ast_assignment));
|
||||
|
|
@ -42,6 +42,7 @@ struct ast *ast_create_assignment(char *assignment)
|
|||
return NULL;
|
||||
|
||||
init_assignments(assignment_data, assignment);
|
||||
assignment_data->global = global;
|
||||
return ast_create(AST_ASSIGNMENT, assignment_data);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,11 +7,12 @@ struct ast_assignment
|
|||
{
|
||||
char *name;
|
||||
char *value;
|
||||
bool global;
|
||||
};
|
||||
|
||||
bool ast_is_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);
|
||||
|
||||
#endif /* ! AST_ASSIGNMENT_H */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue