feat(ast): add ast_assignements. Similar to how redirections are handled.
This commit is contained in:
parent
308ac9ac8d
commit
d775ab6c1a
5 changed files with 59 additions and 3 deletions
36
src/utils/ast/ast_assignement.c
Normal file
36
src/utils/ast/ast_assignement.c
Normal 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);
|
||||||
|
}
|
||||||
17
src/utils/ast/ast_assignement.h
Normal file
17
src/utils/ast/ast_assignement.h
Normal 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 */
|
||||||
|
|
@ -15,7 +15,8 @@ enum ast_type
|
||||||
AST_CMD,
|
AST_CMD,
|
||||||
AST_WORD,
|
AST_WORD,
|
||||||
AST_PIPE,
|
AST_PIPE,
|
||||||
AST_NEG
|
AST_NEG,
|
||||||
|
AST_ASSIGNEMENT
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ast
|
struct ast
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
#include "../lists/lists.h"
|
#include "../lists/lists.h"
|
||||||
|
|
||||||
struct ast *ast_create_command(struct list *command,
|
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));
|
struct ast_command *command_data = malloc(sizeof(struct ast_command));
|
||||||
if (!command_data)
|
if (!command_data)
|
||||||
|
|
@ -36,5 +36,6 @@ void ast_free_command(struct ast_command *command_data)
|
||||||
return;
|
return;
|
||||||
list_deep_destroy(command_data->command);
|
list_deep_destroy(command_data->command);
|
||||||
ast_list_deep_destroy(command_data->redirections);
|
ast_list_deep_destroy(command_data->redirections);
|
||||||
|
ast_list_deep_destroy(command_data->assignements);
|
||||||
free(command_data);
|
free(command_data);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ struct ast_command
|
||||||
{
|
{
|
||||||
struct list *command; // A list of words (char*)
|
struct list *command; // A list of words (char*)
|
||||||
struct ast_list *redirections; // A list of ASTs, all ast_redir
|
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.
|
* Creates a new AST node representing a command.
|
||||||
*/
|
*/
|
||||||
struct ast *ast_create_command(struct list *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.
|
* @brief: frees the given ast_command and sets the pointer to NULL.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue