Skip to content

Commit

Permalink
Merge pull request #67 from ErwannLesech/add_comments
Browse files Browse the repository at this point in the history
doc<files> add description of files and functions
  • Loading branch information
ErwannLesech authored Jan 18, 2024
2 parents 4963915 + c5d404e commit 31f62e6
Show file tree
Hide file tree
Showing 28 changed files with 340 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/ast/ast.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* \file ast.h
* \brief Represent a node of the AST.
* \author Erwann Lesech, Valentin Gibert, Ugo Majer, Alexandre Privat
* \author Erwann Lesech, Valentin Gibbe, Ugo Majer, Alexandre Privat
* \version 1.0
* \date 12/01/2024
*/
Expand Down
33 changes: 31 additions & 2 deletions src/execute/ast_eval.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* \file ast_eval.c
* \brief Evaluate the AST.
* \author Erwann Lesech, Valentin Gibert, Ugo Majer, Alexandre Privat
* \author Erwann Lesech, Valentin Gibbe, Ugo Majer, Alexandre Privat
* \version 1.0
* \date 12/01/2024
*/
Expand All @@ -21,6 +21,7 @@
// error else

/**
* \struct builtin_function
* \brief Structure representing a builtin function.
*/
struct builtin_function
Expand All @@ -29,10 +30,20 @@ struct builtin_function
int (*fun)(struct ast_node *);
};

/**
* \struct builtin_function
* \brief Structure representing a builtin function.
*/
struct builtin_function builtin[] = { { .name = "echo", .fun = echo_fun },
{ .name = "true", .fun = true_fun },
{ .name = "false", .fun = false_fun } };

/**
* \brief Evaluate the while loop
* \param node The AST to evaluate.
* \param logger_enabled Whether the logger is enabled or not.
* \return The exit status of the last command.
*/
int exec_cmd(struct ast_node *node, bool logger_enabled)
{
int pid = fork();
Expand Down Expand Up @@ -83,6 +94,12 @@ int ast_command(struct ast_node *node, bool logger_enabled)
return match_ast(node->children[0], logger_enabled);
}

/**
* \brief Evaluate simple command from ast
* \param node The AST to evaluate.
* \param logger_enabled Whether the logger is enabled or not.
* \return The exit status of the last command.
*/
int ast_eval_simple_command(struct ast_node *node, bool logger_enabled)
{
if (node->children[0]->type == AST_WORD_ASSIGNMENT)
Expand All @@ -100,6 +117,12 @@ int ast_eval_simple_command(struct ast_node *node, bool logger_enabled)
return exec_cmd(node, logger_enabled);
}

/**
* \brief Evaluate condition from ast
* \param node The AST to evaluate.
* \param logger_enabled Whether the logger is enabled or not.
* \return The exit status of the last command.
*/
int ast_eval_condition(struct ast_node *node, bool logger_enabled)
{
int cond = match_ast(node->children[0], logger_enabled);
Expand All @@ -117,6 +140,12 @@ int ast_eval_condition(struct ast_node *node, bool logger_enabled)
}
}

/**
* \brief Evaluate command list from ast
* \param node The AST to evaluate.
* \param logger_enabled Whether the logger is enabled or not.
* \return The exit status of the last command.
*/
int ast_eval_command_list(struct ast_node *node, bool logger_enabled)
{
int status = 0;
Expand Down Expand Up @@ -158,4 +187,4 @@ int match_ast(struct ast_node *node, bool logger_enabled)
default:
return -1;
}
}
}
65 changes: 61 additions & 4 deletions src/execute/ast_eval.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* \file ast_eval.h
* \brief Evaluate the AST.
* \author Erwann Lesech, Valentin Gibert, Ugo Majer, Alexandre Privat
* \author Erwann Lesech, Valentin Gibbe, Ugo Majer, Alexandre Privat
* \version 1.0
* \date 12/01/2024
*/
Expand All @@ -15,17 +15,74 @@
#include "options/options.h"

/**
* \brief Evaluates the given AST and returns the exit status of the last
* command. \param node The AST to evaluate. \param logger_enabled Whether the
* logger is enabled or not. \return The exit status of the last command.
* \brief Evaluate the given AST
* \param node The AST to evaluate.
* \param logger_enabled Whether the logger is enabled or not.
* \return The exit status of the last command.
*/
int match_ast(struct ast_node *node, bool logger_enabled);

/**
* \brief Evaluate the while loop
* \param node The AST to evaluate.
* \param logger_enabled Whether the logger is enabled or not.
* \return The exit status of the last command.
*/
int while_loop(struct ast_node *node, bool logger_enabled);

/**
* \brief Evaluate the until loop
* \param node The AST to evaluate.
* \param logger_enabled Whether the logger is enabled or not.
* \return The exit status of the last command.
*/
int until_loop(struct ast_node *node, bool logger_enabled);

/**
* \brief Evaluate the for loop
* \param node The AST to evaluate.
* \param logger_enabled Whether the logger is enabled or not.
* \return The exit status of the last command.
*/
int for_loop(struct ast_node *node, bool logger_enabled);

/**
* \brief Pipeline evaluation.
* \param node The AST to evaluate.
* \param logger_enabled Whether the logger is enabled or not.
* \return The exit status of the last command.
*/
int pipeline_eval(struct ast_node *node, bool logger_enabled);

/**
* \brief Evaluate and_or
* \param node The AST to evaluate.
* \param logger_enabled Whether the logger is enabled or not.
* \return The exit status of the last command.
*/
int ast_and_or(struct ast_node *node, bool logger_enabled);

/**
* \brief Evaluate the ast assignement
* \param node The AST to evaluate.
* \param logger_enabled Whether the logger is enabled or not.
* \return The exit status of the last command.
*/
int ast_eval_assignment(struct ast_node *node, bool logger_enabled);

/**
* \brief Evaluate command from ast
* \param node The AST to evaluate.
* \param logger_enabled Whether the logger is enabled or not.
* \return The exit status of the last command.
*/
int ast_command(struct ast_node *node, bool logger_enabled);

/**
* \brief Handle word assignment
* \param node The AST to evaluate.
* \return The exit status of the last command.
*/
char *handle_word(struct ast_node *node);

#endif /* AST_EVAL_H */
37 changes: 35 additions & 2 deletions src/execute/ast_variable.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
/**
* \file ast_variable.c
* \brief Handle variables.
* \author Erwann Lesech, Valentin Gibbe, Ugo Majer, Alexandre Privat
* \version 1.0
* \date 12/01/2024
*/

#include "ast_eval.h"
#include "hash_map/hash_map.h"
#include "parser/parser.h"

struct hash_map *variables = NULL;

/**
* \brief Initialize the variables hash map.
*/
void init_variables()
{
if (variables == NULL)
Expand All @@ -12,19 +23,32 @@ void init_variables()
}
}

/**
* \brief Free the variables hash map.
*/
void free_variables()
{
if (variables != NULL)
hash_map_free(variables);
}

/**
* \brief Set a variable in the hash map.
* \param key The key of the variable.
* \param value The value of the variable.
*/
void set_variable(char *key, char *value)
{
init_variables();
bool updated;
hash_map_insert(variables, key, value, &updated);
}

/**
* \brief Get a variable from the hash map.
* \param key The key of the variable.
* \return The value of the variable or NULL if not found.
*/
char *get_variable(char *key)
{
key++;
Expand All @@ -51,6 +75,12 @@ char *get_variable(char *key)
return "";
}

/**
* \brief Evaluate a node from the AST.
* \param node The AST to evaluate.
* \param logger_enabled Whether the logger is enabled or not.
* \return The exit status of the last command 0 if success, 1 if error.
*/
int ast_eval_assignment(struct ast_node *node, bool logger_enabled)
{
if (logger_enabled)
Expand All @@ -63,8 +93,11 @@ int ast_eval_assignment(struct ast_node *node, bool logger_enabled)
return 0;
}

// Small function for handle variable (just check ast_node type and check the
// hash table)
/**
* \brief Small function for handle variable (just check ast_node type and check the hash table)
* \param node The AST to evaluate.
* \return The value of the variable or NULL if not found.
*/
char *handle_word(struct ast_node *node)
{
if (node->type == AST_WORD)
Expand Down
13 changes: 11 additions & 2 deletions src/execute/builtin.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* \file builtin.c
* \brief Builtin functions.
* \author Erwann Lesech, Valentin Gibert, Ugo Majer, Alexandre Privat
* \author Erwann Lesech, Valentin Gibbe, Ugo Majer, Alexandre Privat
* \version 1.0
* \date 12/01/2024
*/
Expand All @@ -26,6 +26,11 @@ int false_fun(struct ast_node *node)
return 1;
}

/**
* \brief Print the current working directory.
* \param node The AST to evaluate.
* \return The exit status of the last command 0 if success, 1 if error.
*/
void print_echo(struct ast_node *node, int enable_escapes, int j)
{
for (int i = j; i < node->children_count; i++)
Expand Down Expand Up @@ -66,7 +71,11 @@ void print_echo(struct ast_node *node, int enable_escapes, int j)
}
}

// TO REDUCE
/**
* \brief Echo builtin function and returns the exit status.
* \param node The AST node.
* \return The exit status of the last command 0 if success, 1 if error.
*/
int echo_fun(struct ast_node *node)
{
int no_newline = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/execute/builtin.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* \file builtin.h
* \brief Builtin functions.
* \author Erwann Lesech, Valentin Gibert, Ugo Majer, Alexandre Privat
* \brief Header of the builtin functions.
* \author Erwann Lesech, Valentin Gibbe, Ugo Majer, Alexandre Privat
* \version 1.0
* \date 12/01/2024
*/
Expand Down
8 changes: 8 additions & 0 deletions src/execute/hash_map/hash.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* \file hash.c
* \brief Implement the hash function.
* \author Erwann Lesech, Valentin Gibbe, Ugo Majer, Alexandre Privat
* \version 1.0
* \date 12/01/2024
*/

#include <stddef.h>

#include "hash_map.h"
Expand Down
8 changes: 8 additions & 0 deletions src/execute/hash_map/hash_map.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* \file hash_map.c
* \brief Implement the hash map.
* \author Erwann Lesech, Valentin Gibbe, Ugo Majer, Alexandre Privat
* \version 1.0
* \date 12/01/2024
*/

#include "hash_map.h"

#include <stdbool.h>
Expand Down
Loading

0 comments on commit 31f62e6

Please sign in to comment.