Update to ast* and related stuff

This commit is contained in:
Maix0 2024-06-14 17:45:00 +02:00
parent ea76333cf5
commit 9f41a868a7
15 changed files with 479 additions and 39 deletions

View file

@ -0,0 +1,123 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_ast.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */
/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef VEC_AST_H
#define VEC_AST_H
#include "ast/ast_forward_def.h"
#include "me/types.h"
/// @brief A function that takes two t_ast_node * and compare them
typedef bool (*t_vec_ast_sort_fn)(t_ast_node * *, t_ast_node * *);
/// @brief A function that free an t_ast_node *
typedef void (*t_free_ast_item)(t_ast_node *);
/// @brief A dynamic array of t_ast_node *
typedef struct s_vec_ast
{
t_free_ast_item free_func;
t_usize len;
t_usize capacity;
t_ast_node * *buffer;
} t_vec_ast;
/// @brief Create a new vec_ast with a given capacity
/// @param capacity The capacity of the new vec_ast (in terms of elements)
/// @param free_function The function that will be used to free the elements of the vec_ast
t_vec_ast vec_ast_new(t_usize capacity, t_free_ast_item free_function);
/// @brief Push an element to the last position of the vec_ast
/// @param vec The vec_ast to push the element to
/// @param element The element to push
t_error vec_ast_push(t_vec_ast *vec, t_ast_node * element);
/// @brief Push an element to the first position of the vec_ast
/// @param vec The vec_ast to push the element to
/// @param element The element to push
/// @note This operation is O(n)
t_error vec_ast_push_front(t_vec_ast *vec, t_ast_node * element);
/// @brief Get the last element from the vec_ast, and remove it from the vec_ast
/// @param vec The vec_ast to get the element from
/// @param[out] out The last element of the vec_ast
/// @return true if the operation failed, false otherwise
t_error vec_ast_pop(t_vec_ast *vec, t_ast_node * *value);
/// @brief Get the first element from the vec_ast, and remove it from the vec_ast
/// @param vec The vec_ast to get the element from
/// @param[out] out The first element of the vec_ast
/// @return true if the operation failed, false otherwise
/// @note This operation is O(n)
t_error vec_ast_pop_front(t_vec_ast *vec, t_ast_node * *value);
/// @brief Free the vector and all its elements
/// @param vec The vec_ast to free
void vec_ast_free(t_vec_ast vec);
/// @brief Make the vec_ast at least the given capacity
/// @param vec The vec_ast to reserve
/// @param wanted_capacity The minimum capacity to reserve
/// @return true if the operation failed, false otherwise
t_error vec_ast_reserve(t_vec_ast *vec, t_usize wanted_capacity);
/// @brief Run the function and returns the index of the first element that returns true
/// @param vec The vec_ast to search in
/// @param fn The function to run on each element
/// @param[out] index The index of the first element that returns true
t_error vec_ast_find(t_vec_ast *vec, bool (*fn)(const t_ast_node * *), t_usize *index);
/// @brief Run the function and returns the index of the first element that returns true, but starting at index starting_index
/// @param vec The vec_ast to search in
/// @param fn The function to run on each element
/// @param starting_index The index to start the search from
/// @param[out] index The index of the first element that returns true
t_error vec_ast_find_starting(t_vec_ast *vec, bool (*fn)(const t_ast_node * *), t_usize starting_index, t_usize *index);
/// @brief Run the function on every element of the vec_ast and returns if all elements returned true
/// @param vec The vec_ast to search in
/// @param fn The function to run on each element
/// @param[out] result The result of the operation
/// @return true if the operation failed, false otherwise
/// @note If the vec_ast is empty, result will be true
t_error vec_ast_all(t_vec_ast *vec, bool (*fn)(const t_ast_node * *), bool *result);
/// @brief Run the function on every element of the vec_ast and returns if any element returned true
/// @param vec The vec_ast to search in
/// @param fn The function to run on each element
/// @param[out] result The result of the operation
/// @return true if the operation failed, false otherwise
/// @note If the vec_ast is empty, result will be false
t_error vec_ast_any(t_vec_ast *vec, bool (*fn)(const t_ast_node * *), bool *result);
/// @brief Run the function on every element of the vec_ast
/// @param vec The vec_ast to iterate over
/// @param fn The function to run on each element
/// @param state The state to pass to the function
void vec_ast_iter(t_vec_ast *vec, void (*fn)(t_usize index, t_ast_node * *value, void *state), void *state);
/// @brief Reverse the order of the elements in the vec_ast
/// @param vec The vec_ast to reverse
void vec_ast_reverse(t_vec_ast *vec);
/// @brief Sort the elements of the vec_ast
/// @param vec The vec_ast to sort
/// @param is_sorted The function to use to compare the elements
void vec_ast_sort(t_vec_ast *vec, t_vec_ast_sort_fn is_sorted);
/// @brief Get a pointer to the last element of the vec_ast
/// @param vec The vec_ast to get the element from
/// @param[out] out A pointer to the last element of the vec_ast
/// @return true if the operation failed, false otherwise
t_error vec_ast_back(t_vec_ast *vec, t_ast_node * **out);
#endif

95
output/src/vec/ast/ast.c Normal file
View file

@ -0,0 +1,95 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_ast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */
/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include "me/mem/mem.h"
#include "me/vec/vec_ast.h"
#include <stdlib.h>
t_vec_ast vec_ast_new(t_usize capacity,
t_free_ast_item free_function)
{
t_vec_ast out;
out = (t_vec_ast){0};
out.free_func = free_function;
out.buffer = mem_alloc_array(capacity, sizeof(t_ast_node *));
if (out.buffer)
out.capacity = capacity;
return (out);
}
/// Return true in case of an error
t_error vec_ast_push(t_vec_ast *vec, t_ast_node * element)
{
if (vec == NULL)
return (ERROR);
vec_ast_reserve(vec, vec->len + 1);
vec->buffer[vec->len] = element;
vec->len += 1;
return (NO_ERROR);
}
/// Return true in case of an error
t_error vec_ast_reserve(t_vec_ast *vec, t_usize wanted_capacity)
{
size_t new_capacity;
if (vec == NULL)
return (ERROR);
if (wanted_capacity > vec->capacity)
{
new_capacity = (vec->capacity * 3) / 2 + 1;
while (wanted_capacity > new_capacity)
new_capacity = (new_capacity * 3) / 2 + 1;
vec->buffer = mem_realloc_array(vec->buffer, new_capacity, sizeof(t_ast_node *));
vec->capacity = new_capacity;
}
return (NO_ERROR);
}
/// Return true if the vector is empty
/// This function is safe to call with value being NULL
t_error vec_ast_pop(t_vec_ast *vec, t_ast_node * *value)
{
t_ast_node * temp_value;
t_ast_node * *ptr;
if (vec == NULL)
return (ERROR);
ptr = value;
if (vec->len == 0)
return (ERROR);
if (value == NULL)
ptr = &temp_value;
vec->len--;
*ptr = vec->buffer[vec->len];
mem_set_zero(&vec->buffer[vec->len], sizeof(t_ast_node *));
return (NO_ERROR);
}
/// This function is safe to call with `free_elem` being NULL
void vec_ast_free(t_vec_ast vec)
{
if (vec.free_func)
{
while (vec.len)
{
vec.free_func(vec.buffer[vec.len - 1]);
vec.len--;
}
}
mem_free(vec.buffer);
}

View file

