feat(ast): add ast_assignements. Similar to how redirections are handled.

This commit is contained in:
matteo 2026-01-29 09:56:28 +01:00
parent 75d417eecf
commit fde653ac5d
5 changed files with 59 additions and 3 deletions

View file

@ -0,0 +1,36 @@
#include "ast_assignement.h"
#include <stdlib.h>
bool ast_is_assignement(struct ast *node)
{
return node != NULL && node->type == AST_ASSIGNEMENT;
}
struct ast_assignement *ast_get_assignement(struct ast *node)
{
if (node == NULL || node->type != AST_ASSIGNEMENT)
return NULL;
return (struct ast_assignement *)node->data;
}
struct ast *ast_create_assignement(char *name, char *value)
{
struct ast_assignement *assignement_data = malloc(sizeof(struct ast_assignement));
if (!assignement_data)
return NULL;
assignement_data->name = name;
assignement_data->value = value;
return ast_create(AST_ASSIGNEMENT, assignement_data);
}
void ast_free_assignement(struct ast_assignement *assignement_data)
{
if (assignement_data == NULL)
return;
free(assignement_data->name);
free(assignement_data->value);
free(assignement_data);
}

View file

@ -0,0 +1,17 @@
#ifndef AST_ASSIGNEMENT_H
#define AST_ASSIGNEMENT_H
#include "ast_base.h"
struct ast_assignement
{
char *name;
char *value;
};
bool ast_is_assignement(struct ast *node);
struct ast_assignement *ast_get_assignement(struct ast *node);
struct ast *ast_create_assignement(char *name, char *value);
void ast_free_assignement(struct ast_assignement *assignement_data);
#endif /* ! AST_ASSIGNEMENT_H */

View file

@ -15,7 +15,8 @@ enum ast_type
AST_CMD,
AST_WORD,
AST_PIPE,
AST_NEG
AST_NEG,
AST_ASSIGNEMENT
};
struct ast

View file

@ -6,7 +6,7 @@
#include "../lists/lists.h"
struct ast *ast_create_command(struct list *command,
struct list *redirections)
struct list *redirections, struct ast_list *assignements)
{
struct ast_command *command_data = malloc(sizeof(struct ast_command));
if (!command_data)
@ -36,5 +36,6 @@ void ast_free_command(struct ast_command *command_data)
return;
list_deep_destroy(command_data->command);
ast_list_deep_destroy(command_data->redirections);
ast_list_deep_destroy(command_data->assignements);
free(command_data);
}

View file

@ -8,6 +8,7 @@ struct ast_command
{
struct list *command; // A list of words (char*)
struct ast_list *redirections; // A list of ASTs, all ast_redir
struct ast_list *assignements; // A list of ASTs, all ast_assignement
};
/**
@ -25,7 +26,7 @@ struct ast_command *ast_get_command(struct ast *node);
* Creates a new AST node representing a command.
*/
struct ast *ast_create_command(struct list *command,
struct list *redirections);
struct list *redirections, struct ast_list *assignements);
/*
* @brief: frees the given ast_command and sets the pointer to NULL.