WIP IFS spliiting

This commit is contained in:
Maieul BOYER 2024-07-24 18:54:13 +02:00
parent de9a72e368
commit f56432b372
No known key found for this signature in database
18 changed files with 629 additions and 37 deletions

View file

@ -17,6 +17,9 @@ src/hashmap/env/env_utils \
src/vec/ast/ast \ src/vec/ast/ast \
src/vec/ast/ast_functions2 \ src/vec/ast/ast_functions2 \
src/vec/ast/ast_functions3 \ src/vec/ast/ast_functions3 \
src/vec/estr/estr \
src/vec/estr/estr_functions2 \
src/vec/estr/estr_functions3 \
src/vec/str/str \ src/vec/str/str \
src/vec/str/str_functions2 \ src/vec/str/str_functions2 \
src/vec/str/str_functions3 \ src/vec/str/str_functions3 \

View file

@ -101,6 +101,6 @@ build_filelist:
@printf '%-78s\\\n' "GEN_FILES =" >> Filelist.$(ANAME).mk @printf '%-78s\\\n' "GEN_FILES =" >> Filelist.$(ANAME).mk
@tree $(GEN_DIR) -ifF | rg '$(GEN_DIR)/(.*)\.c$$' --replace '$$1' | sed -re 's/^(.*)_([0-9]+)$$/\1|\2/g' | sort -t'|' --key=1,1 --key=2,2n | sed -e's/|/_/' | xargs printf '%-78s\\\n' >> Filelist.$(ANAME).mk @tree $(GEN_DIR) -ifF | rg '$(GEN_DIR)/(.*)\.c$$' --replace '$$1' | sed -re 's/^(.*)_([0-9]+)$$/\1|\2/g' | sort -t'|' --key=1,1 --key=2,2n | sed -e's/|/_/' | xargs printf '%-78s\\\n' >> Filelist.$(ANAME).mk
@echo "" >> Filelist.$(ANAME).mk @echo "" >> Filelist.$(ANAME).mk
@echo -e "$(GREY) Populating $(GREEN) Filelist.$(ANAME).mk$(END)" @echo -e '$(GREY) Populating $(GREEN) Filelist.$(ANAME).mk$(END)'
-include $(DEP) -include $(DEP)

View file

@ -85,6 +85,6 @@ build_filelist:
@printf '%-78s\\\n' "SRC_FILES =" > Filelist.$(ANAME).mk @printf '%-78s\\\n' "SRC_FILES =" > Filelist.$(ANAME).mk
@tree $(SRC_DIR) -ifF | rg '$(SRC_DIR)/(.*)\.c$$' --replace '$$1' | sed -re 's/^(.*)_([0-9]+)$$/\1|\2/g' | sort -t'|' --key=1,1 --key=2,2n | sed -e's/|/_/' | xargs printf '%-78s\\\n' >> Filelist.$(ANAME).mk @tree $(SRC_DIR) -ifF | rg '$(SRC_DIR)/(.*)\.c$$' --replace '$$1' | sed -re 's/^(.*)_([0-9]+)$$/\1|\2/g' | sort -t'|' --key=1,1 --key=2,2n | sed -e's/|/_/' | xargs printf '%-78s\\\n' >> Filelist.$(ANAME).mk
@echo "" >> Filelist.$(ANAME).mk @echo "" >> Filelist.$(ANAME).mk
@echo -e "$(COL_GRAY) Populating $(COL_GREEN) Filelist.$(ANAME).mk$(END)" @echo -e '$(COL_GRAY) Populating $(COL_GREEN) Filelist.$(ANAME).mk$(END)'
-include $(DEPS) -include $(DEPS)

View file