@ -0,0 +1,112 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_ast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */
/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include "me/vec/vec_ast.h"
#include <stdlib.h>
t_error vec_ast_find(t_vec_ast *vec,
bool (*fn)(const t_ast_node * *), t_usize *index)
{
t_usize idx;
if (vec == NULL || fn == NULL || index == NULL)
return (ERROR);
idx = 0;
while (idx < vec->len)
{
if (fn((const t_ast_node * *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
}
idx++;
}
return (ERROR);
}
t_error vec_ast_find_starting(t_vec_ast *vec,
bool (*fn)(const t_ast_node * *),
t_usize starting_index, t_usize *index)
{
t_usize idx;
if (vec == NULL || fn == NULL || index == NULL)
return (ERROR);
idx = starting_index;
while (idx < vec->len)
{
if (fn((const t_ast_node * *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
}
idx++;
}
return (ERROR);
}
t_error vec_ast_all(t_vec_ast *vec,
bool (*fn)(const t_ast_node * *), bool *result)
{
t_usize idx;
if (vec == NULL || fn == NULL || result == NULL)
return (ERROR);
idx = 0;
*result = true;
while (*result && idx < vec->len)
{
if (!fn((const t_ast_node * *)&vec->buffer[idx]))
*result = false;
idx++;
}
return (ERROR);
}
t_error vec_ast_any(t_vec_ast *vec,
bool (*fn)(const t_ast_node * *), bool *result)
{
t_usize idx;
if (vec == NULL || fn == NULL || result == NULL)
return (ERROR);
idx = 0;
*result = false;
while (*result && idx < vec->len)
{
if (fn((const t_ast_node * *)&vec->buffer[idx]))
*result = true;
idx++;
}
return (ERROR);
}
void vec_ast_iter(t_vec_ast *vec,
void (*fn)(t_usize index, t_ast_node * *value,
void *state),
void *state)
{
t_usize idx;
if (vec == NULL || fn == NULL)
return;
idx = 0;
while (idx < vec->len)
{
fn(idx, &vec->buffer[idx], state);
idx++;
}
}

View file

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_ast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */
/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include "me/vec/vec_ast.h"
#include <stdlib.h>
t_error vec_ast_push_front(t_vec_ast *vec,
t_ast_node * element)
{
t_usize i;
if (vec->len == 0)
return (vec_ast_push(vec, element));
i = vec->len - 1;
if (vec->capacity < vec->len + 1 &&
vec_ast_reserve(vec, 3 * vec->len / 2 + 1))
return (ERROR);
while (i > 0)
{
vec->buffer[i + 1] = vec->buffer[i];
i--;
}
vec->buffer[1] = vec->buffer[0];
vec->buffer[0] = element;
vec->len++;
return (NO_ERROR);
}
t_error vec_ast_pop_front(t_vec_ast *vec, t_ast_node * *value)
{
t_usize i;
if (vec->len <= 1)
return (vec_ast_pop(vec, value));
i = 0;
*value = vec->buffer[0];
vec->len--;
while (i < vec->len)
{
vec->buffer[i] = vec->buffer[i + 1];
i++;
}
mem_set_zero(&vec->buffer[i], sizeof(*vec->buffer));
return (NO_ERROR);
}
void vec_ast_reverse(t_vec_ast *vec)
{
t_ast_node * temporary;
t_usize i;
i = 0;
while (i < vec->len / 2)
{
temporary = vec->buffer[vec->len - 1 - i];
vec->buffer[vec->len - 1 - i] = vec->buffer[i];
vec->buffer[i] = temporary;
i++;
}
}
t_error vec_ast_back(t_vec_ast *vec, t_ast_node * **out)
{
t_ast_node * *temporary;
if (out == NULL)
out = &temporary;
if (vec->len != 0)
return (*out = &vec->buffer[vec->len - 1], true);
return (false);
}

View file

@ -27,7 +27,7 @@ t_error vec_parser_heredoc_find(t_vec_parser_heredoc *vec,
idx = 0;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_heredoc *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
@ -48,7 +48,7 @@ t_error vec_parser_heredoc_find_starting(t_vec_parser_heredoc *vec,
idx = starting_index;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_heredoc *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
@ -69,7 +69,7 @@ t_error vec_parser_heredoc_all(t_vec_parser_heredoc *vec,
*result = true;
while (*result && idx < vec->len)
{
if (!fn(&vec->buffer[idx]))
if (!fn((const t_heredoc *)&vec->buffer[idx]))
*result = false;
idx++;
}
@ -87,7 +87,7 @@ t_error vec_parser_heredoc_any(t_vec_parser_heredoc *vec,
*result = false;
while (*result && idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_heredoc *)&vec->buffer[idx]))
*result = true;
idx++;
}

View file

@ -27,7 +27,7 @@ t_error vec_parser_range_find(t_vec_parser_range *vec,
idx = 0;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_parser_range *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
@ -48,7 +48,7 @@ t_error vec_parser_range_find_starting(t_vec_parser_range *vec,
idx = starting_index;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_parser_range *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
@ -69,7 +69,7 @@ t_error vec_parser_range_all(t_vec_parser_range *vec,
*result = true;
while (*result && idx < vec->len)
{
if (!fn(&vec->buffer[idx]))
if (!fn((const t_parser_range *)&vec->buffer[idx]))
*result = false;
idx++;
}
@ -87,7 +87,7 @@ t_error vec_parser_range_any(t_vec_parser_range *vec,
*result = false;
while (*result && idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_parser_range *)&vec->buffer[idx]))
*result = true;
idx++;
}

View file

@ -27,7 +27,7 @@ t_error vec_reduce_action_find(t_vec_reduce_action *vec,
idx = 0;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_reduce_action *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
@ -48,7 +48,7 @@ t_error vec_reduce_action_find_starting(t_vec_reduce_action *vec,
idx = starting_index;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_reduce_action *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
@ -69,7 +69,7 @@ t_error vec_reduce_action_all(t_vec_reduce_action *vec,
*result = true;
while (*result && idx < vec->len)
{
if (!fn(&vec->buffer[idx]))
if (!fn((const t_reduce_action *)&vec->buffer[idx]))
*result = false;
idx++;
}
@ -87,7 +87,7 @@ t_error vec_reduce_action_any(t_vec_reduce_action *vec,
*result = false;
while (*result && idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_reduce_action *)&vec->buffer[idx]))
*result = true;
idx++;
}

View file

@ -27,7 +27,7 @@ t_error vec_str_find(t_vec_str *vec,
idx = 0;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_str *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
@ -48,7 +48,7 @@ t_error vec_str_find_starting(t_vec_str *vec,
idx = starting_index;
while (idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_str *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
@ -69,7 +69,7 @@ t_error vec_str_all(t_vec_str *vec,
*result = true;
while (*result && idx < vec->len)
{
if (!fn(&vec->buffer[idx]))
if (!fn((const t_str *)&vec->buffer[idx]))
*result = false;
idx++;
}
@ -87,7 +87,7 @@ t_error vec_str_any(t_vec_str *vec,
*result = false;
while (*result && idx < vec->len)
{
if (fn(&vec->buffer[idx]))
if (fn((const t_str *)&vec->buffer[idx]))
*result = true;
idx++;
}