feat(dev): expansion

This commit is contained in:
william.valenduc 2026-01-22 12:09:18 +00:00
parent 609c1667af
commit 4fc43e4678
8 changed files with 36 additions and 32 deletions

View file

@ -9,7 +9,9 @@
#include <sys/wait.h>
#include <unistd.h>
#include "../expansion/expansion.h"
#include "../utils/ast/ast.h"
#include "../utils/hash_map/hash_map.h"
// --- Helpers ---
@ -205,7 +207,7 @@ static int exec_command(struct ast_command *command)
* @param ast The AST to execute
* @return int The exit status of the last executed command.
*/
int execution(struct ast *ast)
int execution(struct ast *ast, struct hash_map *vars)
{
if (!ast)
{
@ -219,18 +221,20 @@ int execution(struct ast *ast)
}
case AST_CMD: {
struct ast_command *command = ast_get_command(ast);
if (!expand(command, vars))
fprintf(stderr, "Error: Variable expansion failed\n");
return exec_command(command);
}
case AST_IF: {
struct ast_if *if_node = ast_get_if(ast);
int cond = execution(if_node->condition);
int cond = execution(if_node->condition, vars);
if (cond == 0) // True
{
return execution(if_node->then_clause);
return execution(if_node->then_clause, vars);
}
else // False
{
return execution(if_node->else_clause);
return execution(if_node->else_clause, vars);
}
}
case AST_LIST: {
@ -240,25 +244,25 @@ int execution(struct ast *ast)
while (cur)
{
struct ast *child = (struct ast *)cur->data;
ret = execution(child);
ret = execution(child, vars);
cur = cur->next;
}
return ret;
}
case AST_AND_OR: {
struct ast_and_or *ao_node = ast_get_and_or(ast);
int left_ret = execution(ao_node->left);
int left_ret = execution(ao_node->left, vars);
if (ao_node->type == AST_AND_OR_TYPE_AND)
{
if (left_ret == 0)
return execution(ao_node->right);
return execution(ao_node->right, vars);
return left_ret;
}
else // OR
{
if (left_ret != 0)
return execution(ao_node->right);
return execution(ao_node->right, vars);
return left_ret;
}
}
@ -335,7 +339,7 @@ int execution(struct ast *ast)
close(new_fd);
}
int ret = execution(redir->child);
int ret = execution(redir->child, vars);
if (saved_fd != -1)
{

View file

@ -3,6 +3,7 @@
#include "../utils/ast/ast.h"
#include "../utils/lists/lists.h"
#include "../utils/hash_map/hash_map.h"
/**
* @brief Execute the AST
@ -10,6 +11,6 @@
* @param ast Pointer to the AST structure
* @return int Execution status code of the last command
*/
int execution(struct ast *ast);
int execution(struct ast *ast, struct hash_map *vars);
#endif /* ! EXECUTION_H */