@ -85,6 +85,6 @@ build_filelist:
@printf '%-78s\\\n' "SRC_FILES =" > Filelist.$(ANAME).mk @printf '%-78s\\\n' "SRC_FILES =" > Filelist.$(ANAME).mk
@tree src -ifF | rg 'src/(.*)\.c$$' --replace '$$1' | sed -re 's/^(.*)_([0-9]+)$$/\1|\2/g' | sort -t'|' --key=1,1 --key=2,2n | sed -e's/|/_/' | xargs printf '%-78s\\\n' >> Filelist.$(ANAME).mk @tree src -ifF | rg 'src/(.*)\.c$$' --replace '$$1' | sed -re 's/^(.*)_([0-9]+)$$/\1|\2/g' | sort -t'|' --key=1,1 --key=2,2n | sed -e's/|/_/' | xargs printf '%-78s\\\n' >> Filelist.$(ANAME).mk
@echo "" >> Filelist.$(ANAME).mk @echo "" >> Filelist.$(ANAME).mk
@echo -e "$(COL_GRAY) Populating $(COL_GREEN) Filelist.$(ANAME).mk$(END)" @echo -e '$(COL_GRAY) Populating $(COL_GREEN) Filelist.$(ANAME).mk$(END)'
-include $(DEPS) -include $(DEPS)

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/14 17:46:58 by maiboyer #+# #+# */ /* Created: 2024/06/14 17:46:58 by maiboyer #+# #+# */
/* Updated: 2024/07/22 16:51:56 by rparodi ### ########.fr */ /* Updated: 2024/07/24 15:40:23 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -121,6 +121,8 @@ struct s_ast_raw_string
t_str str; t_str str;
t_usize len; t_usize len;
t_ast_word_kind kind; t_ast_word_kind kind;
bool start;
bool end;
}; };
struct s_ast_word struct s_ast_word

View file

@ -342,6 +342,9 @@ t_ast_node ast_alloc(t_ast_node_kind kind)
{ {
ret->data.raw_string.len = 0; ret->data.raw_string.len = 0;
ret->data.raw_string.str = NULL; ret->data.raw_string.str = NULL;
ret->data.raw_string.kind = AST_WORD_NO_QUOTE;
ret->data.raw_string.start = true;
ret->data.raw_string.end = true;
} }
if (kind == AST_SUBSHELL) if (kind == AST_SUBSHELL)
{ {
@ -1683,6 +1686,8 @@ t_error build_sym_raw_string(t_parse_node self, t_const_str input, t_ast_node *o
temp->data.raw_string.str = temp_str; temp->data.raw_string.str = temp_str;
temp->data.raw_string.len = str_len(temp_str); temp->data.raw_string.len = str_len(temp_str);
temp->data.raw_string.kind = AST_WORD_SINGLE_QUOTE; temp->data.raw_string.kind = AST_WORD_SINGLE_QUOTE;
temp->data.raw_string.start = true;
temp->data.raw_string.end = true;
ret->data.word.kind = AST_WORD_SINGLE_QUOTE; ret->data.word.kind = AST_WORD_SINGLE_QUOTE;
vec_ast_push(&ret->data.word.inner, temp); vec_ast_push(&ret->data.word.inner, temp);
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);

View file

@ -83,6 +83,6 @@ build_filelist:
@printf '%-78s\\\n' "SRC_FILES =" > Filelist.$(ANAME).mk @printf '%-78s\\\n' "SRC_FILES =" > Filelist.$(ANAME).mk
@tree src -ifF | rg 'src/(.*)\.c$$' --replace '$$1' | sed -re 's/^(.*)_([0-9]+)$$/\1|\2/g' | sort -t'|' --key=1,1 --key=2,2n | sed -e's/|/_/' | xargs printf '%-78s\\\n' >> Filelist.$(ANAME).mk @tree src -ifF | rg 'src/(.*)\.c$$' --replace '$$1' | sed -re 's/^(.*)_([0-9]+)$$/\1|\2/g' | sort -t'|' --key=1,1 --key=2,2n | sed -e's/|/_/' | xargs printf '%-78s\\\n' >> Filelist.$(ANAME).mk
@echo "" >> Filelist.$(ANAME).mk @echo "" >> Filelist.$(ANAME).mk
@echo -e "$(COL_GRAY) Populating $(COL_GREEN) Filelist.$(ANAME).mk$(END)" @echo -e '$(COL_GRAY) Populating $(COL_GREEN) Filelist.$(ANAME).mk$(END)'
-include $(DEPS) -include $(DEPS)

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* _tuple_expanded_str.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/24 15:45:08 by maiboyer #+# #+# */
/* Updated: 2024/07/24 16:02:04 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef _TUPLE_EXPANDED_STR_H
# define _TUPLE_EXPANDED_STR_H
# include "me/types.h"
typedef struct s_expandable_str t_expandable_str;
struct s_expandable_str {
t_str value;
bool do_expand;
};
static inline void free_expandable_str(t_expandable_str self)
{
void mem_free(void *ptr);
mem_free(self.value);
}
#endif /* _TUPLE_EXPANDED_STR_H */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */ /* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */
/* Updated: 2024/07/24 13:58:11 by maiboyer ### ########.fr */ /* Updated: 2024/07/24 18:53:43 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,6 +21,7 @@
#include "me/str/str.h" #include "me/str/str.h"
#include "me/string/string.h" #include "me/string/string.h"
#include "me/types.h" #include "me/types.h"
#include "me/vec/vec_estr.h"
#include "me/vec/vec_str.h" #include "me/vec/vec_str.h"
#include <stdio.h> #include <stdio.h>
@ -47,7 +48,7 @@ typedef struct s_word_result t_word_result;
struct s_word_result struct s_word_result
{ {
bool has_error; bool has_error;
t_vec_str value; t_vec_estr value;
t_ast_word_kind kind; t_ast_word_kind kind;
}; };
@ -245,7 +246,7 @@ t_error _handle_expansion_operator(t_ast_expansion *self, t_state *state, t_expa
return (NO_ERROR); return (NO_ERROR);
} }
t_error _ast_get_str__raw__no_quote(t_ast_node elem, t_word_iterator *state, t_vec_str *out) t_error _ast_get_str__raw__no_quote(t_ast_node elem, t_word_iterator *state, t_vec_estr *out)
{ {
bool last_backslash; bool last_backslash;
t_string ret; t_string ret;
@ -265,10 +266,10 @@ t_error _ast_get_str__raw__no_quote(t_ast_node elem, t_word_iterator *state, t_v
last_backslash = true; last_backslash = true;
i++; i++;
} }
return (vec_str_push(out, ret.buf), NO_ERROR); return (vec_estr_push(out, (t_expandable_str){.do_expand = false, .value = str_clone(ret.buf)}), NO_ERROR);
} }
t_error _ast_get_str__raw__single_quote(t_ast_node elem, t_word_iterator *state, t_vec_str *out) t_error _ast_get_str__raw__single_quote(t_ast_node elem, t_word_iterator *state, t_vec_estr *out)
{ {
t_string ret; t_string ret;
t_usize i; t_usize i;
@ -280,10 +281,10 @@ t_error _ast_get_str__raw__single_quote(t_ast_node elem, t_word_iterator *state,
while (elem->data.raw_string.str[i]) while (elem->data.raw_string.str[i])
string_push_char(&ret, elem->data.raw_string.str[i++]); string_push_char(&ret, elem->data.raw_string.str[i++]);
string_pop(&ret); string_pop(&ret);
return (vec_str_push(out, ret.buf), NO_ERROR); return (vec_estr_push(out, (t_expandable_str){.do_expand = false, .value = str_clone(ret.buf)}), NO_ERROR);
} }
t_error _ast_get_str__raw__double_quote(t_ast_node elem, t_word_iterator *state, t_vec_str *out) t_error _ast_get_str__raw__double_quote(t_ast_node elem, t_word_iterator *state, t_vec_estr *out)
{ {
bool last_backslash; bool last_backslash;
t_string ret; t_string ret;
@ -294,8 +295,6 @@ t_error _ast_get_str__raw__double_quote(t_ast_node elem, t_word_iterator *state,
i = 0; i = 0;
ret = string_new(elem->data.raw_string.len); ret = string_new(elem->data.raw_string.len);
last_backslash = false; last_backslash = false;
if (elem->data.raw_string.str[0] == '"')
i++;
while (elem->data.raw_string.str[i]) while (elem->data.raw_string.str[i])
{ {
if (elem->data.raw_string.str[i] != '\\' || last_backslash) if (elem->data.raw_string.str[i] != '\\' || last_backslash)
@ -305,13 +304,10 @@ t_error _ast_get_str__raw__double_quote(t_ast_node elem, t_word_iterator *state,
last_backslash = true; last_backslash = true;
i++; i++;
} }
if (elem->data.raw_string.len >= 2 && elem->data.raw_string.str[elem->data.raw_string.len - 1] == '"' && return (vec_estr_push(out, (t_expandable_str){.do_expand = false, .value = str_clone(ret.buf)}), NO_ERROR);
elem->data.raw_string.str[elem->data.raw_string.len - 2] == '\\')
string_pop(&ret);
return (vec_str_push(out, ret.buf), NO_ERROR);
} }
t_error _ast_get_str__raw(t_ast_node elem, t_word_iterator *state, t_vec_str *out) t_error _ast_get_str__raw(t_ast_node elem, t_word_iterator *state, t_vec_estr *out)
{ {
if (elem == NULL || state == NULL || out == NULL || elem->kind != AST_RAW_STRING) if (elem == NULL || state == NULL || out == NULL || elem->kind != AST_RAW_STRING)
return (ERROR); return (ERROR);
@ -324,11 +320,29 @@ t_error _ast_get_str__raw(t_ast_node elem, t_word_iterator *state, t_vec_str *ou
return (ERROR); return (ERROR);
} }
t_error _split_ifs_expansion(t_ast_word_kind wkind, t_state *state, t_ast_expansion *exp, t_vec_str *out) t_str _get_ifs_value(t_state *state)
{ {
t_str ifs; t_str ifs;
t_str *ifs_entry;
t_str ifs_key;
ifs_key = "IFS";
ifs = NULL;
ifs_entry = hmap_env_get(state->env, (t_str *)&ifs_key);
if (ifs_entry != NULL)
ifs = *ifs_entry;
if (ifs == NULL)
ifs = " \t\n";
return (ifs);
}
/*
t_error _split_ifs_expansion(t_ast_word_kind wkind, t_state *state, t_ast_expansion *exp, t_vec_str *out)
{
const t_str ifs_key = "IFS"; const t_str ifs_key = "IFS";
t_str ifs;
t_expansion_result exp_out; t_expansion_result exp_out;
t_vec_str split_res;
t_str tmp;
if (exp == NULL || out == NULL) if (exp == NULL || out == NULL)
return (ERROR); return (ERROR);
@ -339,16 +353,20 @@ t_error _split_ifs_expansion(t_ast_word_kind wkind, t_state *state, t_ast_expans
ifs = " \t\n"; ifs = " \t\n";
if (run_expansion(exp, state, &exp_out)) if (run_expansion(exp, state, &exp_out))
return (ERROR); return (ERROR);
if (!(exp_out.exists && exp_out.value != NULL))
return (NO_ERROR);
if (wkind != AST_WORD_NO_QUOTE) if (wkind != AST_WORD_NO_QUOTE)
{ return (vec_str_push(out, str_clone(exp_out.value)), NO_ERROR);
if (exp_out.exists && exp_out.value != NULL) if (str_split(exp_out.value, ifs, &split_res))
vec_str_push(out, str_clone(exp_out.value)); return (ERROR);
return (NO_ERROR); while (vec_str_pop_front(&split_res, &tmp))
} vec_str_push(out, tmp);
vec_str_free(split_res);
return (NO_ERROR); return (NO_ERROR);
} }
*/
t_error _ast_get_str__expansion(t_ast_node elem, t_word_iterator *state, t_vec_str *out) t_error _ast_get_str__expansion(t_ast_node elem, t_word_iterator *state, t_vec_estr *out)
{ {
t_expansion_result exp_ret; t_expansion_result exp_ret;
t_vec_str words; t_vec_str words;
@ -357,24 +375,27 @@ t_error _ast_get_str__expansion(t_ast_node elem, t_word_iterator *state, t_vec_s
return (ERROR); return (ERROR);
if (run_expansion(&elem->data.expansion, state->state, &exp_ret)) if (run_expansion(&elem->data.expansion, state->state, &exp_ret))
return (ERROR); return (ERROR);
return (vec_str_push(out, exp_ret.value), NO_ERROR); return (vec_estr_push(out, (t_expandable_str){.do_expand = state->res.kind == AST_WORD_NO_QUOTE, .value = str_clone(exp_ret.value)}),
NO_ERROR);
} }
t_error _ast_get_str__arimethic_expansion(t_ast_node elem, t_word_iterator *state, t_vec_str *out) t_error _ast_get_str__arimethic_expansion(t_ast_node elem, t_word_iterator *state, t_vec_estr *out)
{ {
if (elem == NULL || state == NULL || out == NULL || elem->kind != AST_ARITHMETIC_EXPANSION) if (elem == NULL || state == NULL || out == NULL || elem->kind != AST_ARITHMETIC_EXPANSION)
return (ERROR); return (ERROR);
// vec_estr_push(out, (t_expandable_str){.do_expand = state->res.kind == AST_WORD_NO_QUOTE, .value = str_clone(exp_out.value)});
return (ERROR); return (ERROR);
} }
t_error _ast_get_str__command_substitution(t_ast_node elem, t_word_iterator *state, t_vec_str *out) t_error _ast_get_str__command_substitution(t_ast_node elem, t_word_iterator *state, t_vec_estr *out)
{ {
if (elem == NULL || state == NULL || out == NULL || elem->kind != AST_COMMAND_SUBSTITUTION) if (elem == NULL || state == NULL || out == NULL || elem->kind != AST_COMMAND_SUBSTITUTION)
return (ERROR); return (ERROR);
// vec_estr_push(out, (t_expandable_str){.do_expand = state->res.kind == AST_WORD_NO_QUOTE, .value = str_clone(exp_out.value)});
return (ERROR); return (ERROR);
} }
t_error _ast_get_str(t_ast_node elem, t_word_iterator *state, t_vec_str *out) t_error _ast_get_str(t_ast_node elem, t_word_iterator *state, t_vec_estr *out)
{ {
if (elem == NULL || state == NULL || out == NULL) if (elem == NULL || state == NULL || out == NULL)
return (ERROR); return (ERROR);
@ -400,6 +421,109 @@ void _run_word_into_str(t_usize idx, t_ast_node *elem, t_word_iterator *state)
return; return;
} }
t_error _exp_into_str(t_ast_node self, t_state *state, t_vec_str *append)
{
t_expansion_result res;
t_vec_str splitted;
t_str tmp;
if (self == NULL || state == NULL || append == NULL || self->kind == AST_EXPANSION)
return (ERROR);
if (run_expansion(&self->data.expansion, state, &res))
return (ERROR);
if (res.value == NULL)
return (NO_ERROR);
if (str_split(res.value, _get_ifs_value(state), &splitted))
return (ERROR);
while (vec_str_pop_front(&splitted, &tmp))
vec_str_push(append, tmp);
vec_str_free(splitted);
return (NO_ERROR);
}
t_error _arith_into_str(t_ast_node self, t_state *state, t_vec_str *append)
{
if (self == NULL || state == NULL || append == NULL || self->kind == AST_ARITHMETIC_EXPANSION)
return (ERROR);
return (NO_ERROR);
}
t_error _cmd_into_str(t_ast_node self, t_state *state, t_vec_str *append)
{
if (self == NULL || state == NULL || append == NULL || self->kind == AST_COMMAND_SUBSTITUTION)
return (ERROR);
/*
if (str_split(res.value, _get_ifs_value(state), &splitted))
return (ERROR);
while (vec_str_pop_front(&splitted, &tmp))
vec_str_push(append, tmp);
vec_str_free(splitted);
*/
return (ERROR);
}
t_error _word_into_str(t_ast_node self, t_state *state, t_vec_str *append)
{
t_word_result res;
t_vec_str splitted;
t_string tmp;
t_usize i;
t_str ifs;
if (self == NULL || state == NULL || append == NULL || self->kind == AST_WORD)
return (ERROR);
if (run_word(&self->data.word, state, &res))
return (ERROR);
if (res.kind == AST_WORD_NO_QUOTE)
{
tmp = string_new(1024);
i = 0;
while (i < res.value.len)
{
if (!res.value.buffer[i].do_expand)
string_push(&tmp, res.value.buffer[i].value);
else
{
ifs = _get_ifs_value(state);
while (*ifs != '\0' && str_find_chr(res.value.buffer[i].value, *ifs) == NULL)
ifs++;
if (*ifs == '\0')
string_push(&tmp, res.value.buffer[i].value);
else
{
if (str_split(res.value.buffer[i].value, ifs, &splitted))
return (ERROR);
}
}
}
vec_str_push(append, tmp.buf);
}
else
{
tmp = string_new(1024);
i = 0;
while (i < res.value.len)
string_push(&tmp, res.value.buffer[i++].value);
vec_str_push(append, tmp.buf);
}
return (NO_ERROR);
}
t_error _ast_into_str(t_ast_node self, t_state *state, t_vec_str *append)
{
if (self == NULL || state == NULL || append == NULL)
return (ERROR);
if (self->kind == AST_EXPANSION)
return (_exp_into_str(self, state, append));
if (self->kind == AST_ARITHMETIC_EXPANSION)
return (_arith_into_str(self, state, append));
if (self->kind == AST_COMMAND_SUBSTITUTION)
return (_cmd_into_str(self, state, append));
if (self->kind == AST_WORD)
return (_word_into_str(self, state, append));
return (ERROR);
}
// End Internals funcs // End Internals funcs
t_error run_expansion(t_ast_expansion *self, t_state *state, t_expansion_result *out); t_error run_expansion(t_ast_expansion *self, t_state *state, t_expansion_result *out);
@ -464,6 +588,7 @@ t_error run_command(t_ast_command *command, t_state *state, t_command_result *ou
i = 0; i = 0;
while (i < command->cmd_word.len) while (i < command->cmd_word.len)
{ {
i++; i++;
} }
@ -473,16 +598,19 @@ t_error run_command(t_ast_command *command, t_state *state, t_command_result *ou
t_error run_word(t_ast_word *word, t_state *state, t_word_result *out) t_error run_word(t_ast_word *word, t_state *state, t_word_result *out)
{ {
t_word_iterator iter_state; t_word_iterator iter_state;
t_vec_str tmp;
t_str tmp_str;
t_expandable_str s;
if (word == NULL || state == NULL || out == NULL) if (word == NULL || state == NULL || out == NULL)
return (ERROR); return (ERROR);
iter_state.res.value = vec_str_new(64, (void (*)())mem_free); iter_state.res.value = vec_estr_new(64, free_expandable_str);
iter_state.res.has_error = false; iter_state.res.has_error = false;
iter_state.res.kind = word->kind; iter_state.res.kind = word->kind;
iter_state.state = state; iter_state.state = state;
vec_ast_iter(&word->inner, (void (*)())_run_word_into_str, &iter_state); vec_ast_iter(&word->inner, (void (*)())_run_word_into_str, &iter_state);
if (iter_state.res.has_error) if (iter_state.res.has_error)
return (vec_str_free(iter_state.res.value), ERROR); return (vec_estr_free(iter_state.res.value), ERROR);
return (*out = iter_state.res, NO_ERROR); return (*out = iter_state.res, NO_ERROR);
} }

View file

@ -62,3 +62,11 @@ replace.C__TYPENAME__ = "t_ast_node"
replace.C__TYPEHEADER__ = '#include "ast/ast_forward_def.h"' replace.C__TYPEHEADER__ = '#include "ast/ast_forward_def.h"'
replace.C__PREFIX__ = "ast" replace.C__PREFIX__ = "ast"
replace.C__PREFIXUP__ = "AST" replace.C__PREFIXUP__ = "AST"
[[create.vec]]
sources_output = "src/vec/C__PREFIX__/"
headers_output = "include/me/vec/"
replace.C__TYPENAME__ = "t_expandable_str"
replace.C__TYPEHEADER__ = '#include "exec/_tuple_expanded_str.h"'
replace.C__PREFIX__ = "estr"
replace.C__PREFIXUP__ = "ESTR"

View file

@ -81,6 +81,6 @@ build_filelist:
@printf '%-78s\\\n' "SRC_FILES =" > Filelist.$(ANAME).mk @printf '%-78s\\\n' "SRC_FILES =" > Filelist.$(ANAME).mk
@tree src -ifF | rg 'src/(.*)\.c$$' --replace '$$1' | sed -re 's/^(.*)_([0-9]+)$$/\1|\2/g' | sort -t'|' --key=1,1 --key=2,2n | sed -e's/|/_/' | xargs printf '%-78s\\\n' >> Filelist.$(ANAME).mk @tree src -ifF | rg 'src/(.*)\.c$$' --replace '$$1' | sed -re 's/^(.*)_([0-9]+)$$/\1|\2/g' | sort -t'|' --key=1,1 --key=2,2n | sed -e's/|/_/' | xargs printf '%-78s\\\n' >> Filelist.$(ANAME).mk
@echo "" >> Filelist.$(ANAME).mk @echo "" >> Filelist.$(ANAME).mk
@echo -e "$(COL_GRAY) Populating $(COL_GREEN) Filelist.$(ANAME).mk$(END)" @echo -e '$(COL_GRAY) Populating $(COL_GREEN) Filelist.$(ANAME).mk$(END)'
-include $(DEPS) -include $(DEPS)

View file

@ -0,0 +1,123 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_estr.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_ESTR_H
#define VEC_ESTR_H
#include "exec/_tuple_expanded_str.h"
#include "me/types.h"
/// @brief A function that takes two t_expandable_str and compare them
typedef bool (*t_vec_estr_sort_fn)(t_expandable_str *, t_expandable_str *);
/// @brief A function that free an t_expandable_str
typedef void (*t_free_estr_item)(t_expandable_str);
/// @brief A dynamic array of t_expandable_str
typedef struct s_vec_estr
{
t_free_estr_item free_func;
t_usize len;
t_usize capacity;
t_expandable_str *buffer;
} t_vec_estr;
/// @brief Create a new vec_estr with a given capacity
/// @param capacity The capacity of the new vec_estr (in terms of elements)
/// @param free_function The function that will be used to free the elements of the vec_estr
t_vec_estr vec_estr_new(t_usize capacity, t_free_estr_item free_function);
/// @brief Push an element to the last position of the vec_estr
/// @param vec The vec_estr to push the element to
/// @param element The element to push
t_error vec_estr_push(t_vec_estr *vec, t_expandable_str element);
/// @brief Push an element to the first position of the vec_estr
/// @param vec The vec_estr to push the element to
/// @param element The element to push
/// @note This operation is O(n)
t_error vec_estr_push_front(t_vec_estr *vec, t_expandable_str element);
/// @brief Get the last element from the vec_estr, and remove it from the vec_estr
/// @param vec The vec_estr to get the element from
/// @param[out] out The last element of the vec_estr
/// @return true if the operation failed, false otherwise
t_error vec_estr_pop(t_vec_estr *vec, t_expandable_str *value);
/// @brief Get the first element from the vec_estr, and remove it from the vec_estr
/// @param vec The vec_estr to get the element from
/// @param[out] out The first element of the vec_estr
/// @return true if the operation failed, false otherwise
/// @note This operation is O(n)
t_error vec_estr_pop_front(t_vec_estr *vec, t_expandable_str *value);
/// @brief Free the vector and all its elements
/// @param vec The vec_estr to free
void vec_estr_free(t_vec_estr vec);
/// @brief Make the vec_estr at least the given capacity
/// @param vec The vec_estr to reserve
/// @param wanted_capacity The minimum capacity to reserve
/// @return true if the operation failed, false otherwise
t_error vec_estr_reserve(t_vec_estr *vec, t_usize wanted_capacity);
/// @brief Run the function and returns the index of the first element that returns true
/// @param vec The vec_estr 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_estr_find(t_vec_estr *vec, bool (*fn)(const t_expandable_str *), 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_estr 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_estr_find_starting(t_vec_estr *vec, bool (*fn)(const t_expandable_str *), t_usize starting_index, t_usize *index);
/// @brief Run the function on every element of the vec_estr and returns if all elements returned true
/// @param vec The vec_estr 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_estr is empty, result will be true
t_error vec_estr_all(t_vec_estr *vec, bool (*fn)(const t_expandable_str *), bool *result);
/// @brief Run the function on every element of the vec_estr and returns if any element returned true
/// @param vec The vec_estr 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_estr is empty, result will be false
t_error vec_estr_any(t_vec_estr *vec, bool (*fn)(const t_expandable_str *), bool *result);
/// @brief Run the function on every element of the vec_estr
/// @param vec The vec_estr to iterate over
/// @param fn The function to run on each element
/// @param state The state to pass to the function
void vec_estr_iter(t_vec_estr *vec, void (*fn)(t_usize index, t_expandable_str *value, void *state), void *state);
/// @brief Reverse the order of the elements in the vec_estr
/// @param vec The vec_estr to reverse
void vec_estr_reverse(t_vec_estr *vec);
/// @brief Sort the elements of the vec_estr
/// @param vec The vec_estr to sort
/// @param is_sorted The function to use to compare the elements
void vec_estr_sort(t_vec_estr *vec, t_vec_estr_sort_fn is_sorted);
/// @brief Get a pointer to the last element of the vec_estr
/// @param vec The vec_estr to get the element from
/// @param[out] out A pointer to the last element of the vec_estr
/// @return true if the operation failed, false otherwise
t_error vec_estr_back(t_vec_estr *vec, t_expandable_str **out);
#endif

View file

@ -0,0 +1,95 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_estr.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/types.h"
#include "me/vec/vec_estr.h"
#include <stdlib.h>
t_vec_estr vec_estr_new(t_usize capacity,
t_free_estr_item free_function)
{
t_vec_estr out;
out = (t_vec_estr){0};
out.free_func = free_function;
out.buffer = mem_alloc_array(capacity, sizeof(t_expandable_str));
if (out.buffer)
out.capacity = capacity;
return (out);
}
/// Return true in case of an error
t_error vec_estr_push(t_vec_estr *vec, t_expandable_str element)
{
if (vec == NULL)
return (ERROR);
vec_estr_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_estr_reserve(t_vec_estr *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_expandable_str));
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_estr_pop(t_vec_estr *vec, t_expandable_str *value)
{
t_expandable_str temp_value;
t_expandable_str *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_expandable_str));
return (NO_ERROR);
}
/// This function is safe to call with `free_elem` being NULL
void vec_estr_free(t_vec_estr vec)
{
if (vec.buffer == NULL)
return;
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_estr.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_estr.h"
#include <stdlib.h>
t_error vec_estr_find(t_vec_estr *vec,
bool (*fn)(const t_expandable_str *), 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_expandable_str *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
}
idx++;
}
return (ERROR);
}
t_error vec_estr_find_starting(t_vec_estr *vec,
bool (*fn)(const t_expandable_str *),
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_expandable_str *)&vec->buffer[idx]))
{
*index = idx;
return (NO_ERROR);
}
idx++;
}
return (ERROR);
}
t_error vec_estr_all(t_vec_estr *vec,
bool (*fn)(const t_expandable_str *), 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_expandable_str *)&vec->buffer[idx]))
*result = false;
idx++;
}
return (ERROR);
}
t_error vec_estr_any(t_vec_estr *vec,
bool (*fn)(const t_expandable_str *), 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_expandable_str *)&vec->buffer[idx]))
*result = true;
idx++;
}
return (ERROR);
}
void vec_estr_iter(t_vec_estr *vec,
void (*fn)(t_usize index, t_expandable_str *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_estr.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_estr.h"
#include <stdlib.h>
t_error vec_estr_push_front(t_vec_estr *vec,
t_expandable_str element)
{
t_usize i;
if (vec->len == 0)
return (vec_estr_push(vec, element));
i = vec->len - 1;
if (vec->capacity < vec->len + 1 &&
vec_estr_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_estr_pop_front(t_vec_estr *vec, t_expandable_str *value)
{
t_usize i;
if (vec->len <= 1)
return (vec_estr_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_estr_reverse(t_vec_estr *vec)
{
t_expandable_str 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_estr_back(t_vec_estr *vec, t_expandable_str **out)
{
t_expandable_str *temporary;
if (out == NULL)
out = &temporary;
if (vec->len != 0)
return (*out = &vec->buffer[vec->len - 1], true);
return (false);
}

View file

@ -78,6 +78,6 @@ build_filelist:
@printf '%-78s\\\n' "SRC_FILES =" > Filelist.$(ANAME).mk @printf '%-78s\\\n' "SRC_FILES =" > Filelist.$(ANAME).mk
@tree static -ifF | rg 'static/(.*)\.c$$' --replace '$$1' | sed -re 's/^(.*)_([0-9]+)$$/\1|\2/g' | sort -t'|' --key=1,1 --key=2,2n | sed -e's/|/_/' | xargs printf '%-78s\\\n' >> Filelist.$(ANAME).mk @tree static -ifF | rg 'static/(.*)\.c$$' --replace '$$1' | sed -re 's/^(.*)_([0-9]+)$$/\1|\2/g' | sort -t'|' --key=1,1 --key=2,2n | sed -e's/|/_/' | xargs printf '%-78s\\\n' >> Filelist.$(ANAME).mk
@echo "" >> Filelist.$(ANAME).mk @echo "" >> Filelist.$(ANAME).mk
@echo -e "$(GREY) Populating $(GREEN) Filelist.$(ANAME).mk$(END)" @echo -e '$(GREY) Populating $(GREEN) Filelist.$(ANAME).mk$(END)'
-include $(DEPS) -include $(DEPS)

View file

@ -89,6 +89,6 @@ build_filelist:
@printf '%-78s\\\n' "GEN_FILES =" >> Filelist.$(ANAME).mk @printf '%-78s\\\n' "GEN_FILES =" >> Filelist.$(ANAME).mk
@tree $(GEN_DIR) -ifF | rg '$(GEN_DIR)/(.*)\.c$$' --replace '$$1' | sed -re 's/^(.*)_([0-9]+)$$/\1|\2/g' | sort -t'|' --key=1,1 --key=2,2n | sed -e's/|/_/' | xargs printf '%-78s\\\n' >> Filelist.$(ANAME).mk @tree $(GEN_DIR) -ifF | rg '$(GEN_DIR)/(.*)\.c$$' --replace '$$1' | sed -re 's/^(.*)_([0-9]+)$$/\1|\2/g' | sort -t'|' --key=1,1 --key=2,2n | sed -e's/|/_/' | xargs printf '%-78s\\\n' >> Filelist.$(ANAME).mk
@echo "" >> Filelist.$(ANAME).mk @echo "" >> Filelist.$(ANAME).mk
@echo -e "$(GREY) Populating $(GREEN) Filelist.$(ANAME).mk$(END)" @echo -e '$(GREY) Populating $(GREEN) Filelist.$(ANAME).mk$(END)'
-include $(DEPS) -include $(DEPS)

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 13:22:14 by maiboyer #+# #+# */ /* Created: 2024/05/02 13:22:14 by maiboyer #+# #+# */
/* Updated: 2024/07/22 12:30:50 by maiboyer ### ########.fr */ /* Updated: 2024/07/24 16:02:17 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */